From aaa7784d3943ec1a5fea894b652bbd896ee8ab2b Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Fri, 25 Aug 2017 13:53:57 -0700 Subject: [PATCH] Add methods to fetch scratch-blocks XML and JSON --- src/engine/runtime.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 773ef067e..1b0a9ddca 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -102,10 +102,10 @@ class Runtime extends EventEmitter { /** * Map to look up all block information by extended opcode. - * @type {Object.} + * @type {Array.} * @private */ - this._blockInfo = {}; + this._blockInfo = []; /** * Map to look up hat blocks' metadata. @@ -363,13 +363,16 @@ class Runtime extends EventEmitter { name: extensionInfo.name, color1: '#FF6680', color2: '#FF4D6A', - color3: '#FF3355' + color3: '#FF3355', + blocks: [] }; + this._blockInfo.push(categoryInfo); + for (const blockInfo of extensionInfo.blocks) { const convertedBlock = this._convertForScratchBlocks(blockInfo, categoryInfo); const opcode = convertedBlock.json.id; - this._blockInfo[opcode] = convertedBlock; + categoryInfo.blocks.push(convertedBlock); this._primitives[opcode] = convertedBlock.info.func; } } @@ -471,6 +474,30 @@ class Runtime extends EventEmitter { }; } + /** + * @returns {string} scratch-blocks XML description for all dynamic blocks, wrapped in elements. + */ + getBlocksXML () { + const xmlParts = []; + for (const categoryInfo of this._blockInfo) { + const {name, color1, color2} = categoryInfo; + xmlParts.push(``); + // @todo only add this label for user-loaded extensions? + xmlParts.push(`'); + } + return xmlParts.join('\n'); + } + + /** + * @returns {Array.} - an array containing the scratch-blocks JSON information for each dynamic block. + */ + getBlocksJSON () { + return this._blockInfo.reduce( + (result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []); + } + /** * Retrieve the function associated with the given opcode. * @param {!string} opcode The opcode to look up.