2026-07-30 11:10:59 +08:00
|
|
|
import React from 'react';
|
2026-07-30 12:20:57 +08:00
|
|
|
import { shortcutRegistry, type ShortcutActionId, type ShortcutHandler } from '@/lib/shortcuts';
|
2026-07-30 11:10:59 +08:00
|
|
|
|
|
|
|
|
export function useKeybind(actionId: ShortcutActionId, handler: ShortcutHandler): void {
|
|
|
|
|
const handlerRef = React.useRef(handler);
|
|
|
|
|
handlerRef.current = handler;
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => shortcutRegistry.register(actionId, (event) => handlerRef.current(event)), [actionId]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 01:24:00 +08:00
|
|
|
export type ShortcutBindings<
|
|
|
|
|
Bindings extends Partial<Record<ShortcutActionId, ShortcutHandler>>,
|
|
|
|
|
> = Bindings & Record<Exclude<keyof Bindings, ShortcutActionId>, never>;
|
|
|
|
|
|
|
|
|
|
export function useKeybinds<
|
|
|
|
|
const Bindings extends Partial<Record<ShortcutActionId, ShortcutHandler>>,
|
|
|
|
|
>(bindings: ShortcutBindings<Bindings>): void {
|
2026-07-30 11:10:59 +08:00
|
|
|
const handlersRef = React.useRef(bindings);
|
|
|
|
|
handlersRef.current = bindings;
|
|
|
|
|
const actionIdsKey = Object.keys(bindings).sort().join('\0');
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
const actionIds = (actionIdsKey ? actionIdsKey.split('\0') : []) as ShortcutActionId[];
|
|
|
|
|
const unregister = actionIds.map((actionId) => shortcutRegistry.register(actionId, (event) => {
|
|
|
|
|
const handler = handlersRef.current[actionId];
|
|
|
|
|
return handler ? handler(event) : false;
|
|
|
|
|
}));
|
|
|
|
|
return () => unregister.forEach((remove) => remove());
|
|
|
|
|
}, [actionIdsKey]);
|
|
|
|
|
}
|