@@ -10,8 +10,10 @@ import * as Blockly from 'blockly/core';
1010 * A class that provides methods for indexing and searching blocks.
1111 */
1212export class BlockSearcher {
13- private blockCreationWorkspace = new Blockly . Workspace ( ) ;
14- private trigramsToBlocks = new Map < string , Set < string > > ( ) ;
13+ private trigramsToBlocks = new Map <
14+ string ,
15+ Set < Blockly . utils . toolbox . BlockInfo >
16+ > ( ) ;
1517
1618 /**
1719 * Populates the cached map of trigrams to the blocks they correspond to.
@@ -21,17 +23,19 @@ export class BlockSearcher {
2123 * indexes their types and human-readable text, and cleans up after
2224 * itself.
2325 *
24- * @param blockTypes A list of block types to index.
26+ * @param blockInfos A list of blocks to index.
2527 */
26- indexBlocks ( blockTypes : string [ ] ) {
28+ indexBlocks ( blockInfos : Blockly . utils . toolbox . BlockInfo [ ] ) {
2729 const blockCreationWorkspace = new Blockly . Workspace ( ) ;
28- blockTypes . forEach ( ( blockType ) => {
29- const block = blockCreationWorkspace . newBlock ( blockType ) ;
30- this . indexBlockText ( blockType . replaceAll ( '_' , ' ' ) , blockType ) ;
30+ blockInfos . forEach ( ( blockInfo ) => {
31+ const type = blockInfo . type ;
32+ if ( ! type || type === '' ) return ;
33+ const block = blockCreationWorkspace . newBlock ( type ) ;
34+ this . indexBlockText ( type . replaceAll ( '_' , ' ' ) , blockInfo ) ;
3135 block . inputList . forEach ( ( input ) => {
3236 input . fieldRow . forEach ( ( field ) => {
33- this . indexDropdownOption ( field , blockType ) ;
34- this . indexBlockText ( field . getText ( ) , blockType ) ;
37+ this . indexDropdownOption ( field , blockInfo ) ;
38+ this . indexBlockText ( field . getText ( ) , blockInfo ) ;
3539 } ) ;
3640 } ) ;
3741 } ) ;
@@ -41,15 +45,18 @@ export class BlockSearcher {
4145 * Check if the field is a dropdown, and index every text in the option
4246 *
4347 * @param field We need to check the type of field
44- * @param blockType The block type to associate the trigrams with.
48+ * @param block The block to associate the trigrams with.
4549 */
46- private indexDropdownOption ( field : Blockly . Field , blockType : string ) {
50+ private indexDropdownOption (
51+ field : Blockly . Field ,
52+ block : Blockly . utils . toolbox . BlockInfo ,
53+ ) {
4754 if ( field instanceof Blockly . FieldDropdown ) {
4855 field . getOptions ( true ) . forEach ( ( option ) => {
4956 if ( typeof option [ 0 ] === 'string' ) {
50- this . indexBlockText ( option [ 0 ] , blockType ) ;
57+ this . indexBlockText ( option [ 0 ] , block ) ;
5158 } else if ( 'alt' in option [ 0 ] ) {
52- this . indexBlockText ( option [ 0 ] . alt , blockType ) ;
59+ this . indexBlockText ( option [ 0 ] . alt , block ) ;
5360 }
5461 } ) ;
5562 }
@@ -59,13 +66,16 @@ export class BlockSearcher {
5966 * Filters the available blocks based on the current query string.
6067 *
6168 * @param query The text to use to match blocks against.
62- * @returns A list of block types matching the query.
69+ * @returns A list of blocks matching the query.
6370 */
64- blockTypesMatching ( query : string ) : string [ ] {
71+ blockTypesMatching ( query : string ) : Blockly . utils . toolbox . BlockInfo [ ] {
6572 return [
6673 ...this . generateTrigrams ( query )
6774 . map ( ( trigram ) => {
68- return this . trigramsToBlocks . get ( trigram ) ?? new Set < string > ( ) ;
75+ return (
76+ this . trigramsToBlocks . get ( trigram ) ??
77+ new Set < Blockly . utils . toolbox . BlockInfo > ( )
78+ ) ;
6979 } )
7080 . reduce ( ( matches , current ) => {
7181 return this . getIntersection ( matches , current ) ;
@@ -76,15 +86,17 @@ export class BlockSearcher {
7686
7787 /**
7888 * Generates trigrams for the given text and associates them with the given
79- * block type .
89+ * block.
8090 *
8191 * @param text The text to generate trigrams of.
82- * @param blockType The block type to associate the trigrams with.
92+ * @param block The block to associate the trigrams with.
8393 */
84- private indexBlockText ( text : string , blockType : string ) {
94+ private indexBlockText ( text : string , block : Blockly . utils . toolbox . BlockInfo ) {
8595 this . generateTrigrams ( text ) . forEach ( ( trigram ) => {
86- const blockSet = this . trigramsToBlocks . get ( trigram ) ?? new Set < string > ( ) ;
87- blockSet . add ( blockType ) ;
96+ const blockSet =
97+ this . trigramsToBlocks . get ( trigram ) ??
98+ new Set < Blockly . utils . toolbox . BlockInfo > ( ) ;
99+ blockSet . add ( block ) ;
88100 this . trigramsToBlocks . set ( trigram , blockSet ) ;
89101 } ) ;
90102 }
@@ -115,7 +127,10 @@ export class BlockSearcher {
115127 * @param b The second set.
116128 * @returns The intersection of the two sets.
117129 */
118- private getIntersection ( a : Set < string > , b : Set < string > ) : Set < string > {
130+ private getIntersection (
131+ a : Set < Blockly . utils . toolbox . BlockInfo > ,
132+ b : Set < Blockly . utils . toolbox . BlockInfo > ,
133+ ) : Set < Blockly . utils . toolbox . BlockInfo > {
119134 return new Set ( [ ...a ] . filter ( ( value ) => b . has ( value ) ) ) ;
120135 }
121136}
0 commit comments