From 9a1d6684d0610e4e28863c62e6e982b0e2d17919 Mon Sep 17 00:00:00 2001 From: alschmiedt Date: Thu, 29 Nov 2018 14:17:56 -0800 Subject: [PATCH] Pulls from MakeCode fork the ability to rename and delete variables in flyout --- blocks/variables.js | 89 ++++++++++++++++++++++++--------- blocks/variables_dynamic.js | 99 +++++++++++++++++++++++++++---------- 2 files changed, 139 insertions(+), 49 deletions(-) diff --git a/blocks/variables.js b/blocks/variables.js index 7d0c8d36f..0d8e0d950 100644 --- a/blocks/variables.js +++ b/blocks/variables.js @@ -100,31 +100,76 @@ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { * @this Blockly.Block */ customContextMenu: function(options) { - if (this.isInFlyout){ - return; - } - // Getter blocks have the option to create a setter block, and vice versa. - if (this.type == 'variables_get') { - var opposite_type = 'variables_set'; - var contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET']; - } else { - var opposite_type = 'variables_get'; - var contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET']; - } + if (!this.isInFlyout){ + // Getter blocks have the option to create a setter block, and vice versa. + if (this.type == 'variables_get') { + var opposite_type = 'variables_set'; + var contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET']; + } else { + var opposite_type = 'variables_get'; + var contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET']; + } - var option = {enabled: this.workspace.remainingCapacity() > 0}; - var name = this.getField('VAR').getText(); - option.text = contextMenuMsg.replace('%1', name); - var xmlField = document.createElement('field'); - xmlField.setAttribute('name', 'VAR'); - xmlField.appendChild(document.createTextNode(name)); - var xmlBlock = document.createElement('block'); - xmlBlock.setAttribute('type', opposite_type); - xmlBlock.appendChild(xmlField); - option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); - options.push(option); + var option = {enabled: this.workspace.remainingCapacity() > 0}; + var name = this.getField('VAR').getText(); + option.text = contextMenuMsg.replace('%1', name); + var xmlField = document.createElement('field'); + xmlField.setAttribute('name', 'VAR'); + xmlField.appendChild(document.createTextNode(name)); + var xmlBlock = document.createElement('block'); + xmlBlock.setAttribute('type', opposite_type); + xmlBlock.appendChild(xmlField); + option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); + options.push(option); + // Getter blocks have the option to rename or delete that variable. + } else { + if (this.type == 'variables_get' || this.type == 'variables_get_reporter'){ + var renameOption = { + text: Blockly.Msg.RENAME_VARIABLE, + enabled: true, + callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this) + }; + var name = this.getField('VAR').getText(); + var deleteOption = { + text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name), + enabled: true, + callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this) + }; + options.unshift(renameOption); + options.unshift(deleteOption); + } + } } }; +/** + * 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.Variables.RENAME_OPTION_CALLBACK_FACTORY = function(block) { + return function() { + var workspace = block.workspace; + var variable = block.getField('VAR').getVariable(); + Blockly.Variables.renameVariable(workspace, variable); + }; +}; + +/** + * 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.Variables.DELETE_OPTION_CALLBACK_FACTORY = function(block) { + return function() { + var workspace = block.workspace; + var variable = block.getField('VAR').getVariable(); + workspace.deleteVariableById(variable.getId()); + workspace.refreshToolboxSelection(); + }; +}; + Blockly.Extensions.registerMixin('contextMenu_variableSetterGetter', Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN); diff --git a/blocks/variables_dynamic.js b/blocks/variables_dynamic.js index 5aaa0aab0..fe30b8f22 100644 --- a/blocks/variables_dynamic.js +++ b/blocks/variables_dynamic.js @@ -97,34 +97,50 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI */ customContextMenu: function(options) { // Getter blocks have the option to create a setter block, and vice versa. - if (this.isInFlyout) { - return; - } - var opposite_type; - var contextMenuMsg; - var id = this.getFieldValue('VAR'); - var variableModel = this.workspace.getVariableById(id); - var varType = variableModel.type; - if (this.type == 'variables_get_dynamic') { - opposite_type = 'variables_set_dynamic'; - contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET']; - } else { - opposite_type = 'variables_get_dynamic'; - contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET']; - } + if (!this.isInFlyout) { + var opposite_type; + var contextMenuMsg; + var id = this.getFieldValue('VAR'); + var variableModel = this.workspace.getVariableById(id); + var varType = variableModel.type; + if (this.type == 'variables_get_dynamic') { + opposite_type = 'variables_set_dynamic'; + contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET']; + } else { + opposite_type = 'variables_get_dynamic'; + contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET']; + } - var option = {enabled: this.workspace.remainingCapacity() > 0}; - var name = this.getField('VAR').getText(); - option.text = contextMenuMsg.replace('%1', name); - var xmlField = document.createElement('field'); - xmlField.setAttribute('name', 'VAR'); - xmlField.setAttribute('variabletype', varType); - xmlField.appendChild(document.createTextNode(name)); - var xmlBlock = document.createElement('block'); - xmlBlock.setAttribute('type', opposite_type); - xmlBlock.appendChild(xmlField); - option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); - options.push(option); + var option = {enabled: this.workspace.remainingCapacity() > 0}; + var name = this.getField('VAR').getText(); + option.text = contextMenuMsg.replace('%1', name); + var xmlField = document.createElement('field'); + xmlField.setAttribute('name', 'VAR'); + xmlField.setAttribute('variabletype', varType); + xmlField.appendChild(document.createTextNode(name)); + var xmlBlock = document.createElement('block'); + xmlBlock.setAttribute('type', opposite_type); + xmlBlock.appendChild(xmlField); + option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); + options.push(option); + } else { + if (this.type == 'variables_get_dynamic' || + this.type == 'variables_get_reporter_dynamic') { + var renameOption = { + text: Blockly.Msg.RENAME_VARIABLE, + enabled: true, + callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this) + }; + var name = this.getField('VAR').getText(); + var deleteOption = { + text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name), + enabled: true, + callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this) + }; + options.unshift(renameOption); + options.unshift(deleteOption); + } + } }, onchange: function() { var id = this.getFieldValue('VAR'); @@ -137,5 +153,34 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI } }; +/** + * 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.VariablesDynamic.RENAME_OPTION_CALLBACK_FACTORY = function(block) { + return function() { + var workspace = block.workspace; + var variable = block.getField('VAR').getVariable(); + Blockly.Variables.renameVariable(workspace, variable); + }; +}; + +/** + * 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.VariablesDynamic.DELETE_OPTION_CALLBACK_FACTORY = function(block) { + return function() { + var workspace = block.workspace; + var variable = block.getField('VAR').getVariable(); + workspace.deleteVariableById(variable.getId()); + workspace.refreshToolboxSelection(); + }; +}; + Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter', Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);