Files
blockly/blocks/variables.js
Christopher Allen 5078dcbc6d refactor(blocks)!: Rename Blockly.blocks.* modules to Blockly.libraryBlocks.* (#5953)
...and rename Blockly.blocks.all (blocks/all.js) to
Blockly.libraryBlocks (blocks/blocks.js

BREAKING CHANGE: (only) because the exports object from the
`blocks_compressed.js` chunk will be accessed as
`Blockly.libraryBlocks` instead of `Blockly.blocks.all` when the
chunk is loaded in a browser via a `<script>` tag.  There will
be no changes visible when the chunk is loaded via ES module
`import` or CJS `require`.
2022-02-23 21:25:52 +00:00

175 lines
5.4 KiB
JavaScript

/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Variable blocks for Blockly.
* @suppress {checkTypes}
*/
'use strict';
goog.module('Blockly.libraryBlocks.variables');
const ContextMenu = goog.require('Blockly.ContextMenu');
const Extensions = goog.require('Blockly.Extensions');
const Variables = goog.require('Blockly.Variables');
const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {BlockDefinition} = goog.requireType('Blockly.blocks');
const {Msg} = goog.require('Blockly.Msg');
const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldLabel');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldVariable');
/**
* A dictionary of the block definitions provided by this module.
* @type {!Object<string, !BlockDefinition>}
*/
const blocks = createBlockDefinitionsFromJsonArray([
// Block for variable getter.
{
'type': 'variables_get',
'message0': '%1',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': '%{BKY_VARIABLES_DEFAULT_NAME}',
},
],
'output': null,
'style': 'variable_blocks',
'helpUrl': '%{BKY_VARIABLES_GET_HELPURL}',
'tooltip': '%{BKY_VARIABLES_GET_TOOLTIP}',
'extensions': ['contextMenu_variableSetterGetter'],
},
// 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'],
},
]);
exports.blocks = blocks;
/**
* 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 Block
* @package
* @readonly
*/
const 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 {Block}
*/
customContextMenu: function(options) {
if (!this.isInFlyout) {
let oppositeType;
let contextMenuMsg;
// Getter blocks have the option to create a setter block, and vice versa.
if (this.type === 'variables_get') {
oppositeType = 'variables_set';
contextMenuMsg = Msg['VARIABLES_GET_CREATE_SET'];
} else {
oppositeType = 'variables_get';
contextMenuMsg = Msg['VARIABLES_SET_CREATE_GET'];
}
const option = {enabled: this.workspace.remainingCapacity() > 0};
const name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
const xmlField = xmlUtils.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.appendChild(xmlUtils.createTextNode(name));
const xmlBlock = xmlUtils.createElement('block');
xmlBlock.setAttribute('type', oppositeType);
xmlBlock.appendChild(xmlField);
option.callback = 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') {
const renameOption = {
text: Msg['RENAME_VARIABLE'],
enabled: true,
callback: renameOptionCallbackFactory(this),
};
const name = this.getField('VAR').getText();
const deleteOption = {
text: Msg['DELETE_VARIABLE'].replace('%1', name),
enabled: true,
callback: deleteOptionCallbackFactory(this),
};
options.unshift(renameOption);
options.unshift(deleteOption);
}
}
},
};
/**
* Factory for callbacks for rename variable dropdown menu option
* associated with a variable getter block.
* @param {!Block} block The block with the variable to rename.
* @return {!function()} A function that renames the variable.
*/
const renameOptionCallbackFactory = function(block) {
return function() {
const workspace = block.workspace;
const variable = block.getField('VAR').getVariable();
Variables.renameVariable(workspace, variable);
};
};
/**
* Factory for callbacks for delete variable dropdown menu option
* associated with a variable getter block.
* @param {!Block} block The block with the variable to delete.
* @return {!function()} A function that deletes the variable.
*/
const deleteOptionCallbackFactory = function(block) {
return function() {
const workspace = block.workspace;
const variable = block.getField('VAR').getVariable();
workspace.deleteVariableById(variable.getId());
workspace.refreshToolboxSelection();
};
};
Extensions.registerMixin(
'contextMenu_variableSetterGetter',
CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
// Register provided blocks.
defineBlocks(blocks);