From acd6af1c458e90c3348f5385c11a6ec2a732efdd Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Tue, 2 Feb 2016 00:28:49 -0800 Subject: [PATCH] Add Move event. --- core/block.js | 17 ++++++--- core/block_svg.js | 8 +++++ core/events.js | 88 +++++++++++++++++++++++++++++++++++++++++++++-- core/scrollbar.js | 1 - 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/core/block.js b/core/block.js index 9c74eb446..3f2c0d39b 100644 --- a/core/block.js +++ b/core/block.js @@ -393,6 +393,10 @@ Blockly.Block.prototype.getChildren = function() { * @param {Blockly.Block} newParent New parent block. */ Blockly.Block.prototype.setParent = function(newParent) { + var event; + if (Blockly.Events.isEnabled() && !this.isShadow()) { + event = new Blockly.Events.Move(this); + } if (this.parentBlock_) { // Remove this block from the old parent's child list. var children = this.parentBlock_.childBlocks_; @@ -415,11 +419,7 @@ Blockly.Block.prototype.setParent = function(newParent) { // its connection locations. } else { // Remove this block from the workspace's list of top-most blocks. - // Note that during realtime sync we sometimes create child blocks that are - // not top level so we check first before removing. - if (goog.array.contains(this.workspace.getTopBlocks(false), this)) { - this.workspace.removeTopBlock(this); - } + this.workspace.removeTopBlock(this); } this.parentBlock_ = newParent; @@ -429,6 +429,10 @@ Blockly.Block.prototype.setParent = function(newParent) { } else { this.workspace.addTopBlock(this); } + if (event) { + event.recordNew(); + Blockly.Events.fire(event); + } }; /** @@ -1275,7 +1279,10 @@ Blockly.Block.prototype.getRelativeToSurfaceXY = function() { * @param {number} dy Vertical offset. */ Blockly.Block.prototype.moveBy = function(dx, dy) { + var event = new Blockly.Events.Move(this); this.xy_.translate(dx, dy); + event.recordNew(); + Blockly.Events.fire(event); }; /** diff --git a/core/block_svg.js b/core/block_svg.js index dfe4da466..e7c12ae43 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -217,6 +217,11 @@ Blockly.BlockSvg.terminateDrag_ = function() { // Update the connection locations. var xy = selected.getRelativeToSurfaceXY(); var dxy = goog.math.Coordinate.difference(xy, selected.dragStartXY_); + var event = new Blockly.Events.Move(selected); + event.oldCoordinate = selected.dragStartXY_; + event.recordNew(); + Blockly.Events.fire(event); + selected.moveConnections_(dxy.x, dxy.y); delete selected.draggedBubbles_; selected.setDragging_(false); @@ -286,10 +291,13 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() { * @param {number} dy Vertical offset. */ Blockly.BlockSvg.prototype.moveBy = function(dx, dy) { + var event = new Blockly.Events.Move(this); var xy = this.getRelativeToSurfaceXY(); this.getSvgRoot().setAttribute('transform', 'translate(' + (xy.x + dx) + ',' + (xy.y + dy) + ')'); this.moveConnections_(dx, dy); + event.recordNew(); + Blockly.Events.fire(event); }; /** diff --git a/core/events.js b/core/events.js index 9fcd27578..713f3e2ac 100644 --- a/core/events.js +++ b/core/events.js @@ -52,13 +52,19 @@ Blockly.Events.DELETE = 'delete'; */ Blockly.Events.CHANGE = 'change'; +/** + * Name of event that moves a block. + * @const + */ +Blockly.Events.MOVE = 'move'; + /** * Create a custom event and fire it. - * @param {Object} detail Custom data for event. + * @param {!Blockly.Events.Abstract} detail Custom data for event. */ Blockly.Events.fire = function(detail) { - if (!Blockly.Events.isEnabled()) { - return; // No events allowed. + if (!Blockly.Events.isEnabled() || detail.isNull()) { + return; } console.log(detail); var workspace = Blockly.Workspace.getById(detail.workspaceId); @@ -105,6 +111,14 @@ Blockly.Events.isEnabled = function() { */ Blockly.Events.Abstract = function() {}; +/** + * Does this event record any change of state? + * @return {boolean} True if something changed. + */ +Blockly.Events.Abstract.prototype.isNull = function() { + return false; +}; + /** * Class for a block creation event. * @param {!Blockly.Workspace} workspace The workspace. @@ -158,3 +172,71 @@ Blockly.Events.Change = function(block, element, name, oldValue, newValue) { this.newValue = newValue; }; goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract); + +/** + * Does this event record any change of state? + * @return {boolean} True if something changed. + */ +Blockly.Events.Change.prototype.isNull = function() { + return this.oldValue == this.newValue; +}; + +/** + * Class for a block move event. Created before the move. + * @param {!Blockly.Block} block The moved block. + * @extends {Blockly.Events.Abstract} + * @constructor + */ +Blockly.Events.Move = function(block) { + this.type = Blockly.Events.MOVE; + this.workspaceId = block.workspace.id; + this.blockId = block.id; + + var location = this.currentLocation_(); + this.oldParentId = location.parentId; + this.oldInputName = location.inputName; + this.oldCoordinate = location.coordinate; +}; +goog.inherits(Blockly.Events.Move, Blockly.Events.Abstract); + +/** + * Record the block's new location. Called after the move. + */ +Blockly.Events.Move.prototype.recordNew = function() { + var location = this.currentLocation_(); + this.newParentId = location.parentId; + this.newInputName = location.inputName; + this.newCoordinate = location.coordinate; +}; + +/** + * Returns the parentId and input if the block is connected, + * or the XY location if disconnected. + * @return {!Object} Collection of location info. + * @private + */ +Blockly.Events.Move.prototype.currentLocation_ = function() { + var block = Blockly.Block.getById(this.blockId); + var location = {}; + var parent = block.getParent(); + if (parent) { + location.parentId = parent.id; + var input = parent.getInputWithBlock(block); + if (input) { + location.inputName = input.name + } + } else { + location.coordinate = block.getRelativeToSurfaceXY(); + } + return location; +}; + +/** + * Does this event record any change of state? + * @return {boolean} True if something changed. + */ +Blockly.Events.Move.prototype.isNull = function() { + return this.oldParentId == this.newParentId && + this.oldInputName == this.newInputName && + goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate); +}; diff --git a/core/scrollbar.js b/core/scrollbar.js index e83a42fbf..04629c6bf 100644 --- a/core/scrollbar.js +++ b/core/scrollbar.js @@ -162,7 +162,6 @@ Blockly.ScrollbarPair.prototype.set = function(x, y) { xyRatio.y = ratio; this.workspace_.setMetrics(xyRatio); - }; // --------------------------------------------------------------------