@kitlangton/ghui 0.1.22 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +0,0 @@
1
- import { useBindings } from "@opentui/keymap/react"
2
- import { useRef } from "react"
3
-
4
- export type ScopedBindingAction = (() => void) | string
5
-
6
- export interface ScopedBindingsOptions {
7
- readonly when: boolean
8
- readonly bindings: Readonly<Record<string, ScopedBindingAction>>
9
- }
10
-
11
- /**
12
- * Wraps `@opentui/keymap/react`'s `useBindings` so callers don't have to write
13
- * the ref dance themselves. The layer registers exactly once (deps=[]); the
14
- * `when` flag and the action callbacks read latest values via refs, so the
15
- * binding-shape is captured at first render but closures stay fresh.
16
- *
17
- * Don't change which keys you bind across renders — only what they do.
18
- */
19
- export const useScopedBindings = ({ when, bindings }: ScopedBindingsOptions): void => {
20
- const activeRef = useRef(false)
21
- activeRef.current = when
22
-
23
- const actionsRef = useRef(bindings)
24
- actionsRef.current = bindings
25
-
26
- useBindings(() => ({
27
- enabled: () => activeRef.current,
28
- bindings: Object.entries(bindings).map(([key, action]) => ({
29
- key,
30
- cmd: typeof action === "string" ? action : () => {
31
- const current = actionsRef.current[key]
32
- if (typeof current === "function") current()
33
- },
34
- })),
35
- }), [])
36
- }
37
-
38
- /**
39
- * Vim-style scroll bindings: j/k/up/down for line scroll, ctrl-u/d/v + pageup/down
40
- * for half-page scroll. When `scrollTo` is given, also adds home/end and gg/G to
41
- * jump to start/end.
42
- */
43
- export const scrollBindings = (
44
- scrollBy: (delta: number) => void,
45
- halfPage: number,
46
- scrollTo?: (y: number) => void,
47
- ): Record<string, ScopedBindingAction> => {
48
- const bindings: Record<string, ScopedBindingAction> = {
49
- up: () => scrollBy(-1),
50
- k: () => scrollBy(-1),
51
- down: () => scrollBy(1),
52
- j: () => scrollBy(1),
53
- pageup: () => scrollBy(-halfPage),
54
- pagedown: () => scrollBy(halfPage),
55
- "ctrl+u": () => scrollBy(-halfPage),
56
- "ctrl+d": () => scrollBy(halfPage),
57
- "ctrl+v": () => scrollBy(halfPage),
58
- }
59
- if (scrollTo) {
60
- bindings.home = () => scrollTo(0)
61
- bindings.end = () => scrollTo(Number.MAX_SAFE_INTEGER)
62
- bindings["g g"] = () => scrollTo(0)
63
- bindings["shift+g"] = () => scrollTo(Number.MAX_SAFE_INTEGER)
64
- }
65
- return bindings
66
- }