Skip to content

Commit

Permalink
refactor: move the values from constants to config
Browse files Browse the repository at this point in the history
  • Loading branch information
alschmiedt committed Feb 1, 2022
1 parent c545d0e commit 149f43b
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 116 deletions.
6 changes: 3 additions & 3 deletions blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const Events = goog.require('Blockly.Events');
const Procedures = goog.require('Blockly.Procedures');
const Variables = goog.require('Blockly.Variables');
const Xml = goog.require('Blockly.Xml');
const constants = goog.require('Blockly.constants');
const xmlUtils = goog.require('Blockly.utils.xml');
const {Align} = goog.require('Blockly.Input');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Blocks} = goog.require('Blockly.blocks');
const {config} = goog.require('Blockly.config');
/* eslint-disable-next-line no-unused-vars */
const {FieldCheckbox} = goog.require('Blockly.FieldCheckbox');
const {FieldLabel} = goog.require('Blockly.FieldLabel');
Expand Down Expand Up @@ -950,8 +950,8 @@ const PROCEDURE_CALL_COMMON = {
const block = xmlUtils.createElement('block');
block.setAttribute('type', this.defType_);
const xy = this.getRelativeToSurfaceXY();
const x = xy.x + constants.SNAP_RADIUS * (this.RTL ? -1 : 1);
const y = xy.y + constants.SNAP_RADIUS * 2;
const x = xy.x + config.snapRadius * (this.RTL ? -1 : 1);
const y = xy.y + config.snapRadius * 2;
block.setAttribute('x', x);
block.setAttribute('y', y);
const mutation = this.mutationToDom();
Expand Down
7 changes: 4 additions & 3 deletions core/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {ASTNode} = goog.require('Blockly.ASTNode');
const {Block} = goog.require('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {Comment} = goog.requireType('Blockly.Comment');
const {config} = goog.require('Blockly.config');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
/* eslint-disable-next-line no-unused-vars */
const {Connection} = goog.requireType('Blockly.Connection');
Expand Down Expand Up @@ -1552,7 +1553,7 @@ BlockSvg.prototype.bumpNeighbours = function() {
connection.targetBlock().bumpNeighbours();
}

const neighbours = connection.neighbours(constants.SNAP_RADIUS);
const neighbours = connection.neighbours(config.snapRadius);
for (let j = 0, otherConnection; (otherConnection = neighbours[j]); j++) {
// If both connections are connected, that's probably fine. But if
// either one of them is unconnected, then there could be confusion.
Expand Down Expand Up @@ -1585,13 +1586,13 @@ BlockSvg.prototype.scheduleSnapAndBump = function() {
eventUtils.setGroup(group);
block.snapToGrid();
eventUtils.setGroup(false);
}, constants.BUMP_DELAY / 2);
}, config.bumpDelay / 2);

setTimeout(function() {
eventUtils.setGroup(group);
block.bumpNeighbours();
eventUtils.setGroup(false);
}, constants.BUMP_DELAY);
}, config.bumpDelay);
};

/**
Expand Down
4 changes: 3 additions & 1 deletion core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {ConnectionChecker} = goog.require('Blockly.ConnectionChecker');
const {ConnectionDB} = goog.require('Blockly.ConnectionDB');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {Connection} = goog.require('Blockly.Connection');
const {config} = goog.require('Blockly.config');
const {ContextMenuRegistry} = goog.require('Blockly.ContextMenuRegistry');
const {Cursor} = goog.require('Blockly.Cursor');
const {DeleteArea} = goog.require('Blockly.DeleteArea');
Expand Down Expand Up @@ -657,7 +658,7 @@ exports.bindEventWithChecks_ = bindEventWithChecks_;
// Aliases to allow external code to access these values for legacy reasons.
exports.COLLAPSE_CHARS = internalConstants.COLLAPSE_CHARS;
exports.DRAG_STACK = internalConstants.DRAG_STACK;
exports.SPRITE = internalConstants.SPRITE;
exports.SPRITE = constants.SPRITE;
exports.OPPOSITE_TYPE = internalConstants.OPPOSITE_TYPE;
exports.RENAME_VARIABLE_ID = internalConstants.RENAME_VARIABLE_ID;
exports.DELETE_VARIABLE_ID = internalConstants.DELETE_VARIABLE_ID;
Expand Down Expand Up @@ -819,6 +820,7 @@ exports.clipboard = clipboard;
exports.common = common;
/** @deprecated Use Blockly.ConnectionType instead. */
exports.connectionTypes = ConnectionType;
exports.config = config;
exports.constants = constants;
exports.dialog = dialog;
exports.fieldRegistry = fieldRegistry;
Expand Down
60 changes: 13 additions & 47 deletions core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,6 @@
goog.module('Blockly.constants');


