import { useEffect } from 'react'; import hotkeys from 'hotkeys-js'; export function useGlobalHotkeys(opts: { onNewDoc: () => void; onSave: () => void; onFocusSearch: () => void; onToggleOutline: () => void; onCheat: () => void; onSettings: () => void }) { useEffect(() => { hotkeys('ctrl+n', e => { e.preventDefault(); opts.onNewDoc(); }); hotkeys('ctrl+s', e => { e.preventDefault(); opts.onSave(); }); hotkeys('ctrl+shift+f', e => { e.preventDefault(); opts.onFocusSearch(); }); hotkeys('ctrl+shift+l', e => { e.preventDefault(); opts.onToggleOutline(); }); hotkeys('?', e => { if (document.activeElement?.tagName !== 'INPUT' && document.activeElement?.tagName !== 'TEXTAREA') { e.preventDefault(); opts.onCheat(); } }); return () => { hotkeys.unbind('ctrl+n'); hotkeys.unbind('ctrl+s'); hotkeys.unbind('ctrl+shift+f'); hotkeys.unbind('ctrl+shift+l'); hotkeys.unbind('?'); }; }, [opts.onNewDoc, opts.onSave, opts.onFocusSearch, opts.onToggleOutline, opts.onCheat, opts.onSettings]); }