Browse Source

Adds Ctrl/Cmd+F to focus the search box

main
Eric Amodio 2 years ago
parent
commit
e516e9b71d
2 changed files with 27 additions and 9 deletions
  1. +23
    -9
      src/webviews/apps/shared/components/search/search-box.ts
  2. +4
    -0
      src/webviews/apps/shared/components/search/search-input.ts

+ 23
- 9
src/webviews/apps/shared/components/search/search-box.ts View File

@ -9,6 +9,7 @@ import './search-input';
const template = html<SearchBox>`<template> const template = html<SearchBox>`<template>
<search-input <search-input
id="search-input"
errorMessage="${x => x.errorMessage}" errorMessage="${x => x.errorMessage}"
label="${x => x.label}" label="${x => x.label}"
placeholder="${x => x.placeholder}" placeholder="${x => x.placeholder}"
@ -169,6 +170,7 @@ export class SearchBox extends FASTElement {
} }
private _disposable: Disposable | undefined; private _disposable: Disposable | undefined;
override connectedCallback(): void { override connectedCallback(): void {
super.connectedCallback(); super.connectedCallback();
@ -181,6 +183,10 @@ export class SearchBox extends FASTElement {
this._disposable?.dispose(); this._disposable?.dispose();
} }
override focus(options?: FocusOptions): void {
this.shadowRoot?.getElementById('search-input')?.focus(options);
}
next() { next() {
this.$emit('next'); this.$emit('next');
} }
@ -190,18 +196,26 @@ export class SearchBox extends FASTElement {
} }
handleShortcutKeys(e: KeyboardEvent) { handleShortcutKeys(e: KeyboardEvent) {
if (
(e.key !== 'F3' && e.key !== 'g') ||
(e.key !== 'g' && (e.ctrlKey || e.metaKey || e.altKey)) ||
(e.key === 'g' && (!e.metaKey || !isMac))
) {
if (e.altKey) return;
if ((e.key === 'F3' && !e.ctrlKey && !e.metaKey) || (e.key === 'g' && e.metaKey && !e.ctrlKey && isMac)) {
e.preventDefault();
e.stopImmediatePropagation();
if (e.shiftKey) {
this.previous();
} else {
this.next();
}
return; return;
} }
if (e.shiftKey) {
this.previous();
} else {
this.next();
if (e.key === 'f' && ((e.metaKey && !e.ctrlKey && isMac) || (e.ctrlKey && !isMac))) {
e.preventDefault();
e.stopImmediatePropagation();
this.focus();
} }
} }

+ 4
- 0
src/webviews/apps/shared/components/search/search-input.ts View File

@ -227,6 +227,10 @@ export class SearchInput extends FASTElement {
return this.matchRegex ? this.matchCase : true; return this.matchRegex ? this.matchCase : true;
} }
override focus(options?: FocusOptions): void {
this.shadowRoot?.getElementById('search')?.focus(options);
}
handleClear(_e: Event) { handleClear(_e: Event) {
this.value = ''; this.value = '';
this.emitSearch(); this.emitSearch();

Loading…
Cancel
Save