From 5f1cb3b92c8659934eba6ffdcadf230bbd3c0da0 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Tue, 29 Mar 2016 08:10:44 -0700 Subject: [PATCH] Add click UI event. --- core/block_svg.js | 25 ++++++++++++++++++++----- core/events.js | 25 ++++++++++--------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 2c295b9c0..1cb17891d 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -123,11 +123,20 @@ Blockly.BlockSvg.prototype.initSvg = function() { * Select this block. Highlight it visually. */ Blockly.BlockSvg.prototype.select = function() { - if (Blockly.selected) { - // Unselect any previously selected block. - Blockly.selected.unselect(); + if (Blockly.selected == this) { + return; } - Blockly.Events.fire(new Blockly.Events.Ui(this, 'selected', false, true)); + var oldId = null; + if (Blockly.selected) { + oldId = Blockly.selected.id; + // Unselect any previously selected block. + Blockly.Events.disable(); + Blockly.selected.unselect(); + Blockly.Events.enable(); + } + var event = new Blockly.Events.Ui(null, 'selected', oldId, this.id); + event.workspaceId = this.workspace.id; + Blockly.Events.fire(event); Blockly.selected = this; this.addSelect(); Blockly.fireUiEvent(this.workspace.getCanvas(), 'blocklySelectChange'); @@ -140,7 +149,9 @@ Blockly.BlockSvg.prototype.unselect = function() { if (Blockly.selected != this) { return; } - Blockly.Events.fire(new Blockly.Events.Ui(this, 'selected', true, false)); + var event = new Blockly.Events.Ui(null, 'selected', this.id, null); + event.workspaceId = this.workspace.id; + Blockly.Events.fire(event); Blockly.selected = null; this.removeSelect(); Blockly.fireUiEvent(this.workspace.getCanvas(), 'blocklySelectChange'); @@ -548,6 +559,10 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { * @private */ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) { + if (Blockly.dragMode_ != 2) { + Blockly.Events.fire( + new Blockly.Events.Ui(this, 'click', undefined, undefined)); + } Blockly.setPageSelectable(true); Blockly.terminateDrag_(); if (Blockly.selected && Blockly.highlightedConnection_) { diff --git a/core/events.js b/core/events.js index f5f7a0cd0..8e90f6583 100644 --- a/core/events.js +++ b/core/events.js @@ -241,12 +241,14 @@ Blockly.Events.getDescendantIds_ = function(block) { /** * Abstract class for an event. - * @param {!Blockly.Block} block The block. + * @param {Blockly.Block} block The block. * @constructor */ Blockly.Events.Abstract = function(block) { - this.blockId = block.id; - this.workspaceId = block.workspace.id; + if (block) { + this.blockId = block.id; + this.workspaceId = block.workspace.id; + } this.group = Blockly.Events.group_; this.recordUndo = Blockly.Events.recordUndo; }; @@ -269,7 +271,7 @@ Blockly.Events.Abstract.prototype.toJson = function() { /** * Does this event record any change of state? - * @return {boolean} True if something changed. + * @return {boolean} True if null, false if something changed. */ Blockly.Events.Abstract.prototype.isNull = function() { return false; @@ -623,7 +625,7 @@ Blockly.Events.Move.prototype.run = function(forward) { /** * Class for a UI event. - * @param {!Blockly.Block} block The affected block. + * @param {Blockly.Block} block The affected block. * @param {string} element One of 'selected', 'comment', 'mutator', etc. * @param {string} oldValue Previous value of element. * @param {string} newValue New value of element. @@ -652,15 +654,8 @@ Blockly.Events.Ui.prototype.type = Blockly.Events.UI; Blockly.Events.Change.prototype.toJson = function() { var json = Blockly.Events.Change.superClass_.toJson.call(this); json['element'] = this.element; - json['newValue'] = this.newValue; + if (this.newValue !== undefined) { + json['newValue'] = this.newValue; + } return json; }; - -/** - * Does this event record any change of state? - * @return {boolean} True if something changed. - */ -Blockly.Events.Ui.prototype.isNull = function() { - return this.oldValue == this.newValue; -}; -