From 9b1210eb5e3fffc4b44300e76f666ce10c04eed3 Mon Sep 17 00:00:00 2001 From: Amrit Kashyap Borah Date: Wed, 16 Oct 2024 13:40:01 +0530 Subject: [PATCH] fix: create timeout only if keydown is registered --- .../UseRegisterShortcutProvider.tsx | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx b/src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx index e08d45236..71e0f5ede 100644 --- a/src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx +++ b/src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx @@ -31,7 +31,7 @@ const UseRegisterShortcutProvider = ({ }: UseRegisterShortcutProviderType) => { const disableShortcutsRef = useRef(false) const shortcutsRef = useRef>({}) - const keysDownRef = useRef>(new Set()) + const keysDownRef = useRef>>(new Set()) const keyDownTimeoutRef = useRef>(-1) const ignoredTags = ignoreTags ?? IGNORE_TAGS_FALLBACK @@ -116,38 +116,37 @@ const UseRegisterShortcutProvider = ({ }, []) const handleKeydownEvent = useCallback((event: KeyboardEvent) => { - if (keyDownTimeoutRef.current === -1) { - keyDownTimeoutRef.current = setTimeout(() => { - handleKeyupEvent() - }, shortcutTimeout ?? DEFAULT_TIMEOUT) - } - if (preventDefault) { event.preventDefault() } if ( ignoredTags.map((tag) => tag.toUpperCase()).indexOf((event.target as HTMLElement).tagName.toUpperCase()) > - -1 + -1 || + disableShortcutsRef.current ) { return } - if (!disableShortcutsRef.current) { - keysDownRef.current.add(event.key.toUpperCase()) + keysDownRef.current.add(event.key.toUpperCase() as Uppercase) - if (event.ctrlKey) { - keysDownRef.current.add('CONTROL') - } - if (event.metaKey) { - keysDownRef.current.add('META') - } - if (event.altKey) { - keysDownRef.current.add('ALT') - } - if (event.shiftKey) { - keysDownRef.current.add('SHIFT') - } + if (event.ctrlKey) { + keysDownRef.current.add('CONTROL') + } + if (event.metaKey) { + keysDownRef.current.add('META') + } + if (event.altKey) { + keysDownRef.current.add('ALT') + } + if (event.shiftKey) { + keysDownRef.current.add('SHIFT') + } + + if (keyDownTimeoutRef.current === -1) { + keyDownTimeoutRef.current = setTimeout(() => { + handleKeyupEvent() + }, shortcutTimeout ?? DEFAULT_TIMEOUT) } }, [])