Spaces:
Sleeping
Sleeping
Download ui/src/lib/useKeybinds.ts from charlesyapai/hcc-meat-coder: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/charlesyapai/hcc-meat-coder/resolve/main/ui/src/lib/useKeybinds.ts
- Command line
-
hf download hf://spaces/charlesyapai/hcc-meat-coder/ui/src/lib/useKeybinds.ts
-
curl -L -o useKeybinds.ts https://huggingface.co/spaces/charlesyapai/hcc-meat-coder/resolve/main/ui/src/lib/useKeybinds.ts
1.35 kB
| import { useEffect } from "react"; | |
| interface Opts { | |
| onCode: () => void; | |
| onNext: () => void; | |
| onPrev: () => void; | |
| enabled?: boolean; | |
| } | |
| /** | |
| * Global keybinds: | |
| * Cmd/Ctrl + Enter — Code the current note | |
| * j / ArrowDown — Select next HCC candidate | |
| * k / ArrowUp — Select previous HCC candidate | |
| * | |
| * Ignored while the user is typing into an editable control so it doesn't | |
| * fight with the textarea. | |
| */ | |
| export function useKeybinds({ onCode, onNext, onPrev, enabled = true }: Opts) { | |
| useEffect(() => { | |
| if (!enabled) return; | |
| function handler(e: KeyboardEvent) { | |
| const target = e.target as HTMLElement | null; | |
| const inEditable = | |
| !!target && | |
| (target.tagName === "INPUT" || | |
| target.tagName === "TEXTAREA" || | |
| target.isContentEditable); | |
| if ((e.metaKey || e.ctrlKey) && e.key === "Enter") { | |
| e.preventDefault(); | |
| onCode(); | |
| return; | |
| } | |
| if (inEditable) return; | |
| if (e.key === "j" || e.key === "ArrowDown") { | |
| e.preventDefault(); | |
| onNext(); | |
| } else if (e.key === "k" || e.key === "ArrowUp") { | |
| e.preventDefault(); | |
| onPrev(); | |
| } | |
| } | |
| window.addEventListener("keydown", handler); | |
| return () => window.removeEventListener("keydown", handler); | |
| }, [onCode, onNext, onPrev, enabled]); | |
| } | |