Fix missing events for variable creation

This commit is contained in:
Rachel Fenichel
2017-12-06 16:41:17 -08:00
parent abc83b12d7
commit 0909af76e5
3 changed files with 38 additions and 29 deletions

View File

@@ -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.<!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.
@@ -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);

View File

@@ -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.<!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.
* @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;
};

View File

@@ -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;
};