Skip to content
Open
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
83 changes: 53 additions & 30 deletions plugins/toolbox-search/src/toolbox_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,40 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
this.searchField = document.createElement('input');
this.searchField.id = this.SEARCH_INPUT_ID;
this.searchField.type = 'search';
this.searchField.placeholder = 'Search';
this.searchField.placeholder = 'Search for blocks';
this.workspace_.RTL
? (this.searchField.style.marginRight = '8px')
: (this.searchField.style.marginLeft = '8px');
this.searchField.addEventListener('keyup', (event) => {
if (event.key === 'Escape') {
this.parentToolbox_.clearSelection();
this.searchField.addEventListener('keydown', (event) => {
if (event.key === 'ArrowUp' && this.searchField?.selectionStart === 0) {
const previous = this.parentToolbox_.getNavigator().getPreviousNode();
if (previous) {
Blockly.getFocusManager().focusNode(previous);
}
return;
} else if (
event.key === 'ArrowRight' &&
this.searchField?.selectionStart === this.searchField?.value.length
) {
const previous = this.parentToolbox_.getNavigator().getInNode();
if (previous) {
Blockly.getFocusManager().focusNode(previous);
}
return;
} else if (
event.key === 'ArrowDown' &&
this.searchField?.selectionStart === this.searchField?.value.length
) {
const next = this.parentToolbox_.getNavigator().getNextNode();
if (next) {
Blockly.getFocusManager().focusNode(next);
}
return;
} else if (event.key === 'Escape' && this.searchField) {
if (this.searchField.value !== '') {
this.searchField.value = '';
event.stopPropagation();
}
}

this.matchBlocks();
Expand All @@ -74,23 +100,6 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
return this.SEARCH_INPUT_ID;
}

/**
* Returns the numerical position of this category in its parent toolbox.
*
* @returns The zero-based index of this category in its parent toolbox, or -1
* if it cannot be determined, e.g. if this is a nested category.
*/
private getPosition() {
const categories = this.workspace_.options.languageTree?.contents || [];
for (let i = 0; i < categories.length; i++) {
if (categories[i].kind === ToolboxSearchCategory.SEARCH_CATEGORY_KIND) {
return i;
}
}

return -1;
}

/**
* Registers a shortcut for displaying the toolbox search category.
*/
Expand All @@ -102,8 +111,6 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
Blockly.ShortcutRegistry.registry.register({
name: ToolboxSearchCategory.START_SEARCH_SHORTCUT,
callback: () => {
const position = this.getPosition();
if (position < 0) return false;
Blockly.getFocusManager().focusNode(this);
return true;
},
Expand Down Expand Up @@ -154,13 +161,12 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {

/** See IFocusableNode.onNodeFocus. */
override onNodeFocus(): void {
this.matchBlocks();
}

/** See IFocusableNode.onNodeBlur. */
override onNodeBlur(): void {
if (!this.searchField) return;
this.searchField.value = '';
super.onNodeFocus();
if (!this.searchField?.value) {
Blockly.renderManagement.finishQueuedRenders().then(() => {
this.matchBlocks();
});
}
}

/**
Expand All @@ -169,10 +175,18 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
private matchBlocks() {
const query = this.searchField?.value || '';

const oldCount = this.flyoutItems_.length;
this.flyoutItems_ = query
? this.blockSearcher.blockTypesMatching(query)
: [];

const newCount = this.flyoutItems_.length;
if (oldCount !== newCount) {
Blockly.utils.aria.announceDynamicAriaState(
`${newCount} matching blocks`,
);
}

if (!this.flyoutItems_.length) {
this.flyoutItems_.push({
kind: 'label',
Expand All @@ -196,6 +210,15 @@ export class ToolboxSearchCategory extends Blockly.ToolboxCategory {
}
}

// Make the clear button clickable in Safari.
Blockly.Css.register(`
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button;
pointer-events: auto !important;
position: relative;
z-index: 10;
}`);

Blockly.registry.register(
Blockly.registry.Type.TOOLBOX_ITEM,
ToolboxSearchCategory.SEARCH_CATEGORY_KIND,
Expand Down
Loading