Skip to content

Commit

Permalink
Add ability to specify allowed blocks in the jpblockly files
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Oct 1, 2024
1 parent 10aa25e commit 44f2eb7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
79 changes: 78 additions & 1 deletion packages/blockly/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { ISignal, Signal } from '@lumino/signaling';
import * as Blockly from 'blockly';

import { BlocklyRegistry } from './registry';
import { ToolboxDefinition } from 'blockly/core/utils/toolbox';
import {
BlockInfo,
DynamicCategoryInfo,
StaticCategoryInfo,
ToolboxDefinition,
ToolboxInfo,
ToolboxItemInfo
} from 'blockly/core/utils/toolbox';

/**
* BlocklyManager the manager for each document
Expand All @@ -17,6 +24,7 @@ import { ToolboxDefinition } from 'blockly/core/utils/toolbox';
*/
export class BlocklyManager {
private _toolbox: string;
private _allowedBlocks: string[];
private _generator: Blockly.Generator;
private _registry: BlocklyRegistry;
private _selectedKernel: KernelSpec.ISpecModel;
Expand All @@ -37,6 +45,7 @@ export class BlocklyManager {
this._mimetypeService = mimetypeService;

this._toolbox = 'default';
this._filterToolbox();
this._generator = this._registry.generators.get('python');

this._changed = new Signal<this, BlocklyManager.Change>(this);
Expand Down Expand Up @@ -112,6 +121,7 @@ export class BlocklyManager {
if (this._toolbox !== name) {
const toolbox = this._registry.toolboxes.get(name);
this._toolbox = toolbox ? name : 'default';
this._filterToolbox();
this._changed.emit('toolbox');
}
}
Expand All @@ -129,6 +139,73 @@ export class BlocklyManager {
return list;
}

/**
* Get the list of allowed blocks. If undefined, all blocks are allowed.
*
* @returns The list of allowed blocks.
*/
getAllowedBlocks() {
return this._allowedBlocks;
}

/**
* Set the list of allowed blocks. If undefined, all blocks are allowed.
*
* @param allowedBlocks The list of allowed blocks.
*/
setAllowedBlocks(allowedBlocks: string[]) {
this._allowedBlocks = allowedBlocks;
this._filterToolbox();
this._changed.emit('toolbox');
}

private _filterToolbox() {
const toolbox = this._registry.toolboxes.get(this._toolbox) as ToolboxInfo;
if (toolbox) {
this._filterContents(toolbox.contents);
}
}

private _filterContents(contents: ToolboxItemInfo[]): number {
let visible = 0;
contents.forEach(itemInfo => {
if ("kind" in itemInfo) {

Check failure on line 172 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"kind"` with `'kind'`

Check failure on line 172 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
if (itemInfo.kind.toUpperCase() === "CATEGORY") {

Check failure on line 173 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"CATEGORY"` with `'CATEGORY'`

Check failure on line 173 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
if ("contents" in itemInfo) {

Check failure on line 174 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"contents"` with `'contents'`

Check failure on line 174 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
const categoryInfo = itemInfo as StaticCategoryInfo;
if (this._filterContents(categoryInfo.contents) > 0) {
visible++;
categoryInfo.hidden = "false";

Check failure on line 178 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"false"` with `'false'`

Check failure on line 178 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
} else {
categoryInfo.hidden = "true";

Check failure on line 180 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"true"` with `'true'`

Check failure on line 180 in packages/blockly/src/manager.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
}
} else if ("custom" in itemInfo) {
const categoryInfo = itemInfo as DynamicCategoryInfo;
if (this._allowedBlocks === undefined || this._allowedBlocks.includes(categoryInfo.custom.toLowerCase())) {
categoryInfo.hidden = "false";
visible++;
console.log("Category " + categoryInfo.custom + " is allowed");
} else {
categoryInfo.hidden = "true";
console.log("Category " + categoryInfo.custom + " is not allowed");
}
}
} else if (itemInfo.kind.toUpperCase() === "BLOCK") {
const blockInfo = itemInfo as BlockInfo;
if (this._allowedBlocks === undefined || this._allowedBlocks.includes(blockInfo.type.toLowerCase())) {
blockInfo.disabled = false;
blockInfo.disabledReasons = [];
visible++;
} else {
blockInfo.disabled = true;
blockInfo.disabledReasons = ["This block is not allowed"];
}
}
}
});
return visible;
}

/**
* Set the selected kernel.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/blockly/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export class BlocklyPanel extends SplitPanel {
console.warn(`Unknown kernel in blockly file: ` + kernel);
}
}
if (metadata['allowed_blocks']) {
this._manager.setAllowedBlocks(metadata['allowed_blocks']);
}
}
} else {
// Unsupported format
Expand All @@ -194,6 +197,7 @@ export class BlocklyPanel extends SplitPanel {
workspace: workspace as any,
metadata: {
toolbox: this._manager.getToolbox(),
allowed_blocks: this._manager.getAllowedBlocks(),
kernel: this._manager.kernel
}
};
Expand Down

0 comments on commit 44f2eb7

Please sign in to comment.