From 3ae7ac1cd4c1383636b9d70928ff9e21115f7261 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Mon, 4 Dec 2017 15:46:44 -0800 Subject: [PATCH] Make unique variable names in the flyout --- core/field_variable.js | 9 ++++++++- core/flyout_base.js | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/field_variable.js b/core/field_variable.js index 0b9238f02..67604d2a5 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -63,6 +63,7 @@ Blockly.FieldVariable.getOrCreateVariable = function(workspace, text, type, id) { var potentialVariableMap = workspace.isFlyout ? workspace.targetWorkspace.potentialVariableMap_ : null; + if (id) { var variable = workspace.getVariableById(id); if (!variable && potentialVariableMap) { @@ -76,6 +77,12 @@ Blockly.FieldVariable.getOrCreateVariable = function(workspace, text, type, } // Didn't find the variable. if (!variable) { + if (!text) { + var ws = workspace.isFlyout ? workspace.targetWorkspace : workspace; + // Variables without names get uniquely named for this workspace. + text = Blockly.Variables.generateUniqueName(ws); + } + if (potentialVariableMap) { variable = potentialVariableMap.createVariable(text, type, id); } else { @@ -126,7 +133,7 @@ Blockly.FieldVariable.prototype.initModel = function() { } this.workspace_ = this.sourceBlock_.workspace; var variable = Blockly.FieldVariable.getOrCreateVariable( - this.workspace_, this.defaultVariableName, this.defaultType_, null); + this.workspace_, name, this.defaultType_, null); this.setValue(variable.getId()); }; diff --git a/core/flyout_base.js b/core/flyout_base.js index c56ea6253..2e3a33b2c 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -415,6 +415,7 @@ Blockly.Flyout.prototype.hide = function() { this.workspace_.removeChangeListener(this.reflowWrapper_); this.reflowWrapper_ = null; } + // Do NOT delete the blocks here. Wait until Flyout.show. // https://neil.fraser.name/news/2014/08/09/ }; @@ -542,6 +543,9 @@ Blockly.Flyout.prototype.clearOldBlocks_ = function() { button.dispose(); } this.buttons_.length = 0; + + // Clear potential variables from the previous showing. + this.targetWorkspace_.potentialVariableMap_.clear(); }; /**