mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
Addressing PR comment. Moving functionality to get newly added variables into a helper function.
This commit is contained in:
@@ -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.<Blockly.VariableModel>} originalVariables The array of
|
||||
* variables that existed in the workspace before adding the new block.
|
||||
* @return {!Array.<Blockly.VariableModel>} 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));
|
||||
|
||||
Reference in New Issue
Block a user