Skip to content

Commit 44f2eb7

Browse files
committed
Add ability to specify allowed blocks in the jpblockly files
1 parent 10aa25e commit 44f2eb7

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

packages/blockly/src/manager.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import { ISignal, Signal } from '@lumino/signaling';
88
import * as Blockly from 'blockly';
99

1010
import { BlocklyRegistry } from './registry';
11-
import { ToolboxDefinition } from 'blockly/core/utils/toolbox';
11+
import {
12+
BlockInfo,
13+
DynamicCategoryInfo,
14+
StaticCategoryInfo,
15+
ToolboxDefinition,
16+
ToolboxInfo,
17+
ToolboxItemInfo
18+
} from 'blockly/core/utils/toolbox';
1219

1320
/**
1421
* BlocklyManager the manager for each document
@@ -17,6 +24,7 @@ import { ToolboxDefinition } from 'blockly/core/utils/toolbox';
1724
*/
1825
export class BlocklyManager {
1926
private _toolbox: string;
27+
private _allowedBlocks: string[];
2028
private _generator: Blockly.Generator;
2129
private _registry: BlocklyRegistry;
2230
private _selectedKernel: KernelSpec.ISpecModel;
@@ -37,6 +45,7 @@ export class BlocklyManager {
3745
this._mimetypeService = mimetypeService;
3846

3947
this._toolbox = 'default';
48+
this._filterToolbox();
4049
this._generator = this._registry.generators.get('python');
4150

4251
this._changed = new Signal<this, BlocklyManager.Change>(this);
@@ -112,6 +121,7 @@ export class BlocklyManager {
112121
if (this._toolbox !== name) {
113122
const toolbox = this._registry.toolboxes.get(name);
114123
this._toolbox = toolbox ? name : 'default';
124+
this._filterToolbox();
115125
this._changed.emit('toolbox');
116126
}
117127
}
@@ -129,6 +139,73 @@ export class BlocklyManager {
129139
return list;
130140
}
131141

142+
/**
143+
* Get the list of allowed blocks. If undefined, all blocks are allowed.
144+
*
145+
* @returns The list of allowed blocks.
146+
*/
147+
getAllowedBlocks() {
148+
return this._allowedBlocks;
149+
}
150+
151+
/**
152+
* Set the list of allowed blocks. If undefined, all blocks are allowed.
153+
*
154+
* @param allowedBlocks The list of allowed blocks.
155+
*/
156+
setAllowedBlocks(allowedBlocks: string[]) {
157+
this._allowedBlocks = allowedBlocks;
158+
this._filterToolbox();
159+
this._changed.emit('toolbox');
160+
}
161+
162+
private _filterToolbox() {
163+
const toolbox = this._registry.toolboxes.get(this._toolbox) as ToolboxInfo;
164+
if (toolbox) {
165+
this._filterContents(toolbox.contents);
166+
}
167+
}
168+
169+
private _filterContents(contents: ToolboxItemInfo[]): number {
170+
let visible = 0;
171+
contents.forEach(itemInfo => {
172+
if ("kind" in itemInfo) {
173+
if (itemInfo.kind.toUpperCase() === "CATEGORY") {
174+
if ("contents" in itemInfo) {
175+
const categoryInfo = itemInfo as StaticCategoryInfo;
176+
if (this._filterContents(categoryInfo.contents) > 0) {
177+
visible++;
178+
categoryInfo.hidden = "false";
179+
} else {
180+
categoryInfo.hidden = "true";
181+
}
182+
} else if ("custom" in itemInfo) {
183+
const categoryInfo = itemInfo as DynamicCategoryInfo;
184+
if (this._allowedBlocks === undefined || this._allowedBlocks.includes(categoryInfo.custom.toLowerCase())) {
185+
categoryInfo.hidden = "false";
186+
visible++;
187+
console.log("Category " + categoryInfo.custom + " is allowed");
188+
} else {
189+
categoryInfo.hidden = "true";
190+
console.log("Category " + categoryInfo.custom + " is not allowed");
191+
}
192+
}
193+
} else if (itemInfo.kind.toUpperCase() === "BLOCK") {
194+
const blockInfo = itemInfo as BlockInfo;
195+
if (this._allowedBlocks === undefined || this._allowedBlocks.includes(blockInfo.type.toLowerCase())) {
196+
blockInfo.disabled = false;
197+
blockInfo.disabledReasons = [];
198+
visible++;
199+
} else {
200+
blockInfo.disabled = true;
201+
blockInfo.disabledReasons = ["This block is not allowed"];
202+
}
203+
}
204+
}
205+
});
206+
return visible;
207+
}
208+
132209
/**
133210
* Set the selected kernel.
134211
*

packages/blockly/src/widget.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ export class BlocklyPanel extends SplitPanel {
174174
console.warn(`Unknown kernel in blockly file: ` + kernel);
175175
}
176176
}
177+
if (metadata['allowed_blocks']) {
178+
this._manager.setAllowedBlocks(metadata['allowed_blocks']);
179+
}
177180
}
178181
} else {
179182
// Unsupported format
@@ -194,6 +197,7 @@ export class BlocklyPanel extends SplitPanel {
194197
workspace: workspace as any,
195198
metadata: {
196199
toolbox: this._manager.getToolbox(),
200+
allowed_blocks: this._manager.getAllowedBlocks(),
197201
kernel: this._manager.kernel
198202
}
199203
};

0 commit comments

Comments
 (0)