diff --git a/src/engine/runtime.js b/src/engine/runtime.js index f407dbdd5c8..503a1fbc449 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1429,6 +1429,11 @@ class Runtime extends EventEmitter { break; } + // Allow extensiosn to override outputShape + if (blockInfo.blockShape) { + blockJSON.outputShape = blockInfo.blockShape; + } + const blockText = Array.isArray(blockInfo.text) ? blockInfo.text : [blockInfo.text]; let inTextNum = 0; // text for the next block "arm" is blockText[inTextNum] let inBranchNum = 0; // how many branches have we placed into the JSON so far? diff --git a/src/extension-support/tw-block-shape.js b/src/extension-support/tw-block-shape.js new file mode 100644 index 00000000000..854975ada88 --- /dev/null +++ b/src/extension-support/tw-block-shape.js @@ -0,0 +1,25 @@ +// Use the constants instead of manually redefining them again +const ScratchBlocksConstants = require('../engine/scratch-blocks-constants'); + +/** + * Types of block shapes + * @enum {number} + */ +const BlockShape = { + /** + * Output shape: hexagonal (booleans/predicates). + */ + HEXAGONAL: ScratchBlocksConstants.OUTPUT_SHAPE_HEXAGONAL, + + /** + * Output shape: rounded (numbers). + */ + ROUND: ScratchBlocksConstants.OUTPUT_SHAPE_ROUND, + + /** + * Output shape: squared (any/all values; strings). + */ + SQUARE: ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE +}; + +module.exports = BlockShape; diff --git a/src/extension-support/tw-extension-api-common.js b/src/extension-support/tw-extension-api-common.js index e2ac0b74d0f..1f5928fbc0f 100644 --- a/src/extension-support/tw-extension-api-common.js +++ b/src/extension-support/tw-extension-api-common.js @@ -1,11 +1,13 @@ const ArgumentType = require('./argument-type'); const BlockType = require('./block-type'); +const BlockShape = require('./tw-block-shape'); const TargetType = require('./target-type'); const Cast = require('../util/cast'); const Scratch = { ArgumentType, BlockType, + BlockShape, TargetType, Cast }; diff --git a/test/unit/tw_block_shape.js b/test/unit/tw_block_shape.js new file mode 100644 index 00000000000..a5ef50282a6 --- /dev/null +++ b/test/unit/tw_block_shape.js @@ -0,0 +1,38 @@ +const {test} = require('tap'); +const Runtime = require('../../src/engine/runtime'); +const Scratch = require('../../src/extension-support/tw-extension-api-common'); + +test('blockShape', t => { + const rt = new Runtime(); + rt._registerExtensionPrimitives({ + id: 'shapetest', + name: 'shapetest', + blocks: [ + { + blockType: Scratch.BlockType.REPORTER, + blockShape: Scratch.BlockShape.HEXAGONAL, + opcode: 'hexagonal', + text: 'hexagonal' + }, + { + blockType: Scratch.BlockType.BOOLEAN, + blockShape: Scratch.BlockShape.ROUND, + opcode: 'round', + text: 'round' + }, + { + blockType: Scratch.BlockType.REPORTER, + blockShape: Scratch.BlockShape.SQUARE, + opcode: 'square', + text: 'square' + } + ] + }); + + const json = rt.getBlocksJSON(); + t.equal(json.length, 3); + t.equal(json[0].outputShape, 1); + t.equal(json[1].outputShape, 2); + t.equal(json[2].outputShape, 3); + t.end(); +});