From aae1b1bb74c8dfd658cfce3cfb10c390353390a3 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 14:51:11 -0500 Subject: [PATCH] Addressing PR comment. Moving functionality to get newly added variables into a helper function. --- core/flyout_base.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index fe9a17bcf..305be33e0 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -596,6 +596,33 @@ 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. @@ -616,24 +643,12 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } - var variablesAfterCreation = this.targetWorkspace_.getAllVariables(); - var newVariables = []; - 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)) { - newVariables.push(variable); - } - } - } + var newVariables = this.getAddedVariables_(variablesBeforeCreation); if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); Blockly.Events.fire(new Blockly.Events.Create(newBlock)); + // 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));