2017-04-17 15:10:04 -04:00
|
|
|
const adapter = require('./adapter');
|
|
|
|
const mutationAdapter = require('./mutation-adapter');
|
|
|
|
const xmlEscape = require('../util/xml-escape');
|
2017-05-26 13:50:50 -04:00
|
|
|
const MonitorRecord = require('./monitor-record');
|
2017-09-07 11:51:21 -04:00
|
|
|
const Clone = require('../util/clone');
|
2018-01-08 20:22:18 -05:00
|
|
|
const {Map} = require('immutable');
|
2018-04-10 16:29:51 -04:00
|
|
|
const BlocksExecuteCache = require('./blocks-execute-cache');
|
2019-01-18 17:21:35 -05:00
|
|
|
const BlocksRuntimeCache = require('./blocks-runtime-cache');
|
2018-05-21 11:30:22 -04:00
|
|
|
const log = require('../util/log');
|
2018-06-19 14:02:54 -04:00
|
|
|
const Variable = require('./variable');
|
2018-11-08 00:37:28 -05:00
|
|
|
const getMonitorIdForBlockWithArgs = require('../util/get-monitor-id');
|
2016-06-08 13:44:09 -04:00
|
|
|
|
2016-06-06 15:29:34 -04:00
|
|
|
/**
|
2016-06-08 13:27:01 -04:00
|
|
|
* @fileoverview
|
2016-06-06 15:29:34 -04:00
|
|
|
* Store and mutate the VM block representation,
|
|
|
|
* and handle updates from Scratch Blocks events.
|
|
|
|
*/
|
|
|
|
|
2018-08-22 14:44:06 -04:00
|
|
|
/**
|
|
|
|
* Create a block container.
|
2019-01-23 18:18:38 -05:00
|
|
|
* @param {Runtime} runtime The runtime this block container operates within
|
2018-08-22 14:44:06 -04:00
|
|
|
* @param {boolean} optNoGlow Optional flag to indicate that blocks in this container
|
|
|
|
* should not request glows. This does not affect glows when clicking on a block to execute it.
|
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
class Blocks {
|
2019-01-23 18:18:38 -05:00
|
|
|
constructor (runtime, optNoGlow) {
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* All blocks in the workspace.
|
|
|
|
* Keys are block IDs, values are metadata about the block.
|
|
|
|
* @type {Object.<string, Object>}
|
|
|
|
*/
|
|
|
|
this._blocks = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All top-level scripts in the workspace.
|
|
|
|
* A list of block IDs that represent scripts (i.e., first block in script).
|
|
|
|
* @type {Array.<String>}
|
|
|
|
*/
|
|
|
|
this._scripts = [];
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runtime Cache
|
|
|
|
* @type {{inputs: {}, procedureParamNames: {}, procedureDefinitions: {}}}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-06-12 14:10:01 -04:00
|
|
|
Object.defineProperty(this, '_cache', {writable: true, enumerable: false});
|
2017-11-20 10:22:51 -05:00
|
|
|
this._cache = {
|
|
|
|
/**
|
|
|
|
* Cache block inputs by block id
|
|
|
|
* @type {object.<string, !Array.<object>>}
|
|
|
|
*/
|
|
|
|
inputs: {},
|
|
|
|
/**
|
|
|
|
* Cache procedure Param Names by block id
|
|
|
|
* @type {object.<string, ?Array.<string>>}
|
|
|
|
*/
|
|
|
|
procedureParamNames: {},
|
|
|
|
/**
|
|
|
|
* Cache procedure definitions by block id
|
|
|
|
* @type {object.<string, ?string>}
|
|
|
|
*/
|
2018-04-10 16:29:51 -04:00
|
|
|
procedureDefinitions: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache for execute to use and store on. Only available to
|
|
|
|
* execute.
|
|
|
|
* @type {object.<string, object>}
|
|
|
|
*/
|
2018-11-01 17:14:56 -04:00
|
|
|
_executeCached: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of block IDs and targets to start threads on as they are
|
|
|
|
* actively monitored.
|
|
|
|
* @type {Array<{blockId: string, target: Target}>}
|
|
|
|
*/
|
2019-01-18 17:21:35 -05:00
|
|
|
_monitored: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of hat opcodes to collection of theads to execute.
|
|
|
|
* @type {object.<string, object>}
|
|
|
|
*/
|
|
|
|
scripts: {}
|
2017-11-20 10:22:51 -05:00
|
|
|
};
|
|
|
|
|
2018-08-22 14:44:06 -04:00
|
|
|
/**
|
|
|
|
* Flag which indicates that blocks in this container should not glow.
|
|
|
|
* Blocks will still glow when clicked on, but this flag is used to control
|
|
|
|
* whether the blocks in this container can request a glow as part of
|
|
|
|
* a running stack. E.g. the flyout block container and the monitor block container
|
|
|
|
* should not be able to request a glow, but blocks containers belonging to
|
|
|
|
* sprites should.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.forceNoGlow = optNoGlow || false;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 13:27:01 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Blockly inputs that represent statements/branch.
|
|
|
|
* are prefixed with this string.
|
|
|
|
* @const{string}
|
2016-06-08 13:27:01 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get BRANCH_INPUT_PREFIX () {
|
|
|
|
return 'SUBSTACK';
|
|
|
|
}
|
2016-06-08 13:27:01 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Provide an object with metadata for the requested block ID.
|
|
|
|
* @param {!string} blockId ID of block we have stored.
|
|
|
|
* @return {?object} Metadata about the block, if it exists.
|
2016-06-08 13:27:01 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
getBlock (blockId) {
|
|
|
|
return this._blocks[blockId];
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get all known top-level blocks that start scripts.
|
|
|
|
* @return {Array.<string>} List of block IDs.
|
|
|
|
*/
|
|
|
|
getScripts () {
|
|
|
|
return this._scripts;
|
|
|
|
}
|
2016-06-09 13:26:07 -04:00
|
|
|
|
2017-08-26 13:07:47 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Get the next block for a particular block
|
|
|
|
* @param {?string} id ID of block to get the next block for
|
|
|
|
* @return {?string} ID of next block in the sequence
|
|
|
|
*/
|
|
|
|
getNextBlock (id) {
|
|
|
|
const block = this._blocks[id];
|
|
|
|
return (typeof block === 'undefined') ? null : block.next;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the branch for a particular C-shaped block.
|
|
|
|
* @param {?string} id ID for block to get the branch for.
|
|
|
|
* @param {?number} branchNum Which branch to select (e.g. for if-else).
|
|
|
|
* @return {?string} ID of block in the branch.
|
|
|
|
*/
|
|
|
|
getBranch (id, branchNum) {
|
|
|
|
const block = this._blocks[id];
|
|
|
|
if (typeof block === 'undefined') return null;
|
|
|
|
if (!branchNum) branchNum = 1;
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
let inputName = Blocks.BRANCH_INPUT_PREFIX;
|
|
|
|
if (branchNum > 1) {
|
|
|
|
inputName += branchNum;
|
|
|
|
}
|
2016-06-06 15:31:14 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Empty C-block?
|
|
|
|
const input = block.inputs[inputName];
|
|
|
|
return (typeof input === 'undefined') ? null : input.block;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the opcode for a particular block
|
|
|
|
* @param {?object} block The block to query
|
|
|
|
* @return {?string} the opcode corresponding to that block
|
|
|
|
*/
|
|
|
|
getOpcode (block) {
|
|
|
|
return (typeof block === 'undefined') ? null : block.opcode;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get all fields and their values for a block.
|
|
|
|
* @param {?object} block The block to query.
|
|
|
|
* @return {?object} All fields and their values.
|
|
|
|
*/
|
|
|
|
getFields (block) {
|
|
|
|
return (typeof block === 'undefined') ? null : block.fields;
|
|
|
|
}
|
2016-06-09 13:26:07 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get all non-branch inputs for a block.
|
|
|
|
* @param {?object} block the block to query.
|
2017-11-20 10:22:51 -05:00
|
|
|
* @return {?Array.<object>} All non-branch inputs and their associated blocks.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
|
|
|
getInputs (block) {
|
|
|
|
if (typeof block === 'undefined') return null;
|
2017-11-20 10:22:51 -05:00
|
|
|
let inputs = this._cache.inputs[block.id];
|
|
|
|
if (typeof inputs !== 'undefined') {
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs = {};
|
2017-04-17 19:42:48 -04:00
|
|
|
for (const input in block.inputs) {
|
|
|
|
// Ignore blocks prefixed with branch prefix.
|
|
|
|
if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length) !==
|
|
|
|
Blocks.BRANCH_INPUT_PREFIX) {
|
|
|
|
inputs[input] = block.inputs[input];
|
|
|
|
}
|
2016-06-09 13:26:07 -04:00
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
this._cache.inputs[block.id] = inputs;
|
2017-04-17 19:42:48 -04:00
|
|
|
return inputs;
|
2016-06-09 13:26:07 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get mutation data for a block.
|
|
|
|
* @param {?object} block The block to query.
|
|
|
|
* @return {?object} Mutation for the block.
|
|
|
|
*/
|
|
|
|
getMutation (block) {
|
|
|
|
return (typeof block === 'undefined') ? null : block.mutation;
|
|
|
|
}
|
2016-10-03 17:43:24 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the top-level script for a given block.
|
|
|
|
* @param {?string} id ID of block to query.
|
|
|
|
* @return {?string} ID of top-level script block.
|
|
|
|
*/
|
|
|
|
getTopLevelScript (id) {
|
|
|
|
let block = this._blocks[id];
|
|
|
|
if (typeof block === 'undefined') return null;
|
|
|
|
while (block.parent !== null) {
|
|
|
|
block = this._blocks[block.parent];
|
|
|
|
}
|
|
|
|
return block.id;
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the procedure definition for a given name.
|
|
|
|
* @param {?string} name Name of procedure to query.
|
|
|
|
* @return {?string} ID of procedure definition.
|
|
|
|
*/
|
|
|
|
getProcedureDefinition (name) {
|
2017-11-20 10:22:51 -05:00
|
|
|
const blockID = this._cache.procedureDefinitions[name];
|
|
|
|
if (typeof blockID !== 'undefined') {
|
|
|
|
return blockID;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
for (const id in this._blocks) {
|
|
|
|
if (!this._blocks.hasOwnProperty(id)) continue;
|
|
|
|
const block = this._blocks[id];
|
2017-11-16 14:17:08 -05:00
|
|
|
if (block.opcode === 'procedures_definition') {
|
2017-10-10 12:19:27 -04:00
|
|
|
const internal = this._getCustomBlockInternal(block);
|
|
|
|
if (internal && internal.mutation.proccode === name) {
|
2017-11-20 10:22:51 -05:00
|
|
|
this._cache.procedureDefinitions[name] = id; // The outer define block id
|
|
|
|
return id;
|
2017-10-10 12:19:27 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
this._cache.procedureDefinitions[name] = null;
|
2017-04-17 19:42:48 -04:00
|
|
|
return null;
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2018-10-04 18:28:00 -04:00
|
|
|
* Get names and ids of parameters for the given procedure.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @param {?string} name Name of procedure to query.
|
2017-11-06 17:28:34 -05:00
|
|
|
* @return {?Array.<string>} List of param names for a procedure.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-11-29 12:00:00 -05:00
|
|
|
getProcedureParamNamesAndIds (name) {
|
2018-10-04 18:28:00 -04:00
|
|
|
return this.getProcedureParamNamesIdsAndDefaults(name).slice(0, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get names, ids, and defaults of parameters for the given procedure.
|
|
|
|
* @param {?string} name Name of procedure to query.
|
|
|
|
* @return {?Array.<string>} List of param names for a procedure.
|
|
|
|
*/
|
|
|
|
getProcedureParamNamesIdsAndDefaults (name) {
|
2017-11-20 10:22:51 -05:00
|
|
|
const cachedNames = this._cache.procedureParamNames[name];
|
|
|
|
if (typeof cachedNames !== 'undefined') {
|
|
|
|
return cachedNames;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
for (const id in this._blocks) {
|
|
|
|
if (!this._blocks.hasOwnProperty(id)) continue;
|
|
|
|
const block = this._blocks[id];
|
2017-11-16 14:17:08 -05:00
|
|
|
if (block.opcode === 'procedures_prototype' &&
|
2017-04-17 19:42:48 -04:00
|
|
|
block.mutation.proccode === name) {
|
2017-11-29 12:00:00 -05:00
|
|
|
const names = JSON.parse(block.mutation.argumentnames);
|
|
|
|
const ids = JSON.parse(block.mutation.argumentids);
|
2018-10-04 18:28:00 -04:00
|
|
|
const defaults = JSON.parse(block.mutation.argumentdefaults);
|
|
|
|
|
|
|
|
this._cache.procedureParamNames[name] = [names, ids, defaults];
|
2017-11-29 12:00:00 -05:00
|
|
|
return this._cache.procedureParamNames[name];
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-13 13:11:26 -04:00
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
this._cache.procedureParamNames[name] = null;
|
2017-04-17 19:42:48 -04:00
|
|
|
return null;
|
2016-10-13 13:11:26 -04:00
|
|
|
}
|
|
|
|
|
2017-09-07 11:51:21 -04:00
|
|
|
duplicate () {
|
2019-01-23 18:18:38 -05:00
|
|
|
const newBlocks = new Blocks(this.runtime, this.forceNoGlow);
|
2017-09-07 11:51:21 -04:00
|
|
|
newBlocks._blocks = Clone.simple(this._blocks);
|
|
|
|
newBlocks._scripts = Clone.simple(this._scripts);
|
|
|
|
return newBlocks;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// ---------------------------------------------------------------------
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2018-06-01 18:10:25 -04:00
|
|
|
* Create event listener for blocks, variables, and comments. Handles validation and
|
2017-06-15 17:29:15 -04:00
|
|
|
* serves as a generic adapter between the blocks, variables, and the
|
|
|
|
* runtime interface.
|
|
|
|
* @param {object} e Blockly "block" or "variable" event
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2019-01-23 18:18:38 -05:00
|
|
|
blocklyListen (e) {
|
2017-04-17 19:42:48 -04:00
|
|
|
// Validate event
|
|
|
|
if (typeof e !== 'object') return;
|
2018-06-01 18:10:25 -04:00
|
|
|
if (typeof e.blockId !== 'string' && typeof e.varId !== 'string' &&
|
|
|
|
typeof e.commentId !== 'string') {
|
2017-06-15 17:29:15 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-01-23 18:18:38 -05:00
|
|
|
const stage = this.runtime.getTargetForStage();
|
|
|
|
const editingTarget = this.runtime.getEditingTarget();
|
2016-08-31 11:34:17 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// UI event: clicked scripts toggle in the runtime.
|
|
|
|
if (e.element === 'stackclick') {
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.toggleScript(e.blockId, {stackClick: true});
|
2017-04-17 19:42:48 -04:00
|
|
|
return;
|
2016-06-08 13:44:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Block create/update/destroy
|
|
|
|
switch (e.type) {
|
|
|
|
case 'create': {
|
|
|
|
const newBlocks = adapter(e);
|
|
|
|
// A create event can create many blocks. Add them all.
|
|
|
|
for (let i = 0; i < newBlocks.length; i++) {
|
|
|
|
this.createBlock(newBlocks[i]);
|
|
|
|
}
|
|
|
|
break;
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
case 'change':
|
|
|
|
this.changeBlock({
|
|
|
|
id: e.blockId,
|
|
|
|
element: e.element,
|
|
|
|
name: e.name,
|
|
|
|
value: e.newValue
|
2019-01-23 18:18:38 -05:00
|
|
|
});
|
2017-04-17 19:42:48 -04:00
|
|
|
break;
|
|
|
|
case 'move':
|
|
|
|
this.moveBlock({
|
|
|
|
id: e.blockId,
|
|
|
|
oldParent: e.oldParentId,
|
|
|
|
oldInput: e.oldInputName,
|
|
|
|
newParent: e.newParentId,
|
|
|
|
newInput: e.newInputName,
|
2018-06-07 10:18:34 -04:00
|
|
|
newCoordinate: e.newCoordinate
|
|
|
|
});
|
2017-04-17 19:42:48 -04:00
|
|
|
break;
|
2018-02-13 18:09:15 -05:00
|
|
|
case 'dragOutside':
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.emitBlockDragUpdate(e.isOutside);
|
2018-02-12 10:25:42 -05:00
|
|
|
break;
|
|
|
|
case 'endDrag':
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.emitBlockDragUpdate(false /* areBlocksOverGui */);
|
2018-02-23 11:57:19 -05:00
|
|
|
|
2019-01-23 18:18:38 -05:00
|
|
|
// Drag blocks onto another sprite
|
|
|
|
if (e.isOutside) {
|
|
|
|
const newBlocks = adapter(e);
|
|
|
|
this.runtime.emitBlockEndDrag(newBlocks, e.blockId);
|
2018-02-12 10:25:42 -05:00
|
|
|
}
|
|
|
|
break;
|
2017-04-17 19:42:48 -04:00
|
|
|
case 'delete':
|
|
|
|
// Don't accept delete events for missing blocks,
|
|
|
|
// or shadow blocks being obscured.
|
|
|
|
if (!this._blocks.hasOwnProperty(e.blockId) ||
|
|
|
|
this._blocks[e.blockId].shadow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Inform any runtime to forget about glows on this script.
|
2019-01-23 18:18:38 -05:00
|
|
|
if (this._blocks[e.blockId].topLevel) {
|
|
|
|
this.runtime.quietGlow(e.blockId);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-11-15 11:34:20 -05:00
|
|
|
this.deleteBlock(e.blockId);
|
2017-04-17 19:42:48 -04:00
|
|
|
break;
|
2017-06-15 17:29:15 -04:00
|
|
|
case 'var_create':
|
2018-07-03 23:20:49 -04:00
|
|
|
// Check if the variable being created is global or local
|
|
|
|
// If local, create a local var on the current editing target, as long
|
|
|
|
// as there are no conflicts, and the current target is actually a sprite
|
|
|
|
// If global or if the editing target is not present or we somehow got
|
|
|
|
// into a state where a local var was requested for the stage,
|
|
|
|
// create a stage (global) var after checking for name conflicts
|
|
|
|
// on all the sprites.
|
2018-11-13 14:48:49 -05:00
|
|
|
if (e.isLocal && editingTarget && !editingTarget.isStage && !e.isCloud) {
|
2018-06-27 16:32:10 -04:00
|
|
|
if (!editingTarget.lookupVariableById(e.varId)) {
|
2018-07-03 23:20:49 -04:00
|
|
|
editingTarget.createVariable(e.varId, e.varName, e.varType);
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2018-07-03 23:20:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-29 17:43:18 -05:00
|
|
|
if (stage.lookupVariableById(e.varId)) {
|
|
|
|
// Do not re-create a variable if it already exists
|
|
|
|
return;
|
|
|
|
}
|
2018-07-03 23:20:49 -04:00
|
|
|
// Check for name conflicts in all of the targets
|
2019-01-23 18:18:38 -05:00
|
|
|
const allTargets = this.runtime.targets.filter(t => t.isOriginal);
|
2018-07-03 23:20:49 -04:00
|
|
|
for (const target of allTargets) {
|
|
|
|
if (target.lookupVariableByNameAndType(e.varName, e.varType, true)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-29 10:43:07 -05:00
|
|
|
}
|
2018-11-13 14:48:49 -05:00
|
|
|
stage.createVariable(e.varId, e.varName, e.varType, e.isCloud);
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2017-10-25 11:16:42 -04:00
|
|
|
}
|
2017-06-15 17:29:15 -04:00
|
|
|
break;
|
|
|
|
case 'var_rename':
|
2018-07-15 18:58:56 -04:00
|
|
|
if (editingTarget && editingTarget.variables.hasOwnProperty(e.varId)) {
|
2018-06-27 16:32:10 -04:00
|
|
|
// This is a local variable, rename on the current target
|
|
|
|
editingTarget.renameVariable(e.varId, e.newName);
|
|
|
|
// Update all the blocks on the current target that use
|
|
|
|
// this variable
|
|
|
|
editingTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName);
|
|
|
|
} else {
|
|
|
|
// This is a global variable
|
|
|
|
stage.renameVariable(e.varId, e.newName);
|
|
|
|
// Update all blocks on all targets that use the renamed variable
|
2019-01-23 18:18:38 -05:00
|
|
|
const targets = this.runtime.targets;
|
2018-01-26 16:58:45 -05:00
|
|
|
for (let i = 0; i < targets.length; i++) {
|
|
|
|
const currTarget = targets[i];
|
|
|
|
currTarget.blocks.updateBlocksAfterVarRename(e.varId, e.newName);
|
|
|
|
}
|
2018-01-22 15:14:43 -05:00
|
|
|
}
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2017-06-15 17:29:15 -04:00
|
|
|
break;
|
2018-06-27 16:32:10 -04:00
|
|
|
case 'var_delete': {
|
2018-07-15 19:00:55 -04:00
|
|
|
const target = (editingTarget && editingTarget.variables.hasOwnProperty(e.varId)) ?
|
2018-06-27 16:32:10 -04:00
|
|
|
editingTarget : stage;
|
|
|
|
target.deleteVariable(e.varId);
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2017-06-15 17:29:15 -04:00
|
|
|
break;
|
2018-06-27 16:32:10 -04:00
|
|
|
}
|
2018-06-01 18:10:25 -04:00
|
|
|
case 'comment_create':
|
2019-01-23 18:18:38 -05:00
|
|
|
if (this.runtime.getEditingTarget()) {
|
|
|
|
const currTarget = this.runtime.getEditingTarget();
|
2018-06-01 18:10:25 -04:00
|
|
|
currTarget.createComment(e.commentId, e.blockId, e.text,
|
|
|
|
e.xy.x, e.xy.y, e.width, e.height, e.minimized);
|
2018-06-12 17:10:08 -04:00
|
|
|
|
|
|
|
if (currTarget.comments[e.commentId].x === null &&
|
|
|
|
currTarget.comments[e.commentId].y === null) {
|
|
|
|
// Block comments imported from 2.0 projects are imported with their
|
|
|
|
// x and y coordinates set to null so that scratch-blocks can
|
|
|
|
// auto-position them. If we are receiving a create event for these
|
|
|
|
// comments, then the auto positioning should have taken place.
|
|
|
|
// Update the x and y position of these comments to match the
|
|
|
|
// one from the event.
|
|
|
|
currTarget.comments[e.commentId].x = e.xy.x;
|
|
|
|
currTarget.comments[e.commentId].y = e.xy.y;
|
|
|
|
}
|
2018-06-01 18:10:25 -04:00
|
|
|
}
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2018-06-01 18:10:25 -04:00
|
|
|
break;
|
|
|
|
case 'comment_change':
|
2019-01-23 18:18:38 -05:00
|
|
|
if (this.runtime.getEditingTarget()) {
|
|
|
|
const currTarget = this.runtime.getEditingTarget();
|
2018-06-01 18:10:25 -04:00
|
|
|
if (!currTarget.comments.hasOwnProperty(e.commentId)) {
|
|
|
|
log.warn(`Cannot change comment with id ${e.commentId} because it does not exist.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const comment = currTarget.comments[e.commentId];
|
|
|
|
const change = e.newContents_;
|
2018-06-07 10:09:38 -04:00
|
|
|
if (change.hasOwnProperty('minimized')) {
|
|
|
|
comment.minimized = change.minimized;
|
2018-06-07 11:04:03 -04:00
|
|
|
}
|
|
|
|
if (change.hasOwnProperty('width') && change.hasOwnProperty('height')){
|
2018-06-07 10:09:38 -04:00
|
|
|
comment.width = change.width;
|
|
|
|
comment.height = change.height;
|
2018-06-07 11:04:03 -04:00
|
|
|
}
|
|
|
|
if (change.hasOwnProperty('text')) {
|
2018-06-07 10:09:38 -04:00
|
|
|
comment.text = change.text;
|
2018-06-01 18:10:25 -04:00
|
|
|
}
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2018-06-01 18:10:25 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'comment_move':
|
2019-01-23 18:18:38 -05:00
|
|
|
if (this.runtime.getEditingTarget()) {
|
|
|
|
const currTarget = this.runtime.getEditingTarget();
|
2018-06-05 15:13:03 -04:00
|
|
|
if (currTarget && !currTarget.comments.hasOwnProperty(e.commentId)) {
|
2018-06-01 18:10:25 -04:00
|
|
|
log.warn(`Cannot change comment with id ${e.commentId} because it does not exist.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const comment = currTarget.comments[e.commentId];
|
|
|
|
const newCoord = e.newCoordinate_;
|
2018-06-05 15:12:25 -04:00
|
|
|
comment.x = newCoord.x;
|
|
|
|
comment.y = newCoord.y;
|
2019-01-29 17:43:18 -05:00
|
|
|
|
|
|
|
this.emitProjectChanged();
|
2018-06-01 18:10:25 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'comment_delete':
|
2019-01-23 18:18:38 -05:00
|
|
|
if (this.runtime.getEditingTarget()) {
|
|
|
|
const currTarget = this.runtime.getEditingTarget();
|
2018-06-01 18:10:25 -04:00
|
|
|
if (!currTarget.comments.hasOwnProperty(e.commentId)) {
|
|
|
|
// If we're in this state, we have probably received
|
|
|
|
// a delete event from a workspace that we switched from
|
|
|
|
// (e.g. a delete event for a comment on sprite a's workspace
|
|
|
|
// when switching from sprite a to sprite b)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete currTarget.comments[e.commentId];
|
|
|
|
if (e.blockId) {
|
|
|
|
const block = currTarget.blocks.getBlock(e.blockId);
|
|
|
|
if (!block) {
|
|
|
|
log.warn(`Could not find block referenced by comment with id: ${e.commentId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete block.comment;
|
|
|
|
}
|
2019-01-29 17:43:18 -05:00
|
|
|
|
|
|
|
this.emitProjectChanged();
|
2018-06-01 18:10:25 -04:00
|
|
|
}
|
|
|
|
break;
|
2016-09-15 13:51:40 -04:00
|
|
|
}
|
2016-08-31 11:34:17 -04:00
|
|
|
}
|
2016-06-08 13:44:09 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// ---------------------------------------------------------------------
|
2016-06-08 13:44:09 -04:00
|
|
|
|
2017-11-20 10:22:51 -05:00
|
|
|
/**
|
|
|
|
* Reset all runtime caches.
|
|
|
|
*/
|
|
|
|
resetCache () {
|
|
|
|
this._cache.inputs = {};
|
|
|
|
this._cache.procedureParamNames = {};
|
|
|
|
this._cache.procedureDefinitions = {};
|
2018-04-10 16:29:51 -04:00
|
|
|
this._cache._executeCached = {};
|
2018-11-01 17:14:56 -04:00
|
|
|
this._cache._monitored = null;
|
2019-01-18 17:21:35 -05:00
|
|
|
this._cache.scripts = {};
|
2017-11-20 10:22:51 -05:00
|
|
|
}
|
|
|
|
|
2019-01-29 17:43:18 -05:00
|
|
|
/**
|
|
|
|
* Emit a project changed event if this is a block container
|
|
|
|
* that can affect the project state.
|
|
|
|
*/
|
|
|
|
emitProjectChanged () {
|
|
|
|
if (!this.forceNoGlow) {
|
|
|
|
this.runtime.emitProjectChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Block management: create blocks and scripts from a `create` event
|
|
|
|
* @param {!object} block Blockly create event to be processed
|
|
|
|
*/
|
|
|
|
createBlock (block) {
|
|
|
|
// Does the block already exist?
|
|
|
|
// Could happen, e.g., for an unobscured shadow.
|
|
|
|
if (this._blocks.hasOwnProperty(block.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Create new block.
|
|
|
|
this._blocks[block.id] = block;
|
|
|
|
// Push block id to scripts array.
|
|
|
|
// Blocks are added as a top-level stack if they are marked as a top-block
|
|
|
|
// (if they were top-level XML in the event).
|
|
|
|
if (block.topLevel) {
|
|
|
|
this._addScript(block.id);
|
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
this.resetCache();
|
2019-01-29 17:43:18 -05:00
|
|
|
|
|
|
|
// A new block was actually added to the block container,
|
|
|
|
// emit a project changed event
|
|
|
|
this.emitProjectChanged();
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Block management: change block field values
|
|
|
|
* @param {!object} args Blockly change event to be processed
|
|
|
|
*/
|
2019-01-23 18:18:38 -05:00
|
|
|
changeBlock (args) {
|
2017-04-17 19:42:48 -04:00
|
|
|
// Validate
|
2017-05-08 09:53:16 -04:00
|
|
|
if (['field', 'mutation', 'checkbox'].indexOf(args.element) === -1) return;
|
2018-11-08 00:37:28 -05:00
|
|
|
let block = this._blocks[args.id];
|
2017-04-17 19:42:48 -04:00
|
|
|
if (typeof block === 'undefined') return;
|
2017-05-08 09:53:16 -04:00
|
|
|
switch (args.element) {
|
|
|
|
case 'field':
|
2018-11-08 00:37:28 -05:00
|
|
|
// TODO when the field of a monitored block changes,
|
|
|
|
// update the checkbox in the flyout based on whether
|
|
|
|
// a monitor for that current combination of selected parameters exists
|
|
|
|
// e.g.
|
|
|
|
// 1. check (current [v year])
|
|
|
|
// 2. switch dropdown in flyout block to (current [v minute])
|
|
|
|
// 3. the checkbox should become unchecked if we're not already
|
|
|
|
// monitoring current minute
|
|
|
|
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Update block value
|
|
|
|
if (!block.fields[args.name]) return;
|
2017-11-21 16:48:48 -05:00
|
|
|
if (args.name === 'VARIABLE' || args.name === 'LIST' ||
|
|
|
|
args.name === 'BROADCAST_OPTION') {
|
2017-06-27 19:15:07 -04:00
|
|
|
// Get variable name using the id in args.value.
|
2019-01-23 18:18:38 -05:00
|
|
|
const variable = this.runtime.getEditingTarget().lookupVariableById(args.value);
|
2017-06-27 19:15:07 -04:00
|
|
|
if (variable) {
|
|
|
|
block.fields[args.name].value = variable.name;
|
|
|
|
block.fields[args.name].id = args.value;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-08 20:22:18 -05:00
|
|
|
// Changing the value in a dropdown
|
2017-06-27 19:15:07 -04:00
|
|
|
block.fields[args.name].value = args.value;
|
2018-01-08 20:22:18 -05:00
|
|
|
|
2018-11-20 16:32:08 -05:00
|
|
|
// The selected item in the sensing of block menu needs to change based on the
|
|
|
|
// selected target. Set it to the first item in the menu list.
|
2018-11-28 12:34:49 -05:00
|
|
|
// TODO: (#1787)
|
2018-11-20 16:32:08 -05:00
|
|
|
if (block.opcode === 'sensing_of_object_menu') {
|
|
|
|
if (block.fields.OBJECT.value === '_stage_') {
|
|
|
|
this._blocks[block.parent].fields.PROPERTY.value = 'backdrop #';
|
|
|
|
} else {
|
|
|
|
this._blocks[block.parent].fields.PROPERTY.value = 'x position';
|
|
|
|
}
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.requestBlocksUpdate();
|
2018-11-20 16:32:08 -05:00
|
|
|
}
|
2018-01-08 20:22:18 -05:00
|
|
|
|
2018-01-10 17:50:48 -05:00
|
|
|
const flyoutBlock = block.shadow && block.parent ? this._blocks[block.parent] : block;
|
|
|
|
if (flyoutBlock.isMonitored) {
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.requestUpdateMonitor(Map({
|
2018-01-10 17:50:48 -05:00
|
|
|
id: flyoutBlock.id,
|
|
|
|
params: this._getBlockParams(flyoutBlock)
|
2018-01-08 20:22:18 -05:00
|
|
|
}));
|
|
|
|
}
|
2017-06-27 19:15:07 -04:00
|
|
|
}
|
2017-05-08 09:53:16 -04:00
|
|
|
break;
|
|
|
|
case 'mutation':
|
2017-04-17 19:42:48 -04:00
|
|
|
block.mutation = mutationAdapter(args.value);
|
2017-05-08 09:53:16 -04:00
|
|
|
break;
|
2018-01-09 14:55:46 -05:00
|
|
|
case 'checkbox': {
|
2018-11-08 00:37:28 -05:00
|
|
|
// A checkbox usually has a one to one correspondence with the monitor
|
|
|
|
// block but in the case of monitored reporters that have arguments,
|
|
|
|
// map the old id to a new id, creating a new monitor block if necessary
|
|
|
|
if (block.fields && Object.keys(block.fields).length > 0 &&
|
|
|
|
block.opcode !== 'data_variable' && block.opcode !== 'data_listcontents') {
|
|
|
|
|
2018-11-08 01:33:26 -05:00
|
|
|
// This block has an argument which needs to get separated out into
|
|
|
|
// multiple monitor blocks with ids based on the selected argument
|
|
|
|
const newId = getMonitorIdForBlockWithArgs(block.id, block.fields);
|
2018-11-08 00:37:28 -05:00
|
|
|
// Note: we're not just constantly creating a longer and longer id everytime we check
|
|
|
|
// the checkbox because we're using the id of the block in the flyout as the base
|
|
|
|
|
2018-11-08 14:43:07 -05:00
|
|
|
// check if a block with the new id already exists, otherwise create
|
2019-01-23 18:18:38 -05:00
|
|
|
let newBlock = this.runtime.monitorBlocks.getBlock(newId);
|
2018-11-08 00:37:28 -05:00
|
|
|
if (!newBlock) {
|
|
|
|
newBlock = JSON.parse(JSON.stringify(block));
|
|
|
|
newBlock.id = newId;
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.monitorBlocks.createBlock(newBlock);
|
2018-11-08 00:37:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
block = newBlock; // Carry on through the rest of this code with newBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
const wasMonitored = block.isMonitored;
|
|
|
|
block.isMonitored = args.value;
|
|
|
|
|
2018-05-08 14:09:18 -04:00
|
|
|
// Variable blocks may be sprite specific depending on the owner of the variable
|
|
|
|
let isSpriteLocalVariable = false;
|
|
|
|
if (block.opcode === 'data_variable') {
|
2019-01-23 18:18:38 -05:00
|
|
|
isSpriteLocalVariable = !(this.runtime.getTargetForStage().variables[block.fields.VARIABLE.id]);
|
2018-05-08 14:09:18 -04:00
|
|
|
} else if (block.opcode === 'data_listcontents') {
|
2019-01-23 18:18:38 -05:00
|
|
|
isSpriteLocalVariable = !(this.runtime.getTargetForStage().variables[block.fields.LIST.id]);
|
2018-05-08 14:09:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const isSpriteSpecific = isSpriteLocalVariable ||
|
2019-01-23 18:18:38 -05:00
|
|
|
(this.runtime.monitorBlockInfo.hasOwnProperty(block.opcode) &&
|
|
|
|
this.runtime.monitorBlockInfo[block.opcode].isSpriteSpecific);
|
2018-12-05 15:02:27 -05:00
|
|
|
if (isSpriteSpecific) {
|
|
|
|
// If creating a new sprite specific monitor, the only possible target is
|
|
|
|
// the current editing one b/c you cannot dynamically create monitors.
|
|
|
|
// Also, do not change the targetId if it has already been assigned
|
2019-01-23 18:18:38 -05:00
|
|
|
block.targetId = block.targetId || this.runtime.getEditingTarget().id;
|
2018-12-05 15:02:27 -05:00
|
|
|
} else {
|
|
|
|
block.targetId = null;
|
|
|
|
}
|
2018-04-10 16:29:51 -04:00
|
|
|
|
2018-01-08 20:22:18 -05:00
|
|
|
if (wasMonitored && !block.isMonitored) {
|
2019-01-23 18:18:38 -05:00
|
|
|
this.runtime.requestHideMonitor(block.id);
|
2018-01-08 20:22:18 -05:00
|
|
|
} else if (!wasMonitored && block.isMonitored) {
|
2018-06-08 16:00:12 -04:00
|
|
|
// Tries to show the monitor for specified block. If it doesn't exist, add the monitor.
|
2019-01-23 18:18:38 -05:00
|
|
|
if (!this.runtime.requestShowMonitor(block.id)) {
|
|
|
|
this.runtime.requestAddMonitor(MonitorRecord({
|
2018-06-08 16:00:12 -04:00
|
|
|
id: block.id,
|
|
|
|
targetId: block.targetId,
|
2019-01-23 18:18:38 -05:00
|
|
|
spriteName: block.targetId ? this.runtime.getTargetById(block.targetId).getName() : null,
|
2018-06-08 16:00:12 -04:00
|
|
|
opcode: block.opcode,
|
|
|
|
params: this._getBlockParams(block),
|
|
|
|
// @todo(vm#565) for numerical values with decimals, some countries use comma
|
|
|
|
value: '',
|
2018-06-08 17:04:52 -04:00
|
|
|
mode: block.opcode === 'data_listcontents' ? 'list' : 'default'
|
2018-06-08 16:00:12 -04:00
|
|
|
}));
|
|
|
|
}
|
2017-05-10 15:47:06 -04:00
|
|
|
}
|
2017-05-08 09:53:16 -04:00
|
|
|
break;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2018-01-09 14:55:46 -05:00
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
|
|
|
|
2017-11-20 10:22:51 -05:00
|
|
|
this.resetCache();
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Block management: move blocks from parent to parent
|
|
|
|
* @param {!object} e Blockly move event to be processed
|
|
|
|
*/
|
2018-06-07 10:18:34 -04:00
|
|
|
moveBlock (e) {
|
2017-04-17 19:42:48 -04:00
|
|
|
if (!this._blocks.hasOwnProperty(e.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2019-01-29 17:43:18 -05:00
|
|
|
const block = this._blocks[e.id];
|
|
|
|
// Track whether a change actually occurred
|
|
|
|
// ignoring changes like routine re-positioning
|
|
|
|
// of a block when loading a workspace
|
|
|
|
let didChange = false;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Move coordinate changes.
|
|
|
|
if (e.newCoordinate) {
|
2019-01-29 17:43:18 -05:00
|
|
|
|
|
|
|
didChange = (block.x !== e.newCoordinate.x) || (block.y !== e.newCoordinate.y);
|
|
|
|
|
|
|
|
block.x = e.newCoordinate.x;
|
|
|
|
block.y = e.newCoordinate.y;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-08-31 11:34:29 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Remove from any old parent.
|
|
|
|
if (typeof e.oldParent !== 'undefined') {
|
|
|
|
const oldParent = this._blocks[e.oldParent];
|
|
|
|
if (typeof e.oldInput !== 'undefined' &&
|
|
|
|
oldParent.inputs[e.oldInput].block === e.id) {
|
|
|
|
// This block was connected to the old parent's input.
|
|
|
|
oldParent.inputs[e.oldInput].block = null;
|
|
|
|
} else if (oldParent.next === e.id) {
|
|
|
|
// This block was connected to the old parent's next connection.
|
|
|
|
oldParent.next = null;
|
|
|
|
}
|
|
|
|
this._blocks[e.id].parent = null;
|
2019-01-29 17:43:18 -05:00
|
|
|
didChange = true;
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
|
|
|
|
2019-01-29 17:43:18 -05:00
|
|
|
// Is this block a top-level block?
|
2017-04-17 19:42:48 -04:00
|
|
|
if (typeof e.newParent === 'undefined') {
|
|
|
|
this._addScript(e.id);
|
2016-10-23 17:55:31 -04:00
|
|
|
} else {
|
2017-04-17 19:42:48 -04:00
|
|
|
// Remove script, if one exists.
|
|
|
|
this._deleteScript(e.id);
|
|
|
|
// Otherwise, try to connect it in its new place.
|
|
|
|
if (typeof e.newInput === 'undefined') {
|
|
|
|
// Moved to the new parent's next connection.
|
|
|
|
this._blocks[e.newParent].next = e.id;
|
|
|
|
} else {
|
|
|
|
// Moved to the new parent's input.
|
|
|
|
// Don't obscure the shadow block.
|
|
|
|
let oldShadow = null;
|
|
|
|
if (this._blocks[e.newParent].inputs.hasOwnProperty(e.newInput)) {
|
|
|
|
oldShadow = this._blocks[e.newParent].inputs[e.newInput].shadow;
|
|
|
|
}
|
2019-03-11 14:52:40 -04:00
|
|
|
|
|
|
|
// If the block being attached is itself a shadow, make sure to set
|
|
|
|
// both block and shadow to that blocks ID. This happens when adding
|
|
|
|
// inputs to a custom procedure.
|
|
|
|
if (this._blocks[e.id].shadow) oldShadow = e.id;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
this._blocks[e.newParent].inputs[e.newInput] = {
|
|
|
|
name: e.newInput,
|
|
|
|
block: e.id,
|
|
|
|
shadow: oldShadow
|
|
|
|
};
|
2016-09-08 09:40:01 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this._blocks[e.id].parent = e.newParent;
|
2019-01-29 17:43:18 -05:00
|
|
|
didChange = true;
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
2017-11-20 10:22:51 -05:00
|
|
|
this.resetCache();
|
2019-01-29 17:43:18 -05:00
|
|
|
|
|
|
|
if (didChange) this.emitProjectChanged();
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-05-22 13:38:29 -04:00
|
|
|
|
2017-05-08 09:53:16 -04:00
|
|
|
/**
|
|
|
|
* Block management: run all blocks.
|
|
|
|
* @param {!object} runtime Runtime to run all blocks in.
|
|
|
|
*/
|
|
|
|
runAllMonitored (runtime) {
|
2018-11-01 17:14:56 -04:00
|
|
|
if (this._cache._monitored === null) {
|
|
|
|
this._cache._monitored = Object.keys(this._blocks)
|
|
|
|
.filter(blockId => this.getBlock(blockId).isMonitored)
|
|
|
|
.map(blockId => {
|
|
|
|
const targetId = this.getBlock(blockId).targetId;
|
|
|
|
return {
|
|
|
|
blockId,
|
|
|
|
target: targetId ? runtime.getTargetById(targetId) : null
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const monitored = this._cache._monitored;
|
|
|
|
for (let i = 0; i < monitored.length; i++) {
|
|
|
|
const {blockId, target} = monitored[i];
|
|
|
|
runtime.addMonitorScript(blockId, target);
|
|
|
|
}
|
2017-05-08 09:53:16 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-11-15 11:55:22 -05:00
|
|
|
* Block management: delete blocks and their associated scripts. Does nothing if a block
|
|
|
|
* with the given ID does not exist.
|
2017-11-15 11:34:20 -05:00
|
|
|
* @param {!string} blockId Id of block to delete
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-11-15 11:34:20 -05:00
|
|
|
deleteBlock (blockId) {
|
2017-04-17 19:42:48 -04:00
|
|
|
// @todo In runtime, stop threads running on this script.
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Get block
|
2017-11-15 11:34:20 -05:00
|
|
|
const block = this._blocks[blockId];
|
2017-11-15 11:55:22 -05:00
|
|
|
if (!block) {
|
|
|
|
// No block with the given ID exists
|
|
|
|
return;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Delete children
|
|
|
|
if (block.next !== null) {
|
2017-11-15 11:34:20 -05:00
|
|
|
this.deleteBlock(block.next);
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
// Delete inputs (including branches)
|
|
|
|
for (const input in block.inputs) {
|
|
|
|
// If it's null, the block in this input moved away.
|
|
|
|
if (block.inputs[input].block !== null) {
|
2017-11-15 11:34:20 -05:00
|
|
|
this.deleteBlock(block.inputs[input].block);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
// Delete obscured shadow blocks.
|
|
|
|
if (block.inputs[input].shadow !== null &&
|
|
|
|
block.inputs[input].shadow !== block.inputs[input].block) {
|
2017-11-15 11:34:20 -05:00
|
|
|
this.deleteBlock(block.inputs[input].shadow);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Delete any script starting with this block.
|
2017-11-15 11:34:20 -05:00
|
|
|
this._deleteScript(blockId);
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Delete block itself.
|
2017-11-15 11:34:20 -05:00
|
|
|
delete this._blocks[blockId];
|
2017-11-20 10:22:51 -05:00
|
|
|
|
|
|
|
this.resetCache();
|
2019-01-29 17:43:18 -05:00
|
|
|
this.emitProjectChanged();
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2022-12-12 16:54:32 -05:00
|
|
|
/**
|
|
|
|
* Delete all blocks and their associated scripts.
|
|
|
|
*/
|
|
|
|
deleteAllBlocks () {
|
|
|
|
const blockIds = Object.keys(this._blocks);
|
|
|
|
blockIds.forEach(blockId => this.deleteBlock(blockId));
|
|
|
|
}
|
|
|
|
|
2018-06-19 14:02:54 -04:00
|
|
|
/**
|
2018-06-19 15:31:08 -04:00
|
|
|
* Returns a map of all references to variables or lists from blocks
|
|
|
|
* in this block container.
|
2018-07-09 10:44:45 -04:00
|
|
|
* @param {Array<object>} optBlocks Optional list of blocks to constrain the search to.
|
|
|
|
* This is useful for getting variable/list references for a stack of blocks instead
|
|
|
|
* of all blocks on the workspace
|
2019-02-06 16:58:46 -05:00
|
|
|
* @param {?boolean} optIncludeBroadcast Optional whether to include broadcast fields.
|
2018-06-19 15:31:08 -04:00
|
|
|
* @return {object} A map of variable ID to a list of all variable references
|
|
|
|
* for that ID. A variable reference contains the field referencing that variable
|
|
|
|
* and also the type of the variable being referenced.
|
2018-06-19 14:02:54 -04:00
|
|
|
*/
|
2019-02-06 16:58:46 -05:00
|
|
|
getAllVariableAndListReferences (optBlocks, optIncludeBroadcast) {
|
2018-07-09 10:44:45 -04:00
|
|
|
const blocks = optBlocks ? optBlocks : this._blocks;
|
2018-06-19 15:31:08 -04:00
|
|
|
const allReferences = Object.create(null);
|
2018-06-19 14:02:54 -04:00
|
|
|
for (const blockId in blocks) {
|
|
|
|
let varOrListField = null;
|
|
|
|
let varType = null;
|
|
|
|
if (blocks[blockId].fields.VARIABLE) {
|
|
|
|
varOrListField = blocks[blockId].fields.VARIABLE;
|
|
|
|
varType = Variable.SCALAR_TYPE;
|
|
|
|
} else if (blocks[blockId].fields.LIST) {
|
|
|
|
varOrListField = blocks[blockId].fields.LIST;
|
|
|
|
varType = Variable.LIST_TYPE;
|
2019-02-06 16:58:46 -05:00
|
|
|
} else if (optIncludeBroadcast && blocks[blockId].fields.BROADCAST_OPTION) {
|
|
|
|
varOrListField = blocks[blockId].fields.BROADCAST_OPTION;
|
|
|
|
varType = Variable.BROADCAST_MESSAGE_TYPE;
|
2018-06-19 14:02:54 -04:00
|
|
|
}
|
|
|
|
if (varOrListField) {
|
|
|
|
const currVarId = varOrListField.id;
|
2018-06-19 15:31:08 -04:00
|
|
|
if (allReferences[currVarId]) {
|
|
|
|
allReferences[currVarId].push({
|
|
|
|
referencingField: varOrListField,
|
|
|
|
type: varType
|
|
|
|
});
|
2018-06-19 14:02:54 -04:00
|
|
|
} else {
|
2018-06-19 15:31:08 -04:00
|
|
|
allReferences[currVarId] = [{
|
|
|
|
referencingField: varOrListField,
|
|
|
|
type: varType
|
|
|
|
}];
|
2018-06-19 14:02:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 15:31:08 -04:00
|
|
|
return allReferences;
|
2018-06-19 14:02:54 -04:00
|
|
|
}
|
|
|
|
|
2018-01-26 16:58:45 -05:00
|
|
|
/**
|
|
|
|
* Keep blocks up to date after a variable gets renamed.
|
|
|
|
* @param {string} varId The id of the variable that was renamed
|
|
|
|
* @param {string} newName The new name of the variable that was renamed
|
|
|
|
*/
|
|
|
|
updateBlocksAfterVarRename (varId, newName) {
|
|
|
|
const blocks = this._blocks;
|
|
|
|
for (const blockId in blocks) {
|
|
|
|
let varOrListField = null;
|
|
|
|
if (blocks[blockId].fields.VARIABLE) {
|
|
|
|
varOrListField = blocks[blockId].fields.VARIABLE;
|
|
|
|
} else if (blocks[blockId].fields.LIST) {
|
|
|
|
varOrListField = blocks[blockId].fields.LIST;
|
|
|
|
}
|
|
|
|
if (varOrListField) {
|
|
|
|
const currFieldId = varOrListField.id;
|
|
|
|
if (varId === currFieldId) {
|
|
|
|
varOrListField.value = newName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:57 -04:00
|
|
|
/**
|
|
|
|
* Keep blocks up to date after they are shared between targets.
|
|
|
|
* @param {boolean} isStage If the new target is a stage.
|
|
|
|
*/
|
|
|
|
updateTargetSpecificBlocks (isStage) {
|
|
|
|
const blocks = this._blocks;
|
|
|
|
for (const blockId in blocks) {
|
|
|
|
if (isStage && blocks[blockId].opcode === 'event_whenthisspriteclicked') {
|
|
|
|
blocks[blockId].opcode = 'event_whenstageclicked';
|
|
|
|
} else if (!isStage && blocks[blockId].opcode === 'event_whenstageclicked') {
|
|
|
|
blocks[blockId].opcode = 'event_whenthisspriteclicked';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:35:52 -05:00
|
|
|
/**
|
|
|
|
* Update blocks after a sound, costume, or backdrop gets renamed.
|
|
|
|
* Any block referring to the old name of the asset should get updated
|
|
|
|
* to refer to the new name.
|
|
|
|
* @param {string} oldName The old name of the asset that was renamed.
|
|
|
|
* @param {string} newName The new name of the asset that was renamed.
|
|
|
|
* @param {string} assetType String representation of the kind of asset
|
2018-02-05 16:54:58 -05:00
|
|
|
* that was renamed. This can be one of 'sprite','costume', 'sound', or
|
|
|
|
* 'backdrop'.
|
2018-02-05 14:35:52 -05:00
|
|
|
*/
|
|
|
|
updateAssetName (oldName, newName, assetType) {
|
|
|
|
let getAssetField;
|
|
|
|
if (assetType === 'costume') {
|
|
|
|
getAssetField = this._getCostumeField.bind(this);
|
|
|
|
} else if (assetType === 'sound') {
|
|
|
|
getAssetField = this._getSoundField.bind(this);
|
|
|
|
} else if (assetType === 'backdrop') {
|
|
|
|
getAssetField = this._getBackdropField.bind(this);
|
2018-02-05 16:54:58 -05:00
|
|
|
} else if (assetType === 'sprite') {
|
|
|
|
getAssetField = this._getSpriteField.bind(this);
|
2018-02-05 14:35:52 -05:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const blocks = this._blocks;
|
|
|
|
for (const blockId in blocks) {
|
|
|
|
const assetField = getAssetField(blockId);
|
|
|
|
if (assetField && assetField.value === oldName) {
|
|
|
|
assetField.value = newName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 06:00:43 -04:00
|
|
|
/**
|
|
|
|
* Update sensing_of blocks after a variable gets renamed.
|
|
|
|
* @param {string} oldName The old name of the variable that was renamed.
|
|
|
|
* @param {string} newName The new name of the variable that was renamed.
|
|
|
|
* @param {string} targetName The name of the target the variable belongs to.
|
|
|
|
* @return {boolean} Returns true if any of the blocks were updated.
|
|
|
|
*/
|
|
|
|
updateSensingOfReference (oldName, newName, targetName) {
|
|
|
|
const blocks = this._blocks;
|
|
|
|
let blockUpdated = false;
|
|
|
|
for (const blockId in blocks) {
|
|
|
|
const block = blocks[blockId];
|
|
|
|
if (block.opcode === 'sensing_of' &&
|
|
|
|
block.fields.PROPERTY.value === oldName &&
|
|
|
|
// If block and shadow are different, it means a block is inserted to OBJECT, and should be ignored.
|
|
|
|
block.inputs.OBJECT.block === block.inputs.OBJECT.shadow) {
|
|
|
|
const inputBlock = this.getBlock(block.inputs.OBJECT.block);
|
|
|
|
if (inputBlock.fields.OBJECT.value === targetName) {
|
|
|
|
block.fields.PROPERTY.value = newName;
|
|
|
|
blockUpdated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 11:24:05 -04:00
|
|
|
if (blockUpdated) this.resetCache();
|
2020-05-22 06:00:43 -04:00
|
|
|
return blockUpdated;
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:35:52 -05:00
|
|
|
/**
|
|
|
|
* Helper function to retrieve a costume menu field from a block given its id.
|
|
|
|
* @param {string} blockId A unique identifier for a block
|
|
|
|
* @return {?object} The costume menu field of the block with the given block id.
|
|
|
|
* Null if either a block with the given id doesn't exist or if a costume menu field
|
|
|
|
* does not exist on the block with the given id.
|
|
|
|
*/
|
|
|
|
_getCostumeField (blockId) {
|
|
|
|
const block = this.getBlock(blockId);
|
|
|
|
if (block && block.fields.hasOwnProperty('COSTUME')) {
|
|
|
|
return block.fields.COSTUME;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retrieve a sound menu field from a block given its id.
|
|
|
|
* @param {string} blockId A unique identifier for a block
|
|
|
|
* @return {?object} The sound menu field of the block with the given block id.
|
|
|
|
* Null, if either a block with the given id doesn't exist or if a sound menu field
|
|
|
|
* does not exist on the block with the given id.
|
|
|
|
*/
|
|
|
|
_getSoundField (blockId) {
|
|
|
|
const block = this.getBlock(blockId);
|
|
|
|
if (block && block.fields.hasOwnProperty('SOUND_MENU')) {
|
|
|
|
return block.fields.SOUND_MENU;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to retrieve a backdrop menu field from a block given its id.
|
|
|
|
* @param {string} blockId A unique identifier for a block
|
|
|
|
* @return {?object} The backdrop menu field of the block with the given block id.
|
|
|
|
* Null, if either a block with the given id doesn't exist or if a backdrop menu field
|
|
|
|
* does not exist on the block with the given id.
|
|
|
|
*/
|
|
|
|
_getBackdropField (blockId) {
|
|
|
|
const block = this.getBlock(blockId);
|
|
|
|
if (block && block.fields.hasOwnProperty('BACKDROP')) {
|
|
|
|
return block.fields.BACKDROP;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-05 16:54:58 -05:00
|
|
|
/**
|
|
|
|
* Helper function to retrieve a sprite menu field from a block given its id.
|
|
|
|
* @param {string} blockId A unique identifier for a block
|
|
|
|
* @return {?object} The sprite menu field of the block with the given block id.
|
|
|
|
* Null, if either a block with the given id doesn't exist or if a sprite menu field
|
|
|
|
* does not exist on the block with the given id.
|
|
|
|
*/
|
|
|
|
_getSpriteField (blockId) {
|
|
|
|
const block = this.getBlock(blockId);
|
|
|
|
if (!block) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const spriteMenuNames = ['TOWARDS', 'TO', 'OBJECT', 'VIDEOONMENU2',
|
|
|
|
'DISTANCETOMENU', 'TOUCHINGOBJECTMENU', 'CLONE_OPTION'];
|
|
|
|
for (let i = 0; i < spriteMenuNames.length; i++) {
|
|
|
|
const menuName = spriteMenuNames[i];
|
|
|
|
if (block.fields.hasOwnProperty(menuName)) {
|
|
|
|
return block.fields[menuName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// ---------------------------------------------------------------------
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Encode all of `this._blocks` as an XML string usable
|
|
|
|
* by a Blockly/scratch-blocks workspace.
|
2018-05-21 13:51:43 -04:00
|
|
|
* @param {object<string, Comment>} comments Map of comments referenced by id
|
2017-04-17 19:42:48 -04:00
|
|
|
* @return {string} String of XML representing this object's blocks.
|
|
|
|
*/
|
2018-05-21 11:30:22 -04:00
|
|
|
toXML (comments) {
|
|
|
|
return this._scripts.map(script => this.blockToXML(script, comments)).join();
|
2016-08-31 11:38:45 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Recursively encode an individual block and its children
|
|
|
|
* into a Blockly/scratch-blocks XML string.
|
|
|
|
* @param {!string} blockId ID of block to encode.
|
2018-05-21 13:51:43 -04:00
|
|
|
* @param {object<string, Comment>} comments Map of comments referenced by id
|
2017-04-17 19:42:48 -04:00
|
|
|
* @return {string} String of XML representing this block and any children.
|
|
|
|
*/
|
2018-05-21 11:30:22 -04:00
|
|
|
blockToXML (blockId, comments) {
|
2017-04-17 19:42:48 -04:00
|
|
|
const block = this._blocks[blockId];
|
2019-01-15 10:58:06 -05:00
|
|
|
// block should exist, but currently some blocks' next property point
|
|
|
|
// to a blockId for non-existent blocks. Until we track down that behavior,
|
|
|
|
// this early exit allows the project to load.
|
|
|
|
if (!block) return;
|
2017-04-17 19:42:48 -04:00
|
|
|
// Encode properties of this block.
|
|
|
|
const tagName = (block.shadow) ? 'shadow' : 'block';
|
2017-04-19 17:36:33 -04:00
|
|
|
let xmlString =
|
|
|
|
`<${tagName}
|
|
|
|
id="${block.id}"
|
|
|
|
type="${block.opcode}"
|
2017-08-26 13:07:47 -04:00
|
|
|
${block.topLevel ? `x="${block.x}" y="${block.y}"` : ''}
|
2017-04-19 17:36:33 -04:00
|
|
|
>`;
|
2018-05-21 11:30:22 -04:00
|
|
|
const commentId = block.comment;
|
|
|
|
if (commentId) {
|
|
|
|
if (comments) {
|
|
|
|
if (comments.hasOwnProperty(commentId)) {
|
|
|
|
xmlString += comments[commentId].toXML();
|
|
|
|
} else {
|
|
|
|
log.warn(`Could not find comment with id: ${commentId} in provided comment descriptions.`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.warn(`Cannot serialize comment with id: ${commentId}; no comment descriptions provided.`);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Add any mutation. Must come before inputs.
|
|
|
|
if (block.mutation) {
|
|
|
|
xmlString += this.mutationToXML(block.mutation);
|
|
|
|
}
|
|
|
|
// Add any inputs on this block.
|
|
|
|
for (const input in block.inputs) {
|
|
|
|
if (!block.inputs.hasOwnProperty(input)) continue;
|
|
|
|
const blockInput = block.inputs[input];
|
|
|
|
// Only encode a value tag if the value input is occupied.
|
|
|
|
if (blockInput.block || blockInput.shadow) {
|
|
|
|
xmlString += `<value name="${blockInput.name}">`;
|
|
|
|
if (blockInput.block) {
|
2018-05-21 11:30:22 -04:00
|
|
|
xmlString += this.blockToXML(blockInput.block, comments);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
if (blockInput.shadow && blockInput.shadow !== blockInput.block) {
|
|
|
|
// Obscured shadow.
|
2018-05-21 11:30:22 -04:00
|
|
|
xmlString += this.blockToXML(blockInput.shadow, comments);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
xmlString += '</value>';
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
// Add any fields on this block.
|
|
|
|
for (const field in block.fields) {
|
|
|
|
if (!block.fields.hasOwnProperty(field)) continue;
|
|
|
|
const blockField = block.fields[field];
|
2017-11-13 14:24:30 -05:00
|
|
|
xmlString += `<field name="${blockField.name}"`;
|
|
|
|
const fieldId = blockField.id;
|
|
|
|
if (fieldId) {
|
|
|
|
xmlString += ` id="${fieldId}"`;
|
|
|
|
}
|
|
|
|
const varType = blockField.variableType;
|
|
|
|
if (typeof varType === 'string') {
|
|
|
|
xmlString += ` variabletype="${varType}"`;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
let value = blockField.value;
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
value = xmlEscape(blockField.value);
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2017-11-13 14:24:30 -05:00
|
|
|
xmlString += `>${value}</field>`;
|
2016-08-31 11:38:45 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Add blocks connected to the next connection.
|
|
|
|
if (block.next) {
|
2018-05-21 11:30:22 -04:00
|
|
|
xmlString += `<next>${this.blockToXML(block.next, comments)}</next>`;
|
2016-09-13 17:51:17 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
xmlString += `</${tagName}>`;
|
|
|
|
return xmlString;
|
2016-08-31 11:38:45 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Recursively encode a mutation object to XML.
|
|
|
|
* @param {!object} mutation Object representing a mutation.
|
|
|
|
* @return {string} XML string representing a mutation.
|
|
|
|
*/
|
|
|
|
mutationToXML (mutation) {
|
|
|
|
let mutationString = `<${mutation.tagName}`;
|
|
|
|
for (const prop in mutation) {
|
|
|
|
if (prop === 'children' || prop === 'tagName') continue;
|
2019-04-23 11:44:45 -04:00
|
|
|
let mutationValue = (typeof mutation[prop] === 'string') ?
|
2017-04-17 19:42:48 -04:00
|
|
|
xmlEscape(mutation[prop]) : mutation[prop];
|
2019-04-23 11:44:45 -04:00
|
|
|
|
|
|
|
// Handle dynamic extension blocks
|
|
|
|
if (prop === 'blockInfo') {
|
|
|
|
mutationValue = xmlEscape(JSON.stringify(mutation[prop]));
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
mutationString += ` ${prop}="${mutationValue}"`;
|
|
|
|
}
|
|
|
|
mutationString += '>';
|
|
|
|
for (let i = 0; i < mutation.children.length; i++) {
|
|
|
|
mutationString += this.mutationToXML(mutation.children[i]);
|
|
|
|
}
|
|
|
|
mutationString += `</${mutation.tagName}>`;
|
|
|
|
return mutationString;
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// ---------------------------------------------------------------------
|
2017-05-23 09:55:42 -04:00
|
|
|
/**
|
|
|
|
* Helper to serialize block fields and input fields for reporting new monitors
|
|
|
|
* @param {!object} block Block to be paramified.
|
|
|
|
* @return {!object} object of param key/values.
|
|
|
|
*/
|
|
|
|
_getBlockParams (block) {
|
|
|
|
const params = {};
|
|
|
|
for (const key in block.fields) {
|
|
|
|
params[key] = block.fields[key].value;
|
|
|
|
}
|
|
|
|
for (const inputKey in block.inputs) {
|
|
|
|
const inputBlock = this._blocks[block.inputs[inputKey].block];
|
|
|
|
for (const key in inputBlock.fields) {
|
|
|
|
params[key] = inputBlock.fields[key].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
2016-08-31 11:38:45 -04:00
|
|
|
|
2017-10-10 12:19:27 -04:00
|
|
|
/**
|
|
|
|
* Helper to get the corresponding internal procedure definition block
|
|
|
|
* @param {!object} defineBlock Outer define block.
|
|
|
|
* @return {!object} internal definition block which has the mutation.
|
|
|
|
*/
|
|
|
|
_getCustomBlockInternal (defineBlock) {
|
|
|
|
if (defineBlock.inputs && defineBlock.inputs.custom_block) {
|
|
|
|
return this._blocks[defineBlock.inputs.custom_block.block];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Helper to add a stack to `this._scripts`.
|
|
|
|
* @param {?string} topBlockId ID of block that starts the script.
|
|
|
|
*/
|
|
|
|
_addScript (topBlockId) {
|
|
|
|
const i = this._scripts.indexOf(topBlockId);
|
|
|
|
if (i > -1) return; // Already in scripts.
|
|
|
|
this._scripts.push(topBlockId);
|
|
|
|
// Update `topLevel` property on the top block.
|
|
|
|
this._blocks[topBlockId].topLevel = true;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Helper to remove a script from `this._scripts`.
|
|
|
|
* @param {?string} topBlockId ID of block that starts the script.
|
|
|
|
*/
|
|
|
|
_deleteScript (topBlockId) {
|
|
|
|
const i = this._scripts.indexOf(topBlockId);
|
|
|
|
if (i > -1) this._scripts.splice(i, 1);
|
|
|
|
// Update `topLevel` property on the top block.
|
|
|
|
if (this._blocks[topBlockId]) this._blocks[topBlockId].topLevel = false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 13:27:01 -04:00
|
|
|
|
2018-04-10 16:29:51 -04:00
|
|
|
/**
|
|
|
|
* A private method shared with execute to build an object containing the block
|
|
|
|
* information execute needs and that is reset when other cached Blocks info is
|
|
|
|
* reset.
|
|
|
|
* @param {Blocks} blocks Blocks containing the expected blockId
|
|
|
|
* @param {string} blockId blockId for the desired execute cache
|
2018-05-07 10:31:28 -04:00
|
|
|
* @param {function} CacheType constructor for cached block information
|
2018-04-10 16:29:51 -04:00
|
|
|
* @return {object} execute cache object
|
|
|
|
*/
|
2018-05-07 10:31:28 -04:00
|
|
|
BlocksExecuteCache.getCached = function (blocks, blockId, CacheType) {
|
2018-04-10 16:29:51 -04:00
|
|
|
let cached = blocks._cache._executeCached[blockId];
|
|
|
|
if (typeof cached !== 'undefined') {
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
2018-05-04 12:33:42 -04:00
|
|
|
const block = blocks.getBlock(blockId);
|
|
|
|
if (typeof block === 'undefined') return null;
|
|
|
|
|
2018-05-07 10:31:28 -04:00
|
|
|
if (typeof CacheType === 'undefined') {
|
|
|
|
cached = {
|
2018-06-05 23:18:07 -04:00
|
|
|
id: blockId,
|
2018-05-07 10:31:28 -04:00
|
|
|
opcode: blocks.getOpcode(block),
|
|
|
|
fields: blocks.getFields(block),
|
|
|
|
inputs: blocks.getInputs(block),
|
|
|
|
mutation: blocks.getMutation(block)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
cached = new CacheType(blocks, {
|
2018-06-05 23:18:07 -04:00
|
|
|
id: blockId,
|
2018-05-07 10:31:28 -04:00
|
|
|
opcode: blocks.getOpcode(block),
|
|
|
|
fields: blocks.getFields(block),
|
|
|
|
inputs: blocks.getInputs(block),
|
|
|
|
mutation: blocks.getMutation(block)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-10 16:29:51 -04:00
|
|
|
blocks._cache._executeCached[blockId] = cached;
|
|
|
|
return cached;
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:21:35 -05:00
|
|
|
/**
|
|
|
|
* Cache class constructor for runtime. Used to consider what threads should
|
|
|
|
* start based on hat data.
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
const RuntimeScriptCache = BlocksRuntimeCache._RuntimeScriptCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of scripts from a block container prefiltered to match opcode.
|
|
|
|
* @param {Blocks} blocks - Container of blocks
|
|
|
|
* @param {string} opcode - Opcode to filter top blocks by
|
|
|
|
* @returns {Array.<RuntimeScriptCache>} - Array of RuntimeScriptCache cache
|
|
|
|
* objects
|
|
|
|
*/
|
|
|
|
BlocksRuntimeCache.getScripts = function (blocks, opcode) {
|
|
|
|
let scripts = blocks._cache.scripts[opcode];
|
|
|
|
if (!scripts) {
|
|
|
|
scripts = blocks._cache.scripts[opcode] = [];
|
|
|
|
|
|
|
|
const allScripts = blocks._scripts;
|
|
|
|
for (let i = 0; i < allScripts.length; i++) {
|
|
|
|
const topBlockId = allScripts[i];
|
|
|
|
const block = blocks.getBlock(topBlockId);
|
|
|
|
if (block.opcode === opcode) {
|
|
|
|
scripts.push(new RuntimeScriptCache(blocks, topBlockId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scripts;
|
|
|
|
};
|
|
|
|
|
2016-06-08 13:27:01 -04:00
|
|
|
module.exports = Blocks;
|