Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update block info when dropdown field matches search query #2013

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 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,40 @@ 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 (const input of block.inputList) {
for (const field of input.fieldRow) {
if (field instanceof Blockly.FieldDropdown) {
for (const option of field.getOptions(true)) {
if (typeof option[0] === 'string' &&
option[0].toLowerCase().includes(query.toLowerCase())) {
return {
kind: 'block',
type: blockType,
fields: {
[field.name]: option[1],
},
};
}
}
}
}
}

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

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