Files
blockly/core/variables_dynamic.ts
Christopher Allen b0a7c004a9 refactor(build): Delete Closure Library (#7415)
* fix(build): Restore erroneously-deleted filter function

  This was deleted in PR #7406 as it was mainly being used to
  filter core/ vs. test/mocha/ deps into separate deps files -
  but it turns out also to be used for filtering error
  messages too.  Oops.

* refactor(tests): Migrate advanced compilation test to ES Modules

* refactor(build): Migrate main.js to TypeScript

  This turns out to be pretty straight forward, even if it would
  cause crashing if one actually tried to import this module
  instead of just feeding it to Closure Compiler.

* chore(build): Remove goog.declareModuleId calls

  Replace goog.declareModuleId calls with a comment recording the
  former module ID for posterity (or at least until we decide
  how to reformat the renamings file.

* chore(tests): Delete closure/goog/*

  For the moment we still need something to serve as base.js for
  the benefit of closure-make-deps, so we keep a vestigial
  base.js around, containing only the @provideGoog declaration.

* refactor(build): Remove vestigial base.js

  By changing slightly the command line arguments to
  closure-make-deps and closure-calculate-chunks the need to have
  any base.js is eliminated.

* chore: Typo fix for PR #7415
2023-08-31 00:24:47 +01:00

144 lines
4.3 KiB
TypeScript

/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.VariablesDynamic
import {Blocks} from './blocks.js';
import {Msg} from './msg.js';
import * as xml from './utils/xml.js';
import {VariableModel} from './variable_model.js';
import * as Variables from './variables.js';
import type {Workspace} from './workspace.js';
import type {WorkspaceSvg} from './workspace_svg.js';
import type {FlyoutButton} from './flyout_button.js';
/**
* String for use in the "custom" attribute of a category in toolbox XML.
* This string indicates that the category should be dynamically populated with
* variable blocks.
* See also Blockly.Variables.CATEGORY_NAME and
* Blockly.Procedures.CATEGORY_NAME.
*/
export const CATEGORY_NAME = 'VARIABLE_DYNAMIC';
/**
* Click handler for a button that creates String variables.
*
* @param button
*/
function stringButtonClickHandler(button: FlyoutButton) {
Variables.createVariableButtonHandler(
button.getTargetWorkspace(),
undefined,
'String',
);
}
// eslint-disable-next-line camelcase
export const onCreateVariableButtonClick_String = stringButtonClickHandler;
/**
* Click handler for a button that creates Number variables.
*
* @param button
*/
function numberButtonClickHandler(button: FlyoutButton) {
Variables.createVariableButtonHandler(
button.getTargetWorkspace(),
undefined,
'Number',
);
}
// eslint-disable-next-line camelcase
export const onCreateVariableButtonClick_Number = numberButtonClickHandler;
/**
* Click handler for a button that creates Colour variables.
*
* @param button
*/
function colourButtonClickHandler(button: FlyoutButton) {
Variables.createVariableButtonHandler(
button.getTargetWorkspace(),
undefined,
'Colour',
);
}
// eslint-disable-next-line camelcase
export const onCreateVariableButtonClick_Colour = colourButtonClickHandler;
/**
* Construct the elements (blocks and button) required by the flyout for the
* variable category.
*
* @param workspace The workspace containing variables.
* @returns Array of XML elements.
*/
export function flyoutCategory(workspace: WorkspaceSvg): Element[] {
let xmlList = new Array<Element>();
let button = document.createElement('button');
button.setAttribute('text', Msg['NEW_STRING_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_STRING');
xmlList.push(button);
button = document.createElement('button');
button.setAttribute('text', Msg['NEW_NUMBER_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_NUMBER');
xmlList.push(button);
button = document.createElement('button');
button.setAttribute('text', Msg['NEW_COLOUR_VARIABLE']);
button.setAttribute('callbackKey', 'CREATE_VARIABLE_COLOUR');
xmlList.push(button);
workspace.registerButtonCallback(
'CREATE_VARIABLE_STRING',
stringButtonClickHandler,
);
workspace.registerButtonCallback(
'CREATE_VARIABLE_NUMBER',
numberButtonClickHandler,
);
workspace.registerButtonCallback(
'CREATE_VARIABLE_COLOUR',
colourButtonClickHandler,
);
const blockList = flyoutCategoryBlocks(workspace);
xmlList = xmlList.concat(blockList);
return xmlList;
}
/**
* Construct the blocks required by the flyout for the variable category.
*
* @param workspace The workspace containing variables.
* @returns Array of XML block elements.
*/
export function flyoutCategoryBlocks(workspace: Workspace): Element[] {
const variableModelList = workspace.getAllVariables();
const xmlList = [];
if (variableModelList.length > 0) {
if (Blocks['variables_set_dynamic']) {
const firstVariable = variableModelList[variableModelList.length - 1];
const block = xml.createElement('block');
block.setAttribute('type', 'variables_set_dynamic');
block.setAttribute('gap', '24');
block.appendChild(Variables.generateVariableFieldDom(firstVariable));
xmlList.push(block);
}
if (Blocks['variables_get_dynamic']) {
variableModelList.sort(VariableModel.compareByName);
for (let i = 0, variable; (variable = variableModelList[i]); i++) {
const block = xml.createElement('block');
block.setAttribute('type', 'variables_get_dynamic');
block.setAttribute('gap', '8');
block.appendChild(Variables.generateVariableFieldDom(variable));
xmlList.push(block);
}
}
}
return xmlList;
}