2016-10-03 17:43:24 -04:00
|
|
|
var mutationAdapter = require('./mutation-adapter');
|
2016-04-26 09:49:52 -04:00
|
|
|
var html = require('htmlparser2');
|
|
|
|
|
|
|
|
/**
|
2016-06-06 14:09:27 -04:00
|
|
|
* Convert outer blocks DOM from a Blockly CREATE event
|
|
|
|
* to a usable form for the Scratch runtime.
|
|
|
|
* This structure is based on Blockly xml.js:`domToWorkspace` and `domToBlock`.
|
|
|
|
* @param {Element} blocksDOM DOM tree for this event.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {Array.<object>} Usable list of blocks from this CREATE event.
|
2016-04-26 09:49:52 -04:00
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
var domToBlocks = function (blocksDOM) {
|
2016-06-06 14:09:27 -04:00
|
|
|
// At this level, there could be multiple blocks adjacent in the DOM tree.
|
|
|
|
var blocks = {};
|
|
|
|
for (var i = 0; i < blocksDOM.length; i++) {
|
|
|
|
var block = blocksDOM[i];
|
2016-06-07 12:01:47 -04:00
|
|
|
if (!block.name || !block.attribs) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-06 14:09:27 -04:00
|
|
|
var tagName = block.name.toLowerCase();
|
2016-10-23 17:55:31 -04:00
|
|
|
if (tagName === 'block' || tagName === 'shadow') {
|
2016-09-08 09:40:53 -04:00
|
|
|
domToBlock(block, blocks, true, null);
|
2016-06-06 14:09:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Flatten blocks object into a list.
|
|
|
|
var blocksList = [];
|
|
|
|
for (var b in blocks) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!blocks.hasOwnProperty(b)) continue;
|
2016-06-06 14:09:27 -04:00
|
|
|
blocksList.push(blocks[b]);
|
|
|
|
}
|
|
|
|
return blocksList;
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adapter between block creation events and block representation which can be
|
|
|
|
* used by the Scratch runtime.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {object} e `Blockly.events.create`
|
|
|
|
* @return {Array.<object>} List of blocks from this CREATE event.
|
2016-10-23 17:55:31 -04:00
|
|
|
*/
|
2016-10-24 11:49:34 -04:00
|
|
|
var adapter = function (e) {
|
2016-10-23 17:55:31 -04:00
|
|
|
// Validate input
|
|
|
|
if (typeof e !== 'object') return;
|
|
|
|
if (typeof e.xml !== 'object') return;
|
|
|
|
|
|
|
|
return domToBlocks(html.parseDOM(e.xml.outerHTML));
|
|
|
|
};
|
2016-04-26 09:49:52 -04:00
|
|
|
|
2016-06-06 14:09:27 -04:00
|
|
|
/**
|
|
|
|
* Convert and an individual block DOM to the representation tree.
|
|
|
|
* Based on Blockly's `domToBlockHeadless_`.
|
|
|
|
* @param {Element} blockDOM DOM tree for an individual block.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {object} blocks Collection of blocks to add to.
|
|
|
|
* @param {boolean} isTopBlock Whether blocks at this level are "top blocks."
|
2016-09-08 09:40:53 -04:00
|
|
|
* @param {?string} parent Parent block ID.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @return {undefined}
|
2016-06-06 14:09:27 -04:00
|
|
|
*/
|
2016-10-23 17:55:31 -04:00
|
|
|
var domToBlock = function (blockDOM, blocks, isTopBlock, parent) {
|
2016-06-06 14:09:27 -04:00
|
|
|
// Block skeleton.
|
|
|
|
var block = {
|
2016-06-06 14:44:51 -04:00
|
|
|
id: blockDOM.attribs.id, // Block ID
|
|
|
|
opcode: blockDOM.attribs.type, // For execution, "event_whengreenflag".
|
2016-06-06 14:09:27 -04:00
|
|
|
inputs: {}, // Inputs to this block and the blocks they point to.
|
|
|
|
fields: {}, // Fields on this block and their values.
|
|
|
|
next: null, // Next block in the stack, if one exists.
|
2016-08-31 11:32:59 -04:00
|
|
|
topLevel: isTopBlock, // If this block starts a stack.
|
2016-09-08 09:40:53 -04:00
|
|
|
parent: parent, // Parent block ID, if available.
|
2016-10-23 17:55:31 -04:00
|
|
|
shadow: blockDOM.name === 'shadow', // If this represents a shadow/slot.
|
2016-08-31 11:32:59 -04:00
|
|
|
x: blockDOM.attribs.x, // X position of script, if top-level.
|
|
|
|
y: blockDOM.attribs.y // Y position of script, if top-level.
|
2016-04-26 09:49:52 -04:00
|
|
|
};
|
|
|
|
|
2016-06-06 14:09:27 -04:00
|
|
|
// Add the block to the representation tree.
|
|
|
|
blocks[block.id] = block;
|
2016-04-26 09:49:52 -04:00
|
|
|
|
2016-06-06 14:09:27 -04:00
|
|
|
// Process XML children and find enclosed blocks, fields, etc.
|
|
|
|
for (var i = 0; i < blockDOM.children.length; i++) {
|
|
|
|
var xmlChild = blockDOM.children[i];
|
|
|
|
// Enclosed blocks and shadows
|
|
|
|
var childBlockNode = null;
|
|
|
|
var childShadowNode = null;
|
|
|
|
for (var j = 0; j < xmlChild.children.length; j++) {
|
|
|
|
var grandChildNode = xmlChild.children[j];
|
|
|
|
if (!grandChildNode.name) {
|
|
|
|
// Non-XML tag node.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var grandChildNodeName = grandChildNode.name.toLowerCase();
|
2016-10-23 17:55:31 -04:00
|
|
|
if (grandChildNodeName === 'block') {
|
2016-06-06 14:09:27 -04:00
|
|
|
childBlockNode = grandChildNode;
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (grandChildNodeName === 'shadow') {
|
2016-06-06 14:09:27 -04:00
|
|
|
childShadowNode = grandChildNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use shadow block only if there's no real block node.
|
|
|
|
if (!childBlockNode && childShadowNode) {
|
|
|
|
childBlockNode = childShadowNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all Blockly-type blocks are handled here,
|
|
|
|
// as we won't be using all of them for Scratch.
|
|
|
|
switch (xmlChild.name.toLowerCase()) {
|
|
|
|
case 'field':
|
|
|
|
// Add the field to this block.
|
|
|
|
var fieldName = xmlChild.attribs.name;
|
2016-06-14 18:14:04 -04:00
|
|
|
var fieldData = '';
|
|
|
|
if (xmlChild.children.length > 0 && xmlChild.children[0].data) {
|
|
|
|
fieldData = xmlChild.children[0].data;
|
|
|
|
} else {
|
|
|
|
// If the child of the field with a data property
|
|
|
|
// doesn't exist, set the data to an empty string.
|
|
|
|
fieldData = '';
|
|
|
|
}
|
2016-06-06 14:09:27 -04:00
|
|
|
block.fields[fieldName] = {
|
|
|
|
name: fieldName,
|
2016-06-14 18:14:04 -04:00
|
|
|
value: fieldData
|
2016-06-06 14:09:27 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'value':
|
|
|
|
case 'statement':
|
|
|
|
// Recursively generate block structure for input block.
|
2016-09-08 09:40:53 -04:00
|
|
|
domToBlock(childBlockNode, blocks, false, block.id);
|
2016-10-23 17:55:31 -04:00
|
|
|
if (childShadowNode && childBlockNode !== childShadowNode) {
|
2016-09-06 10:55:52 -04:00
|
|
|
// Also generate the shadow block.
|
2016-09-08 09:40:53 -04:00
|
|
|
domToBlock(childShadowNode, blocks, false, block.id);
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2016-06-06 14:09:27 -04:00
|
|
|
// Link this block's input to the child block.
|
|
|
|
var inputName = xmlChild.attribs.name;
|
|
|
|
block.inputs[inputName] = {
|
|
|
|
name: inputName,
|
2016-09-06 10:55:52 -04:00
|
|
|
block: childBlockNode.attribs.id,
|
|
|
|
shadow: childShadowNode ? childShadowNode.attribs.id : null
|
2016-06-06 14:09:27 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'next':
|
2016-06-07 12:01:47 -04:00
|
|
|
if (!childBlockNode || !childBlockNode.attribs) {
|
|
|
|
// Invalid child block.
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-06 14:09:27 -04:00
|
|
|
// Recursively generate block structure for next block.
|
2016-09-08 09:40:53 -04:00
|
|
|
domToBlock(childBlockNode, blocks, false, block.id);
|
2016-06-06 14:09:27 -04:00
|
|
|
// Link next block to this block.
|
|
|
|
block.next = childBlockNode.attribs.id;
|
|
|
|
break;
|
2016-10-03 17:43:24 -04:00
|
|
|
case 'mutation':
|
|
|
|
block.mutation = mutationAdapter(xmlChild);
|
|
|
|
break;
|
2016-06-06 14:09:27 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-10-24 11:49:34 -04:00
|
|
|
|
|
|
|
module.exports = adapter;
|