mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-05-19 03:41:52 +00:00
Merge pull request #1233 from Stremio/player/fix-shortcuts-hold-repeat-throttle
Player: Correctly throttle holding shortcut actions
This commit is contained in:
commit
21620c81e0
1 changed files with 11 additions and 1 deletions
|
|
@ -19,10 +19,20 @@ type Props = {
|
||||||
onShortcut: (name: ShortcutName) => void,
|
onShortcut: (name: ShortcutName) => void,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const REPEAT_THROTTLE_MS = 130;
|
||||||
|
|
||||||
const ShortcutsProvider = ({ children, onShortcut }: Props) => {
|
const ShortcutsProvider = ({ children, onShortcut }: Props) => {
|
||||||
const listeners = useRef<Map<ShortcutName, Set<ShortcutListener>>>(new Map());
|
const listeners = useRef<Map<ShortcutName, Set<ShortcutListener>>>(new Map());
|
||||||
|
const lastRepeatTime = useRef<Map<string, number>>(new Map());
|
||||||
|
|
||||||
|
const onKeyDown = useCallback(({ ctrlKey, shiftKey, code, key, repeat }: KeyboardEvent) => {
|
||||||
|
if (repeat) {
|
||||||
|
const now = Date.now();
|
||||||
|
const last = lastRepeatTime.current.get(code) ?? 0;
|
||||||
|
if (now - last < REPEAT_THROTTLE_MS) return;
|
||||||
|
lastRepeatTime.current.set(code, now);
|
||||||
|
}
|
||||||
|
|
||||||
const onKeyDown = useCallback(({ ctrlKey, shiftKey, code, key }: KeyboardEvent) => {
|
|
||||||
SHORTCUTS.forEach(({ name, combos }) => combos.forEach((keys) => {
|
SHORTCUTS.forEach(({ name, combos }) => combos.forEach((keys) => {
|
||||||
const modifers = (keys.includes('Ctrl') ? ctrlKey : true)
|
const modifers = (keys.includes('Ctrl') ? ctrlKey : true)
|
||||||
&& (keys.includes('Shift') ? shiftKey : true);
|
&& (keys.includes('Shift') ? shiftKey : true);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue