Skip to content

Commit

Permalink
feat: update block info when dropdown field matches search query
Browse files Browse the repository at this point in the history
  • Loading branch information
ludizhan committed Oct 7, 2023
1 parent abc713b commit bfa2300
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions plugins/toolbox-search/src/toolbox_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
import * as Blockly from 'blockly/core';
import {BlockSearcher} from './block_searcher';
import type {BlockInfo} from 'blockly/core/utils/toolbox';

/* eslint-disable @typescript-eslint/naming-convention */

Expand Down Expand Up @@ -161,10 +162,7 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
this.flyoutItems_ = query ?
this.blockSearcher.blockTypesMatching(query).map(
(blockType) => {
return {
kind: 'block',
type: blockType,
};
return this.generateFlyoutItem(blockType, query);
}) : [];

if (!this.flyoutItems_.length) {
Expand All @@ -177,6 +175,39 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
this.parentToolbox_.refreshSelection();
}

/**
* Generate BlockInfo for the block, with fields value if there is a dropdown
* field matching the search query.
* @param blockType The block type that we want to return the info of.
* @param query The search query.
* @returns The json representation of block.
*/
private generateFlyoutItem(blockType: string, query: string): BlockInfo {
const block = this.workspace_.newBlock(blockType);
for (let input of block.inputList) {

Check failure on line 187 in plugins/toolbox-search/src/toolbox_search.ts

View workflow job for this annotation

GitHub Actions / lint

'input' is never reassigned. Use 'const' instead
for (let field of input.fieldRow) {

Check failure on line 188 in plugins/toolbox-search/src/toolbox_search.ts

View workflow job for this annotation

GitHub Actions / lint

'field' is never reassigned. Use 'const' instead
if (field instanceof Blockly.FieldDropdown) {
for (let option of field.getOptions(true)) {

Check failure on line 190 in plugins/toolbox-search/src/toolbox_search.ts

View workflow job for this annotation

GitHub Actions / lint

'option' is never reassigned. Use 'const' instead
if (typeof option[0] === 'string' && option[0].toLowerCase().includes(query.toLowerCase())) {

Check failure on line 191 in plugins/toolbox-search/src/toolbox_search.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 105. Maximum allowed is 80
return {
kind: 'block',
type: blockType,
fields: {
[field.name]: option[1],
},
};
}
}
}
}
}

return {
kind: 'block',
type: blockType,
};
}

/**
* Disposes of this category.
*/
Expand Down

0 comments on commit bfa2300

Please sign in to comment.