Use ENUM for dragMode. Blocks no longer sticky when created from disappearing flyout.

This commit is contained in:
Neil Fraser
2016-03-29 08:36:11 -07:00
parent 5f1cb3b92c
commit ac0b5e7871
12 changed files with 79 additions and 54 deletions

View File

@@ -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();

View File

@@ -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;

View File

@@ -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.

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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()) {

View File

@@ -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);
};
};

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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++) {

View File

@@ -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()) {

View File

@@ -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);