From d35452091048fcb23d49fda35f582988a26e0f67 Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 17 Aug 2023 17:34:09 +0200 Subject: [PATCH 1/3] fix(core/selector): avoid selectedIndex to go NaN if filteredOptions empty --- .../src/components/selectorModal.component.ts | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/tabby-core/src/components/selectorModal.component.ts b/tabby-core/src/components/selectorModal.component.ts index 76092314..255dd44f 100644 --- a/tabby-core/src/components/selectorModal.component.ts +++ b/tabby-core/src/components/selectorModal.component.ts @@ -29,34 +29,36 @@ export class SelectorModalComponent { } @HostListener('keydown', ['$event']) onKeyUp (event: KeyboardEvent): void { - if (event.key === 'PageUp' || event.key === 'ArrowUp' && event.metaKey) { - this.selectedIndex -= 10 - event.preventDefault() - } else if (event.key === 'PageDown' || event.key === 'ArrowDown' && event.metaKey) { - this.selectedIndex += 10 - event.preventDefault() - } else if (event.key === 'ArrowUp') { - this.selectedIndex-- - event.preventDefault() - } else if (event.key === 'ArrowDown') { - this.selectedIndex++ - event.preventDefault() - } else if (event.key === 'Enter') { - this.selectOption(this.filteredOptions[this.selectedIndex]) - } else if (event.key === 'Escape') { + if (event.key === 'Escape') { this.close() - } - if (event.key === 'Backspace' && this.canEditSelected()) { - event.preventDefault() - this.filter = this.filteredOptions[this.selectedIndex].freeInputEquivalent! - this.onFilterChange() - } + } else if (this.filteredOptions.length > 0) { + if (event.key === 'PageUp' || event.key === 'ArrowUp' && event.metaKey) { + this.selectedIndex -= 10 + event.preventDefault() + } else if (event.key === 'PageDown' || event.key === 'ArrowDown' && event.metaKey) { + this.selectedIndex += 10 + event.preventDefault() + } else if (event.key === 'ArrowUp') { + this.selectedIndex-- + event.preventDefault() + } else if (event.key === 'ArrowDown') { + this.selectedIndex++ + event.preventDefault() + } else if (event.key === 'Enter') { + this.selectOption(this.filteredOptions[this.selectedIndex]) + } else if (event.key === 'Backspace' && this.canEditSelected()) { + event.preventDefault() + this.filter = this.filteredOptions[this.selectedIndex].freeInputEquivalent! + this.onFilterChange() + } - this.selectedIndex = (this.selectedIndex + this.filteredOptions.length) % this.filteredOptions.length - Array.from(this.itemChildren)[this.selectedIndex]?.nativeElement.scrollIntoView({ - behavior: 'smooth', - block: 'nearest', - }) + this.selectedIndex = (this.selectedIndex + this.filteredOptions.length) % this.filteredOptions.length + + Array.from(this.itemChildren)[this.selectedIndex]?.nativeElement.scrollIntoView({ + behavior: 'smooth', + block: 'nearest', + }) + } } onFilterChange (): void { From ad3b03cb839717d98af3eb871ab7c35bf59f954c Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 17 Aug 2023 17:58:41 +0200 Subject: [PATCH 2/3] feat(core/selector): PageUp/Down toogle at end of filteredOptions --- tabby-core/src/components/selectorModal.component.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tabby-core/src/components/selectorModal.component.ts b/tabby-core/src/components/selectorModal.component.ts index 255dd44f..a0a25fb9 100644 --- a/tabby-core/src/components/selectorModal.component.ts +++ b/tabby-core/src/components/selectorModal.component.ts @@ -33,10 +33,11 @@ export class SelectorModalComponent { this.close() } else if (this.filteredOptions.length > 0) { if (event.key === 'PageUp' || event.key === 'ArrowUp' && event.metaKey) { - this.selectedIndex -= 10 + this.selectedIndex -= Math.min(10, this.selectedIndex === 0 ? 1 : this.selectedIndex) event.preventDefault() } else if (event.key === 'PageDown' || event.key === 'ArrowDown' && event.metaKey) { - this.selectedIndex += 10 + const newI = this.filteredOptions.length - this.selectedIndex - 1 + this.selectedIndex += Math.min(10, newI === 0 ? 1 : newI) event.preventDefault() } else if (event.key === 'ArrowUp') { this.selectedIndex-- From 634d88d220a3471610cbb0df2e784c7c9257d73b Mon Sep 17 00:00:00 2001 From: Clem Date: Thu, 17 Aug 2023 19:32:52 +0200 Subject: [PATCH 3/3] refactoring ad3b03cb839717d98af3eb871ab7c35bf59f954c --- tabby-core/src/components/selectorModal.component.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tabby-core/src/components/selectorModal.component.ts b/tabby-core/src/components/selectorModal.component.ts index a0a25fb9..772adef2 100644 --- a/tabby-core/src/components/selectorModal.component.ts +++ b/tabby-core/src/components/selectorModal.component.ts @@ -33,11 +33,10 @@ export class SelectorModalComponent { this.close() } else if (this.filteredOptions.length > 0) { if (event.key === 'PageUp' || event.key === 'ArrowUp' && event.metaKey) { - this.selectedIndex -= Math.min(10, this.selectedIndex === 0 ? 1 : this.selectedIndex) + this.selectedIndex -= Math.min(10, Math.max(1, this.selectedIndex)) event.preventDefault() } else if (event.key === 'PageDown' || event.key === 'ArrowDown' && event.metaKey) { - const newI = this.filteredOptions.length - this.selectedIndex - 1 - this.selectedIndex += Math.min(10, newI === 0 ? 1 : newI) + this.selectedIndex += Math.min(10, Math.max(1, this.filteredOptions.length - this.selectedIndex - 1)) event.preventDefault() } else if (event.key === 'ArrowUp') { this.selectedIndex--