This commit is contained in:
Rachel Fenichel
2018-01-26 15:20:19 -08:00
parent 5913f42b0a
commit 3b168f5370
8 changed files with 41 additions and 49 deletions

View File

@@ -47,20 +47,13 @@ Blockly.Variables.NAME_TYPE = Blockly.VARIABLE_CATEGORY_NAME;
/**
* 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.
* To get a list of all variables on a workspace, including unused variables,
* call Workspace.getAllVariables.
* @param {!Blockly.Workspace} ws The workspace to search for variables.
* @return {!Array.<!Blockly.VariableModel>} Array of variable models.
*/
Blockly.Variables.allUsedVariables = function(root) {
var blocks;
if (root instanceof Blockly.Block) {
// Root is Block.
blocks = root.getDescendants();
} else if (root.getAllBlocks) {
// Root is Workspace.
blocks = root.getAllBlocks();
} else {
throw 'Not Block or Workspace: ' + root;
}
Blockly.Variables.allUsedVarModels = function(ws) {
var blocks = ws.getAllBlocks();
var variableHash = Object.create(null);
// Iterate through every block and add each variable to the hash.
for (var x = 0; x < blocks.length; x++) {
@@ -68,36 +61,32 @@ Blockly.Variables.allUsedVariables = function(root) {
if (blockVariables) {
for (var y = 0; y < blockVariables.length; y++) {
var variable = blockVariables[y];
// Variable ID may be null if the block is only half-built.
if (variable.getId()) {
variableHash[variable.name.toLowerCase()] = variable.name;
variableHash[variable.getId()] = variable;
}
}
}
}
// Flatten the hash into a list.
var variableList = [];
for (var name in variableHash) {
variableList.push(variableHash[name]);
for (var id in variableHash) {
variableList.push(variableHash[id]);
}
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.<Blockly.VariableModel>} Array of variable models.
* Find all user-created variables that are in use in the workspace and return
* only their names.
* For use by generators.
* To get a list of all variables on a workspace, including unused variables,
* call Workspace.getAllVariables.
* @deprecated January 2018
*/
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 {};
}
return root.getAllVariables();
Blockly.Variables.allUsedVariables = function() {
console.warn('Deprecated call to Blockly.Variables.allUsedVariables. ' +
'Use Blockly.Variables.allUsedVarModels instead.\nIf this is a major ' +
'issue please file a bug on GitHub.');
};
/**

View File

@@ -42,7 +42,8 @@ goog.require('goog.dom');
*/
Blockly.Xml.workspaceToDom = function(workspace, opt_noId) {
var xml = goog.dom.createDom('xml');
xml.appendChild(Blockly.Xml.variablesToDom(workspace.getAllVariables()));
xml.appendChild(Blockly.Xml.variablesToDom(
Blockly.Variables.allUsedVarModels(workspace)));
var blocks = workspace.getTopBlocks(true);
for (var i = 0, block; block = blocks[i]; i++) {
xml.appendChild(Blockly.Xml.blockToDomWithXY(block, opt_noId));

View File

@@ -112,8 +112,8 @@ Blockly.Dart.init = function(workspace) {
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
// Add user variables.
var variables = workspace.getAllVariables();
// Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.Dart.variableDB_.getName(variables[i].getId(),
Blockly.Variables.NAME_TYPE));

View File

@@ -165,8 +165,8 @@ Blockly.JavaScript.init = function(workspace) {
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
// Add user variables.
var variables = workspace.getAllVariables();
// Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.JavaScript.variableDB_.getName(variables[i].getId(),
Blockly.Variables.NAME_TYPE));

View File

@@ -159,8 +159,8 @@ Blockly.PHP.init = function(workspace) {
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ';');
}
// Add user-created variables.
var variables = workspace.getAllVariables();
// Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0, variable; variable = variables[i]; i++) {
defvars.push(Blockly.PHP.variableDB_.getName(variable.getId(),
Blockly.Variables.NAME_TYPE) + ';');

View File

@@ -35,7 +35,7 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
var globals = [];
var varName;
var workspace = block.workspace;
var variables = workspace.getAllVariables() || [];
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
for (var i = 0, variable; variable = variables[i]; i++) {
varName = variable.name;
if (block.arguments_.indexOf(varName) == -1) {
@@ -49,7 +49,8 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
globals.push(Blockly.PHP.variableDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
globals = globals.length ? Blockly.PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
globals = globals.length ?
Blockly.PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
@@ -57,8 +58,8 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
if (Blockly.PHP.STATEMENT_PREFIX) {
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
branch = Blockly.PHP.prefixLines(
Blockly.PHP.STATEMENT_PREFIX.replace(/%1/g,
'\'' + id + '\''), Blockly.PHP.INDENT) + branch;
Blockly.PHP.STATEMENT_PREFIX.replace(
/%1/g, '\'' + id + '\''), Blockly.PHP.INDENT) + branch;
}
if (Blockly.PHP.INFINITE_LOOP_TRAP) {
branch = Blockly.PHP.INFINITE_LOOP_TRAP.replace(/%1/g,

View File

@@ -170,8 +170,8 @@ Blockly.Python.init = function(workspace) {
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None');
}
// Add user-created variables.
var variables = workspace.getAllVariables();
// Add user variables, but only ones that are being used.
var variables = Blockly.Variables.allUsedVarModels(workspace);
for (var i = 0; i < variables.length; i++) {
defvars.push(Blockly.Python.variableDB_.getName(variables[i].getId(),
Blockly.Variables.NAME_TYPE) + ' = None');

View File

@@ -36,7 +36,7 @@ Blockly.Python['procedures_defreturn'] = function(block) {
var globals = [];
var varName;
var workspace = block.workspace;
var variables = workspace.getAllVariables() || [];
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
for (var i = 0, variable; variable = variables[i]; i++) {
varName = variable.name;
if (block.arguments_.indexOf(varName) == -1) {
@@ -51,15 +51,16 @@ Blockly.Python['procedures_defreturn'] = function(block) {
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
}
globals = globals.length ? Blockly.Python.INDENT + 'global ' + globals.join(', ') + '\n' : '';
var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
Blockly.Procedures.NAME_TYPE);
globals = globals.length ?
Blockly.Python.INDENT + 'global ' + globals.join(', ') + '\n' : '';
var funcName = Blockly.Python.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var branch = Blockly.Python.statementToCode(block, 'STACK');
if (Blockly.Python.STATEMENT_PREFIX) {
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
branch = Blockly.Python.prefixLines(
Blockly.Python.STATEMENT_PREFIX.replace(/%1/g,
'\'' + id + '\''), Blockly.Python.INDENT) + branch;
Blockly.Python.STATEMENT_PREFIX.replace(
/%1/g, '\'' + id + '\''), Blockly.Python.INDENT) + branch;
}
if (Blockly.Python.INFINITE_LOOP_TRAP) {
branch = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,