Ensure last variable category is last variable added.

Resolves #2050.
This commit is contained in:
Neil Fraser
2019-07-13 00:16:42 -07:00
committed by Neil Fraser
parent 2ac3447383
commit 2d3b514425

View File

@@ -188,17 +188,18 @@ Blockly.VariableMap.prototype.createVariable = function(name,
if (opt_id && this.getVariableById(opt_id)) {
throw Error('Variable id, "' + opt_id + '", is already in use.');
}
opt_id = opt_id || Blockly.utils.genUid();
opt_type = opt_type || '';
var id = opt_id || Blockly.utils.genUid();
var type = opt_type || '';
variable = new Blockly.VariableModel(this.workspace, name, type, id);
var variables = this.variableMap_[type] || [];
variables.push(variable);
// Delete the list of variables of this type, and re-add it so that
// the most recent addition is at the end.
// This is used so the toolbox's set block is set to the most recent variable.
delete this.variableMap_[type];
this.variableMap_[type] = variables;
variable = new Blockly.VariableModel(this.workspace, name, opt_type, opt_id);
// If opt_type is not a key, create a new list.
if (!this.variableMap_[opt_type]) {
this.variableMap_[opt_type] = [variable];
} else {
// Else append the variable to the preexisting list.
this.variableMap_[opt_type].push(variable);
}
return variable;
};