mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
* chore(build): Add "all" modules for blocks & generators
These modules (Blockly.blocks.all and Blockly.<Generator>.all) will
be the entry points for the corresponding chunks.
They also make it easier to pull in all the modules in each package
(e.g. for playground and tests).
It is necessary to set the Closure Compiler dependency_mode to
SORT_ONLY as otherwise it tries to compile the "all" modules before
their dependencies, which fails.
The only impact on the _compressed.js files is the addition of a short
string to the very end of each file, e.g.:
var module$exports$Blockly$JavaScript$all={};
* chore(deps): Add devDependency on closure-calculate-chunks
* feat(build): First pass at chunked complation
Add a new buildCompiled gulp target (npm run build:compiled) that
uses closure-calculate-chunks to do chunked compliation of core/,
blocks/ and generators/ all in a single pass.
This work is incomplete: the resulting *_compressed.js files don't
(yet) have UMD wrappers.
* chore(build): Generate chunk wrappers
A first pass; this does not have support for a namespace object yet.
* refactor(build): Use chunked compilation by default
Remove old "compressed" gulp tasks in favour of new "compiled" task.
* chore(build): Remove cruft from buildCompiled
Remove unneeded `done` parameter and commented-out options that had
been cargo-culted from the old build pipeline.
* fix(build): Fix test failures caused by new build pipeline
- Exclude closure/goog/base.js from compiler input; use
externs/goog-externs.js instead.
- Have the build:debug and build:strict targets only build the first
chunk (blockly_compressed.js).
- Fix namespace entries for blocks and generators.
* fix(build): Fix build failures on node v12
closure-calculate-chunks requires node.js v14 or later.
When running on node.js v14 or later have getChunkOptions save
the output of closure-calculate-chunks to
scripts/gulpfiles/chunks.json. When running on older versions of
node.js have it use this checked-in, cached output instead of
attempting to run closure-calculate-chunks.
* chore(build): enable --rename_prefix_namespace
This will allow modules in blocks/ and generators/ to use
goog.require to obtain the exports object of goog.modules from
core/.
* fix(build): Always build all chunks
The previous commit enabled --rename_prefix_namespace option to
Closure Compiler, and this causes the buildCompressed target to
work fine when run without --debug or --strict, but adding either
of those flags (as for example when `npm test` runs
`npm run build:debug`) causes an issue:
- Because of many compiler errors in blocks/ and generators/,
a previous commit added a hack to only build the first chunk
when doing debug/strict builds.
- When asked to build only one chunk, Closure Compiler ignores the
--rename_prefix_namespace flag, because it 'correctly' infers
that there are no later chunks that will need to access global
variables from the first chunk.
- This causes a test failure, because `npm test` first runs
`npm run build`, which generates a valid blockly_compressed.js,
but this is then overrwritten by an invalid one when it next runs
`npm run build:debug`.
(The invalid one is missing all `$.` prefixes on 'global' variables,
including on Blockly, so the wrapper's last two lines -
"$.Blockly.internal_ = $;" and "return $.Blockly" - fail.)
The fix is to add appropriate @suppress annotations to blocks/*.js and
generators/**/*.js and then remove the first-chunk-only hack.
* refactor(build): Just build once
Since the previous commit caused `npm run build:debug` to do
everything that `... build:compressed` does - and to produce
byte-for-byte identical output - it doesn't make sense to run
both when testing. To that end:
- Replace the build:debug and build:strict package scripts that
did `gulp buildCompressed --...` with new scripts build-debug
and build-strict that do `gulp build --...` instead.
(The target names are changed so as to extend our existing naming
convention as follows: a target named "foo:bar" does some sub-part
of the job done by target "foo", but a target named "foo-bar" does
all the work of the target "foo" with some extra options.)
- build:debug:log and build:strict:log are similarly replaced with
build-debug-log and build-strict-log.
- Modify run_all_tests.js to just do `npm run build-debug` instead of
doing both `npm run build` and `npm run build:debug`.
- Also remove the 'build:blocks' script that should have been removed
when the buildBlocks gulp task was deleted previously.
* refactor(build): Compile with base_minimal.js instead of base.js
Introduce a (very!) cut-down version of closure/goog/base.js named
base_minimal.js that is used as input to the compiler as an
alternative to using externs/goog-externs.js (which will be deleted
once the buildAdvancedCompilationTest target has been updated).
This will allow use of goog.setTestOnly since it will now exist in
compiled mode, and allows the changes made in 5b112db to filter
base.js out of the files for the first chunk to be reverted.
(It also obliges a change to the compiled-mode check in blockly.js.)
* fix(build): Fix buildAdvanceCompilationTest
- In build_tasks.js:
- Replace the old compile() function with a new one factored out of
buildCompiled().
- Update buildAdvancedCompilationTest to use the new compile()
and other helpers created in the meantime.
- Remove no-longer-used maybeAddClosureLibrary().
- Remove externs/{block,generator,goog}-externs.js, which are no longer
used by any compile pipeline.
- Update core/blockly.js to fix issue with detection of compiled mode
when using ADVANCED_OPTIMISATIONS.
- Update only other use of globalThis, in core/utils/xml.js, to
consistently treat it as a dictionary object.
- Update instructions in tests/compile/index.html.
This commit is sort-of-a-prerequisite to #5602; test:compile:advanced
was previously working but the generated `main_compresed.js` would
throw errors upon loading.
931 lines
30 KiB
JavaScript
931 lines
30 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview List blocks for Blockly.
|
|
* @suppress {extraRequire|missingRequire|checkTypes}
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.blocks.lists');
|
|
goog.provide('Blockly.Constants.Lists');
|
|
|
|
goog.require('Blockly');
|
|
goog.require('Blockly.FieldDropdown');
|
|
goog.require('Blockly.FieldLabel');
|
|
goog.require('Blockly.Mutator');
|
|
|
|
|
|
/**
|
|
* Unused constant for the common HSV hue for all blocks in this category.
|
|
* @deprecated Use Blockly.Msg['LISTS_HUE']. (2018 April 5)
|
|
*/
|
|
Blockly.Constants.Lists.HUE = 260;
|
|
|
|
Blockly.defineBlocksWithJsonArray([
|
|
// Block for creating an empty list
|
|
// The 'list_create_with' block is preferred as it is more flexible.
|
|
// <block type="lists_create_with">
|
|
// <mutation items="0"></mutation>
|
|
// </block>
|
|
{
|
|
"type": "lists_create_empty",
|
|
"message0": "%{BKY_LISTS_CREATE_EMPTY_TITLE}",
|
|
"output": "Array",
|
|
"style": "list_blocks",
|
|
"tooltip": "%{BKY_LISTS_CREATE_EMPTY_TOOLTIP}",
|
|
"helpUrl": "%{BKY_LISTS_CREATE_EMPTY_HELPURL}",
|
|
},
|
|
// Block for creating a list with one element repeated.
|
|
{
|
|
"type": "lists_repeat",
|
|
"message0": "%{BKY_LISTS_REPEAT_TITLE}",
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "ITEM",
|
|
},
|
|
{
|
|
"type": "input_value",
|
|
"name": "NUM",
|
|
"check": "Number",
|
|
},
|
|
],
|
|
"output": "Array",
|
|
"style": "list_blocks",
|
|
"tooltip": "%{BKY_LISTS_REPEAT_TOOLTIP}",
|
|
"helpUrl": "%{BKY_LISTS_REPEAT_HELPURL}",
|
|
},
|
|
// Block for reversing a list.
|
|
{
|
|
"type": "lists_reverse",
|
|
"message0": "%{BKY_LISTS_REVERSE_MESSAGE0}",
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "LIST",
|
|
"check": "Array",
|
|
},
|
|
],
|
|
"output": "Array",
|
|
"inputsInline": true,
|
|
"style": "list_blocks",
|
|
"tooltip": "%{BKY_LISTS_REVERSE_TOOLTIP}",
|
|
"helpUrl": "%{BKY_LISTS_REVERSE_HELPURL}",
|
|
},
|
|
// Block for checking if a list is empty
|
|
{
|
|
"type": "lists_isEmpty",
|
|
"message0": "%{BKY_LISTS_ISEMPTY_TITLE}",
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "VALUE",
|
|
"check": ["String", "Array"],
|
|
},
|
|
],
|
|
"output": "Boolean",
|
|
"style": "list_blocks",
|
|
"tooltip": "%{BKY_LISTS_ISEMPTY_TOOLTIP}",
|
|
"helpUrl": "%{BKY_LISTS_ISEMPTY_HELPURL}",
|
|
},
|
|
// Block for getting the list length
|
|
{
|
|
"type": "lists_length",
|
|
"message0": "%{BKY_LISTS_LENGTH_TITLE}",
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "VALUE",
|
|
"check": ["String", "Array"],
|
|
},
|
|
],
|
|
"output": "Number",
|
|
"style": "list_blocks",
|
|
"tooltip": "%{BKY_LISTS_LENGTH_TOOLTIP}",
|
|
"helpUrl": "%{BKY_LISTS_LENGTH_HELPURL}",
|
|
},
|
|
]);
|
|
|
|
Blockly.Blocks['lists_create_with'] = {
|
|
/**
|
|
* Block for creating a list with any number of elements of any type.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
this.setHelpUrl(Blockly.Msg['LISTS_CREATE_WITH_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
this.itemCount_ = 3;
|
|
this.updateShape_();
|
|
this.setOutput(true, 'Array');
|
|
this.setMutator(new Blockly.Mutator(['lists_create_with_item']));
|
|
this.setTooltip(Blockly.Msg['LISTS_CREATE_WITH_TOOLTIP']);
|
|
},
|
|
/**
|
|
* Create XML to represent list inputs.
|
|
* Backwards compatible serialization implementation.
|
|
* @return {!Element} XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
mutationToDom: function() {
|
|
const container = Blockly.utils.xml.createElement('mutation');
|
|
container.setAttribute('items', this.itemCount_);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the list inputs.
|
|
* Backwards compatible serialization implementation.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
|
this.updateShape_();
|
|
},
|
|
/**
|
|
* Returns the state of this block as a JSON serializable object.
|
|
* @return {{itemCount: number}} The state of this block, ie the item count.
|
|
*/
|
|
saveExtraState: function() {
|
|
return {
|
|
'itemCount': this.itemCount_,
|
|
};
|
|
},
|
|
/**
|
|
* Applies the given state to this block.
|
|
* @param {*} state The state to apply to this block, ie the item count.
|
|
*/
|
|
loadExtraState: function(state) {
|
|
this.itemCount_ = state['itemCount'];
|
|
this.updateShape_();
|
|
},
|
|
/**
|
|
* Populate the mutator's dialog with this block's components.
|
|
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
|
* @return {!Blockly.Block} Root block in mutator.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
decompose: function(workspace) {
|
|
const containerBlock = workspace.newBlock('lists_create_with_container');
|
|
containerBlock.initSvg();
|
|
let connection = containerBlock.getInput('STACK').connection;
|
|
for (let i = 0; i < this.itemCount_; i++) {
|
|
const itemBlock = workspace.newBlock('lists_create_with_item');
|
|
itemBlock.initSvg();
|
|
connection.connect(itemBlock.previousConnection);
|
|
connection = itemBlock.nextConnection;
|
|
}
|
|
return containerBlock;
|
|
},
|
|
/**
|
|
* Reconfigure this block based on the mutator dialog's components.
|
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
compose: function(containerBlock) {
|
|
let itemBlock = containerBlock.getInputTargetBlock('STACK');
|
|
// Count number of inputs.
|
|
const connections = [];
|
|
while (itemBlock && !itemBlock.isInsertionMarker()) {
|
|
connections.push(itemBlock.valueConnection_);
|
|
itemBlock = itemBlock.nextConnection &&
|
|
itemBlock.nextConnection.targetBlock();
|
|
}
|
|
// Disconnect any children that don't belong.
|
|
for (let i = 0; i < this.itemCount_; i++) {
|
|
const connection = this.getInput('ADD' + i).connection.targetConnection;
|
|
if (connection && connections.indexOf(connection) === -1) {
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
this.itemCount_ = connections.length;
|
|
this.updateShape_();
|
|
// Reconnect any child blocks.
|
|
for (let i = 0; i < this.itemCount_; i++) {
|
|
Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
|
|
}
|
|
},
|
|
/**
|
|
* Store pointers to any connected child blocks.
|
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
saveConnections: function(containerBlock) {
|
|
let itemBlock = containerBlock.getInputTargetBlock('STACK');
|
|
let i = 0;
|
|
while (itemBlock) {
|
|
const input = this.getInput('ADD' + i);
|
|
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
|
itemBlock =
|
|
itemBlock.nextConnection && itemBlock.nextConnection.targetBlock();
|
|
i++;
|
|
}
|
|
},
|
|
/**
|
|
* Modify this block to have the correct number of inputs.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateShape_: function() {
|
|
if (this.itemCount_ && this.getInput('EMPTY')) {
|
|
this.removeInput('EMPTY');
|
|
} else if (!this.itemCount_ && !this.getInput('EMPTY')) {
|
|
this.appendDummyInput('EMPTY')
|
|
.appendField(Blockly.Msg['LISTS_CREATE_EMPTY_TITLE']);
|
|
}
|
|
// Add new inputs.
|
|
for (let i = 0; i < this.itemCount_; i++) {
|
|
if (!this.getInput('ADD' + i)) {
|
|
const input = this.appendValueInput('ADD' + i)
|
|
.setAlign(Blockly.ALIGN_RIGHT);
|
|
if (i === 0) {
|
|
input.appendField(Blockly.Msg['LISTS_CREATE_WITH_INPUT_WITH']);
|
|
}
|
|
}
|
|
}
|
|
// Remove deleted inputs.
|
|
for (let i = this.itemCount_; this.getInput('ADD' + i); i++) {
|
|
this.removeInput('ADD' + i);
|
|
}
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_create_with_container'] = {
|
|
/**
|
|
* Mutator block for list container.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
this.setStyle('list_blocks');
|
|
this.appendDummyInput()
|
|
.appendField(Blockly.Msg['LISTS_CREATE_WITH_CONTAINER_TITLE_ADD']);
|
|
this.appendStatementInput('STACK');
|
|
this.setTooltip(Blockly.Msg['LISTS_CREATE_WITH_CONTAINER_TOOLTIP']);
|
|
this.contextMenu = false;
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_create_with_item'] = {
|
|
/**
|
|
* Mutator block for adding items.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
this.setStyle('list_blocks');
|
|
this.appendDummyInput()
|
|
.appendField(Blockly.Msg['LISTS_CREATE_WITH_ITEM_TITLE']);
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
this.setTooltip(Blockly.Msg['LISTS_CREATE_WITH_ITEM_TOOLTIP']);
|
|
this.contextMenu = false;
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_indexOf'] = {
|
|
/**
|
|
* Block for finding an item in the list.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
const OPERATORS =
|
|
[
|
|
[Blockly.Msg['LISTS_INDEX_OF_FIRST'], 'FIRST'],
|
|
[Blockly.Msg['LISTS_INDEX_OF_LAST'], 'LAST'],
|
|
];
|
|
this.setHelpUrl(Blockly.Msg['LISTS_INDEX_OF_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
this.setOutput(true, 'Number');
|
|
this.appendValueInput('VALUE')
|
|
.setCheck('Array')
|
|
.appendField(Blockly.Msg['LISTS_INDEX_OF_INPUT_IN_LIST']);
|
|
this.appendValueInput('FIND')
|
|
.appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
|
|
this.setInputsInline(true);
|
|
// Assign 'this' to a variable for use in the tooltip closure below.
|
|
const thisBlock = this;
|
|
this.setTooltip(function() {
|
|
return Blockly.Msg['LISTS_INDEX_OF_TOOLTIP'].replace('%1',
|
|
thisBlock.workspace.options.oneBasedIndex ? '0' : '-1');
|
|
});
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_getIndex'] = {
|
|
/**
|
|
* Block for getting element at index.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
const MODE =
|
|
[
|
|
[Blockly.Msg['LISTS_GET_INDEX_GET'], 'GET'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_GET_REMOVE'], 'GET_REMOVE'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_REMOVE'], 'REMOVE'],
|
|
];
|
|
this.WHERE_OPTIONS =
|
|
[
|
|
[Blockly.Msg['LISTS_GET_INDEX_FROM_START'], 'FROM_START'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_FROM_END'], 'FROM_END'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_FIRST'], 'FIRST'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_LAST'], 'LAST'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_RANDOM'], 'RANDOM'],
|
|
];
|
|
this.setHelpUrl(Blockly.Msg['LISTS_GET_INDEX_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
const modeMenu = new Blockly.FieldDropdown(MODE,
|
|
/**
|
|
* @param {*} value The input value.
|
|
* @this {Blockly.FieldDropdown}
|
|
*/
|
|
function(value) {
|
|
const isStatement = (value === 'REMOVE');
|
|
this.getSourceBlock().updateStatement_(isStatement);
|
|
}
|
|
);
|
|
this.appendValueInput('VALUE')
|
|
.setCheck('Array')
|
|
.appendField(Blockly.Msg['LISTS_GET_INDEX_INPUT_IN_LIST']);
|
|
this.appendDummyInput()
|
|
.appendField(modeMenu, 'MODE')
|
|
.appendField('', 'SPACE');
|
|
this.appendDummyInput('AT');
|
|
if (Blockly.Msg['LISTS_GET_INDEX_TAIL']) {
|
|
this.appendDummyInput('TAIL')
|
|
.appendField(Blockly.Msg['LISTS_GET_INDEX_TAIL']);
|
|
}
|
|
this.setInputsInline(true);
|
|
this.setOutput(true);
|
|
this.updateAt_(true);
|
|
// Assign 'this' to a variable for use in the tooltip closure below.
|
|
const thisBlock = this;
|
|
this.setTooltip(function() {
|
|
const mode = thisBlock.getFieldValue('MODE');
|
|
const where = thisBlock.getFieldValue('WHERE');
|
|
let tooltip = '';
|
|
switch (mode + ' ' + where) {
|
|
case 'GET FROM_START':
|
|
case 'GET FROM_END':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_FROM'];
|
|
break;
|
|
case 'GET FIRST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_FIRST'];
|
|
break;
|
|
case 'GET LAST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_LAST'];
|
|
break;
|
|
case 'GET RANDOM':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_RANDOM'];
|
|
break;
|
|
case 'GET_REMOVE FROM_START':
|
|
case 'GET_REMOVE FROM_END':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FROM'];
|
|
break;
|
|
case 'GET_REMOVE FIRST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FIRST'];
|
|
break;
|
|
case 'GET_REMOVE LAST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_LAST'];
|
|
break;
|
|
case 'GET_REMOVE RANDOM':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_RANDOM'];
|
|
break;
|
|
case 'REMOVE FROM_START':
|
|
case 'REMOVE FROM_END':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_REMOVE_FROM'];
|
|
break;
|
|
case 'REMOVE FIRST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_REMOVE_FIRST'];
|
|
break;
|
|
case 'REMOVE LAST':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_REMOVE_LAST'];
|
|
break;
|
|
case 'REMOVE RANDOM':
|
|
tooltip = Blockly.Msg['LISTS_GET_INDEX_TOOLTIP_REMOVE_RANDOM'];
|
|
break;
|
|
}
|
|
if (where === 'FROM_START' || where === 'FROM_END') {
|
|
const msg = (where === 'FROM_START') ?
|
|
Blockly.Msg['LISTS_INDEX_FROM_START_TOOLTIP'] :
|
|
Blockly.Msg['LISTS_INDEX_FROM_END_TOOLTIP'];
|
|
tooltip += ' ' + msg.replace('%1',
|
|
thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
|
|
}
|
|
return tooltip;
|
|
});
|
|
},
|
|
/**
|
|
* Create XML to represent whether the block is a statement or a value.
|
|
* Also represent whether there is an 'AT' input.
|
|
* @return {!Element} XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
mutationToDom: function() {
|
|
const container = Blockly.utils.xml.createElement('mutation');
|
|
const isStatement = !this.outputConnection;
|
|
container.setAttribute('statement', isStatement);
|
|
const isAt = this.getInput('AT').type === Blockly.INPUT_VALUE;
|
|
container.setAttribute('at', isAt);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the 'AT' input.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
// Note: Until January 2013 this block did not have mutations,
|
|
// so 'statement' defaults to false and 'at' defaults to true.
|
|
const isStatement = (xmlElement.getAttribute('statement') === 'true');
|
|
this.updateStatement_(isStatement);
|
|
const isAt = (xmlElement.getAttribute('at') !== 'false');
|
|
this.updateAt_(isAt);
|
|
},
|
|
|
|
// This block does not need JSO serialization hooks (saveExtraState and
|
|
// loadExtraState) because the state of this object is already encoded in the
|
|
// dropdown values.
|
|
// XML hooks are kept for backwards compatibility.
|
|
|
|
/**
|
|
* Switch between a value block and a statement block.
|
|
* @param {boolean} newStatement True if the block should be a statement.
|
|
* False if the block should be a value.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateStatement_: function(newStatement) {
|
|
const oldStatement = !this.outputConnection;
|
|
if (newStatement !== oldStatement) {
|
|
this.unplug(true, true);
|
|
if (newStatement) {
|
|
this.setOutput(false);
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
} else {
|
|
this.setPreviousStatement(false);
|
|
this.setNextStatement(false);
|
|
this.setOutput(true);
|
|
}
|
|
}
|
|
},
|
|
/**
|
|
* Create or delete an input for the numeric index.
|
|
* @param {boolean} isAt True if the input should exist.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateAt_: function(isAt) {
|
|
// Destroy old 'AT' and 'ORDINAL' inputs.
|
|
this.removeInput('AT');
|
|
this.removeInput('ORDINAL', true);
|
|
// Create either a value 'AT' input or a dummy input.
|
|
if (isAt) {
|
|
this.appendValueInput('AT').setCheck('Number');
|
|
if (Blockly.Msg['ORDINAL_NUMBER_SUFFIX']) {
|
|
this.appendDummyInput('ORDINAL')
|
|
.appendField(Blockly.Msg['ORDINAL_NUMBER_SUFFIX']);
|
|
}
|
|
} else {
|
|
this.appendDummyInput('AT');
|
|
}
|
|
const menu = new Blockly.FieldDropdown(
|
|
this.WHERE_OPTIONS,
|
|
/**
|
|
* @param {*} value The input value.
|
|
* @this {Blockly.FieldDropdown}
|
|
* @returns {null|undefined} Null if the field has been replaced;
|
|
* otherwise undefined.
|
|
*/
|
|
function(value) {
|
|
const newAt = (value === 'FROM_START') || (value === 'FROM_END');
|
|
// The 'isAt' variable is available due to this function being a
|
|
// closure.
|
|
if (newAt !== isAt) {
|
|
const block = this.getSourceBlock();
|
|
block.updateAt_(newAt);
|
|
// This menu has been destroyed and replaced. Update the
|
|
// replacement.
|
|
block.setFieldValue(value, 'WHERE');
|
|
return null;
|
|
}
|
|
return undefined;
|
|
});
|
|
this.getInput('AT').appendField(menu, 'WHERE');
|
|
if (Blockly.Msg['LISTS_GET_INDEX_TAIL']) {
|
|
this.moveInputBefore('TAIL', null);
|
|
}
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_setIndex'] = {
|
|
/**
|
|
* Block for setting the element at index.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
const MODE =
|
|
[
|
|
[Blockly.Msg['LISTS_SET_INDEX_SET'], 'SET'],
|
|
[Blockly.Msg['LISTS_SET_INDEX_INSERT'], 'INSERT'],
|
|
];
|
|
this.WHERE_OPTIONS =
|
|
[
|
|
[Blockly.Msg['LISTS_GET_INDEX_FROM_START'], 'FROM_START'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_FROM_END'], 'FROM_END'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_FIRST'], 'FIRST'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_LAST'], 'LAST'],
|
|
[Blockly.Msg['LISTS_GET_INDEX_RANDOM'], 'RANDOM'],
|
|
];
|
|
this.setHelpUrl(Blockly.Msg['LISTS_SET_INDEX_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
this.appendValueInput('LIST')
|
|
.setCheck('Array')
|
|
.appendField(Blockly.Msg['LISTS_SET_INDEX_INPUT_IN_LIST']);
|
|
this.appendDummyInput()
|
|
.appendField(new Blockly.FieldDropdown(MODE), 'MODE')
|
|
.appendField('', 'SPACE');
|
|
this.appendDummyInput('AT');
|
|
this.appendValueInput('TO')
|
|
.appendField(Blockly.Msg['LISTS_SET_INDEX_INPUT_TO']);
|
|
this.setInputsInline(true);
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
this.setTooltip(Blockly.Msg['LISTS_SET_INDEX_TOOLTIP']);
|
|
this.updateAt_(true);
|
|
// Assign 'this' to a variable for use in the tooltip closure below.
|
|
const thisBlock = this;
|
|
this.setTooltip(function() {
|
|
const mode = thisBlock.getFieldValue('MODE');
|
|
const where = thisBlock.getFieldValue('WHERE');
|
|
let tooltip = '';
|
|
switch (mode + ' ' + where) {
|
|
case 'SET FROM_START':
|
|
case 'SET FROM_END':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_SET_FROM'];
|
|
break;
|
|
case 'SET FIRST':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_SET_FIRST'];
|
|
break;
|
|
case 'SET LAST':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_SET_LAST'];
|
|
break;
|
|
case 'SET RANDOM':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_SET_RANDOM'];
|
|
break;
|
|
case 'INSERT FROM_START':
|
|
case 'INSERT FROM_END':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_INSERT_FROM'];
|
|
break;
|
|
case 'INSERT FIRST':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_INSERT_FIRST'];
|
|
break;
|
|
case 'INSERT LAST':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_INSERT_LAST'];
|
|
break;
|
|
case 'INSERT RANDOM':
|
|
tooltip = Blockly.Msg['LISTS_SET_INDEX_TOOLTIP_INSERT_RANDOM'];
|
|
break;
|
|
}
|
|
if (where === 'FROM_START' || where === 'FROM_END') {
|
|
tooltip += ' ' + Blockly.Msg['LISTS_INDEX_FROM_START_TOOLTIP']
|
|
.replace('%1',
|
|
thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
|
|
}
|
|
return tooltip;
|
|
});
|
|
},
|
|
/**
|
|
* Create XML to represent whether there is an 'AT' input.
|
|
* @return {!Element} XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
mutationToDom: function() {
|
|
const container = Blockly.utils.xml.createElement('mutation');
|
|
const isAt = this.getInput('AT').type === Blockly.INPUT_VALUE;
|
|
container.setAttribute('at', isAt);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the 'AT' input.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
// Note: Until January 2013 this block did not have mutations,
|
|
// so 'at' defaults to true.
|
|
const isAt = (xmlElement.getAttribute('at') !== 'false');
|
|
this.updateAt_(isAt);
|
|
},
|
|
|
|
// This block does not need JSO serialization hooks (saveExtraState and
|
|
// loadExtraState) because the state of this object is already encoded in the
|
|
// dropdown values.
|
|
// XML hooks are kept for backwards compatibility.
|
|
|
|
/**
|
|
* Create or delete an input for the numeric index.
|
|
* @param {boolean} isAt True if the input should exist.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateAt_: function(isAt) {
|
|
// Destroy old 'AT' and 'ORDINAL' input.
|
|
this.removeInput('AT');
|
|
this.removeInput('ORDINAL', true);
|
|
// Create either a value 'AT' input or a dummy input.
|
|
if (isAt) {
|
|
this.appendValueInput('AT').setCheck('Number');
|
|
if (Blockly.Msg['ORDINAL_NUMBER_SUFFIX']) {
|
|
this.appendDummyInput('ORDINAL')
|
|
.appendField(Blockly.Msg['ORDINAL_NUMBER_SUFFIX']);
|
|
}
|
|
} else {
|
|
this.appendDummyInput('AT');
|
|
}
|
|
const menu = new Blockly.FieldDropdown(
|
|
this.WHERE_OPTIONS,
|
|
/**
|
|
* @param {*} value The input value.
|
|
* @this {Blockly.FieldDropdown}
|
|
* @returns {null|undefined} Null if the field has been replaced;
|
|
* otherwise undefined.
|
|
*/
|
|
function(value) {
|
|
const newAt = (value === 'FROM_START') || (value === 'FROM_END');
|
|
// The 'isAt' variable is available due to this function being a
|
|
// closure.
|
|
if (newAt !== isAt) {
|
|
const block = this.getSourceBlock();
|
|
block.updateAt_(newAt);
|
|
// This menu has been destroyed and replaced. Update the
|
|
// replacement.
|
|
block.setFieldValue(value, 'WHERE');
|
|
return null;
|
|
}
|
|
return undefined;
|
|
});
|
|
this.moveInputBefore('AT', 'TO');
|
|
if (this.getInput('ORDINAL')) {
|
|
this.moveInputBefore('ORDINAL', 'TO');
|
|
}
|
|
|
|
this.getInput('AT').appendField(menu, 'WHERE');
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_getSublist'] = {
|
|
/**
|
|
* Block for getting sublist.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
this['WHERE_OPTIONS_1'] =
|
|
[
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_START_FROM_START'], 'FROM_START'],
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_START_FROM_END'], 'FROM_END'],
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_START_FIRST'], 'FIRST'],
|
|
];
|
|
this['WHERE_OPTIONS_2'] =
|
|
[
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_END_FROM_START'], 'FROM_START'],
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_END_FROM_END'], 'FROM_END'],
|
|
[Blockly.Msg['LISTS_GET_SUBLIST_END_LAST'], 'LAST'],
|
|
];
|
|
this.setHelpUrl(Blockly.Msg['LISTS_GET_SUBLIST_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
this.appendValueInput('LIST')
|
|
.setCheck('Array')
|
|
.appendField(Blockly.Msg['LISTS_GET_SUBLIST_INPUT_IN_LIST']);
|
|
this.appendDummyInput('AT1');
|
|
this.appendDummyInput('AT2');
|
|
if (Blockly.Msg['LISTS_GET_SUBLIST_TAIL']) {
|
|
this.appendDummyInput('TAIL')
|
|
.appendField(Blockly.Msg['LISTS_GET_SUBLIST_TAIL']);
|
|
}
|
|
this.setInputsInline(true);
|
|
this.setOutput(true, 'Array');
|
|
this.updateAt_(1, true);
|
|
this.updateAt_(2, true);
|
|
this.setTooltip(Blockly.Msg['LISTS_GET_SUBLIST_TOOLTIP']);
|
|
},
|
|
/**
|
|
* Create XML to represent whether there are 'AT' inputs.
|
|
* @return {!Element} XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
mutationToDom: function() {
|
|
const container = Blockly.utils.xml.createElement('mutation');
|
|
const isAt1 = this.getInput('AT1').type === Blockly.INPUT_VALUE;
|
|
container.setAttribute('at1', isAt1);
|
|
const isAt2 = this.getInput('AT2').type === Blockly.INPUT_VALUE;
|
|
container.setAttribute('at2', isAt2);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the 'AT' inputs.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
const isAt1 = (xmlElement.getAttribute('at1') === 'true');
|
|
const isAt2 = (xmlElement.getAttribute('at2') === 'true');
|
|
this.updateAt_(1, isAt1);
|
|
this.updateAt_(2, isAt2);
|
|
},
|
|
|
|
// This block does not need JSO serialization hooks (saveExtraState and
|
|
// loadExtraState) because the state of this object is already encoded in the
|
|
// dropdown values.
|
|
// XML hooks are kept for backwards compatibility.
|
|
|
|
/**
|
|
* Create or delete an input for a numeric index.
|
|
* This block has two such inputs, independent of each other.
|
|
* @param {number} n Specify first or second input (1 or 2).
|
|
* @param {boolean} isAt True if the input should exist.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateAt_: function(n, isAt) {
|
|
// Create or delete an input for the numeric index.
|
|
// Destroy old 'AT' and 'ORDINAL' inputs.
|
|
this.removeInput('AT' + n);
|
|
this.removeInput('ORDINAL' + n, true);
|
|
// Create either a value 'AT' input or a dummy input.
|
|
if (isAt) {
|
|
this.appendValueInput('AT' + n).setCheck('Number');
|
|
if (Blockly.Msg['ORDINAL_NUMBER_SUFFIX']) {
|
|
this.appendDummyInput('ORDINAL' + n)
|
|
.appendField(Blockly.Msg['ORDINAL_NUMBER_SUFFIX']);
|
|
}
|
|
} else {
|
|
this.appendDummyInput('AT' + n);
|
|
}
|
|
const menu = new Blockly.FieldDropdown(
|
|
this['WHERE_OPTIONS_' + n],
|
|
/**
|
|
* @param {*} value The input value.
|
|
* @this {Blockly.FieldDropdown}
|
|
* @returns {null|undefined} Null if the field has been replaced;
|
|
* otherwise undefined.
|
|
*/
|
|
function(value) {
|
|
const newAt = (value === 'FROM_START') || (value === 'FROM_END');
|
|
// The 'isAt' variable is available due to this function being a
|
|
// closure.
|
|
if (newAt !== isAt) {
|
|
const block = this.getSourceBlock();
|
|
block.updateAt_(n, newAt);
|
|
// This menu has been destroyed and replaced.
|
|
// Update the replacement.
|
|
block.setFieldValue(value, 'WHERE' + n);
|
|
return null;
|
|
}
|
|
});
|
|
this.getInput('AT' + n)
|
|
.appendField(menu, 'WHERE' + n);
|
|
if (n === 1) {
|
|
this.moveInputBefore('AT1', 'AT2');
|
|
if (this.getInput('ORDINAL1')) {
|
|
this.moveInputBefore('ORDINAL1', 'AT2');
|
|
}
|
|
}
|
|
if (Blockly.Msg['LISTS_GET_SUBLIST_TAIL']) {
|
|
this.moveInputBefore('TAIL', null);
|
|
}
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_sort'] = {
|
|
/**
|
|
* Block for sorting a list.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg['LISTS_SORT_TITLE'],
|
|
"args0": [
|
|
{
|
|
"type": "field_dropdown",
|
|
"name": "TYPE",
|
|
"options": [
|
|
[Blockly.Msg['LISTS_SORT_TYPE_NUMERIC'], "NUMERIC"],
|
|
[Blockly.Msg['LISTS_SORT_TYPE_TEXT'], "TEXT"],
|
|
[Blockly.Msg['LISTS_SORT_TYPE_IGNORECASE'], "IGNORE_CASE"],
|
|
],
|
|
},
|
|
{
|
|
"type": "field_dropdown",
|
|
"name": "DIRECTION",
|
|
"options": [
|
|
[Blockly.Msg['LISTS_SORT_ORDER_ASCENDING'], "1"],
|
|
[Blockly.Msg['LISTS_SORT_ORDER_DESCENDING'], "-1"],
|
|
],
|
|
},
|
|
{
|
|
"type": "input_value",
|
|
"name": "LIST",
|
|
"check": "Array",
|
|
},
|
|
],
|
|
"output": "Array",
|
|
"style": "list_blocks",
|
|
"tooltip": Blockly.Msg['LISTS_SORT_TOOLTIP'],
|
|
"helpUrl": Blockly.Msg['LISTS_SORT_HELPURL'],
|
|
});
|
|
},
|
|
};
|
|
|
|
Blockly.Blocks['lists_split'] = {
|
|
/**
|
|
* Block for splitting text into a list, or joining a list into text.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
init: function() {
|
|
// Assign 'this' to a variable for use in the closures below.
|
|
const thisBlock = this;
|
|
const dropdown = new Blockly.FieldDropdown(
|
|
[
|
|
[Blockly.Msg['LISTS_SPLIT_LIST_FROM_TEXT'], 'SPLIT'],
|
|
[Blockly.Msg['LISTS_SPLIT_TEXT_FROM_LIST'], 'JOIN'],
|
|
],
|
|
function(newMode) {
|
|
thisBlock.updateType_(newMode);
|
|
});
|
|
this.setHelpUrl(Blockly.Msg['LISTS_SPLIT_HELPURL']);
|
|
this.setStyle('list_blocks');
|
|
this.appendValueInput('INPUT')
|
|
.setCheck('String')
|
|
.appendField(dropdown, 'MODE');
|
|
this.appendValueInput('DELIM')
|
|
.setCheck('String')
|
|
.appendField(Blockly.Msg['LISTS_SPLIT_WITH_DELIMITER']);
|
|
this.setInputsInline(true);
|
|
this.setOutput(true, 'Array');
|
|
this.setTooltip(function() {
|
|
const mode = thisBlock.getFieldValue('MODE');
|
|
if (mode === 'SPLIT') {
|
|
return Blockly.Msg['LISTS_SPLIT_TOOLTIP_SPLIT'];
|
|
} else if (mode === 'JOIN') {
|
|
return Blockly.Msg['LISTS_SPLIT_TOOLTIP_JOIN'];
|
|
}
|
|
throw Error('Unknown mode: ' + mode);
|
|
});
|
|
},
|
|
/**
|
|
* Modify this block to have the correct input and output types.
|
|
* @param {string} newMode Either 'SPLIT' or 'JOIN'.
|
|
* @private
|
|
* @this {Blockly.Block}
|
|
*/
|
|
updateType_: function(newMode) {
|
|
const mode = this.getFieldValue('MODE');
|
|
if (mode !== newMode) {
|
|
const inputConnection = this.getInput('INPUT').connection;
|
|
inputConnection.setShadowDom(null);
|
|
const inputBlock = inputConnection.targetBlock();
|
|
if (inputBlock) {
|
|
inputConnection.disconnect();
|
|
if (inputBlock.isShadow()) {
|
|
inputBlock.dispose();
|
|
} else {
|
|
this.bumpNeighbours();
|
|
}
|
|
}
|
|
}
|
|
if (newMode === 'SPLIT') {
|
|
this.outputConnection.setCheck('Array');
|
|
this.getInput('INPUT').setCheck('String');
|
|
} else {
|
|
this.outputConnection.setCheck('String');
|
|
this.getInput('INPUT').setCheck('Array');
|
|
}
|
|
},
|
|
/**
|
|
* Create XML to represent the input and output types.
|
|
* @return {!Element} XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
mutationToDom: function() {
|
|
const container = Blockly.utils.xml.createElement('mutation');
|
|
container.setAttribute('mode', this.getFieldValue('MODE'));
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the input and output types.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this {Blockly.Block}
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
this.updateType_(xmlElement.getAttribute('mode'));
|
|
},
|
|
|
|
// This block does not need JSO serialization hooks (saveExtraState and
|
|
// loadExtraState) because the state of this object is already encoded in the
|
|
// dropdown values.
|
|
// XML hooks are kept for backwards compatibility.
|
|
};
|