From 07d662462585bcb089d4f8b82bfb4d385a0a1bc2 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 12:50:00 -0500 Subject: [PATCH] Fixing bug where VarCreate event does not fire when adding a block with pre-existing variables from the flyout into the workspace. --- core/flyout_base.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/flyout_base.js b/core/flyout_base.js index 1ec79c31b..7e01a6b10 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -606,6 +606,7 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { Blockly.Flyout.prototype.createBlock = function(originalBlock) { var newBlock = null; Blockly.Events.disable(); + var variablesBeforeCreation = this.workspace_.getAllVariables(); this.targetWorkspace_.setResizesEnabled(false); try { newBlock = this.placeNewBlock_(originalBlock); @@ -615,9 +616,28 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } + var variablesAfterCreation = this.workspace_.getAllVariables(); + var variablesToFireVarCreate = []; + if (variablesBeforeCreation.length != variablesAfterCreation.length) { + for (var i = 0; i < variablesAfterCreation.length; i++) { + var variable = variablesAfterCreation[i]; + // for any variable that is present in the list of variables + // after creation but is not present in the list of variables before + // creation, add the variable to the list we will traverse to + // fire the VarCreate event + if (!variablesBeforeCreation.includes(variable)) { + variablesToFireVarCreate.push(variable); + } + } + } + if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); Blockly.Events.fire(new Blockly.Events.Create(newBlock)); + for(var i = 0; i < variablesToFireVarCreate.length; i++) { + var thisVariable = variablesToFireVarCreate[i]; + Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable)); + } } if (this.autoClose) { this.hide();