@@ -8,7 +8,14 @@ import { ISignal, Signal } from '@lumino/signaling';
8
8
import * as Blockly from 'blockly' ;
9
9
10
10
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' ;
12
19
13
20
/**
14
21
* BlocklyManager the manager for each document
@@ -17,6 +24,7 @@ import { ToolboxDefinition } from 'blockly/core/utils/toolbox';
17
24
*/
18
25
export class BlocklyManager {
19
26
private _toolbox : string ;
27
+ private _allowedBlocks : string [ ] ;
20
28
private _generator : Blockly . Generator ;
21
29
private _registry : BlocklyRegistry ;
22
30
private _selectedKernel : KernelSpec . ISpecModel ;
@@ -37,6 +45,7 @@ export class BlocklyManager {
37
45
this . _mimetypeService = mimetypeService ;
38
46
39
47
this . _toolbox = 'default' ;
48
+ this . _filterToolbox ( ) ;
40
49
this . _generator = this . _registry . generators . get ( 'python' ) ;
41
50
42
51
this . _changed = new Signal < this, BlocklyManager . Change > ( this ) ;
@@ -112,6 +121,7 @@ export class BlocklyManager {
112
121
if ( this . _toolbox !== name ) {
113
122
const toolbox = this . _registry . toolboxes . get ( name ) ;
114
123
this . _toolbox = toolbox ? name : 'default' ;
124
+ this . _filterToolbox ( ) ;
115
125
this . _changed . emit ( 'toolbox' ) ;
116
126
}
117
127
}
@@ -129,6 +139,73 @@ export class BlocklyManager {
129
139
return list ;
130
140
}
131
141
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
+
132
209
/**
133
210
* Set the selected kernel.
134
211
*
0 commit comments