|
const mutationAdapter = require('./mutation-adapter'); |
|
const uid = require('../util/uid'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const domToBlock = function (blockDOM, blocks, isTopBlock, parent) { |
|
if (!blockDOM.attributes.id) { |
|
blockDOM.attributes.id = {}; |
|
blockDOM.attributes.id.value = uid(); |
|
} |
|
|
|
|
|
blockDOM.attributes.x ??= {}; |
|
blockDOM.attributes.y ??= {}; |
|
|
|
|
|
const block = { |
|
id: blockDOM.attributes.id.value, |
|
opcode: blockDOM.attributes.type.value, |
|
inputs: {}, |
|
fields: {}, |
|
next: null, |
|
topLevel: isTopBlock, |
|
parent: parent, |
|
shadow: blockDOM.tagName === 'shadow', |
|
x: blockDOM.attributes.x.value, |
|
y: blockDOM.attributes.y.value |
|
}; |
|
|
|
|
|
blocks[block.id] = block; |
|
|
|
|
|
for (let i = 0; i < blockDOM.children.length; i++) { |
|
const xmlChild = blockDOM.children[i]; |
|
|
|
let childBlockNode = null; |
|
let childShadowNode = null; |
|
for (let j = 0; j < xmlChild.children.length; j++) { |
|
const grandChildNode = xmlChild.children[j]; |
|
if (!grandChildNode.tagName) { |
|
|
|
continue; |
|
} |
|
const grandChildNodeName = grandChildNode.tagName; |
|
if (grandChildNodeName === 'block') { |
|
childBlockNode = grandChildNode; |
|
} else if (grandChildNodeName === 'shadow') { |
|
childShadowNode = grandChildNode; |
|
} |
|
} |
|
|
|
|
|
if (!childBlockNode && childShadowNode) { |
|
childBlockNode = childShadowNode; |
|
} |
|
|
|
|
|
|
|
switch (xmlChild.tagName) { |
|
case 'field': |
|
{ |
|
|
|
const fieldName = xmlChild.attributes.name.value; |
|
|
|
xmlChild.attributes.id ??= { value: uid() }; |
|
|
|
const fieldId = xmlChild.attributes.id.value; |
|
let fieldData = ''; |
|
if (xmlChild.innerHTML) { |
|
fieldData = xmlChild.textContent; |
|
} else { |
|
|
|
|
|
fieldData = ''; |
|
} |
|
block.fields[fieldName] = { |
|
name: fieldName, |
|
id: fieldId, |
|
value: fieldData |
|
}; |
|
xmlChild.attributes.variabletype ??= {}; |
|
const fieldVarType = xmlChild.attributes.variabletype.value; |
|
if (typeof fieldVarType === 'string') { |
|
block.fields[fieldName].variableType = fieldVarType; |
|
} |
|
break; |
|
} |
|
case 'comment': |
|
{ |
|
block.comment = xmlChild.attributes.id.value; |
|
break; |
|
} |
|
case 'value': |
|
case 'statement': |
|
{ |
|
|
|
domToBlock(childBlockNode, blocks, false, block.id); |
|
if (childShadowNode && childBlockNode !== childShadowNode) { |
|
|
|
domToBlock(childShadowNode, blocks, false, block.id); |
|
} |
|
|
|
const inputName = xmlChild.attributes.name.value; |
|
block.inputs[inputName] = { |
|
name: inputName, |
|
block: childBlockNode.attributes.id.value, |
|
shadow: childShadowNode ? childShadowNode.attributes.id.value : null |
|
}; |
|
break; |
|
} |
|
case 'next': |
|
{ |
|
if (!childBlockNode || !childBlockNode.attributes) { |
|
|
|
continue; |
|
} |
|
|
|
domToBlock(childBlockNode, blocks, false, block.id); |
|
|
|
block.next = childBlockNode.attributes.id.value; |
|
break; |
|
} |
|
case 'mutation': |
|
{ |
|
block.mutation = mutationAdapter(xmlChild); |
|
break; |
|
} |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const domToBlocks = function (blocksDOM) { |
|
|
|
const blocks = {}; |
|
for (let i = 0; i < blocksDOM.length; i++) { |
|
const block = blocksDOM[i]; |
|
|
|
if (!block.tagName || !block.attributes) { |
|
continue; |
|
} |
|
const tagName = block.tagName; |
|
if (tagName === 'block' || tagName === 'shadow') { |
|
domToBlock(block, blocks, true, null); |
|
} |
|
} |
|
|
|
const blocksList = []; |
|
for (const b in blocks) { |
|
if (!blocks.hasOwnProperty(b)) continue; |
|
blocksList.push(blocks[b]); |
|
} |
|
return blocksList; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const adapter = function (e) { |
|
|
|
if (typeof e !== 'object') return; |
|
if (typeof e.xml !== 'object') return; |
|
const parser = new DOMParser(); |
|
const doc = parser.parseFromString(e.xml.outerHTML, "application/xml"); |
|
return domToBlocks(doc.childNodes); |
|
}; |
|
|
|
module.exports = adapter; |
|
|