mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-13 22:49:02 -04:00
Names are correctly fetched from VariableModels!
This commit is contained in:
parent
ee58eb4791
commit
2bb258165a
2 changed files with 29 additions and 19 deletions
|
@ -134,30 +134,36 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) {
|
|||
* @this {Blockly.FieldVariable}
|
||||
*/
|
||||
Blockly.FieldVariable.dropdownCreate = function() {
|
||||
var variableNameList = [];
|
||||
if (this.sourceBlock_ && this.sourceBlock_.workspace) {
|
||||
// Get a copy of the list, so that adding rename and new variable options
|
||||
// doesn't modify the workspace's list.
|
||||
var variableList = this.sourceBlock_.workspace.getVariablesOfType('');
|
||||
} else {
|
||||
var variableList = [];
|
||||
|
||||
var variableModelList = this.sourceBlock_.workspace.getVariablesOfType('');
|
||||
for (var i = 0; i < variableModelList.length; i++) {
|
||||
variableNameList.push(variableModelList[i].name);
|
||||
}
|
||||
}
|
||||
// Ensure that the currently selected variable is an option.
|
||||
var name = this.getText();
|
||||
if (name && variableList.indexOf(name) == -1) {
|
||||
variableList.push(name);
|
||||
if (name && variableNameList.indexOf(name) == -1) {
|
||||
variableNameList.push(name);
|
||||
}
|
||||
variableList.sort(goog.string.caseInsensitiveCompare);
|
||||
variableNameList.sort(goog.string.caseInsensitiveCompare);
|
||||
|
||||
this.renameVarItemIndex_ = variableList.length;
|
||||
variableList.push(Blockly.Msg.RENAME_VARIABLE);
|
||||
this.renameVarItemIndex_ = variableNameList.length;
|
||||
variableNameList.push(Blockly.Msg.RENAME_VARIABLE);
|
||||
|
||||
this.deleteVarItemIndex_ = variableList.length;
|
||||
variableList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name));
|
||||
this.deleteVarItemIndex_ = variableNameList.length;
|
||||
variableNameList.push(Blockly.Msg.DELETE_VARIABLE.replace('%1', name));
|
||||
// Variables are not language-specific, use the name as both the user-facing
|
||||
// text and the internal representation.
|
||||
var options = [];
|
||||
for (var i = 0; i < variableList.length; i++) {
|
||||
options[i] = [variableList[i], variableList[i]];
|
||||
for (var i = 0; i < variableNameList.length; i++) {
|
||||
// TODO(marisaleung): Set options[i] to [name, uuid]. This requires
|
||||
// changes where the variable gets set since the initialized value would be
|
||||
// id.
|
||||
options[i] = [variableNameList[i], variableNameList[i]];
|
||||
}
|
||||
return options;
|
||||
};
|
||||
|
|
|
@ -105,8 +105,12 @@ Blockly.Variables.allVariables = function(root) {
|
|||
* @return {!Array.<!Element>} Array of XML block elements.
|
||||
*/
|
||||
Blockly.Variables.flyoutCategory = function(workspace) {
|
||||
var variableList = workspace.getVariablesOfType('');
|
||||
variableList.sort(goog.string.caseInsensitiveCompare);
|
||||
var variableNameList = [];
|
||||
var variableModelList = workspace.getVariablesOfType('');
|
||||
for (var i = 0; i < variableModelList.length; i++) {
|
||||
variableNameList.push(variableModelList[i].name);
|
||||
}
|
||||
variableNameList.sort(goog.string.caseInsensitiveCompare);
|
||||
|
||||
var xmlList = [];
|
||||
var button = goog.dom.createDom('button');
|
||||
|
@ -119,12 +123,12 @@ Blockly.Variables.flyoutCategory = function(workspace) {
|
|||
|
||||
xmlList.push(button);
|
||||
|
||||
if (variableList.length > 0) {
|
||||
if (variableNameList.length > 0) {
|
||||
if (Blockly.Blocks['variables_set']) {
|
||||
var gap = Blockly.Blocks['math_change'] ? 8 : 24;
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_set" gap="' + gap + '">' +
|
||||
'<field name="VAR">' + variableList[0] + '</field>' +
|
||||
'<field name="VAR">' + variableNameList[0] + '</field>' +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
|
@ -134,7 +138,7 @@ Blockly.Variables.flyoutCategory = function(workspace) {
|
|||
var gap = Blockly.Blocks['variables_get'] ? 20 : 8;
|
||||
var blockText = '<xml>' +
|
||||
'<block type="math_change" gap="' + gap + '">' +
|
||||
'<field name="VAR">' + variableList[0] + '</field>' +
|
||||
'<field name="VAR">' + variableNameList[0] + '</field>' +
|
||||
'<value name="DELTA">' +
|
||||
'<shadow type="math_number">' +
|
||||
'<field name="NUM">1</field>' +
|
||||
|
@ -146,11 +150,11 @@ Blockly.Variables.flyoutCategory = function(workspace) {
|
|||
xmlList.push(block);
|
||||
}
|
||||
|
||||
for (var i = 0; i < variableList.length; i++) {
|
||||
for (var i = 0; i < variableNameList.length; i++) {
|
||||
if (Blockly.Blocks['variables_get']) {
|
||||
var blockText = '<xml>' +
|
||||
'<block type="variables_get" gap="8">' +
|
||||
'<field name="VAR">' + variableList[i] + '</field>' +
|
||||
'<field name="VAR">' + variableNameList[i] + '</field>' +
|
||||
'</block>' +
|
||||
'</xml>';
|
||||
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue