Modularize Rename Variable prompt and allow custom context menus for flyout.

This commit is contained in:
marisaleung
2017-07-06 15:41:18 -07:00
parent 389a41eedb
commit 170b16706f
2 changed files with 20 additions and 10 deletions

View File

@@ -690,7 +690,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
menuOptions.push(helpOption);
// Allow the block to add or modify menuOptions.
if (this.customContextMenu && !block.isInFlyout) {
if (this.customContextMenu) {
this.customContextMenu(menuOptions);
}

View File

@@ -196,15 +196,8 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
}
else if (id == Blockly.RENAME_VARIABLE_ID) {
// Rename variable.
var oldName = this.getText();
Blockly.hideChaff();
Blockly.Variables.promptName(
Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldName), oldName,
function(newName) {
if (newName) {
workspace.renameVariable(oldName, newName);
}
});
var currentName = this.getText();
Blockly.FieldVariable.renameVariablePrompt(workspace, currentName);
return;
} else if (id == Blockly.DELETE_VARIABLE_ID) {
// Delete variable.
@@ -219,3 +212,20 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
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);
}
});
};