|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { useMemo, useState } from 'react' |
18 |
| -import { context } from './UseRegisterShortcutContext' |
19 |
| -import { UseRegisterShortcutProviderType } from './types' |
| 17 | +import { useCallback, useEffect, useMemo, useRef } from 'react' |
| 18 | +import { deepEquals } from '@rjsf/utils' |
| 19 | +import { UseRegisterShortcutContext } from './UseRegisterShortcutContext' |
| 20 | +import { UseRegisterShortcutProviderType, ShortcutType, UseRegisterShortcutContextType } from './types' |
| 21 | +import { preprocessKeys, verifyCallbackStack } from './utils' |
20 | 22 |
|
21 |
| -const UseRegisterShortcutProvider = ({ children }: UseRegisterShortcutProviderType) => { |
22 |
| - const [registerShortcut, setRegisterShortcut] = useState(true) |
| 23 | +const IGNORE_TAGS_FALLBACK = ['input', 'textarea', 'select'] |
| 24 | +const DEFAULT_TIMEOUT = 300 |
23 | 25 |
|
24 |
| - const providerValue = useMemo( |
| 26 | +const UseRegisterShortcutProvider = ({ |
| 27 | + ignoreTags, |
| 28 | + preventDefault = false, |
| 29 | + shortcutTimeout, |
| 30 | + children, |
| 31 | +}: UseRegisterShortcutProviderType) => { |
| 32 | + const disableShortcutsRef = useRef<boolean>(false) |
| 33 | + const shortcutsRef = useRef<Record<string, ShortcutType>>({}) |
| 34 | + const keysDownRef = useRef<Set<Uppercase<string>>>(new Set()) |
| 35 | + const keyDownTimeoutRef = useRef<ReturnType<typeof setTimeout>>(-1) |
| 36 | + const ignoredTags = ignoreTags ?? IGNORE_TAGS_FALLBACK |
| 37 | + |
| 38 | + const registerShortcut: UseRegisterShortcutContextType['registerShortcut'] = useCallback( |
| 39 | + ({ keys, callback, description = '' }) => { |
| 40 | + const { keys: processedKeys, id } = preprocessKeys(keys) |
| 41 | + if (typeof callback !== 'function') { |
| 42 | + throw new Error('callback provided is not a function') |
| 43 | + } |
| 44 | + |
| 45 | + const match = |
| 46 | + shortcutsRef.current[id] && deepEquals(shortcutsRef.current[id].keys, keys) |
| 47 | + ? shortcutsRef.current[id] |
| 48 | + : null |
| 49 | + |
| 50 | + if (match) { |
| 51 | + verifyCallbackStack(match.callbackStack) |
| 52 | + match.callbackStack.push(callback) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + shortcutsRef.current[id] = { keys: processedKeys, callbackStack: [callback], description } |
| 57 | + }, |
| 58 | + [], |
| 59 | + ) |
| 60 | + |
| 61 | + const unregisterShortcut: UseRegisterShortcutContextType['unregisterShortcut'] = useCallback((keys) => { |
| 62 | + const { id } = preprocessKeys(keys) |
| 63 | + |
| 64 | + if (!shortcutsRef.current[id]) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + const { callbackStack } = shortcutsRef.current[id] |
| 69 | + verifyCallbackStack(callbackStack) |
| 70 | + callbackStack.pop() |
| 71 | + |
| 72 | + if (!callbackStack.length) { |
| 73 | + // NOTE: delete the shortcut only if all registered callbacks are unregistered |
| 74 | + // if 2 shortcuts are registered with the same keys then there needs to be 2 unregister calls |
| 75 | + delete shortcutsRef.current[id] |
| 76 | + } |
| 77 | + }, []) |
| 78 | + |
| 79 | + const setDisableShortcuts: UseRegisterShortcutContextType['setDisableShortcuts'] = useCallback((shouldDisable) => { |
| 80 | + disableShortcutsRef.current = shouldDisable |
| 81 | + }, []) |
| 82 | + |
| 83 | + const triggerShortcut: UseRegisterShortcutContextType['triggerShortcut'] = useCallback((keys) => { |
| 84 | + const { id } = preprocessKeys(keys) |
| 85 | + |
| 86 | + if (!shortcutsRef.current[id]) { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + const { callbackStack } = shortcutsRef.current[id] |
| 91 | + verifyCallbackStack(callbackStack) |
| 92 | + |
| 93 | + // NOTE: call the last callback in the callback stack |
| 94 | + callbackStack[callbackStack.length - 1]() |
| 95 | + }, []) |
| 96 | + |
| 97 | + const handleKeyupEvent = useCallback(() => { |
| 98 | + if (!keysDownRef.current.size) { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + const { id } = preprocessKeys(Array.from(keysDownRef.current.values()) as ShortcutType['keys']) |
| 103 | + |
| 104 | + if (shortcutsRef.current[id]) { |
| 105 | + const { callbackStack } = shortcutsRef.current[id] |
| 106 | + verifyCallbackStack(callbackStack) |
| 107 | + callbackStack[callbackStack.length - 1]() |
| 108 | + } |
| 109 | + |
| 110 | + keysDownRef.current.clear() |
| 111 | + |
| 112 | + if (keyDownTimeoutRef.current > -1) { |
| 113 | + clearTimeout(keyDownTimeoutRef.current) |
| 114 | + keyDownTimeoutRef.current = -1 |
| 115 | + } |
| 116 | + }, []) |
| 117 | + |
| 118 | + const handleKeydownEvent = useCallback((event: KeyboardEvent) => { |
| 119 | + if (preventDefault) { |
| 120 | + event.preventDefault() |
| 121 | + } |
| 122 | + |
| 123 | + if ( |
| 124 | + ignoredTags.map((tag) => tag.toUpperCase()).indexOf((event.target as HTMLElement).tagName.toUpperCase()) > |
| 125 | + -1 || |
| 126 | + disableShortcutsRef.current |
| 127 | + ) { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + keysDownRef.current.add(event.key.toUpperCase() as Uppercase<string>) |
| 132 | + |
| 133 | + if (event.ctrlKey) { |
| 134 | + keysDownRef.current.add('CONTROL') |
| 135 | + } |
| 136 | + if (event.metaKey) { |
| 137 | + keysDownRef.current.add('META') |
| 138 | + } |
| 139 | + if (event.altKey) { |
| 140 | + keysDownRef.current.add('ALT') |
| 141 | + } |
| 142 | + if (event.shiftKey) { |
| 143 | + keysDownRef.current.add('SHIFT') |
| 144 | + } |
| 145 | + |
| 146 | + if (keyDownTimeoutRef.current === -1) { |
| 147 | + keyDownTimeoutRef.current = setTimeout(() => { |
| 148 | + handleKeyupEvent() |
| 149 | + }, shortcutTimeout ?? DEFAULT_TIMEOUT) |
| 150 | + } |
| 151 | + }, []) |
| 152 | + |
| 153 | + const handleBlur = useCallback(() => { |
| 154 | + keysDownRef.current.clear() |
| 155 | + }, []) |
| 156 | + |
| 157 | + useEffect(() => { |
| 158 | + window.addEventListener('keydown', handleKeydownEvent) |
| 159 | + window.addEventListener('keyup', handleKeyupEvent) |
| 160 | + window.addEventListener('blur', handleBlur) |
| 161 | + |
| 162 | + return () => { |
| 163 | + window.removeEventListener('keydown', handleKeydownEvent) |
| 164 | + window.removeEventListener('keyup', handleKeyupEvent) |
| 165 | + window.removeEventListener('blur', handleBlur) |
| 166 | + |
| 167 | + if (keyDownTimeoutRef.current > -1) { |
| 168 | + clearTimeout(keyDownTimeoutRef.current) |
| 169 | + } |
| 170 | + } |
| 171 | + }, [handleKeyupEvent, handleKeydownEvent, handleBlur]) |
| 172 | + |
| 173 | + const providerValue: UseRegisterShortcutContextType = useMemo( |
25 | 174 | () => ({
|
26 | 175 | registerShortcut,
|
27 |
| - setRegisterShortcut: (allowShortcut: boolean) => setRegisterShortcut(allowShortcut), |
| 176 | + unregisterShortcut, |
| 177 | + setDisableShortcuts, |
| 178 | + triggerShortcut, |
28 | 179 | }),
|
29 |
| - [registerShortcut], |
| 180 | + [registerShortcut, unregisterShortcut, setDisableShortcuts, triggerShortcut], |
30 | 181 | )
|
31 | 182 |
|
32 |
| - return <context.Provider value={providerValue}>{children}</context.Provider> |
| 183 | + return <UseRegisterShortcutContext.Provider value={providerValue}>{children}</UseRegisterShortcutContext.Provider> |
33 | 184 | }
|
34 | 185 |
|
35 | 186 | export default UseRegisterShortcutProvider
|
0 commit comments