Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2import { isEditableFocused } from "./lib/editableFocus";
3
4export interface ShortcutModifierState {
5 metaKey: boolean;
6 ctrlKey: boolean;
7 altKey: boolean;
8 shiftKey: boolean;
9}
10
11const EMPTY_SHORTCUT_MODIFIER_STATE: ShortcutModifierState = {
12 metaKey: false,
13 ctrlKey: false,
14 altKey: false,
15 shiftKey: false,
16};
17
18function areShortcutModifierStatesEqual(
19 left: ShortcutModifierState,
20 right: ShortcutModifierState,
21): boolean {
22 return (
23 left.metaKey === right.metaKey &&
24 left.ctrlKey === right.ctrlKey &&
25 left.altKey === right.altKey &&
26 left.shiftKey === right.shiftKey
27 );
28}
29
30export function useShortcutModifierState(ignoreEditable = false): ShortcutModifierState {
31 const [state, setState] = useState(EMPTY_SHORTCUT_MODIFIER_STATE);
32 const stateRef = useRef(EMPTY_SHORTCUT_MODIFIER_STATE);
33
34 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window key, focus, paste and blur listeners that track held shortcut modifiers
35 useEffect(() => {
36 const updateState = (next: ShortcutModifierState) => {
37 if (areShortcutModifierStatesEqual(stateRef.current, next)) return;
38 stateRef.current = next;
39 setState(next);
40 };
41 const onKeyboardEvent = (event: KeyboardEvent) => {
42 updateState(
43 ignoreEditable && isEditableFocused(event.target)
44 ? EMPTY_SHORTCUT_MODIFIER_STATE
45 : shortcutModifierStateAfterKeyboardEvent(stateRef.current, event),
46 );
47 };
48 // Dictation tools can omit Meta keyup after synthetic paste, leaving jump hints stuck.
49 const onResetEvent = () => {
50 updateState(EMPTY_SHORTCUT_MODIFIER_STATE);
51 };
52
53 const onFocus = (event: FocusEvent) => {
54 if (ignoreEditable && isEditableFocused(event.target)) onResetEvent();
55 };
56 window.addEventListener("focusin", onFocus);
57 window.addEventListener("keydown", onKeyboardEvent, true);
58 window.addEventListener("keyup", onKeyboardEvent, true);
59 window.addEventListener("paste", onResetEvent, true);
60 window.addEventListener("blur", onResetEvent);
61 return () => {
62 window.removeEventListener("focusin", onFocus);
63 window.removeEventListener("keydown", onKeyboardEvent, true);
64 window.removeEventListener("keyup", onKeyboardEvent, true);
65 window.removeEventListener("paste", onResetEvent, true);
66 window.removeEventListener("blur", onResetEvent);
67 };
68 }, [ignoreEditable]);
69
70 return state;
71}
72
73function normalizeModifierKey(key: string): keyof ShortcutModifierState | null {
74 switch (key) {
75 case "Meta":
76 case "OS":
77 case "Command":
78 return "metaKey";
79 case "Control":
80 return "ctrlKey";
81 case "Alt":
82 case "Option":
83 return "altKey";
84 case "Shift":
85 return "shiftKey";
86 default:
87 return null;
88 }
89}
90
91export function shortcutModifierStateAfterKeyboardEvent(
92 currentState: ShortcutModifierState,
93 event: KeyboardEvent,
94): ShortcutModifierState {
95 const normalizedModifierKey = normalizeModifierKey(event.key);
96 let nextState: ShortcutModifierState;
97 if (normalizedModifierKey) {
98 nextState = {
99 ...currentState,
100 [normalizedModifierKey]: event.type === "keydown",
101 };
102 } else {
103 // Dictation paste can poison real events with metaKey=true until a physical Meta press.
104 nextState = {
105 metaKey: currentState.metaKey && event.metaKey,
106 ctrlKey: currentState.ctrlKey && event.ctrlKey,
107 altKey: currentState.altKey && event.altKey,
108 shiftKey: currentState.shiftKey && event.shiftKey,
109 };
110 }
111
112 return areShortcutModifierStatesEqual(currentState, nextState) ? currentState : nextState;
113}