/**
* Number of pixels the mouse must move before a drag starts.
* @alias Blockly.constants.DRAG_RADIUS
*/
const DRAG_RADIUS = 5;
exports.DRAG_RADIUS = DRAG_RADIUS;

/**
* Number of pixels the mouse must move before a drag/scroll starts from the
* flyout. Because the drag-intention is determined when this is reached, it is
* larger than DRAG_RADIUS so that the drag-direction is clearer.
* @alias Blockly.constants.FLYOUT_DRAG_RADIUS
*/
const FLYOUT_DRAG_RADIUS = 10;
exports.FLYOUT_DRAG_RADIUS = FLYOUT_DRAG_RADIUS;

/**
* Maximum misalignment between connections for them to snap together.
* @alias Blockly.constants.SNAP_RADIUS
*/
const SNAP_RADIUS = 28;
exports.SNAP_RADIUS = SNAP_RADIUS;

/**
* Maximum misalignment between connections for them to snap together,
* when a connection is already highlighted.
* @alias Blockly.constants.CONNECTING_SNAP_RADIUS
*/
const CONNECTING_SNAP_RADIUS = SNAP_RADIUS;
exports.CONNECTING_SNAP_RADIUS = CONNECTING_SNAP_RADIUS;

/**
* How much to prefer staying connected to the current connection over moving to
* a new connection. The current previewed connection is considered to be this
* much closer to the matching connection on the block than it actually is.
* @alias Blockly.constants.CURRENT_CONNECTION_PREFERENCE
*/
const CURRENT_CONNECTION_PREFERENCE = 8;
exports.CURRENT_CONNECTION_PREFERENCE = CURRENT_CONNECTION_PREFERENCE;

/**
* Delay in ms between trigger and bumping unconnected block out of alignment.
* @alias Blockly.constants.BUMP_DELAY
*/
const BUMP_DELAY = 250;
exports.BUMP_DELAY = BUMP_DELAY;

/**
* The language-neutral ID given to the collapsed input.
* @const {string}
Expand All @@ -78,3 +31,16 @@ exports.COLLAPSED_INPUT_NAME = COLLAPSED_INPUT_NAME;
*/
const COLLAPSED_FIELD_NAME = '_TEMP_COLLAPSED_FIELD';
exports.COLLAPSED_FIELD_NAME = COLLAPSED_FIELD_NAME;

