Merge pull request from marisaleung/develop_refreshToolbox

Add customcontextmenu in flyout for variable getter.
This commit is contained in:
marisaleung 2017-07-17 10:26:11 -07:00 committed by GitHub
commit a64dd54db4
3 changed files with 67 additions and 12 deletions

View file

@ -456,7 +456,10 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
* @this Blockly.Block
*/
customContextMenu: function(options) {
if (!this.isCollapsed()) {
if (this.isCollapsed()) {
return;
}
if (!this.isInFlyout) {
var variablesList = this.workspace.getVariablesOfType('');
for (var i = 0; i < variablesList.length; i++) {
var option = {enabled: true};
@ -467,6 +470,20 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
option.text);
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);
};
};
/**
* 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);
};
};

View file

@ -729,10 +729,9 @@ 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);
}
Blockly.ContextMenu.show(e, menuOptions, this.RTL);
Blockly.ContextMenu.currentBlock = this;
};

View file

@ -194,15 +194,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.
@ -217,3 +210,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);
}
});
};