From e7d906faeb8fdae0f9d8c5552e409e1e9ec98881 Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Fri, 12 Jul 2024 16:03:35 +0300 Subject: [PATCH] Converted `lastDownEvent` listener to `hotkeys` --- .../text-annotator/src/SelectionHandler.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/text-annotator/src/SelectionHandler.ts b/packages/text-annotator/src/SelectionHandler.ts index 6e950cf8..5a0eaafc 100644 --- a/packages/text-annotator/src/SelectionHandler.ts +++ b/packages/text-annotator/src/SelectionHandler.ts @@ -134,13 +134,6 @@ export const SelectionHandler = ( } container.addEventListener('pointerdown', onPointerDown); - const onKeyDown = (evt: KeyboardEvent) => { - if (!evt.repeat) { - lastDownEvent = cloneKeyboardEvent(evt); - } - } - container.addEventListener('keydown', onKeyDown); - const onPointerUp = (evt: PointerEvent) => { const annotatable = !(evt.target as Node).parentElement?.closest(NOT_ANNOTATABLE_SELECTOR); if (!annotatable || !isLeftClick) @@ -173,9 +166,19 @@ export const SelectionHandler = ( } document.addEventListener('pointerup', onPointerUp); + /** + * Track arbitrary keydown events to use them during + * the `selectionchange` annotation selection. + */ + hotkeys('*', { element: container, keyup: false, keydown: true }, (evt, handler) => { + if (!evt.repeat) { + lastDownEvent = cloneKeyboardEvent(evt); + } + }); + /** * Track the "Shift" key lift which signifies the end of a select operation. - * Unfortunately, we cannot track modifier key immediately, so a wildcard is used + * Unfortunately, we cannot track modifier key immediately, so a wildcard is used. */ hotkeys('*', { keyup: true, keydown: false }, (evt) => { if (hotkeys.shift && evt.key === Key.Shift) { @@ -210,7 +213,6 @@ export const SelectionHandler = ( container.removeEventListener('pointerdown', onPointerDown); document.removeEventListener('pointerup', onPointerUp); - container.removeEventListener('keydown', onKeyDown); hotkeys.unbind(); }