/**
* Contains the path to a single png tat holds the images for the trashcan
* as well as the zoom controls.
* @const {!Object}
* @alias Blockly.constants.SPRITE
*/
const SPRITE = {
width: 96,
height: 124,
url: 'sprites.png',
};
exports.SPRITE = SPRITE;
8 changes: 4 additions & 4 deletions core/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const Xml = goog.require('Blockly.Xml');
const aria = goog.require('Blockly.utils.aria');
const browserEvents = goog.require('Blockly.browserEvents');
const clipboard = goog.require('Blockly.clipboard');
const constants = goog.require('Blockly.constants');
const deprecation = goog.require('Blockly.utils.deprecation');
const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const userAgent = goog.require('Blockly.utils.userAgent');
const svgMath = goog.require('Blockly.utils.svgMath');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {config} = goog.require('Blockly.config');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {MenuItem} = goog.require('Blockly.MenuItem');
const {Menu} = goog.require('Blockly.Menu');
Expand Down Expand Up @@ -266,11 +266,11 @@ const callbackFactory = function(block, xml) {
// Move the new block next to the old block.
const xy = block.getRelativeToSurfaceXY();
if (block.RTL) {
xy.x -= constants.SNAP_RADIUS;
xy.x -= config.snapRadius;
} else {
xy.x += constants.SNAP_RADIUS;
xy.x += config.snapRadius;
}
xy.y += constants.SNAP_RADIUS * 2;
xy.y += config.snapRadius * 2;
newBlock.moveBy(xy.x, xy.y);
} finally {
eventUtils.enable();
Expand Down
4 changes: 2 additions & 2 deletions core/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const Touch = goog.require('Blockly.Touch');
const blockAnimations = goog.require('Blockly.blockAnimations');
const browserEvents = goog.require('Blockly.browserEvents');
const common = goog.require('Blockly.common');
const constants = goog.require('Blockly.constants');
const eventUtils = goog.require('Blockly.Events.utils');
const internalConstants = goog.require('Blockly.internalConstants');
const registry = goog.require('Blockly.registry');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
const {BubbleDragger} = goog.require('Blockly.BubbleDragger');
const {config} = goog.require('Blockly.config');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
/* eslint-disable-next-line no-unused-vars */
const {Field} = goog.requireType('Blockly.Field');
Expand Down Expand Up @@ -306,7 +306,7 @@ Gesture.prototype.updateDragDelta_ = function(currentXY) {

// The flyout has a different drag radius from the rest of Blockly.
const limitRadius =
this.flyout_ ? constants.FLYOUT_DRAG_RADIUS : constants.DRAG_RADIUS;
this.flyout_ ? config.flyoutDragRadius : config.dragRadius;

this.hasExceededDragRadius_ = currentDragDelta > limitRadius;
return this.hasExceededDragRadius_;
Expand Down
7 changes: 4 additions & 3 deletions core/insertion_marker_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const eventUtils = goog.require('Blockly.Events.utils');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
const {ComponentManager} = goog.require('Blockly.ComponentManager');
const {config} = goog.require('Blockly.config');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
/* eslint-disable-next-line no-unused-vars */
const {Coordinate} = goog.requireType('Blockly.utils.Coordinate');
Expand Down Expand Up @@ -397,7 +398,7 @@ InsertionMarkerManager.prototype.shouldUpdatePreviews_ = function(
// Slightly prefer the existing preview over a new preview.
return !(
candidateClosest &&
radius > curDistance - constants.CURRENT_CONNECTION_PREFERENCE);
radius > curDistance - config.currentConnectionPreference);
} else if (!this.localConnection_ && !this.closestConnection_) {
// We weren't showing a preview before, but we should now.
return true;
Expand Down Expand Up @@ -465,9 +466,9 @@ InsertionMarkerManager.prototype.getStartRadius_ = function() {
// range. By increasing radiusConnection when a connection already exists, we
// never "lose" the connection from the offset.
if (this.closestConnection_ && this.localConnection_) {
return constants.CONNECTING_SNAP_RADIUS;
return config.connectingSnapRadius;
}
return constants.SNAP_RADIUS;
return config.snapRadius;
};

/**
Expand Down
11 changes: 0 additions & 11 deletions core/internal_constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ exports.COLLAPSE_CHARS = COLLAPSE_CHARS;
const DRAG_STACK = true;
exports.DRAG_STACK = DRAG_STACK;

/**
* Sprited icons and images.
* @alias Blockly.internalConstants.SPRITE
*/
const SPRITE = {
width: 96,
height: 124,
url: 'sprites.png',
};
exports.SPRITE = SPRITE;

/**
* Lookup table for determining the opposite type of a connection.
* @const
Expand Down
4 changes: 2 additions & 2 deletions core/mutator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ goog.module('Blockly.Mutator');

/* eslint-disable-next-line no-unused-vars */
const Abstract = goog.requireType('Blockly.Events.Abstract');
const constants = goog.require('Blockly.constants');
const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
Expand All @@ -35,6 +34,7 @@ const {Block} = goog.requireType('Blockly.Block');
const {Bubble} = goog.require('Blockly.Bubble');
/* eslint-disable-next-line no-unused-vars */
const {Connection} = goog.requireType('Blockly.Connection');
const {config} = goog.require('Blockly.config');
/* eslint-disable-next-line no-unused-vars */
const {Coordinate} = goog.requireType('Blockly.utils.Coordinate');
const {Icon} = goog.require('Blockly.Icon');
Expand Down Expand Up @@ -479,7 +479,7 @@ Mutator.prototype.updateWorkspace_ = function() {
eventUtils.setGroup(mutationGroup);
block.bumpNeighbours();
eventUtils.setGroup(oldGroup);
}, constants.BUMP_DELAY);
}, config.bumpDelay);
}

// Don't update the bubble until the drag has ended, to avoid moving blocks
Expand Down
10 changes: 5 additions & 5 deletions core/rendered_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.RenderedConnection');

