From cf257ea5e9c2f58464e59037d6fc5c63cdc76b70 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 10 Feb 2016 19:25:17 -0800 Subject: [PATCH] Add event filtering. --- core/block_svg.js | 2 +- core/events.js | 93 ++++++++++++++++++++++++++++++++++++--------- core/field_angle.js | 2 + 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 16e82c4b5..76ad55ee9 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -1038,7 +1038,7 @@ Blockly.BlockSvg.TOP_LEFT_CORNER_HIGHLIGHT = Blockly.BlockSvg.INNER_TOP_LEFT_CORNER = Blockly.BlockSvg.NOTCH_PATH_RIGHT + ' h -' + (Blockly.BlockSvg.NOTCH_WIDTH - 15 - Blockly.BlockSvg.CORNER_RADIUS) + - ' h -0.5 a ' + Blockly.BlockSvg.CORNER_RADIUS + ',' + + ' a ' + Blockly.BlockSvg.CORNER_RADIUS + ',' + Blockly.BlockSvg.CORNER_RADIUS + ' 0 0,0 -' + Blockly.BlockSvg.CORNER_RADIUS + ',' + Blockly.BlockSvg.CORNER_RADIUS; diff --git a/core/events.js b/core/events.js index 713f3e2ac..2d0ae0b1b 100644 --- a/core/events.js +++ b/core/events.js @@ -59,29 +59,88 @@ Blockly.Events.CHANGE = 'change'; Blockly.Events.MOVE = 'move'; /** - * Create a custom event and fire it. - * @param {!Blockly.Events.Abstract} detail Custom data for event. + * List of events queued for firing. + * @private */ -Blockly.Events.fire = function(detail) { - if (!Blockly.Events.isEnabled() || detail.isNull()) { +Blockly.Events.FIRE_QUEUE_ = []; + +/** + * PID of next scheduled firing. + * @private + */ +Blockly.Events.fireTask_ = null; + +/** + * Create a custom event and fire it. + * @param {!Blockly.Events.Abstract} event Custom data for event. + */ +Blockly.Events.fire = function(event) { + if (!Blockly.Events.isEnabled()) { return; } - console.log(detail); - var workspace = Blockly.Workspace.getById(detail.workspaceId); - if (workspace.rendered) { - // Create a custom event in a browser-compatible way. - if (typeof CustomEvent == 'function') { - // W3 - var evt = new CustomEvent('blocklyWorkspaceChange', {'detail': detail}); - } else { - // MSIE - var evt = document.createEvent('CustomEvent'); - evt.initCustomEvent(eventName, false, false, detail); - } - workspace.getCanvas().dispatchEvent(evt); + Blockly.Events.FIRE_QUEUE_.push(event); + if (Blockly.Events.fireTask_ === null) { + Blockly.Events.fireTask_ = setTimeout(Blockly.Events.fireNow_, 0); } }; +/** + * Fire all queued events. + * @private + */ +Blockly.Events.fireNow_ = function() { + var queue = Blockly.Events.filter_(Blockly.Events.FIRE_QUEUE_); + Blockly.Events.FIRE_QUEUE_.length = 0; + Blockly.Events.fireTask_ = null; + for (var i = 0, detail; detail = queue[i]; i++) { + console.log(detail); + var workspace = Blockly.Workspace.getById(detail.workspaceId); + if (workspace.rendered) { + // Create a custom event in a browser-compatible way. + if (typeof CustomEvent == 'function') { + // W3 + var evt = new CustomEvent('blocklyWorkspaceChange', {'detail': detail}); + } else { + // MSIE + var evt = document.createEvent('CustomEvent'); + evt.initCustomEvent(eventName, false, false, detail); + } + workspace.getCanvas().dispatchEvent(evt); + } + } +}; + +/** + * Filter the queued events and merge duplicates. + * @param {!Array.} queueIn Array of events. + * @return {!Array.} Array of filtered events. + * @private + */ +Blockly.Events.filter_ = function(queueIn) { + var queue = goog.array.clone(queueIn); + // Merge duplicates. O(n^2), but n should be very small. + for (var i = 0, event1; event1 = queue[i]; i++) { + for (var j = i + 1, event2; event2 = queue[j]; j++) { + if (event1.type == Blockly.Events.MOVE && + event2.type == Blockly.Events.MOVE && + event1.blockId == event2.blockId) { + event1.newParentId = event2.newParentId; + event1.newInputName = event2.newInputName; + event1.newCoordinate = event2.newCoordinate; + queue.splice(j, 1); + j--; + } + } + } + // Remove null events. + for (var i = queue.length - 1; i >= 0; i--) { + if (queue[i].isNull()) { + queue.splice(i, 1); + } + } + return queue; +}; + /** * Stop sending events. Every call to this function MUST also call enable. */ diff --git a/core/field_angle.js b/core/field_angle.js index f39991a37..282a6462d 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -236,8 +236,10 @@ Blockly.FieldAngle.prototype.onMouseMove = function(e) { } angle = Blockly.FieldAngle.angleValidator(angle); Blockly.FieldTextInput.htmlInput_.value = angle; + this.sourceBlock_.setShadow(false); this.setValue(angle); this.validate_(); + this.resizeEditor_(); }; /**