-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
import { useCallback, useMemo, KeyboardEvent } from 'react'; | ||
import { useMemo } from 'react'; | ||
|
||
import { KbsDefinition } from '../types'; | ||
import { cleanShortcuts } from '../utils/cleanShortcuts'; | ||
import { combineShortcuts } from '../utils/combineShortcuts'; | ||
import { eventToKeyOrCode } from '../utils/makeKey'; | ||
import { shouldIgnoreElement } from '../utils/shouldIgnoreElement'; | ||
import { | ||
getKeyDownHandler, | ||
useLastTriggerRef, | ||
} from '../utils/getKeyDownHandler'; | ||
|
||
export function useKbs(shortcuts: KbsDefinition[]) { | ||
const lastTrigger = useLastTriggerRef(); | ||
const combinedShortcuts = useMemo( | ||
() => combineShortcuts(cleanShortcuts([shortcuts])), | ||
[shortcuts], | ||
); | ||
const handleKeyDown = useCallback( | ||
function handleKeyDown(event: KeyboardEvent<HTMLDivElement>) { | ||
if (shouldIgnoreElement(event.target as HTMLElement)) { | ||
return; | ||
} | ||
const { key, code } = eventToKeyOrCode(event); | ||
const shortcut = combinedShortcuts[key] ?? combinedShortcuts[code]; | ||
if (shortcut) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
shortcut.handler(event); | ||
} | ||
}, | ||
[combinedShortcuts], | ||
const handleKeyDown = useMemo( | ||
() => getKeyDownHandler(lastTrigger, combinedShortcuts), | ||
[lastTrigger, combinedShortcuts], | ||
); | ||
return { tabIndex: 0, onKeyDown: handleKeyDown }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
KeyboardEvent as ReactKeyboardEvent, | ||
MutableRefObject, | ||
useRef, | ||
} from 'react'; | ||
|
||
import { KbsInternalShortcut } from '../types'; | ||
|
||
import { eventToKeyOrCode } from './makeKey'; | ||
import { shouldIgnoreElement } from './shouldIgnoreElement'; | ||
|
||
export interface LastTriggerData { | ||
keyOrCode: string; | ||
timestamp: number; | ||
} | ||
|
||
export function useLastTriggerRef() { | ||
return useRef<LastTriggerData>({ keyOrCode: '', timestamp: 0 }); | ||
} | ||
|
||
export function getKeyDownHandler( | ||
lastTrigger: MutableRefObject<LastTriggerData>, | ||
combinedShortcuts: Record<string, KbsInternalShortcut>, | ||
) { | ||
return function handleKeyDown( | ||
event: KeyboardEvent | ReactKeyboardEvent<HTMLDivElement>, | ||
) { | ||
if (shouldIgnoreElement(event.target as HTMLElement)) { | ||
return; | ||
} | ||
const { key, code } = eventToKeyOrCode(event); | ||
let keyOrCode; | ||
let shortcut; | ||
if (combinedShortcuts[key]) { | ||
shortcut = combinedShortcuts[key]; | ||
keyOrCode = key; | ||
} else { | ||
shortcut = combinedShortcuts[code]; | ||
keyOrCode = code; | ||
} | ||
if (shortcut) { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
|
||
if (shortcut.maxFrequency > 0) { | ||
const now = performance.now(); | ||
if ( | ||
event.repeat && | ||
lastTrigger.current.keyOrCode === keyOrCode && | ||
now - lastTrigger.current.timestamp < 1000 / shortcut.maxFrequency | ||
) { | ||
return; | ||
} | ||
lastTrigger.current = { keyOrCode, timestamp: now }; | ||
} | ||
|
||
shortcut.handler(event); | ||
} | ||
}; | ||
} |