mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Merge pull request #990 from marisaleung/develop_refreshToolbox
Add customcontextmenu in flyout for variable getter.
This commit is contained in:
commit
a64dd54db4
3 changed files with 67 additions and 12 deletions
|
@ -456,7 +456,10 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
|
||||||
* @this Blockly.Block
|
* @this Blockly.Block
|
||||||
*/
|
*/
|
||||||
customContextMenu: function(options) {
|
customContextMenu: function(options) {
|
||||||
if (!this.isCollapsed()) {
|
if (this.isCollapsed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.isInFlyout) {
|
||||||
var variablesList = this.workspace.getVariablesOfType('');
|
var variablesList = this.workspace.getVariablesOfType('');
|
||||||
for (var i = 0; i < variablesList.length; i++) {
|
for (var i = 0; i < variablesList.length; i++) {
|
||||||
var option = {enabled: true};
|
var option = {enabled: true};
|
||||||
|
@ -467,6 +470,20 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
|
||||||
option.text);
|
option.text);
|
||||||
options.push(option);
|
options.push(option);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
var renameOption = {
|
||||||
|
text: Blockly.Msg.RENAME_VARIABLE,
|
||||||
|
enabled: true,
|
||||||
|
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this)
|
||||||
|
};
|
||||||
|
var name = this.getField('VARIABLE').text_;
|
||||||
|
var deleteOption = {
|
||||||
|
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
|
||||||
|
enabled: true,
|
||||||
|
callback: Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY(this)
|
||||||
|
};
|
||||||
|
options.push(renameOption);
|
||||||
|
options.push(deleteOption);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -492,3 +509,32 @@ Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY = function(block, name)
|
||||||
variableField.setText(name);
|
variableField.setText(name);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for rename variable dropdown menu option associated with a
|
||||||
|
* variable getter block.
|
||||||
|
* @param {!Blockly.Block} block The block with the variable to rename.
|
||||||
|
* @return {!function()} A function that renames the variable.
|
||||||
|
*/
|
||||||
|
Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY = function(block) {
|
||||||
|
return function() {
|
||||||
|
var workspace = block.workspace;
|
||||||
|
var currentName = block.getField('VARIABLE').text_;
|
||||||
|
Blockly.FieldVariable.renameVariablePrompt(workspace, currentName);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for delete variable dropdown menu option associated with a
|
||||||
|
* variable getter block.
|
||||||
|
* @param {!Blockly.Block} block The block with the variable to delete.
|
||||||
|
* @return {!function()} A function that deletes the variable.
|
||||||
|
*/
|
||||||
|
Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY = function(block) {
|
||||||
|
return function() {
|
||||||
|
var workspace = block.workspace;
|
||||||
|
var name = block.getField('VARIABLE').text_;
|
||||||
|
workspace.deleteVariable(name);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
@ -729,10 +729,9 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
|
||||||
menuOptions.push(helpOption);
|
menuOptions.push(helpOption);
|
||||||
|
|
||||||
// Allow the block to add or modify menuOptions.
|
// Allow the block to add or modify menuOptions.
|
||||||
if (this.customContextMenu && !block.isInFlyout) {
|
if (this.customContextMenu) {
|
||||||
this.customContextMenu(menuOptions);
|
this.customContextMenu(menuOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
Blockly.ContextMenu.show(e, menuOptions, this.RTL);
|
Blockly.ContextMenu.show(e, menuOptions, this.RTL);
|
||||||
Blockly.ContextMenu.currentBlock = this;
|
Blockly.ContextMenu.currentBlock = this;
|
||||||
};
|
};
|
||||||
|
|
|
@ -194,15 +194,8 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
|
||||||
}
|
}
|
||||||
else if (id == Blockly.RENAME_VARIABLE_ID) {
|
else if (id == Blockly.RENAME_VARIABLE_ID) {
|
||||||
// Rename variable.
|
// Rename variable.
|
||||||
var oldName = this.getText();
|
var currentName = this.getText();
|
||||||
Blockly.hideChaff();
|
Blockly.FieldVariable.renameVariablePrompt(workspace, currentName);
|
||||||
Blockly.Variables.promptName(
|
|
||||||
Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldName), oldName,
|
|
||||||
function(newName) {
|
|
||||||
if (newName) {
|
|
||||||
workspace.renameVariable(oldName, newName);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
} else if (id == Blockly.DELETE_VARIABLE_ID) {
|
} else if (id == Blockly.DELETE_VARIABLE_ID) {
|
||||||
// Delete variable.
|
// Delete variable.
|
||||||
|
@ -217,3 +210,20 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
|
||||||
this.setValue(itemText);
|
this.setValue(itemText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prompt the user to rename a variable.
|
||||||
|
* @param {Blockly.Workspace} workspace Workspace the variable to rename is in.
|
||||||
|
* @param {string} currentName Current name of the variable to rename.
|
||||||
|
*/
|
||||||
|
Blockly.FieldVariable.renameVariablePrompt = function(workspace, currentName) {
|
||||||
|
// Rename variable.
|
||||||
|
Blockly.hideChaff();
|
||||||
|
Blockly.Variables.promptName(
|
||||||
|
Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', currentName), currentName,
|
||||||
|
function(newName) {
|
||||||
|
if (newName) {
|
||||||
|
workspace.renameVariable(currentName, newName);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue