diff --git a/core/block.js b/core/block.js index b89c402f0..5abefe053 100644 --- a/core/block.js +++ b/core/block.js @@ -302,7 +302,7 @@ Blockly.Block.prototype.bumpNeighbours_ = function() { if (!this.workspace) { return; // Deleted block. } - if (Blockly.dragMode_ != 0) { + if (Blockly.dragMode_ != Blockly.DRAG_NONE) { return; // Don't bump blocks during a drag. } var rootBlock = this.getRootBlock(); diff --git a/core/block_svg.js b/core/block_svg.js index 1cb17891d..f31bbf035 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -222,7 +222,7 @@ Blockly.BlockSvg.terminateDrag_ = function() { Blockly.BlockSvg.onMouseMoveWrapper_ = null; } var selected = Blockly.selected; - if (Blockly.dragMode_ == 2) { + if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Terminate a drag operation. if (selected) { // Update the connection locations. @@ -253,7 +253,7 @@ Blockly.BlockSvg.terminateDrag_ = function() { Blockly.fireUiEvent(window, 'resize'); } } - Blockly.dragMode_ = 0; + Blockly.dragMode_ = Blockly.DRAG_NONE; Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN); }; @@ -330,7 +330,7 @@ Blockly.BlockSvg.prototype.snapToGrid = function() { if (!this.workspace) { return; // Deleted block. } - if (Blockly.dragMode_ != 0) { + if (Blockly.dragMode_ != Blockly.DRAG_NONE) { return; // Don't bump blocks during a drag. } if (this.getParent()) { @@ -530,7 +530,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { this.dragStartXY_ = this.getRelativeToSurfaceXY(); this.workspace.startDrag(e, this.dragStartXY_.x, this.dragStartXY_.y); - Blockly.dragMode_ = 1; + Blockly.dragMode_ = Blockly.DRAG_STICKY; Blockly.BlockSvg.onMouseUpWrapper_ = Blockly.bindEvent_(document, 'mouseup', this, this.onMouseUp_); Blockly.BlockSvg.onMouseMoveWrapper_ = Blockly.bindEvent_(document, @@ -559,7 +559,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { * @private */ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) { - if (Blockly.dragMode_ != 2) { + if (Blockly.dragMode_ != Blockly.DRAG_FREE) { Blockly.Events.fire( new Blockly.Events.Ui(this, 'click', undefined, undefined)); } @@ -781,6 +781,9 @@ Blockly.BlockSvg.prototype.moveConnections_ = function(dx, dy) { */ Blockly.BlockSvg.prototype.setDragging_ = function(adding) { if (adding) { + var group = this.getSvgRoot(); + group.translate_ = ''; + group.skew_ = ''; this.addDragging(); } else { this.removeDragging(); @@ -811,16 +814,13 @@ Blockly.BlockSvg.prototype.onMouseMove_ = function(e) { var oldXY = this.getRelativeToSurfaceXY(); var newXY = this.workspace.moveDrag(e); - var group = this.getSvgRoot(); - if (Blockly.dragMode_ == 1) { + if (Blockly.dragMode_ == Blockly.DRAG_STICKY) { // Still dragging within the sticky DRAG_RADIUS. var dr = goog.math.Coordinate.distance(oldXY, newXY) * this.workspace.scale; if (dr > Blockly.DRAG_RADIUS) { // Switch to unrestricted dragging. - Blockly.dragMode_ = 2; + Blockly.dragMode_ = Blockly.DRAG_FREE; Blockly.longStop_(); - group.translate_ = ''; - group.skew_ = ''; if (this.parentBlock_) { // Push this block to the very top of the stack. this.unplug(); @@ -829,10 +829,11 @@ Blockly.BlockSvg.prototype.onMouseMove_ = function(e) { this.setDragging_(true); } } - if (Blockly.dragMode_ == 2) { + if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Unrestricted dragging. var dx = oldXY.x - this.dragStartXY_.x; var dy = oldXY.y - this.dragStartXY_.y; + var group = this.getSvgRoot(); group.translate_ = 'translate(' + newXY.x + ',' + newXY.y + ')'; group.setAttribute('transform', group.translate_ + group.skew_); // Drag all the nested bubbles. @@ -1281,7 +1282,7 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) { clearTimeout(this.setWarningText.pid_[id]); delete this.setWarningText.pid_[id]; } - if (Blockly.dragMode_ == 2) { + if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Don't change the warning text during a drag. // Wait until the drag finishes. var thisBlock = this; diff --git a/core/blockly.js b/core/blockly.js index 67213067a..608909d90 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -103,7 +103,7 @@ Blockly.clipboardSource_ = null; * 2 - Freely draggable. * @private */ -Blockly.dragMode_ = 0; +Blockly.dragMode_ = Blockly.DRAG_NONE; /** * Wrapper function called when a touch mouseUp occurs during a drag operation. @@ -269,7 +269,7 @@ Blockly.onKeyDown_ = function(e) { if (deleteBlock) { // Common code for delete and cut. Blockly.hideChaff(); - var heal = Blockly.dragMode_ != 2; + var heal = Blockly.dragMode_ != Blockly.DRAG_FREE; Blockly.selected.dispose(heal, true); if (Blockly.highlightedConnection_) { Blockly.highlightedConnection_.unhighlight(); @@ -331,7 +331,7 @@ Blockly.longStop_ = function() { */ Blockly.copy_ = function(block) { var xmlBlock = Blockly.Xml.blockToDom(block); - if (Blockly.dragMode_ != 2) { + if (Blockly.dragMode_ != Blockly.DRAG_FREE) { Blockly.Xml.deleteNext(xmlBlock); } // Encode start position in XML. diff --git a/core/connection.js b/core/connection.js index 0d98dbf45..70ce2ba05 100644 --- a/core/connection.js +++ b/core/connection.js @@ -567,7 +567,7 @@ Blockly.Connection.prototype.targetBlock = function() { * @private */ Blockly.Connection.prototype.bumpAwayFrom_ = function(staticConnection) { - if (Blockly.dragMode_ != 0) { + if (Blockly.dragMode_ != Blockly.DRAG_NONE) { // Don't move blocks around while the user is doing the same. return; } diff --git a/core/constants.js b/core/constants.js index 5daa4f10c..9869a7f9b 100644 --- a/core/constants.js +++ b/core/constants.js @@ -28,16 +28,29 @@ goog.provide('Blockly.constants'); /** - * Required name space for SVG elements. - * @const + * Number of pixels the mouse must move before a drag starts. */ -Blockly.SVG_NS = 'http://www.w3.org/2000/svg'; +Blockly.DRAG_RADIUS = 5; /** - * Required name space for HTML elements. - * @const + * Maximum misalignment between connections for them to snap together. */ -Blockly.HTML_NS = 'http://www.w3.org/1999/xhtml'; +Blockly.SNAP_RADIUS = 20; + +/** + * Delay in ms between trigger and bumping unconnected block out of alignment. + */ +Blockly.BUMP_DELAY = 250; + +/** + * Number of characters to truncate a collapsed block to. + */ +Blockly.COLLAPSE_CHARS = 30; + +/** + * Length in ms for a touch to become a long press. + */ +Blockly.LONGPRESS = 750; /** * The richness of block colours, regardless of the hue. @@ -60,6 +73,20 @@ Blockly.SPRITE = { url: 'sprites.png' }; +// Constants below this point are not intended to be changed. + +/** + * Required name space for SVG elements. + * @const + */ +Blockly.SVG_NS = 'http://www.w3.org/2000/svg'; + +/** + * Required name space for HTML elements. + * @const + */ +Blockly.HTML_NS = 'http://www.w3.org/1999/xhtml'; + /** * ENUM for a right-facing value input. E.g. 'set item to' or 'return'. * @const @@ -95,17 +122,37 @@ Blockly.DUMMY_INPUT = 5; * @const */ Blockly.ALIGN_LEFT = -1; + /** * ENUM for centre alignment. * @const */ Blockly.ALIGN_CENTRE = 0; + /** * ENUM for right alignment. * @const */ Blockly.ALIGN_RIGHT = 1; +/** + * ENUM for no drag operation. + * @const + */ +Blockly.DRAG_NONE = 0; + +/** + * ENUM for inside the sticky DRAG_RADIUS. + * @const + */ +Blockly.DRAG_STICKY = 1; + +/** + * ENUM for freely draggable. + * @const + */ +Blockly.DRAG_FREE = 2; + /** * Lookup table for determining the opposite type of a connection. * @const @@ -115,28 +162,3 @@ Blockly.OPPOSITE_TYPE[Blockly.INPUT_VALUE] = Blockly.OUTPUT_VALUE; Blockly.OPPOSITE_TYPE[Blockly.OUTPUT_VALUE] = Blockly.INPUT_VALUE; Blockly.OPPOSITE_TYPE[Blockly.NEXT_STATEMENT] = Blockly.PREVIOUS_STATEMENT; Blockly.OPPOSITE_TYPE[Blockly.PREVIOUS_STATEMENT] = Blockly.NEXT_STATEMENT; - -/** - * Number of pixels the mouse must move before a drag starts. - */ -Blockly.DRAG_RADIUS = 5; - -/** - * Maximum misalignment between connections for them to snap together. - */ -Blockly.SNAP_RADIUS = 20; - -/** - * Delay in ms between trigger and bumping unconnected block out of alignment. - */ -Blockly.BUMP_DELAY = 250; - -/** - * Number of characters to truncate a collapsed block to. - */ -Blockly.COLLAPSE_CHARS = 30; - -/** - * Length in ms for a touch to become a long press. - */ -Blockly.LONGPRESS = 750; diff --git a/core/field.js b/core/field.js index 06ff1cf88..6db2f0c97 100644 --- a/core/field.js +++ b/core/field.js @@ -422,7 +422,7 @@ Blockly.Field.prototype.onMouseUp_ = function(e) { } else if (Blockly.isRightButton(e)) { // Right-click. return; - } else if (Blockly.dragMode_ == 2) { + } else if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Drag operation is concluding. Don't open the editor. return; } else if (this.sourceBlock_.isEditable()) { diff --git a/core/flyout.js b/core/flyout.js index e69a73c6f..39d579e86 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -677,6 +677,8 @@ Blockly.Flyout.prototype.createBlockFunc_ = function(originBlock) { } // Start a dragging operation on the new block. block.onMouseDown_(e); + Blockly.dragMode_ = Blockly.DRAG_FREE; + block.setDragging_(true); }; }; diff --git a/core/icon.js b/core/icon.js index d9d041dc7..5793a5b7a 100644 --- a/core/icon.js +++ b/core/icon.js @@ -128,7 +128,7 @@ Blockly.Icon.prototype.isVisible = function() { * @private */ Blockly.Icon.prototype.iconClick_ = function(e) { - if (Blockly.dragMode_ == 2) { + if (Blockly.dragMode_ == Blockly.DRAG_FREE) { // Drag operation is concluding. Don't open the editor. return; } diff --git a/core/inject.js b/core/inject.js index a4e471b7f..15010a4cb 100644 --- a/core/inject.js +++ b/core/inject.js @@ -345,7 +345,7 @@ Blockly.createMainWorkspace_ = function(svg, options) { if (!options.readOnly && !options.hasScrollbars) { var workspaceChanged = function() { - if (Blockly.dragMode_ == 0) { + if (Blockly.dragMode_ == Blockly.DRAG_NONE) { var metrics = mainWorkspace.getMetrics(); var edgeLeft = metrics.viewLeft + metrics.absoluteLeft; var edgeTop = metrics.viewTop + metrics.absoluteTop; diff --git a/core/mutator.js b/core/mutator.js index e8cfac42c..095f957f5 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -267,7 +267,7 @@ Blockly.Mutator.prototype.setVisible = function(visible) { * @private */ Blockly.Mutator.prototype.workspaceChanged_ = function() { - if (Blockly.dragMode_ == 0) { + if (Blockly.dragMode_ == Blockly.DRAG_NONE) { var blocks = this.workspace_.getTopBlocks(false); var MARGIN = 20; for (var b = 0, block; block = blocks[b]; b++) { diff --git a/core/tooltip.js b/core/tooltip.js index 4a8b83f32..6e520e65b 100644 --- a/core/tooltip.js +++ b/core/tooltip.js @@ -184,7 +184,7 @@ Blockly.Tooltip.onMouseMove_ = function(e) { if (!Blockly.Tooltip.element_ || !Blockly.Tooltip.element_.tooltip) { // No tooltip here to show. return; - } else if (Blockly.dragMode_ != 0) { + } else if (Blockly.dragMode_ != Blockly.DRAG_NONE) { // Don't display a tooltip during a drag. return; } else if (Blockly.WidgetDiv.isVisible()) { diff --git a/core/workspace_svg.js b/core/workspace_svg.js index bd1d031e1..7d42e6597 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -428,7 +428,7 @@ Blockly.WorkspaceSvg.prototype.traceOn = function(armed) { * @param {?string} id ID of block to find. */ Blockly.WorkspaceSvg.prototype.highlightBlock = function(id) { - if (this.traceOn_ && Blockly.dragMode_ != 0) { + if (this.traceOn_ && Blockly.dragMode_ != Blockly.DRAG_NONE) { // The blocklySelectChange event normally prevents this, but sometimes // there is a race condition on fast-executing apps. this.traceOn(false);