Skip to content

Commit 5bc497a

Browse files
authored
Add blockShape option to extensions (#210)
1 parent f957e1c commit 5bc497a

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/engine/runtime.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,11 @@ class Runtime extends EventEmitter {
14291429
break;
14301430
}
14311431

1432+
// Allow extensiosn to override outputShape
1433+
if (blockInfo.blockShape) {
1434+
blockJSON.outputShape = blockInfo.blockShape;
1435+
}
1436+
14321437
const blockText = Array.isArray(blockInfo.text) ? blockInfo.text : [blockInfo.text];
14331438
let inTextNum = 0; // text for the next block "arm" is blockText[inTextNum]
14341439
let inBranchNum = 0; // how many branches have we placed into the JSON so far?
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Use the constants instead of manually redefining them again
2+
const ScratchBlocksConstants = require('../engine/scratch-blocks-constants');
3+
4+
/**
5+
* Types of block shapes
6+
* @enum {number}
7+
*/
8+
const BlockShape = {
9+
/**
10+
* Output shape: hexagonal (booleans/predicates).
11+
*/
12+
HEXAGONAL: ScratchBlocksConstants.OUTPUT_SHAPE_HEXAGONAL,
13+
14+
/**
15+
* Output shape: rounded (numbers).
16+
*/
17+
ROUND: ScratchBlocksConstants.OUTPUT_SHAPE_ROUND,
18+
19+
/**
20+
* Output shape: squared (any/all values; strings).
21+
*/
22+
SQUARE: ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE
23+
};
24+
25+
module.exports = BlockShape;

src/extension-support/tw-extension-api-common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const ArgumentType = require('./argument-type');
22
const BlockType = require('./block-type');
3+
const BlockShape = require('./tw-block-shape');
34
const TargetType = require('./target-type');
45
const Cast = require('../util/cast');
56

67
const Scratch = {
78
ArgumentType,
89
BlockType,
10+
BlockShape,
911
TargetType,
1012
Cast
1113
};

test/unit/tw_block_shape.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const {test} = require('tap');
2+
const Runtime = require('../../src/engine/runtime');
3+
const Scratch = require('../../src/extension-support/tw-extension-api-common');
4+
5+
test('blockShape', t => {
6+
const rt = new Runtime();
7+
rt._registerExtensionPrimitives({
8+
id: 'shapetest',
9+
name: 'shapetest',
10+
blocks: [
11+
{
12+
blockType: Scratch.BlockType.REPORTER,
13+
blockShape: Scratch.BlockShape.HEXAGONAL,
14+
opcode: 'hexagonal',
15+
text: 'hexagonal'
16+
},
17+
{
18+
blockType: Scratch.BlockType.BOOLEAN,
19+
blockShape: Scratch.BlockShape.ROUND,
20+
opcode: 'round',
21+
text: 'round'
22+
},
23+
{
24+
blockType: Scratch.BlockType.REPORTER,
25+
blockShape: Scratch.BlockShape.SQUARE,
26+
opcode: 'square',
27+
text: 'square'
28+
}
29+
]
30+
});
31+
32+
const json = rt.getBlocksJSON();
33+
t.equal(json.length, 3);
34+
t.equal(json[0].outputShape, 1);
35+
t.equal(json[1].outputShape, 2);
36+
t.equal(json[2].outputShape, 3);
37+
t.end();
38+
});

0 commit comments

Comments
 (0)