Merge pull request #472 from rachel-fenichel/feature/all_variables

Distinguish between allVariables and allUsedVariables
This commit is contained in:
rachel-fenichel 2016-07-13 12:54:37 -07:00 committed by GitHub
commit 97801a0e2f
2 changed files with 21 additions and 4 deletions

View file

@ -37,13 +37,14 @@ goog.require('goog.string');
Blockly.Variables.NAME_TYPE = 'VARIABLE';
/**
* Find all user-created variables.
* Find all user-created variables that are in use in the workspace.
* For use by generators.
* @param {!Blockly.Block|!Blockly.Workspace} root Root block or workspace.
* @return {!Array.<string>} Array of variable names.
*/
Blockly.Variables.allVariables = function(root) {
Blockly.Variables.allUsedVariables = function(root) {
var blocks;
if (root.getDescendants) {
if (root instanceof Blockly.Block) {
// Root is Block.
blocks = root.getDescendants();
} else if (root.getAllBlocks) {
@ -74,6 +75,22 @@ Blockly.Variables.allVariables = function(root) {
return variableList;
};
/**
* Find all variables that the user has created through the workspace or
* toolbox. For use by generators.
* @param {!Blockly.Workspace} root The workspace to inspect.
* @return {!Array.<string>} Array of variable names.
*/
Blockly.Variables.allVariables = function(root) {
if (root instanceof Blockly.Block) {
// Root is Block.
console.warn('Deprecated call to Blockly.Variables.allVariables ' +
'with a block instead of a workspace. You may want ' +
'Blockly.Variables.allUsedVariables');
}
return root.variableList;
};
/**
* Find all instances of the specified variable and rename them.
* @param {string} oldName Variable to rename.

View file

@ -119,7 +119,7 @@ Blockly.Workspace.SCAN_ANGLE = 3;
Blockly.Workspace.prototype.addTopBlock = function(block) {
this.topBlocks_.push(block);
if (this.isFlyout) {
var variables = Blockly.Variables.allVariables(block);
var variables = Blockly.Variables.allUsedVariables(block);
for (var i = 0; i < variables.length; i++) {
if (this.variableList.indexOf(variables[i]) == -1) {
this.variableList.push(variables[i]);