From 676253ec32d21dfde84bf723922c3a7dc1836686 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 6 Dec 2017 16:41:17 -0800 Subject: [PATCH] Fix missing events for variable creation --- core/flyout_base.js | 30 ++---------------------------- core/variables.js | 29 ++++++++++++++++++++++++++++- core/xml.js | 8 ++++++++ 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index 8b1421e39..dacab6169 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -599,33 +599,6 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { } }; -/** - * Helper function to get the list of variables that have been added to the - * workspace after adding a new block, using the given list of variables that - * were in the workspace before the new block was added. - * @param {!Array.} originalVariables The array of - * variables that existed in the workspace before adding the new block. - * @return {!Array.} The new array of variables that were - * freshly added to the workspace after creating the new block, or [] if no - * new variables were added to the workspace. - * @private - */ -Blockly.Flyout.prototype.getAddedVariables_ = function(originalVariables) { - var allCurrentVariables = this.targetWorkspace_.getAllVariables(); - var addedVariables = []; - if (originalVariables.length != allCurrentVariables.length) { - for (var i = 0; i < allCurrentVariables.length; i++) { - var variable = allCurrentVariables[i]; - // For any variable that is present in allCurrentVariables but not - // present in originalVariables, add the variable to addedVariables. - if (!originalVariables.includes(variable)) { - addedVariables.push(variable); - } - } - } - return addedVariables; -}; - /** * Create a copy of this block on the workspace. * @param {!Blockly.BlockSvg} originalBlock The block to copy from the flyout. @@ -646,7 +619,8 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } - var newVariables = this.getAddedVariables_(variablesBeforeCreation); + var newVariables = Blockly.Variables.getAddedVariables(this.targetWorkspace_, + variablesBeforeCreation); if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); diff --git a/core/variables.js b/core/variables.js index c6e3462da..562068660 100644 --- a/core/variables.js +++ b/core/variables.js @@ -440,5 +440,32 @@ Blockly.Variables.createVariable_ = function(workspace, id, name, type) { Blockly.Variables.getPotentialVariableMap_ = function(workspace) { return workspace.isFlyout && workspace.targetWorkspace ? workspace.targetWorkspace.getPotentialVariableMap() : null; - +}; + +/** + * Helper function to get the list of variables that have been added to the + * workspace after adding a new block, using the given list of variables that + * were in the workspace before the new block was added. + * @param {!Blockly.Workspace} workspace The workspace to inspect. + * @param {!Array.} originalVariables The array of + * variables that existed in the workspace before adding the new block. + * @return {!Array.} The new array of variables that were + * freshly added to the workspace after creating the new block, or [] if no + * new variables were added to the workspace. + * @package + */ +Blockly.Variables.getAddedVariables = function(workspace, originalVariables) { + var allCurrentVariables = workspace.getAllVariables(); + var addedVariables = []; + if (originalVariables.length != allCurrentVariables.length) { + for (var i = 0; i < allCurrentVariables.length; i++) { + var variable = allCurrentVariables[i]; + // For any variable that is present in allCurrentVariables but not + // present in originalVariables, add the variable to addedVariables. + if (!originalVariables.includes(variable)) { + addedVariables.push(variable); + } + } + } + return addedVariables; }; diff --git a/core/xml.js b/core/xml.js index e1eaaa3a3..db8121131 100644 --- a/core/xml.js +++ b/core/xml.js @@ -497,6 +497,7 @@ Blockly.Xml.domToBlock = function(xmlBlock, workspace) { } // Create top-level block. Blockly.Events.disable(); + var variablesBeforeCreation = workspace.getAllVariables(); try { var topBlock = Blockly.Xml.domToBlockHeadless_(xmlBlock, workspace); if (workspace.rendered) { @@ -529,6 +530,13 @@ Blockly.Xml.domToBlock = function(xmlBlock, workspace) { } if (Blockly.Events.isEnabled()) { Blockly.Events.fire(new Blockly.Events.BlockCreate(topBlock)); + var newVariables = Blockly.Variables.getAddedVariables(workspace, + variablesBeforeCreation); + // Fire a VarCreate event for each (if any) new variable created. + for(var i = 0; i < newVariables.length; i++) { + var thisVariable = newVariables[i]; + Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable)); + } } return topBlock; };