/** * @license * Copyright 2012 Google LLC * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview Variable blocks for Blockly. * This file is scraped to extract a .json file of block definitions. The array * passed to defineBlocksWithJsonArray(..) must be strict JSON: double quotes * only, no outside references, no functions, no trailing commas, etc. The one * exception is end-of-line comments, which the scraper will remove. * @author fraser@google.com (Neil Fraser) */ 'use strict'; goog.provide('Blockly.Blocks.variables'); // Deprecated. goog.provide('Blockly.Constants.Variables'); goog.require('Blockly'); goog.require('Blockly.Blocks'); goog.require('Blockly.FieldLabel'); goog.require('Blockly.FieldVariable'); /** * Unused constant for the common HSV hue for all blocks in this category. * @deprecated Use Blockly.Msg['VARIABLES_HUE']. (2018 April 5) */ Blockly.Constants.Variables.HUE = 330; Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT // Block for variable getter. { "type": "variables_get", "message0": "%1", "args0": [ { "type": "field_variable", "name": "VAR", "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" } ], "output": null, "outputShape": Blockly.OUTPUT_SHAPE_ROUND, "style": "variable_blocks", "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}", "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}", "extensions": ["contextMenu_variableSetterGetter"] }, // Block for variable getter. { "type": "variables_get_reporter", "message0": "%1", "args0": [ { "type": "field_variable_getter", "name": "VAR", "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" } ], "output": null, "colour": "%{BKY_VARIABLES_HUE}", "outputShape": Blockly.OUTPUT_SHAPE_ROUND, "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}", "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}", "extensions": ["contextMenu_variableReporter"] }, // Block for variable setter. { "type": "variables_set", "message0": "%{BKY_VARIABLES_SET}", "args0": [ { "type": "field_variable", "name": "VAR", "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" }, { "type": "input_value", "name": "VALUE" } ], "previousStatement": null, "nextStatement": null, "style": "variable_blocks", "tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}", "helpUrl": "%{BKY_VARIABLES_SET_HELPURL}", "extensions": ["contextMenu_variableSetterGetter"] } ]); // END JSON EXTRACT (Do not delete this comment.) /** * Mixin to add context menu items to create getter/setter blocks for this * setter/getter. * Used by blocks 'variables_set' and 'variables_get'. * @mixin * @augments Blockly.Block * @package * @readonly */ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { /** * Add menu option to create getter/setter block for this setter/getter. * @param {!Array} options List of menu options to add to. * @this {Blockly.Block} */ customContextMenu: function(options) { if (this.isCollapsed()) { return; } if (!this.isInFlyout) { // Getter blocks have the option to create a setter block, and vice versa. // pxt-blockly: handle variables_get_reporter if (this.type == 'variables_get' || this.type == 'variables_get_reporter') { 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 && !this.inDebugWorkspace()}; var name = this.getField('VAR').getText(); option.text = contextMenuMsg.replace('%1', name); var xmlField = Blockly.utils.xml.createElement('field'); xmlField.setAttribute('name', 'VAR'); xmlField.appendChild(Blockly.utils.xml.createTextNode(name)); var xmlBlock = Blockly.utils.xml.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: !this.inDebugWorkspace(), 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: !this.inDebugWorkspace(), callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this) }; options.unshift(renameOption); options.unshift(deleteOption); } } } }; /** * pxt-blockly: * Mixin to add context menu items to select a different variable * setter/getter. * Used by blocks 'variables_get_reporter'. * @mixin * @augments Blockly.Block * @package * @readonly */ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_REPORTER_MIXIN = { /** * Add menu option to create getter/setter block for this setter/getter. * @param {!Array} options List of menu options to add to. * @this Blockly.Block */ customContextMenu: function(options) { if (this.isCollapsed()) { return; } var renameOption = { text: Blockly.Msg.RENAME_VARIABLE, enabled: !this.inDebugWorkspace(), callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this) }; options.unshift(renameOption); if (!this.isInFlyout) { var variablesList = this.workspace.getVariablesOfType(''); if (variablesList.length > 0) { var separator = {separator: true}; options.unshift(separator); } for (var i = variablesList.length - 1; i >= 0; i--) { var option = {enabled: !this.inDebugWorkspace()}; option.text = variablesList[i].name; option.callback = Blockly.Constants.Variables.VARIABLE_OPTION_CALLBACK_FACTORY(this, option.text, variablesList[i].getId()); options.unshift(option); } } } }; /** * pxt-blockly: * Callback factory for dropdown menu options associated with a variable getter * reporter block. Each variable on the workspace gets its own item in the dropdown * menu, and clicking on that item changes the text of the field on the source * block. * @param {!Blockly.Block} block The block to update. * @param {string} name The new name to display on the block. * @param {string} id The new id of the variable. * @return {!function()} A function that updates the block with the new name. */ Blockly.Constants.Variables.VARIABLE_OPTION_CALLBACK_FACTORY = function(block, name, id) { return function() { var variableField = block.getField('VAR'); if (!variableField) { console.log("Tried to get a variable field on the wrong type of block."); } variableField.setValue(id); }; }; /** * 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); Blockly.Extensions.registerMixin('contextMenu_variableReporter', Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_REPORTER_MIXIN);