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');
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Blocks {
|
|
|
|
constructor () {
|
|
|
|
/**
|
|
|
|
* 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 = [];
|
|
|
|
}
|
|
|
|
|
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-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.
|
|
|
|
* @return {!object} All non-branch inputs and their associated blocks.
|
|
|
|
*/
|
|
|
|
getInputs (block) {
|
|
|
|
if (typeof block === 'undefined') return null;
|
|
|
|
const inputs = {};
|
|
|
|
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-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) {
|
|
|
|
for (const id in this._blocks) {
|
|
|
|
if (!this._blocks.hasOwnProperty(id)) continue;
|
|
|
|
const block = this._blocks[id];
|
|
|
|
if ((block.opcode === 'procedures_defnoreturn' ||
|
|
|
|
block.opcode === 'procedures_defreturn') &&
|
|
|
|
block.mutation.proccode === name) {
|
|
|
|
return id;
|
|
|
|
}
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Get the procedure definition for a given name.
|
|
|
|
* @param {?string} name Name of procedure to query.
|
|
|
|
* @return {?string} ID of procedure definition.
|
|
|
|
*/
|
|
|
|
getProcedureParamNames (name) {
|
|
|
|
for (const id in this._blocks) {
|
|
|
|
if (!this._blocks.hasOwnProperty(id)) continue;
|
|
|
|
const block = this._blocks[id];
|
|
|
|
if ((block.opcode === 'procedures_defnoreturn' ||
|
|
|
|
block.opcode === 'procedures_defreturn') &&
|
|
|
|
block.mutation.proccode === name) {
|
|
|
|
return JSON.parse(block.mutation.argumentnames);
|
|
|
|
}
|
2016-10-13 13:11:26 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
return null;
|
2016-10-13 13:11:26 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// ---------------------------------------------------------------------
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-06-15 17:29:15 -04:00
|
|
|
* Create event listener for blocks and variables. Handles validation and
|
|
|
|
* 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
|
|
|
* @param {?Runtime} optRuntime Optional runtime to forward click events to.
|
|
|
|
*/
|
|
|
|
blocklyListen (e, optRuntime) {
|
|
|
|
// Validate event
|
|
|
|
if (typeof e !== 'object') return;
|
2017-06-15 17:29:15 -04:00
|
|
|
if (typeof e.blockId !== 'string' && typeof e.varId !== 'string') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stage = optRuntime.getTargetForStage();
|
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') {
|
|
|
|
if (optRuntime) {
|
2017-05-08 16:51:45 -04:00
|
|
|
optRuntime.toggleScript(e.blockId, {showVisualReport: 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
|
2017-05-10 15:47:06 -04:00
|
|
|
}, optRuntime);
|
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,
|
|
|
|
newCoordinate: e.newCoordinate
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
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.
|
|
|
|
if (optRuntime && this._blocks[e.blockId].topLevel) {
|
|
|
|
optRuntime.quietGlow(e.blockId);
|
|
|
|
}
|
|
|
|
this.deleteBlock({
|
|
|
|
id: e.blockId
|
|
|
|
});
|
|
|
|
break;
|
2017-06-15 17:29:15 -04:00
|
|
|
case 'var_create':
|
|
|
|
stage.createVariable(e.varId, e.varName);
|
|
|
|
break;
|
|
|
|
case 'var_rename':
|
|
|
|
stage.renameVariable(e.varId, e.newName);
|
|
|
|
break;
|
|
|
|
case 'var_delete':
|
|
|
|
stage.deleteVariable(e.varId);
|
|
|
|
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-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);
|
|
|
|
}
|
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
|
2017-05-11 15:23:11 -04:00
|
|
|
* @param {?Runtime} optRuntime Optional runtime to allow changeBlock to change VM state.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-05-10 15:47:06 -04:00
|
|
|
changeBlock (args, optRuntime) {
|
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;
|
2017-04-17 19:42:48 -04:00
|
|
|
const block = this._blocks[args.id];
|
|
|
|
if (typeof block === 'undefined') return;
|
|
|
|
|
2017-05-22 13:38:29 -04:00
|
|
|
const wasMonitored = block.isMonitored;
|
2017-05-08 09:53:16 -04:00
|
|
|
switch (args.element) {
|
|
|
|
case 'field':
|
2017-04-17 19:42:48 -04:00
|
|
|
// Update block value
|
|
|
|
if (!block.fields[args.name]) return;
|
|
|
|
block.fields[args.name].value = args.value;
|
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;
|
|
|
|
case 'checkbox':
|
|
|
|
block.isMonitored = args.value;
|
2017-05-10 15:47:06 -04:00
|
|
|
if (optRuntime && wasMonitored && !block.isMonitored) {
|
2017-05-15 10:12:25 -04:00
|
|
|
optRuntime.requestRemoveMonitor(block.id);
|
2017-05-10 17:00:08 -04:00
|
|
|
} else if (optRuntime && !wasMonitored && block.isMonitored) {
|
2017-05-24 15:42:29 -04:00
|
|
|
optRuntime.requestAddMonitor(MonitorRecord({
|
2017-05-22 13:38:29 -04:00
|
|
|
// @todo(vm#564) this will collide if multiple sprites use same block
|
|
|
|
id: block.id,
|
|
|
|
opcode: block.opcode,
|
2017-05-23 09:55:42 -04:00
|
|
|
params: this._getBlockParams(block),
|
2017-05-22 13:38:29 -04:00
|
|
|
// @todo(vm#565) for numerical values with decimals, some countries use comma
|
2017-05-22 14:40:07 -04:00
|
|
|
value: ''
|
2017-05-19 17:28:00 -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
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
moveBlock (e) {
|
|
|
|
if (!this._blocks.hasOwnProperty(e.id)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Move coordinate changes.
|
|
|
|
if (e.newCoordinate) {
|
|
|
|
this._blocks[e.id].x = e.newCoordinate.x;
|
|
|
|
this._blocks[e.id].y = e.newCoordinate.y;
|
|
|
|
}
|
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;
|
2016-06-08 13:27:01 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Has the block become a top-level block?
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
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) {
|
|
|
|
Object.keys(this._blocks).forEach(blockId => {
|
|
|
|
if (this.getBlock(blockId).isMonitored) {
|
|
|
|
// @todo handle specific targets (e.g. apple x position)
|
2017-05-09 17:34:13 -04:00
|
|
|
runtime.toggleScript(blockId, {updateMonitor: true});
|
2017-05-08 09:53:16 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Block management: delete blocks and their associated scripts.
|
|
|
|
* @param {!object} e Blockly delete event to be processed.
|
|
|
|
*/
|
|
|
|
deleteBlock (e) {
|
|
|
|
// @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
|
|
|
|
const block = this._blocks[e.id];
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Delete children
|
|
|
|
if (block.next !== null) {
|
|
|
|
this.deleteBlock({id: 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) {
|
|
|
|
this.deleteBlock({id: block.inputs[input].block});
|
|
|
|
}
|
|
|
|
// Delete obscured shadow blocks.
|
|
|
|
if (block.inputs[input].shadow !== null &&
|
|
|
|
block.inputs[input].shadow !== block.inputs[input].block) {
|
|
|
|
this.deleteBlock({id: block.inputs[input].shadow});
|
|
|
|
}
|
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.
|
|
|
|
this._deleteScript(e.id);
|
2016-06-06 15:29:34 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Delete block itself.
|
|
|
|
delete this._blocks[e.id];
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
|
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.
|
|
|
|
* @return {string} String of XML representing this object's blocks.
|
|
|
|
*/
|
|
|
|
toXML () {
|
2017-05-25 11:44:49 -04:00
|
|
|
return this._scripts.map(script => this.blockToXML(script)).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.
|
|
|
|
* @return {string} String of XML representing this block and any children.
|
|
|
|
*/
|
|
|
|
blockToXML (blockId) {
|
|
|
|
const block = this._blocks[blockId];
|
|
|
|
// 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}"
|
|
|
|
${block.topLevel ?
|
2017-04-21 04:36:33 -04:00
|
|
|
`x="${block.x}" y="${block.y}"` :
|
2017-04-19 17:36:33 -04:00
|
|
|
''
|
|
|
|
}
|
|
|
|
>`;
|
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) {
|
|
|
|
xmlString += this.blockToXML(blockInput.block);
|
|
|
|
}
|
|
|
|
if (blockInput.shadow && blockInput.shadow !== blockInput.block) {
|
|
|
|
// Obscured shadow.
|
|
|
|
xmlString += this.blockToXML(blockInput.shadow);
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
let value = blockField.value;
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
value = xmlEscape(blockField.value);
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2017-04-19 17:36:33 -04:00
|
|
|
xmlString += `<field name="${blockField.name}">${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) {
|
|
|
|
xmlString += `<next>${this.blockToXML(block.next)}</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;
|
|
|
|
const mutationValue = (typeof mutation[prop] === 'string') ?
|
|
|
|
xmlEscape(mutation[prop]) : mutation[prop];
|
|
|
|
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-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
|
|
|
|
|
|
|
module.exports = Blocks;
|