webhotkeys 1.0.0

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.
@@ -0,0 +1,9 @@
1
+ // ESM entry point for the npm package. The library itself is WebHotkeys.js - a single classic
2
+ // browser script - so we only re-export its CommonJS bindings under the ESM names here.
3
+ // (Keep in sync with the module.exports block at the bottom of WebHotkeys.js.)
4
+ import WebHotkeysDefault from "./WebHotkeys.js"
5
+
6
+ export const WebHotkeys = WebHotkeysDefault
7
+ export const Hotkey = WebHotkeysDefault.Hotkey
8
+ export const HotkeyGroup = WebHotkeysDefault.HotkeyGroup
9
+ export default WebHotkeysDefault
package/src/index.d.ts ADDED
@@ -0,0 +1,222 @@
1
+ // Type definitions for webhotkeys
2
+ // Project: https://github.com/CZ-NIC/webhotkeys
3
+
4
+ /** A key combination. Either an `event.code` ("Digit1"), an `event.key` ("1"), a combination
5
+ * with modifiers ("Ctrl+Digit1", "Mod+s") or a space separated sequence ("g i", "Shift Shift"). */
6
+ export type Key = string
7
+
8
+ /** An element, its selector or a callback. Returning `false` from the callback lets the event fall through. */
9
+ export type Action = HTMLElement | string | ((this: WebHotkeys, event: KeyboardEvent) => unknown)
10
+
11
+ /** Where a hotkey is allowed to fire. A selector / element the focus must be within, or a predicate. */
12
+ export type Scope = HTMLElement | string | ((this: WebHotkeys, active: HTMLElement | null, event: KeyboardEvent) => boolean)
13
+
14
+ /** Like a KeyboardEvent, but not guaranteed to carry every property. Ex: `{code: "Digit1", altKey: true}` */
15
+ export type KeyEvent = Partial<KeyboardEvent>
16
+
17
+ export interface WebHotkeysOptions {
18
+ /** Append the shortcut text to the element `title`, to its text (its label for a form element), or nowhere. */
19
+ hint?: "title" | "text" | false
20
+ /** Combination that opens the help. `null` grabs nothing. */
21
+ helpKey?: Key | null
22
+ /** How the help is displayed. The dialog falls back to an alert when the DOM is unavailable. */
23
+ help?: "dialog" | "alert"
24
+ /** Combination toggling the visual badges over every element having a hotkey. `null` grabs nothing. */
25
+ hintKey?: Key | null
26
+ /** Convert the `[accesskey]` elements to hotkeys. */
27
+ replaceAccesskeys?: boolean
28
+ /** Watch the DOM and un/grab the hotkeys as the elements dis/appear. */
29
+ observe?: boolean
30
+ /** Called on a linked element's hotkey toggle. `this` is the hotkey. */
31
+ onToggle?: ((this: Hotkey, element: HTMLElement, enabled: boolean) => void) | null
32
+ /** Called right after a hotkey fired. */
33
+ onTrigger?: ((this: WebHotkeys, hotkey: Hotkey, event: KeyboardEvent) => void) | null
34
+ /** Called when a keystroke matched no hotkey. */
35
+ onMiss?: ((this: WebHotkeys, event: KeyboardEvent) => void) | null
36
+ /** Attribute name linking the DOM elements to the shortcuts. */
37
+ attribute?: string
38
+ /** Attribute name linking the DOM elements to the groups. */
39
+ groupAttribute?: string
40
+ /** Attribute name overriding what happens with the element: "click" | "focus" | "toggle" | "none".
41
+ * Without it a text field is focused, a button/checkbox/radio clicked and a DETAILS toggled. */
42
+ actionAttribute?: string
43
+ /** Selector or predicate. While it matches the focused element, no hotkey fires at all. */
44
+ ignore?: string | ((this: WebHotkeys, active: HTMLElement | null, event: KeyboardEvent) => boolean) | null
45
+ /** Milliseconds a key sequence ("g i") may be spread over. Also the pause committing a recorded sequence. */
46
+ sequenceTimeout?: number
47
+ /** Let the user rebind the combinations right in the F1 dialog. A string is the localStorage key
48
+ * (true means "webhotkeys.remap"), false turns the editing off. A foreign value found under the
49
+ * key is never overwritten - the layout is then not persisted at all. */
50
+ remap?: boolean | string
51
+ /** Called with the whole remapping whenever the user changes a combination. Ex: store it on the server. */
52
+ onRemap?: ((this: WebHotkeys, map: Record<Key, Key>) => void) | null
53
+ /** Render the combinations the Apple way (⌘⌥⇧⌃) and resolve "Mod" to Meta. Null autodetects. */
54
+ mac?: boolean | null
55
+ /** Warn in the console when a newly grabbed hotkey shadows a scope-less one. */
56
+ warnConflicts?: boolean
57
+ }
58
+
59
+ export declare class Hotkey {
60
+ action: HTMLElement | Function
61
+ hint: string
62
+ scope: Scope | null
63
+ /** The whole sequence; a plain hotkey is a sequence of one combination. */
64
+ sequence: KeyEvent[]
65
+ /** The combination that actually triggers the hotkey (the last one of the sequence). */
66
+ event: KeyEvent
67
+ element: HTMLElement | null
68
+ enabled: boolean
69
+ /** Fires even while the user is typing. Set via `grab(..., {inInput: true})`, or directly. */
70
+ allowInput: boolean
71
+ /** The combination the hotkey was grabbed with; survives `rebind`. */
72
+ defaultCombination: Key
73
+
74
+ /** Text representation of the combination, ex: "Ctrl+k" (or "⌘k" on a Mac). */
75
+ readonly clue: string
76
+ /** Platform independent definition string, ex: "Ctrl+k". */
77
+ readonly combination: Key
78
+ /** "Ctrl+k: Hint text" */
79
+ readonly text: string
80
+ /** Move the hotkey to another combination (user remapping). No argument = back to the default. */
81
+ rebind(combination?: Key | null): this
82
+ enable(): this
83
+ disable(): this
84
+ toggle(enable?: boolean | null): this
85
+ /** Forget the hotkey for good (groups, help, the linked element). */
86
+ remove(): this
87
+
88
+ static comboText(event: KeyEvent, mac?: boolean): string
89
+ static fromEvent(event: KeyEvent): Key
90
+ static match(spec: KeyEvent, event: KeyEvent): boolean
91
+ }
92
+
93
+ export declare class HotkeyGroup extends Array<Hotkey> {
94
+ enable(): this
95
+ disable(): this
96
+ toggle(enable?: boolean | null): this
97
+ remove(): this
98
+ }
99
+
100
+ export interface ListOptions {
101
+ /** Selector describing the currently selected item. @default ":focus" */
102
+ current?: string
103
+ /** Injected via the internal `_insertCss` as `query + current + css`. */
104
+ css?: string | null
105
+ /** Jump from the last item to the first one and back. @default false */
106
+ wrap?: boolean
107
+ /** Scroll the newly selected item into the view. @default true */
108
+ scroll?: boolean
109
+ /** Called with (newEl, oldEl) before the change; returning `false` cancels it. */
110
+ onChange?: ((newEl: HTMLElement, oldEl: HTMLElement | null) => boolean | void) | null
111
+ /** Called with (newEl) once the change has happened. */
112
+ onChanged?: ((newEl: HTMLElement) => void) | null
113
+ /** Grab ArrowUp/ArrowDown for this listing. @default false */
114
+ upDown?: boolean
115
+ /** Grab Home/End for this listing. @default false */
116
+ homeEnd?: boolean
117
+ /** `true` uses an 800 ms timeout, a number sets a custom one (ms). @default false */
118
+ typeahead?: boolean | number
119
+ /** Restricts the upDown/homeEnd grabs - give each list a distinct scope so several lists on
120
+ * the same page don't fight over the arrow keys. */
121
+ scope?: Scope | null
122
+ }
123
+
124
+ export declare class HotkeyList {
125
+ selectByPrefix(prefix: string): boolean
126
+ /** The currently selected element (even if it changed since the last `go*` call). */
127
+ current: HTMLElement | null
128
+ goNext(steps?: number): boolean
129
+ goPrev(steps?: number): boolean
130
+ goFirst(): boolean
131
+ goLast(): boolean
132
+ go(forward?: boolean, steps?: number): boolean
133
+ destroy(): this
134
+ }
135
+
136
+ export interface GridOptions {
137
+ /** Selector describing the currently selected cell. @default ":focus" */
138
+ current?: string
139
+ /** Wrap around row/column edges instead of stopping there. @default false */
140
+ wrap?: boolean
141
+ /** Scroll the newly selected cell into the view. @default true */
142
+ scroll?: boolean
143
+ /** Called with (newEl, oldEl) before the change; returning `false` cancels it. */
144
+ onChange?: ((newEl: HTMLElement, oldEl: HTMLElement | null) => boolean | void) | null
145
+ /** Called with (newEl) once the change has happened. */
146
+ onChanged?: ((newEl: HTMLElement) => void) | null
147
+ /** Restricts the Up/Down/Left/Right grabs - give each grid a distinct scope so several
148
+ * tables on the same page don't fight over the arrow keys. */
149
+ scope?: Scope | null
150
+ }
151
+
152
+ export declare class HotkeyGrid {
153
+ /** Move by `rows` down and `cols` right (negative values go up / left); moving between rows
154
+ * keeps the column, clamped to the target row's width. Ex: `go(1, -1)` = down and left. */
155
+ go(rows?: number, cols?: number): boolean
156
+ /** Detach the Up/Down/Left/Right grabs. */
157
+ destroy(): this
158
+ }
159
+
160
+ export declare class WebHotkeys {
161
+ constructor(options?: WebHotkeysOptions)
162
+ options: WebHotkeysOptions
163
+
164
+ setOptions(options: WebHotkeysOptions): this
165
+ /** Detach the listener and the observer, forget every hotkey. */
166
+ destroy(): this
167
+
168
+ grab(hotkey: Key, hint: string, action: Action, scope?: Scope | { scope?: Scope | null, inInput?: boolean, group?: string | null } | null): Hotkey
169
+ group(name: string, definitions?: Array<[Key, string, Action, (Scope | { scope?: Scope | null, inInput?: boolean, group?: string | null } | null)?]>): HotkeyGroup
170
+ /** Every call returns its own independent instance, so several lists can coexist on one page. */
171
+ list(query?: string, options?: ListOptions): HotkeyList
172
+ /** 2D keyboard navigation over a table (rows × cells within a row). Every call returns its own
173
+ * independent instance, so several tables can coexist on one page. */
174
+ grid(rowQuery: string, cellQuery: string, options?: GridOptions): HotkeyGrid
175
+
176
+ /** Manually fire a combination or a whole sequence. */
177
+ simulate(hotkey: Key | KeyEvent): boolean | undefined
178
+
179
+ getHotkeys(enabledOnly?: boolean): Hotkey[]
180
+ getGroups(): { name: string, hotkeys: Hotkey[] }[]
181
+ getText(): string
182
+ getConflicts(): { combination: Key, hotkeys: Hotkey[] }[]
183
+
184
+ /** Wait for the next keystroke, hand over its definition string. Returns a canceller.
185
+ * With `sequence`, the keystrokes are collected ("g i") until the user pauses for `sequenceTimeout`. */
186
+ record(
187
+ callback: (this: WebHotkeys, combination: Key, event: KeyboardEvent) => void,
188
+ options?: {
189
+ sequence?: boolean,
190
+ onProgress?: ((this: WebHotkeys, partial: Key, event: KeyboardEvent) => void) | null
191
+ }
192
+ ): () => void
193
+ /** Get the user changes: the default combination -> the current one. */
194
+ remapping(): Record<Key, Key>
195
+ /** Apply the full remapping state; a hotkey missing from the map returns to its default combination.
196
+ * `null`/`{}` clears every remapping. An entry that is not a key combination is dropped - the
197
+ * map may come from the outside. */
198
+ remapping(map: Record<Key, Key> | null): this
199
+
200
+ /** Toggle the help dialog. `null` toggles, true/false forces the state. */
201
+ toggleHelp(show?: boolean | null): this
202
+ /** Toggle the visual hint badges over the elements having a hotkey. `null` toggles, true/false forces the state. */
203
+ toggleHints(show?: boolean | null): this
204
+
205
+ /** The focused element, piercing the shadow DOM. */
206
+ activeElement(): HTMLElement | null
207
+ isMac(): boolean
208
+ getSheet(): CSSStyleSheet
209
+ }
210
+
211
+ export default WebHotkeys
212
+
213
+ declare global {
214
+ interface Window {
215
+ /** Set when the script is loaded with the `data-register` attribute. */
216
+ webHotkeys?: WebHotkeys
217
+ /** Set by the classic `<script>` path, so `new window.WebHotkeys()` works without `data-register`. */
218
+ WebHotkeys?: typeof WebHotkeys
219
+ Hotkey?: typeof Hotkey
220
+ HotkeyGroup?: typeof HotkeyGroup
221
+ }
222
+ }