const common = goog.require('Blockly.common');
const constants = goog.require('Blockly.constants');
const dom = goog.require('Blockly.utils.dom');
const eventUtils = goog.require('Blockly.Events.utils');
const internalConstants = goog.require('Blockly.internalConstants');
Expand All @@ -27,6 +26,7 @@ const svgMath = goog.require('Blockly.utils.svgMath');
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {config} = goog.require('Blockly.config');
/* eslint-disable-next-line no-unused-vars */
const {ConnectionDB} = goog.requireType('Blockly.ConnectionDB');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
Expand Down Expand Up @@ -187,18 +187,18 @@ RenderedConnection.prototype.bumpAwayFrom = function(staticConnection) {
// Raise it to the top for extra visibility.
const selected = common.getSelected() == rootBlock;
selected || rootBlock.addSelect();
let dx = (staticConnection.x + constants.SNAP_RADIUS +
let dx = (staticConnection.x + config.snapRadius +
Math.floor(Math.random() * BUMP_RANDOMNESS)) -
this.x;
let dy = (staticConnection.y + constants.SNAP_RADIUS +
let dy = (staticConnection.y + config.snapRadius +
Math.floor(Math.random() * BUMP_RANDOMNESS)) -
this.y;
if (reverse) {
// When reversing a bump due to an uneditable block, bump up.
dy = -dy;
}
if (rootBlock.RTL) {
dx = (staticConnection.x - constants.SNAP_RADIUS -
dx = (staticConnection.x - config.snapRadius -
Math.floor(Math.random() * BUMP_RANDOMNESS)) -
this.x;
}
Expand Down Expand Up @@ -453,7 +453,7 @@ RenderedConnection.prototype.onFailedConnect = function(otherConnection) {
this.bumpAwayFrom(otherConnection);
eventUtils.setGroup(false);
}
}.bind(this), constants.BUMP_DELAY);
}.bind(this), config.bumpDelay);
}
};

Expand Down
14 changes: 7 additions & 7 deletions core/trashcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const Abstract = goog.requireType('Blockly.Events.Abstract');
/* eslint-disable-next-line no-unused-vars */
const blocks = goog.requireType('Blockly.serialization.blocks');
const browserEvents = goog.require('Blockly.browserEvents');
const constants = goog.require('Blockly.constants');
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 registry = goog.require('Blockly.registry');
const toolbox = goog.require('Blockly.utils.toolbox');
Expand Down Expand Up @@ -294,32 +294,32 @@ Trashcan.prototype.createDom = function() {
Svg.RECT, {'width': WIDTH, 'height': BODY_HEIGHT, 'y': LID_HEIGHT}, clip);
const body = dom.createSvgElement(
Svg.IMAGE, {
'width': internalConstants.SPRITE.width,
'width': constants.SPRITE.width,
'x': -SPRITE_LEFT,
'height': internalConstants.SPRITE.height,
'height': constants.SPRITE.height,
'y': -SPRITE_TOP,
'clip-path': 'url(#blocklyTrashBodyClipPath' + rnd + ')',
},
this.svgGroup_);
body.setAttributeNS(
dom.XLINK_NS, 'xlink:href',
this.workspace_.options.pathToMedia + internalConstants.SPRITE.url);
this.workspace_.options.pathToMedia + constants.SPRITE.url);

clip = dom.createSvgElement(
Svg.CLIPPATH, {'id': 'blocklyTrashLidClipPath' + rnd}, this.svgGroup_);
dom.createSvgElement(Svg.RECT, {'width': WIDTH, 'height': LID_HEIGHT}, clip);
this.svgLid_ = dom.createSvgElement(
Svg.IMAGE, {
'width': internalConstants.SPRITE.width,
'width': constants.SPRITE.width,
'x': -SPRITE_LEFT,
'height': internalConstants.SPRITE.height,
'height': constants.SPRITE.height,
'y': -SPRITE_TOP,
'clip-path': 'url(#blocklyTrashLidClipPath' + rnd + ')',
},
this.svgGroup_);
this.svgLid_.setAttributeNS(
dom.XLINK_NS, 'xlink:href',
this.workspace_.options.pathToMedia + internalConstants.SPRITE.url);
this.workspace_.options.pathToMedia + constants.SPRITE.url);

// bindEventWithChecks_ quashes events too aggressively. See:
// https://groups.google.com/forum/#!topic/blockly/QF4yB9Wx00s
Expand Down
Loading

2 comments on commit 149f43b

@cpcallen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you inadvertently omit config.js from this commit?

@alschmiedt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, 100%. Just added : )

Please sign in to comment.