From 2d3b5144250a23b3dd74737968c75ab86a31660c Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Sat, 13 Jul 2019 00:16:42 -0700 Subject: [PATCH] Ensure last variable category is last variable added. Resolves #2050. --- core/variable_map.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/core/variable_map.js b/core/variable_map.js index c6f27d26e..aeb5adcba 100644 --- a/core/variable_map.js +++ b/core/variable_map.js @@ -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; };