From 0188d551afce3ea85f7e3cbc2123c629e67ac7a8 Mon Sep 17 00:00:00 2001 From: Matthijs Hofele Date: Wed, 10 Apr 2024 10:31:14 +0200 Subject: [PATCH 1/3] Don't call preventDefault() if the hotkey-fire event has been cancelled --- src/index.ts | 3 +-- src/utils.ts | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index aa1abec..6fa3e0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,8 +52,7 @@ function keyDownHandler(event: KeyboardEvent) { } if (elementToFire && shouldFire) { - fireDeterminedAction(elementToFire, sequenceTracker.path) - event.preventDefault() + if (fireDeterminedAction(elementToFire, sequenceTracker.path)) event.preventDefault() } sequenceTracker.reset() diff --git a/src/utils.ts b/src/utils.ts index d0aab12..7da8e3f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,15 +21,17 @@ export function isFormField(element: Node): boolean { ) } -export function fireDeterminedAction(el: HTMLElement, path: readonly NormalizedHotkeyString[]): void { +export function fireDeterminedAction(el: HTMLElement, path: readonly NormalizedHotkeyString[]): boolean { const delegateEvent = new CustomEvent('hotkey-fire', {cancelable: true, detail: {path}}) const cancelled = !el.dispatchEvent(delegateEvent) - if (cancelled) return + if (cancelled) return false + if (isFormField(el)) { el.focus() } else { el.click() } + return true } export function expandHotkeyToEdges(hotkey: string): NormalizedHotkeyString[][] { From 72b9276e87344583dd53fd89947294fb4fdecd02 Mon Sep 17 00:00:00 2001 From: Matthijs Hofele Date: Wed, 10 Apr 2024 11:37:34 +0200 Subject: [PATCH 2/3] Add test --- test/test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 5119ded..753e0ed 100644 --- a/test/test.js +++ b/test/test.js @@ -46,8 +46,10 @@ describe('hotkey', function () { describe('single key support', function () { it('triggers buttons that have a `data-hotkey` attribute', function () { setHTML('') - document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'})) + const event = new KeyboardEvent('keydown', {cancelable: true, key: 'b'}) + document.dispatchEvent(event) assert.include(elementsActivated, 'button1') + assert.ok(event.defaultPrevented, 'should call preventDefault on keydown event') }) it('triggers buttons that get hotkey passed in as second argument', function () { @@ -118,8 +120,10 @@ describe('hotkey', function () { it('wont trigger action if the hotkey-fire event is cancelled', function () { setHTML('') document.querySelector('#button1').addEventListener('hotkey-fire', event => event.preventDefault()) - document.dispatchEvent(new KeyboardEvent('keydown', {shiftKey: true, code: 'KeyB', key: 'B'})) + const event = new KeyboardEvent('keydown', {cancelable: true, shiftKey: true, code: 'KeyB', key: 'B'}) + document.dispatchEvent(event) assert.notInclude(elementsActivated, 'button1') + assert.equal(event.defaultPrevented, false, 'should not prevent the keydown event when cancelled') }) it('supports comma as a hotkey', function () { From 6edce095fb6fa521327374bcfd98cc921c52a5d6 Mon Sep 17 00:00:00 2001 From: Matthijs Hofele Date: Wed, 10 Apr 2024 14:25:07 +0200 Subject: [PATCH 3/3] Remove white space --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 7da8e3f..5a96a38 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,7 +25,7 @@ export function fireDeterminedAction(el: HTMLElement, path: readonly NormalizedH const delegateEvent = new CustomEvent('hotkey-fire', {cancelable: true, detail: {path}}) const cancelled = !el.dispatchEvent(delegateEvent) if (cancelled) return false - + if (isFormField(el)) { el.focus() } else {