Skip to content

Commit

Permalink
chore: Move functions from utils (#5706)
Browse files Browse the repository at this point in the history
* chore: move functions from utils to more specific files

* chore: use new names for utils functions

* chore: run clang-format

* chore: add deprecation warnings back to utils.js
  • Loading branch information
rachel-fenichel authored Nov 15, 2021
1 parent 6c10b60 commit 969fcac
Show file tree
Hide file tree
Showing 30 changed files with 749 additions and 495 deletions.
9 changes: 5 additions & 4 deletions core/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const eventUtils = goog.require('Blockly.Events.utils');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const idGenerator = goog.require('Blockly.utils.idGenerator');
const object = goog.require('Blockly.utils.object');
const parsing = goog.require('Blockly.utils.parsing');
const utils = goog.require('Blockly.utils');
const {ASTNode} = goog.require('Blockly.ASTNode');
const {Blocks} = goog.require('Blockly.blocks');
Expand Down Expand Up @@ -1015,7 +1016,7 @@ Block.prototype.getHue = function() {
* or a message reference string pointing to one of those two values.
*/
Block.prototype.setColour = function(colour) {
const parsed = utils.parseBlockColour(colour);
const parsed = parsing.parseBlockColour(colour);
this.hue_ = parsed.hue;
this.colour_ = parsed.hex;
};
Expand Down Expand Up @@ -1582,7 +1583,7 @@ Block.prototype.jsonInit = function(json) {
}
if (json['tooltip'] !== undefined) {
const rawValue = json['tooltip'];
const localizedText = utils.replaceMessageReferences(rawValue);
const localizedText = parsing.replaceMessageReferences(rawValue);
this.setTooltip(localizedText);
}
if (json['enableContextMenu'] !== undefined) {
Expand All @@ -1593,7 +1594,7 @@ Block.prototype.jsonInit = function(json) {
}
if (json['helpUrl'] !== undefined) {
const rawValue = json['helpUrl'];
const localizedValue = utils.replaceMessageReferences(rawValue);
const localizedValue = parsing.replaceMessageReferences(rawValue);
this.setHelpUrl(localizedValue);
}
if (typeof json['extensions'] === 'string') {
Expand Down Expand Up @@ -1693,7 +1694,7 @@ Block.prototype.mixin = function(mixinObj, opt_disableCheck) {
*/
Block.prototype.interpolate_ = function(
message, args, lastDummyAlign, warningPrefix) {
const tokens = utils.tokenizeInterpolation(message);
const tokens = parsing.tokenizeInterpolation(message);
this.validateTokens_(tokens, args.length);
const elements = this.interpolateArguments_(tokens, args, lastDummyAlign);

Expand Down
4 changes: 2 additions & 2 deletions core/block_drag_surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
goog.module('Blockly.BlockDragSurfaceSvg');

const dom = goog.require('Blockly.utils.dom');
const utils = goog.require('Blockly.utils');
const svgMath = goog.require('Blockly.utils.svgMath');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {Svg} = goog.require('Blockly.utils.Svg');

Expand Down Expand Up @@ -200,7 +200,7 @@ BlockDragSurfaceSvg.prototype.translateSurface = function(x, y) {
* @return {!Coordinate} Current translation of the surface.
*/
BlockDragSurfaceSvg.prototype.getSurfaceTranslation = function() {
const xy = utils.getRelativeXY(/** @type {!SVGElement} */ (this.SVG_));
const xy = svgMath.getRelativeXY(/** @type {!SVGElement} */ (this.SVG_));
return new Coordinate(xy.x / this.scale_, xy.y / this.scale_);
};

Expand Down
5 changes: 3 additions & 2 deletions core/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const internalConstants = goog.require('Blockly.internalConstants');
const object = goog.require('Blockly.utils.object');
const svgMath = goog.require('Blockly.utils.svgMath');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
const {ASTNode} = goog.require('Blockly.ASTNode');
Expand Down Expand Up @@ -145,7 +146,7 @@ const BlockSvg = function(workspace, prototypeName, opt_id) {
* @private
*/
this.useDragSurface_ =
utils.is3dSupported() && !!workspace.getBlockDragSurface();
svgMath.is3dSupported() && !!workspace.getBlockDragSurface();

const svgPath = this.pathObject.svgPath;
svgPath.tooltip = this;
Expand Down Expand Up @@ -428,7 +429,7 @@ BlockSvg.prototype.getRelativeToSurfaceXY = function() {
if (element) {
do {
// Loop through this block and every parent.
const xy = utils.getRelativeXY(element);
const xy = svgMath.getRelativeXY(element);
x += xy.x;
y += xy.y;
// If this element is the current element on the drag surface, include
Expand Down
4 changes: 2 additions & 2 deletions core/bubble_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
goog.module('Blockly.BubbleDragger');

const eventUtils = goog.require('Blockly.Events.utils');
const utils = goog.require('Blockly.utils');
const svgMath = goog.require('Blockly.utils.svgMath');
/* eslint-disable-next-line no-unused-vars */
const {BlockDragSurfaceSvg} = goog.requireType('Blockly.BlockDragSurfaceSvg');
const {ComponentManager} = goog.require('Blockly.ComponentManager');
Expand Down Expand Up @@ -92,7 +92,7 @@ const BubbleDragger = function(bubble, workspace) {
* @private
*/
this.dragSurface_ =
utils.is3dSupported() && !!workspace.getBlockDragSurface() ?
svgMath.is3dSupported() && !!workspace.getBlockDragSurface() ?
workspace.getBlockDragSurface() :
null;
};
Expand Down
4 changes: 2 additions & 2 deletions core/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const internalConstants = goog.require('Blockly.internalConstants');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
const svgMath = goog.require('Blockly.utils.svgMath');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
Expand Down Expand Up @@ -169,7 +169,7 @@ const populate_ = function(options, rtl) {
*/
const position_ = function(menu, e, rtl) {
// Record windowSize and scrollOffset before adding menu.
const viewportBBox = utils.getViewportBBox();
const viewportBBox = svgMath.getViewportBBox();
// This one is just a point, but we'll pretend that it's a rect so we can use
// some helper functions.
const anchorBBox = new Rect(
Expand Down
9 changes: 5 additions & 4 deletions core/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
goog.module('Blockly.Extensions');

const parsing = goog.require('Blockly.utils.parsing');
const utils = goog.require('Blockly.utils');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
Expand Down Expand Up @@ -376,7 +377,7 @@ const buildTooltipForDropdown = function(dropdownName, lookupTable) {
utils.runAfterPageLoad(function() {
for (const key in lookupTable) {
// Will print warnings if reference is missing.
utils.checkMessageReferences(lookupTable[key]);
parsing.checkMessageReferences(lookupTable[key]);
}
});
}
Expand Down Expand Up @@ -405,7 +406,7 @@ const buildTooltipForDropdown = function(dropdownName, lookupTable) {
console.warn(warning + '.');
}
} else {
tooltip = utils.replaceMessageReferences(tooltip);
tooltip = parsing.replaceMessageReferences(tooltip);
}
return tooltip;
}.bind(this));
Expand Down Expand Up @@ -455,7 +456,7 @@ const buildTooltipWithFieldText = function(msgTemplate, fieldName) {
if (typeof document === 'object') { // Relies on document.readyState
utils.runAfterPageLoad(function() {
// Will print warnings if reference is missing.
utils.checkMessageReferences(msgTemplate);
parsing.checkMessageReferences(msgTemplate);
});
}

Expand All @@ -466,7 +467,7 @@ const buildTooltipWithFieldText = function(msgTemplate, fieldName) {
const extensionFn = function() {
this.setTooltip(function() {
const field = this.getField(fieldName);
return utils.replaceMessageReferences(msgTemplate)
return parsing.replaceMessageReferences(msgTemplate)
.replace('%1', field ? field.getText() : '');
}.bind(this));
};
Expand Down
4 changes: 2 additions & 2 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const Xml = goog.require('Blockly.Xml');
const browserEvents = goog.require('Blockly.browserEvents');
const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const parsing = goog.require('Blockly.utils.parsing');
const style = goog.require('Blockly.utils.style');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
const utilsXml = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
Expand Down Expand Up @@ -287,7 +287,7 @@ Field.prototype.SERIALIZABLE = false;
Field.prototype.configure_ = function(config) {
let tooltip = config['tooltip'];
if (typeof tooltip === 'string') {
tooltip = utils.replaceMessageReferences(config['tooltip']);
tooltip = parsing.replaceMessageReferences(config['tooltip']);
}
tooltip && this.setTooltip(tooltip);

Expand Down
6 changes: 3 additions & 3 deletions core/field_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const aria = goog.require('Blockly.utils.aria');
const dom = goog.require('Blockly.utils.dom');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const parsing = goog.require('Blockly.utils.parsing');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
const utilsString = goog.require('Blockly.utils.string');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {DropDownDiv} = goog.require('Blockly.DropDownDiv');
Expand Down Expand Up @@ -429,10 +429,10 @@ FieldDropdown.prototype.trimOptions_ = function() {
for (let i = 0; i < options.length; i++) {
const label = options[i][0];
if (typeof label === 'string') {
options[i][0] = utils.replaceMessageReferences(label);
options[i][0] = parsing.replaceMessageReferences(label);
} else {
if (label.alt !== null) {
options[i][0].alt = utils.replaceMessageReferences(label.alt);
options[i][0].alt = parsing.replaceMessageReferences(label.alt);
}
hasImages = true;
}
Expand Down
12 changes: 6 additions & 6 deletions core/field_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ goog.module('Blockly.FieldImage');
const dom = goog.require('Blockly.utils.dom');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const utils = goog.require('Blockly.utils');
const parsing = goog.require('Blockly.utils.parsing');
const {Field} = goog.require('Blockly.Field');
const {Size} = goog.require('Blockly.utils.Size');
const {Svg} = goog.require('Blockly.utils.Svg');
Expand Down Expand Up @@ -48,9 +48,9 @@ const FieldImage = function(
if (!src) {
throw Error('Src value of an image field is required');
}
src = utils.replaceMessageReferences(src);
const imageHeight = Number(utils.replaceMessageReferences(height));
const imageWidth = Number(utils.replaceMessageReferences(width));
src = parsing.replaceMessageReferences(src);
const imageHeight = Number(parsing.replaceMessageReferences(height));
const imageWidth = Number(parsing.replaceMessageReferences(width));
if (isNaN(imageHeight) || isNaN(imageWidth)) {
throw Error(
'Height and width values of an image field must cast to' +
Expand Down Expand Up @@ -81,7 +81,7 @@ const FieldImage = function(

if (!opt_config) { // If the config wasn't passed, do old configuration.
this.flipRtl_ = !!opt_flipRtl;
this.altText_ = utils.replaceMessageReferences(opt_alt) || '';
this.altText_ = parsing.replaceMessageReferences(opt_alt) || '';
}

// Initialize other properties.
Expand Down Expand Up @@ -177,7 +177,7 @@ FieldImage.prototype.isDirty_ = false;
FieldImage.prototype.configure_ = function(config) {
FieldImage.superClass_.configure_.call(this, config);
this.flipRtl_ = !!config['flipRtl'];
this.altText_ = utils.replaceMessageReferences(config['alt']) || '';
this.altText_ = parsing.replaceMessageReferences(config['alt']) || '';
};

/**
Expand Down
4 changes: 2 additions & 2 deletions core/field_label.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ goog.module('Blockly.FieldLabel');
const dom = goog.require('Blockly.utils.dom');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const utils = goog.require('Blockly.utils');
const parsing = goog.require('Blockly.utils.parsing');
const {Field} = goog.require('Blockly.Field');


Expand Down Expand Up @@ -69,7 +69,7 @@ FieldLabel.prototype.DEFAULT_VALUE = '';
* @nocollapse
*/
FieldLabel.fromJson = function(options) {
const text = utils.replaceMessageReferences(options['text']);
const text = parsing.replaceMessageReferences(options['text']);
// `this` might be a subclass of FieldLabel if that class doesn't override
// the static fromJson method.
return new this(text, undefined, options);
Expand Down
4 changes: 2 additions & 2 deletions core/field_label_serializable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ goog.module('Blockly.FieldLabelSerializable');

const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const utils = goog.require('Blockly.utils');
const parsing = goog.require('Blockly.utils.parsing');
const {FieldLabel} = goog.require('Blockly.FieldLabel');


Expand Down Expand Up @@ -54,7 +54,7 @@ object.inherits(FieldLabelSerializable, FieldLabel);
* @nocollapse
*/
FieldLabelSerializable.fromJson = function(options) {
const text = utils.replaceMessageReferences(options['text']);
const text = parsing.replaceMessageReferences(options['text']);
// `this` might be a subclass of FieldLabelSerializable if that class doesn't
// override the static fromJson method.
return new this(text, undefined, options);
Expand Down
4 changes: 2 additions & 2 deletions core/field_multilineinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const aria = goog.require('Blockly.utils.aria');
const dom = goog.require('Blockly.utils.dom');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const parsing = goog.require('Blockly.utils.parsing');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
const {FieldTextInput} = goog.require('Blockly.FieldTextInput');
const {Field} = goog.require('Blockly.Field');
const {KeyCodes} = goog.require('Blockly.utils.KeyCodes');
Expand Down Expand Up @@ -90,7 +90,7 @@ FieldMultilineInput.prototype.configure_ = function(config) {
* @nocollapse
*/
FieldMultilineInput.fromJson = function(options) {
const text = utils.replaceMessageReferences(options['text']);
const text = parsing.replaceMessageReferences(options['text']);
// `this` might be a subclass of FieldMultilineInput if that class doesn't
// override the static fromJson method.
return new this(text, undefined, options);
Expand Down
4 changes: 2 additions & 2 deletions core/field_textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const object = goog.require('Blockly.utils.object');
const parsing = goog.require('Blockly.utils.parsing');
const userAgent = goog.require('Blockly.utils.userAgent');
const utils = goog.require('Blockly.utils');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
Expand Down Expand Up @@ -116,7 +116,7 @@ FieldTextInput.prototype.DEFAULT_VALUE = '';
* @nocollapse
*/
FieldTextInput.fromJson = function(options) {
const text = utils.replaceMessageReferences(options['text']);
const text = parsing.replaceMessageReferences(options['text']);
// `this` might be a subclass of FieldTextInput if that class doesn't override
// the static fromJson method.
return new this(text, undefined, options);
Expand Down
4 changes: 2 additions & 2 deletions core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Xml = goog.require('Blockly.Xml');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const internalConstants = goog.require('Blockly.internalConstants');
const object = goog.require('Blockly.utils.object');
const utils = goog.require('Blockly.utils');
const parsing = goog.require('Blockly.utils.parsing');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {FieldDropdown} = goog.require('Blockly.FieldDropdown');
Expand Down Expand Up @@ -104,7 +104,7 @@ object.inherits(FieldVariable, FieldDropdown);
* @nocollapse
*/
FieldVariable.fromJson = function(options) {
const varName = utils.replaceMessageReferences(options['variable']);
const varName = parsing.replaceMessageReferences(options['variable']);
// `this` might be a subclass of FieldVariable if that class doesn't override
// the static fromJson method.
return new this(varName, undefined, undefined, undefined, options);
Expand Down
4 changes: 2 additions & 2 deletions core/flyout_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const dom = goog.require('Blockly.utils.dom');
const style = goog.require('Blockly.utils.style');
/* eslint-disable-next-line no-unused-vars */
const toolbox = goog.requireType('Blockly.utils.toolbox');
const utils = goog.require('Blockly.utils');
const parsing = goog.require('Blockly.utils.parsing');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {Svg} = goog.require('Blockly.utils.Svg');
/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -170,7 +170,7 @@ FlyoutButton.prototype.createDom = function() {
'text-anchor': 'middle',
},
this.svgGroup_);
let text = utils.replaceMessageReferences(this.text_);
let text = parsing.replaceMessageReferences(this.text_);
if (this.workspace_.RTL) {
// Force text to be RTL by adding an RLM.
text += '\u200F';
Expand Down
4 changes: 2 additions & 2 deletions core/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ goog.module('Blockly.Icon');

const browserEvents = goog.require('Blockly.browserEvents');
const dom = goog.require('Blockly.utils.dom');
const utils = goog.require('Blockly.utils');
const svgMath = goog.require('Blockly.utils.svgMath');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -170,7 +170,7 @@ Icon.prototype.setIconLocation = function(xy) {
Icon.prototype.computeIconLocation = function() {
// Find coordinates for the centre of the icon and update the arrow.
const blockXY = this.block_.getRelativeToSurfaceXY();
const iconXY = utils.getRelativeXY(
const iconXY = svgMath.getRelativeXY(
/** @type {!SVGElement} */ (this.iconGroup_));
const newXY = new Coordinate(
blockXY.x + iconXY.x + this.SIZE / 2,
Expand Down
4 changes: 2 additions & 2 deletions core/rendered_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const eventUtils = goog.require('Blockly.Events.utils');
const internalConstants = goog.require('Blockly.internalConstants');
const object = goog.require('Blockly.utils.object');
const svgPaths = goog.require('Blockly.utils.svgPaths');
const utils = goog.require('Blockly.utils');
const svgMath = goog.require('Blockly.utils.svgMath');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -270,7 +270,7 @@ RenderedConnection.prototype.tighten = function() {
throw Error('block is not rendered.');
}
// Workspace coordinates.
const xy = utils.getRelativeXY(svgRoot);
const xy = svgMath.getRelativeXY(svgRoot);
block.getSvgRoot().setAttribute(
'transform', 'translate(' + (xy.x - dx) + ',' + (xy.y - dy) + ')');
block.moveConnections(-dx, -dy);
Expand Down
Loading

0 comments on commit 969fcac

Please sign in to comment.