@weasel-js/headless 1.13.0 → 1.14.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.
- package/README.md +46 -2
- package/dist/index.d.ts +242 -1
- package/dist/index.js +242 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
# @weasel-js/headless
|
|
2
2
|
|
|
3
3
|
Component behavior with no substrate: what a DOM component and a canvas-drawn widget both run,
|
|
4
|
-
each drawing it its own way. Today that is a list's roving highlight and typeahead,
|
|
5
|
-
focus as one contract. No React, no DOM.
|
|
4
|
+
each drawing it its own way. Today that is a list's roving highlight and typeahead, keyboard
|
|
5
|
+
focus as one contract, and how a text field's edit ends. No React, no DOM.
|
|
6
|
+
|
|
7
|
+
each drawing it its own way. Today that is a list's roving highlight and typeahead, a tree's
|
|
8
|
+
expand and collapse keys, and keyboard focus as one contract. No React, no DOM.
|
|
6
9
|
|
|
7
10
|
Part of [weasel](https://github.com/orochi235/weasel), a domain-agnostic 2D
|
|
8
11
|
scene-graph canvas kit for React. See the
|
|
@@ -41,6 +44,17 @@ A list's shape says which items the highlight can land on (`canStop`), which arr
|
|
|
41
44
|
right-to-left layout (`dir`), and whether it wraps at the ends (`wrap`, on by default).
|
|
42
45
|
`@weasel-js/menu` runs every menu level on these.
|
|
43
46
|
|
|
47
|
+
## Trees and rows
|
|
48
|
+
|
|
49
|
+
`treeKey` is a tree's Right and Left, the WAI-ARIA way: Right opens a closed branch or steps to
|
|
50
|
+
its first child, Left closes an open branch or steps to the parent. Its `TreeShape` says which
|
|
51
|
+
items are branches, which are open, and which is whose child.
|
|
52
|
+
|
|
53
|
+
`rowKey` is every key a list of rows answers, in one place: Cmd/Ctrl+A, the arrows with Home and
|
|
54
|
+
End (Shift extends), a tree's Right and Left, Enter and Space, and typeahead. It returns what the
|
|
55
|
+
key asks for (`selectAll`, `move`, `toggle`, `activate`, `type`) and the caller carries it out.
|
|
56
|
+
`@weasel-js/ui`'s `useRowNavigation` runs on it.
|
|
57
|
+
|
|
44
58
|
## Focus
|
|
45
59
|
|
|
46
60
|
`FocusScope<T>` is keyboard focus over a set of targets, whatever draws them: which one is
|
|
@@ -49,6 +63,36 @@ focused, whether its ring shows, `focus`, `moveFocus` through a `tabOrder`, and
|
|
|
49
63
|
`domFocusScope(root)` is one over the elements under `root`, so code that moves focus is
|
|
50
64
|
written once for both. `tabSort` and `tabStep` are the order and the step both use.
|
|
51
65
|
|
|
66
|
+
## Steps and sliders
|
|
67
|
+
|
|
68
|
+
`arrowSteps`, `arrowMove` and `rangeKey` are what an arrow key asks for: one step, or `COARSE_STEPS`
|
|
69
|
+
(ten) with Shift or a Page key, and Home and End for a range's ends. `sliderKey` turns one key into
|
|
70
|
+
a thumb's next value, by step or by whole stops, across an endless end and inside its neighbors;
|
|
71
|
+
`@weasel-js/ui`'s `Slider` runs on it.
|
|
72
|
+
|
|
73
|
+
## Text fields
|
|
74
|
+
|
|
75
|
+
`editEnd(cause, shape)` says how a text field's edit ends: Enter and blur commit, Escape cancels.
|
|
76
|
+
A field's shape can make Escape commit too, and a `multiline` one leaves Shift+Enter to type a
|
|
77
|
+
line break. Caret movement stays with each field: a DOM field gets it from the browser.
|
|
78
|
+
|
|
79
|
+
## Reorder
|
|
80
|
+
|
|
81
|
+
Moving items within a list, whether it is drawn in the DOM or on a canvas. A destination is a gap counted before
|
|
82
|
+
anything is taken out: 0 before the first item, `length` after the last. `moveItems(items, at, to)` moves the items at
|
|
83
|
+
indices `at` into gap `to`; `isNoopDrop` says whether that leaves them where they are; `nudgeGap` is the gap a
|
|
84
|
+
one-place move takes them to, and `nudgeOf` reads the Alt+arrow chord that asks for one. A pointer finds its gap by
|
|
85
|
+
`gapByMidpoint`, for items side by side, each split at its middle, or by `nearestGap`, for gaps drawn at places of
|
|
86
|
+
their own.
|
|
87
|
+
|
|
88
|
+
## Hold-to-repeat
|
|
89
|
+
|
|
90
|
+
`createStepRepeat(step, spent, opts)` is a held stepper: `start(key)` steps once, then after
|
|
91
|
+
`REPEAT_DELAY_MS` once every `REPEAT_EVERY_MS` until `stop()`, or until `spent(key)` says the value
|
|
92
|
+
has nowhere left to go. Time comes from a `Scheduler` (`now`, and `set` returning a cancel), by
|
|
93
|
+
default `timerScheduler`; pass your own to drive it from another clock or a test. `@weasel-js/ui`'s
|
|
94
|
+
number field steppers and `@weasel-js/hud`'s menu number chips both hold through it.
|
|
95
|
+
|
|
52
96
|
## License
|
|
53
97
|
|
|
54
98
|
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
export { COARSE_STEPS } from '@weasel-js/quantity';
|
|
2
|
+
|
|
3
|
+
/** How a list that folds is read: which items are branches, which are open, and which item is whose child. */
|
|
4
|
+
interface TreeShape<T> {
|
|
5
|
+
/** Whether `item` can open, its children shown or not. */
|
|
6
|
+
isBranch(item: T): boolean;
|
|
7
|
+
isOpen(item: T): boolean;
|
|
8
|
+
/** Whether `child` is one of `parent`'s own children. */
|
|
9
|
+
isChildOf(child: T, parent: T): boolean;
|
|
10
|
+
/** Whether a branch can open or close now. A disabled one can't. Default: every branch. */
|
|
11
|
+
canToggle?(item: T): boolean;
|
|
12
|
+
/** The inline direction the rows read in. Under `'rtl'` Left opens and Right closes. Default `'ltr'`. */
|
|
13
|
+
dir?: 'ltr' | 'rtl';
|
|
14
|
+
}
|
|
15
|
+
/** What Right or Left does on one item of a tree: open or close it, move the highlight, or nothing (`handled` false).
|
|
16
|
+
* `at` null is a key the tree takes without moving. */
|
|
17
|
+
type TreeKeyResult = {
|
|
18
|
+
handled: false;
|
|
19
|
+
} | {
|
|
20
|
+
handled: true;
|
|
21
|
+
toggle: number;
|
|
22
|
+
} | {
|
|
23
|
+
handled: true;
|
|
24
|
+
at: number | null;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Right and Left on the item at `at`, the WAI-ARIA tree way: Right opens a closed branch, else steps to its first
|
|
28
|
+
* child; Left closes an open branch, else steps to the parent. Mirrored under `dir: 'rtl'`. `items` are the rows
|
|
29
|
+
* shown, in drawn order.
|
|
30
|
+
*/
|
|
31
|
+
declare function treeKey<T>(items: readonly T[], at: number, key: string, shape: TreeShape<T>): TreeKeyResult;
|
|
32
|
+
|
|
1
33
|
/** How a list's items are read: which ones the highlight can land on, and which way its arrows run. */
|
|
2
34
|
interface ListShape<T> {
|
|
3
35
|
/** Whether the highlight can land on `item`. A separator or a heading can't. Default: every item. */
|
|
@@ -23,6 +55,55 @@ type RovingResult = {
|
|
|
23
55
|
};
|
|
24
56
|
/** Move the highlight `at` by one key: the arrows along the list's orientation, Home and End. */
|
|
25
57
|
declare function rovingKey<T>(items: readonly T[], at: number | null, key: string, shape?: ListShape<T>): RovingResult;
|
|
58
|
+
/** A key as {@link rowKey} reads it: the key and the modifiers held. */
|
|
59
|
+
interface KeyPress {
|
|
60
|
+
key: string;
|
|
61
|
+
shiftKey?: boolean;
|
|
62
|
+
ctrlKey?: boolean;
|
|
63
|
+
metaKey?: boolean;
|
|
64
|
+
altKey?: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** A list of rows, and which of the keys past the arrows its caller answers. */
|
|
67
|
+
interface RowKeysShape<T> extends ListShape<T> {
|
|
68
|
+
/** Set for a list that folds: Right and Left open, close, and walk it ({@link treeKey}). */
|
|
69
|
+
tree?: TreeShape<T>;
|
|
70
|
+
/** Cmd/Ctrl+A selects every row. */
|
|
71
|
+
selectAll?: boolean;
|
|
72
|
+
/** Enter and Space activate a row. */
|
|
73
|
+
activate?: boolean;
|
|
74
|
+
/** Typing a character searches the rows' text. */
|
|
75
|
+
typeahead?: boolean;
|
|
76
|
+
}
|
|
77
|
+
/** What one key on a row asks for, for the caller to carry out. */
|
|
78
|
+
type RowKeyAction =
|
|
79
|
+
/** Select every row. */
|
|
80
|
+
{
|
|
81
|
+
kind: 'selectAll';
|
|
82
|
+
}
|
|
83
|
+
/** Move to row `at`, extending a range from the anchor when `extend`. `at` null takes the key without moving. */
|
|
84
|
+
| {
|
|
85
|
+
kind: 'move';
|
|
86
|
+
at: number | null;
|
|
87
|
+
extend: boolean;
|
|
88
|
+
}
|
|
89
|
+
/** Open or close the branch at `at`. */
|
|
90
|
+
| {
|
|
91
|
+
kind: 'toggle';
|
|
92
|
+
at: number;
|
|
93
|
+
} | {
|
|
94
|
+
kind: 'activate';
|
|
95
|
+
}
|
|
96
|
+
/** Hand the key to typeahead. `held` when the key is the list's whether or not the search finds a row. */
|
|
97
|
+
| {
|
|
98
|
+
kind: 'type';
|
|
99
|
+
held: boolean;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* What a key on row `at` of a list of rows does, in the order the keys are tried: Cmd/Ctrl+A, the arrows with Home
|
|
103
|
+
* and End (Shift to extend), a tree's Right and Left, a space typed mid-search, Enter or Space to activate, then any
|
|
104
|
+
* other character to search. `searching` says a typeahead search is under way. Null for a key that is not the list's.
|
|
105
|
+
*/
|
|
106
|
+
declare function rowKey<T>(items: readonly T[], at: number, press: KeyPress, shape?: RowKeysShape<T>, searching?: boolean): RowKeyAction | null;
|
|
26
107
|
|
|
27
108
|
/** How typeahead reads a list: its shape, and the text each item is found by. */
|
|
28
109
|
interface TypeaheadShape<T> extends Pick<ListShape<T>, 'canStop'> {
|
|
@@ -82,4 +163,164 @@ declare function tabSort<T>(targets: readonly T[], declared: (target: T) => numb
|
|
|
82
163
|
* it sits, which `isBefore` says; without it, such a `from` steps as nothing does. */
|
|
83
164
|
declare function tabStep<T>(order: readonly T[], from: T | null, direction: 'next' | 'prev', isBefore?: (a: T, b: T) => boolean): T | null;
|
|
84
165
|
|
|
85
|
-
|
|
166
|
+
/** Steps an arrow key asks for along one axis: one, or {@link COARSE_STEPS} with Shift, positive
|
|
167
|
+
* toward ArrowRight and ArrowUp. Under `'rtl'` ArrowLeft and ArrowRight swap, as a native range
|
|
168
|
+
* input's do. `null` for any other key. A caller multiplies by its own step. */
|
|
169
|
+
declare function arrowSteps(key: string, shift: boolean, dir?: 'ltr' | 'rtl'): number | null;
|
|
170
|
+
/** Steps an arrow key asks for on a plane, in screen axes: `dy` is positive toward ArrowDown.
|
|
171
|
+
* One step, or {@link COARSE_STEPS} with Shift; `null` for any other key. */
|
|
172
|
+
declare function arrowMove(key: string, shift: boolean): {
|
|
173
|
+
dx: number;
|
|
174
|
+
dy: number;
|
|
175
|
+
} | null;
|
|
176
|
+
/** What a key does along a range: {@link arrowSteps}, the Page keys ({@link COARSE_STEPS} steps),
|
|
177
|
+
* or Home and End (an end of the range). `null` for any other key. */
|
|
178
|
+
declare function rangeKey(key: string, shift: boolean, dir?: 'ltr' | 'rtl'): number | 'home' | 'end' | null;
|
|
179
|
+
|
|
180
|
+
/** A value on a slider's track the thumb can come to rest on, with whatever label draws under it. */
|
|
181
|
+
interface SliderStop<L = unknown> {
|
|
182
|
+
value: number;
|
|
183
|
+
label?: L;
|
|
184
|
+
}
|
|
185
|
+
/** The stops that are actually reachable: inside the range, deduped, ascending.
|
|
186
|
+
* A duplicate keeps the first label given for its value. */
|
|
187
|
+
declare function usableStops<L>(stops: readonly (number | SliderStop<L>)[] | undefined, min: number, max: number): SliderStop<L>[];
|
|
188
|
+
/** The stop nearest `v` along the track, or `v` itself when none lies within
|
|
189
|
+
* `tolerance` (a track fraction). Measured on the track rather than in value
|
|
190
|
+
* units so evenly spaced stops attract over the same distance. */
|
|
191
|
+
declare function attract(v: number, stops: readonly number[], tolerance: number, toFraction: (v: number) => number): number;
|
|
192
|
+
/** Keep thumb `index` strictly between its neighbors: a step apart, or a thousandth of the span without one. */
|
|
193
|
+
declare function clampOrdered(v: number, thumbs: readonly {
|
|
194
|
+
value: number;
|
|
195
|
+
}[], index: number, min: number, max: number, step: number | undefined): number;
|
|
196
|
+
/** What a key press on one thumb of a slider reads. */
|
|
197
|
+
interface SliderKeyShape {
|
|
198
|
+
thumbs: readonly {
|
|
199
|
+
value: number;
|
|
200
|
+
}[];
|
|
201
|
+
/** The thumb the key went to. */
|
|
202
|
+
index: number;
|
|
203
|
+
/** The ends of the track. */
|
|
204
|
+
min: number;
|
|
205
|
+
max: number;
|
|
206
|
+
step?: number;
|
|
207
|
+
/** Usable stops, ascending. With any, a keystroke moves by whole stops rather than by value. */
|
|
208
|
+
stops?: readonly number[];
|
|
209
|
+
/** Keep the thumb between its neighbors. */
|
|
210
|
+
ordered?: boolean;
|
|
211
|
+
/** The finite range, where the track runs past it to an endless end's stop. Default the track. */
|
|
212
|
+
range?: readonly [number, number];
|
|
213
|
+
/** Under `'rtl'` the horizontal arrows are mirrored, as the values run from the right end. */
|
|
214
|
+
dir?: 'ltr' | 'rtl';
|
|
215
|
+
/** This thumb's own limits inside the track. Read only for a key the slider answers to. */
|
|
216
|
+
bounds?(): readonly [number, number];
|
|
217
|
+
/** Where the thumb lands `steps` presses away, for a track whose values aren't what the user counts in. */
|
|
218
|
+
keyStep?(steps: number): number;
|
|
219
|
+
}
|
|
220
|
+
/** The value one key moves thumb `index` to: arrows by a step (ten with Shift), the Page keys by ten,
|
|
221
|
+
* Home and End to the thumb's ends. `null` for a key the slider does not answer to. */
|
|
222
|
+
declare function sliderKey(key: string, shift: boolean, shape: SliderKeyShape): number | null;
|
|
223
|
+
|
|
224
|
+
/** How a text field's edit ends: `'commit'` writes what was typed, `'cancel'` drops it and puts back what was there. */
|
|
225
|
+
type EditEnd = 'commit' | 'cancel';
|
|
226
|
+
/** What a field varies in how its edit ends: what Escape does (`'cancel'` by default), and whether the field holds
|
|
227
|
+
* more than one line, which leaves Shift+Enter to type a line break rather than commit. */
|
|
228
|
+
interface EditEndShape {
|
|
229
|
+
escape?: EditEnd;
|
|
230
|
+
multiline?: boolean;
|
|
231
|
+
}
|
|
232
|
+
/** What may end an edit: a key, with whether Shift is held, or the field losing focus. A key {@link imeKey} claims is
|
|
233
|
+
* the caller's to hold back. */
|
|
234
|
+
type EditCause = 'blur' | {
|
|
235
|
+
key: string;
|
|
236
|
+
shift?: boolean;
|
|
237
|
+
};
|
|
238
|
+
/** How `cause` ends a text field's edit, or null when it leaves the edit open. Enter and blur commit, Escape cancels
|
|
239
|
+
* unless `shape` says otherwise. */
|
|
240
|
+
declare function editEnd(cause: EditCause, shape?: EditEndShape): EditEnd | null;
|
|
241
|
+
/** A keydown as the platform reports it, enough to tell whether an input method owns it. */
|
|
242
|
+
interface KeySource {
|
|
243
|
+
key: string;
|
|
244
|
+
isComposing?: boolean;
|
|
245
|
+
keyCode?: number;
|
|
246
|
+
}
|
|
247
|
+
/** Whether a keydown belongs to an input method, which a field must leave alone: Enter there confirms a candidate and
|
|
248
|
+
* Escape dismisses them. Safari sends the Enter that ends a composition with `isComposing` already false, but with
|
|
249
|
+
* `keyCode` 229. */
|
|
250
|
+
declare function imeKey(e: KeySource): boolean;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Moving items within an ordered list. A destination is a gap, counted before anything is taken out: 0 before the
|
|
254
|
+
* first item, `length` after the last, and `i` between `items[i - 1]` and `items[i]` — the gap a UI marks while it
|
|
255
|
+
* carries something. The items moved are named by their indices, so a selection of several moves in one go.
|
|
256
|
+
*/
|
|
257
|
+
/** `items` with the items at indices `at` taken out and put back, in their order, in gap `to`. */
|
|
258
|
+
declare function moveItems<T>(items: readonly T[], at: readonly number[], to: number): T[];
|
|
259
|
+
/** The index the first of the items at `at` holds once {@link moveItems} has put them in gap `to`. */
|
|
260
|
+
declare function movedIndex(at: readonly number[], to: number): number;
|
|
261
|
+
/** Whether putting the items at `at` in gap `to` leaves them where they are: a contiguous run dropped in a gap at
|
|
262
|
+
* either of its ends or inside it. False for no items. */
|
|
263
|
+
declare function isNoopDrop(at: readonly number[], to: number): boolean;
|
|
264
|
+
/** The gap a one-place move sends the items at `at` to: before the item ahead of the first (`by` -1), or after the
|
|
265
|
+
* item past the last (`by` 1). Null where that runs off either end of a list `length` long, or for no items. */
|
|
266
|
+
declare function nudgeGap(at: readonly number[], by: -1 | 1, length: number): number | null;
|
|
267
|
+
/**
|
|
268
|
+
* The gap a point `p` along a line of items falls in, where each item splits at its middle: how many of the
|
|
269
|
+
* middles `mids`, given in item order, `p` has reached. A line whose middles run backward, as a right-to-left row
|
|
270
|
+
* does along x, counts the other way. For items that sit side by side — rows, tabs, chips.
|
|
271
|
+
*/
|
|
272
|
+
declare function gapByMidpoint(mids: readonly number[], p: number): number;
|
|
273
|
+
/** Which of the gaps at positions `gaps` along a line is nearest `p`, as an index into `gaps`; -1 for none. For
|
|
274
|
+
* gaps drawn at places of their own, between items set apart however the layout put them: picking the nearest
|
|
275
|
+
* keeps the gap chosen the one whose mark is drawn closest. */
|
|
276
|
+
declare function nearestGap(gaps: readonly number[], p: number): number;
|
|
277
|
+
/** The keys and modifiers a nudge chord reads off a keystroke. */
|
|
278
|
+
interface NudgeKey {
|
|
279
|
+
key: string;
|
|
280
|
+
altKey: boolean;
|
|
281
|
+
ctrlKey: boolean;
|
|
282
|
+
metaKey: boolean;
|
|
283
|
+
}
|
|
284
|
+
/** Alt with neither Ctrl nor Cmd held: the chord that moves an item instead of moving focus. */
|
|
285
|
+
declare function isNudgeChord(e: NudgeKey): boolean;
|
|
286
|
+
/** Which way the nudge chord moves an item: `-1` for Alt+`back`, `1` for Alt+`on`, `null` for any other key.
|
|
287
|
+
* The keys are Up and Down unless named. */
|
|
288
|
+
declare function nudgeOf(e: NudgeKey, back?: string, on?: string): -1 | 1 | null;
|
|
289
|
+
|
|
290
|
+
/** Clock and timer, injected so tests and hosts control time. */
|
|
291
|
+
interface Scheduler {
|
|
292
|
+
now(): number;
|
|
293
|
+
/** Run `fn` after `ms`; returns a cancel. */
|
|
294
|
+
set(fn: () => void, ms: number): () => void;
|
|
295
|
+
}
|
|
296
|
+
/** The real-time scheduler: `Date.now` and `setTimeout`. */
|
|
297
|
+
declare const timerScheduler: Scheduler;
|
|
298
|
+
|
|
299
|
+
/** How long a held stepper waits before it repeats, in milliseconds. */
|
|
300
|
+
declare const REPEAT_DELAY_MS = 400;
|
|
301
|
+
/** How often a held stepper steps once it repeats, in milliseconds. */
|
|
302
|
+
declare const REPEAT_EVERY_MS = 60;
|
|
303
|
+
/** The timing of a {@link createStepRepeat}. Each is read when it is next used, so a getter can change it mid-hold. */
|
|
304
|
+
interface StepRepeatOptions {
|
|
305
|
+
/** Default {@link REPEAT_DELAY_MS}. */
|
|
306
|
+
delayMs?: number;
|
|
307
|
+
/** Default {@link REPEAT_EVERY_MS}. */
|
|
308
|
+
everyMs?: number;
|
|
309
|
+
/** Default {@link timerScheduler}. */
|
|
310
|
+
scheduler?: Pick<Scheduler, 'set'>;
|
|
311
|
+
}
|
|
312
|
+
/** A held stepper: {@link createStepRepeat}. */
|
|
313
|
+
interface StepRepeat<K> {
|
|
314
|
+
/** Step `key` once now, then after the delay once every interval until {@link stop}. Replaces any hold. */
|
|
315
|
+
start(key: K): void;
|
|
316
|
+
stop(): void;
|
|
317
|
+
/** The key being held, or null. */
|
|
318
|
+
held(): K | null;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Hold-to-repeat stepping, whatever draws the stepper: `step` moves the value by `key`, and `spent` says `key`
|
|
322
|
+
* has nowhere left to go, which ends the hold. Both are called at the moment of each step.
|
|
323
|
+
*/
|
|
324
|
+
declare function createStepRepeat<K>(step: (key: K) => void, spent: (key: K) => boolean, opts?: StepRepeatOptions): StepRepeat<K>;
|
|
325
|
+
|
|
326
|
+
export { EMPTY_TYPEAHEAD, type EditCause, type EditEnd, type EditEndShape, type FocusScope, type FocusScopeOptions, type KeyPress, type KeySource, type ListShape, type NudgeKey, REPEAT_DELAY_MS, REPEAT_EVERY_MS, type RovingResult, type RowKeyAction, type RowKeysShape, type Scheduler, type SliderKeyShape, type SliderStop, type StepRepeat, type StepRepeatOptions, TYPEAHEAD_RESET_MS, type TreeKeyResult, type TreeShape, type TypeaheadBuffer, type TypeaheadShape, arrowMove, arrowSteps, attract, clampOrdered, createStepRepeat, editEnd, firstStop, gapByMidpoint, imeKey, isNoopDrop, isNudgeChord, moveItems, movedIndex, nearestGap, nudgeGap, nudgeOf, rangeKey, rovingKey, rowKey, sliderKey, stepStop, tabSort, tabStep, timerScheduler, treeKey, typeaheadMatch, typeaheadStep, usableStops };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
import { COARSE_STEPS, snapToStep } from '@weasel-js/quantity';
|
|
2
|
+
export { COARSE_STEPS } from '@weasel-js/quantity';
|
|
3
|
+
import { clamp } from '@weasel-js/geom';
|
|
4
|
+
|
|
5
|
+
// src/tree.ts
|
|
6
|
+
function treeKey(items, at, key, shape) {
|
|
7
|
+
const item = items[at];
|
|
8
|
+
if (item === void 0) return { handled: false };
|
|
9
|
+
const canToggle = shape.canToggle?.(item) ?? true;
|
|
10
|
+
const [opens, closes] = shape.dir === "rtl" ? ["ArrowLeft", "ArrowRight"] : ["ArrowRight", "ArrowLeft"];
|
|
11
|
+
if (key === opens) {
|
|
12
|
+
if (!shape.isBranch(item)) return { handled: false };
|
|
13
|
+
if (!shape.isOpen(item)) return canToggle ? { handled: true, toggle: at } : { handled: true, at: null };
|
|
14
|
+
const next = items[at + 1];
|
|
15
|
+
return { handled: true, at: next !== void 0 && shape.isChildOf(next, item) ? at + 1 : null };
|
|
16
|
+
}
|
|
17
|
+
if (key !== closes) return { handled: false };
|
|
18
|
+
if (shape.isOpen(item) && canToggle) return { handled: true, toggle: at };
|
|
19
|
+
const parent = items.findIndex((p) => shape.isChildOf(item, p));
|
|
20
|
+
return parent < 0 ? { handled: false } : { handled: true, at: parent };
|
|
21
|
+
}
|
|
22
|
+
|
|
1
23
|
// src/list.ts
|
|
2
24
|
var always = () => true;
|
|
3
25
|
function firstStop(items, shape = {}) {
|
|
@@ -38,6 +60,22 @@ function rovingKey(items, at, key, shape = {}) {
|
|
|
38
60
|
const dir = arrowDir(key, shape);
|
|
39
61
|
return dir === 0 ? { handled: false } : { handled: true, at: stepStop(items, at, dir, shape) };
|
|
40
62
|
}
|
|
63
|
+
function rowKey(items, at, press, shape = {}, searching = false) {
|
|
64
|
+
const { key } = press;
|
|
65
|
+
const chord = !!(press.ctrlKey || press.metaKey);
|
|
66
|
+
if (shape.selectAll && key.toLowerCase() === "a" && chord && !press.altKey && !press.shiftKey) return { kind: "selectAll" };
|
|
67
|
+
const step = rovingKey(items, at, key, shape);
|
|
68
|
+
if (step.handled) return { kind: "move", at: step.at, extend: !!press.shiftKey };
|
|
69
|
+
if (shape.tree && (key === "ArrowRight" || key === "ArrowLeft")) {
|
|
70
|
+
const t = treeKey(items, at, key, shape.dir && !shape.tree.dir ? { ...shape.tree, dir: shape.dir } : shape.tree);
|
|
71
|
+
if (!t.handled) return null;
|
|
72
|
+
return "toggle" in t ? { kind: "toggle", at: t.toggle } : { kind: "move", at: t.at, extend: false };
|
|
73
|
+
}
|
|
74
|
+
if (key === " " && shape.typeahead && searching) return { kind: "type", held: true };
|
|
75
|
+
if (key === "Enter" || key === " ") return shape.activate ? { kind: "activate" } : null;
|
|
76
|
+
if (shape.typeahead && key.length === 1 && !chord && !press.altKey) return { kind: "type", held: false };
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
41
79
|
|
|
42
80
|
// src/typeahead.ts
|
|
43
81
|
var TYPEAHEAD_RESET_MS = 500;
|
|
@@ -79,7 +117,210 @@ function tabStep(order, from, direction, isBefore) {
|
|
|
79
117
|
}
|
|
80
118
|
return order[at] ?? null;
|
|
81
119
|
}
|
|
120
|
+
function arrowSteps(key, shift, dir = "ltr") {
|
|
121
|
+
const steps = shift ? COARSE_STEPS : 1;
|
|
122
|
+
if (key === "ArrowUp") return steps;
|
|
123
|
+
if (key === "ArrowDown") return -steps;
|
|
124
|
+
const d = arrowDir(key, { orientation: "horizontal", dir });
|
|
125
|
+
return d === 0 ? null : d * steps;
|
|
126
|
+
}
|
|
127
|
+
function arrowMove(key, shift) {
|
|
128
|
+
const steps = arrowSteps(key, shift);
|
|
129
|
+
if (steps === null) return null;
|
|
130
|
+
return key === "ArrowRight" || key === "ArrowLeft" ? { dx: steps, dy: 0 } : { dx: 0, dy: -steps };
|
|
131
|
+
}
|
|
132
|
+
function rangeKey(key, shift, dir = "ltr") {
|
|
133
|
+
switch (key) {
|
|
134
|
+
case "PageUp":
|
|
135
|
+
return COARSE_STEPS;
|
|
136
|
+
case "PageDown":
|
|
137
|
+
return -COARSE_STEPS;
|
|
138
|
+
case "Home":
|
|
139
|
+
return "home";
|
|
140
|
+
case "End":
|
|
141
|
+
return "end";
|
|
142
|
+
default:
|
|
143
|
+
return arrowSteps(key, shift, dir);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function usableStops(stops, min, max) {
|
|
147
|
+
if (!stops || stops.length === 0) return [];
|
|
148
|
+
const byValue = /* @__PURE__ */ new Map();
|
|
149
|
+
for (const stop of stops) {
|
|
150
|
+
const s = typeof stop === "number" ? { value: stop } : stop;
|
|
151
|
+
if (s.value < min || s.value > max) continue;
|
|
152
|
+
const seen = byValue.get(s.value);
|
|
153
|
+
if (!seen || seen.label === void 0 && s.label !== void 0) byValue.set(s.value, s);
|
|
154
|
+
}
|
|
155
|
+
return [...byValue.values()].sort((a, b) => a.value - b.value);
|
|
156
|
+
}
|
|
157
|
+
function attract(v, stops, tolerance, toFraction) {
|
|
158
|
+
const f = toFraction(v);
|
|
159
|
+
let best = v;
|
|
160
|
+
let bestGap = tolerance;
|
|
161
|
+
for (const stop of stops) {
|
|
162
|
+
const gap = Math.abs(toFraction(stop) - f);
|
|
163
|
+
if (gap <= bestGap) {
|
|
164
|
+
best = stop;
|
|
165
|
+
bestGap = gap;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return best;
|
|
169
|
+
}
|
|
170
|
+
function stepStops(stops, v, direction, count) {
|
|
171
|
+
if (direction > 0) {
|
|
172
|
+
const first2 = stops.findIndex((stop) => stop > v);
|
|
173
|
+
if (first2 === -1) return stops[stops.length - 1];
|
|
174
|
+
return stops[Math.min(stops.length - 1, first2 + count - 1)];
|
|
175
|
+
}
|
|
176
|
+
let first = -1;
|
|
177
|
+
for (let i = stops.length - 1; i >= 0; i--) {
|
|
178
|
+
if (stops[i] < v) {
|
|
179
|
+
first = i;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (first === -1) return stops[0];
|
|
184
|
+
return stops[Math.max(0, first - (count - 1))];
|
|
185
|
+
}
|
|
186
|
+
function defaultStep(step, min, max) {
|
|
187
|
+
if (step !== void 0 && step > 0) return step;
|
|
188
|
+
return (max - min) / 100;
|
|
189
|
+
}
|
|
190
|
+
function clampOrdered(v, thumbs, index, min, max, step) {
|
|
191
|
+
const gap = step !== void 0 && step > 0 ? step : (max - min) / 1e3;
|
|
192
|
+
const lower = index > 0 ? thumbs[index - 1].value + gap : min;
|
|
193
|
+
const upper = index < thumbs.length - 1 ? thumbs[index + 1].value - gap : max;
|
|
194
|
+
return clamp(v, lower, upper);
|
|
195
|
+
}
|
|
196
|
+
function crossEnd(from, delta, stepSize, range, track) {
|
|
197
|
+
const [lo, hi] = range;
|
|
198
|
+
if (from > hi) return delta < 0 ? hi + delta + stepSize : from;
|
|
199
|
+
if (from < lo) return delta > 0 ? lo + delta - stepSize : from;
|
|
200
|
+
if (from + delta > hi) return from < hi ? hi : track[1];
|
|
201
|
+
if (from + delta < lo) return from > lo ? lo : track[0];
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
function sliderKey(key, shift, shape) {
|
|
205
|
+
const move = rangeKey(key, shift, shape.dir);
|
|
206
|
+
if (move === null) return null;
|
|
207
|
+
const { thumbs, index, min, max, step } = shape;
|
|
208
|
+
const from = thumbs[index].value;
|
|
209
|
+
const [bLo, bHi] = shape.bounds?.() ?? [min, max];
|
|
210
|
+
const lo = Math.max(min, bLo);
|
|
211
|
+
const hi = Math.min(max, bHi);
|
|
212
|
+
const stops = shape.stops ?? [];
|
|
213
|
+
let v;
|
|
214
|
+
if (move === "home") v = lo;
|
|
215
|
+
else if (move === "end") v = hi;
|
|
216
|
+
else if (shape.keyStep) v = shape.keyStep(move);
|
|
217
|
+
else if (stops.length > 0) v = stepStops(stops, from, move > 0 ? 1 : -1, Math.abs(move));
|
|
218
|
+
else {
|
|
219
|
+
const range = shape.range ?? [min, max];
|
|
220
|
+
const stepSize = defaultStep(step, range[0], range[1]);
|
|
221
|
+
const delta = move * stepSize;
|
|
222
|
+
v = crossEnd(from, delta, stepSize, range, [min, max]) ?? snapToStep(from + delta, step, min);
|
|
223
|
+
}
|
|
224
|
+
v = clamp(v, lo, hi);
|
|
225
|
+
return shape.ordered ? clampOrdered(v, thumbs, index, lo, hi, step) : v;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/textCommit.ts
|
|
229
|
+
function editEnd(cause, shape = {}) {
|
|
230
|
+
if (cause === "blur") return "commit";
|
|
231
|
+
if (cause.key === "Enter") return shape.multiline && cause.shift ? null : "commit";
|
|
232
|
+
if (cause.key === "Escape") return shape.escape ?? "cancel";
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
function imeKey(e) {
|
|
236
|
+
return e.isComposing === true || e.keyCode === 229 || e.key === "Process" || e.key === "Dead";
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/reorder.ts
|
|
240
|
+
var sortedSet = (at) => [...new Set(at)].sort((a, b) => a - b);
|
|
241
|
+
function moveItems(items, at, to) {
|
|
242
|
+
const taken = new Set(at);
|
|
243
|
+
const moved = sortedSet(at).flatMap((i) => i >= 0 && i < items.length ? [items[i]] : []);
|
|
244
|
+
const out = items.filter((_, i) => !taken.has(i));
|
|
245
|
+
out.splice(movedIndex(at, to), 0, ...moved);
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
function movedIndex(at, to) {
|
|
249
|
+
return to - sortedSet(at).filter((i) => i < to).length;
|
|
250
|
+
}
|
|
251
|
+
function isNoopDrop(at, to) {
|
|
252
|
+
const s = sortedSet(at);
|
|
253
|
+
if (s.length === 0) return false;
|
|
254
|
+
const first = s[0];
|
|
255
|
+
const last = s[s.length - 1];
|
|
256
|
+
return last - first + 1 === s.length && to >= first && to <= last + 1;
|
|
257
|
+
}
|
|
258
|
+
function nudgeGap(at, by, length) {
|
|
259
|
+
const s = sortedSet(at);
|
|
260
|
+
if (s.length === 0) return null;
|
|
261
|
+
const to = by < 0 ? s[0] - 1 : s[s.length - 1] + 2;
|
|
262
|
+
return to < 0 || to > length ? null : to;
|
|
263
|
+
}
|
|
264
|
+
function gapByMidpoint(mids, p) {
|
|
265
|
+
const backward = mids.length > 1 && mids[mids.length - 1] < mids[0];
|
|
266
|
+
return mids.filter((m) => backward ? m >= p : m <= p).length;
|
|
267
|
+
}
|
|
268
|
+
function nearestGap(gaps, p) {
|
|
269
|
+
let best = -1;
|
|
270
|
+
for (let k = 0; k < gaps.length; k++) if (best < 0 || Math.abs(gaps[k] - p) < Math.abs(gaps[best] - p)) best = k;
|
|
271
|
+
return best;
|
|
272
|
+
}
|
|
273
|
+
function isNudgeChord(e) {
|
|
274
|
+
return e.altKey && !e.ctrlKey && !e.metaKey;
|
|
275
|
+
}
|
|
276
|
+
function nudgeOf(e, back = "ArrowUp", on = "ArrowDown") {
|
|
277
|
+
if (!isNudgeChord(e)) return null;
|
|
278
|
+
if (e.key === back) return -1;
|
|
279
|
+
return e.key === on ? 1 : null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/scheduler.ts
|
|
283
|
+
var timerScheduler = {
|
|
284
|
+
now: () => Date.now(),
|
|
285
|
+
set: (fn, ms) => {
|
|
286
|
+
const t = setTimeout(fn, ms);
|
|
287
|
+
return () => clearTimeout(t);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// src/stepRepeat.ts
|
|
292
|
+
var REPEAT_DELAY_MS = 400;
|
|
293
|
+
var REPEAT_EVERY_MS = 60;
|
|
294
|
+
function createStepRepeat(step, spent, opts = {}) {
|
|
295
|
+
let cancel = null;
|
|
296
|
+
let key = null;
|
|
297
|
+
let hold = 0;
|
|
298
|
+
const stop = () => {
|
|
299
|
+
cancel?.();
|
|
300
|
+
cancel = null;
|
|
301
|
+
key = null;
|
|
302
|
+
hold++;
|
|
303
|
+
};
|
|
304
|
+
const tick = (k, ms) => {
|
|
305
|
+
cancel = (opts.scheduler ?? timerScheduler).set(() => {
|
|
306
|
+
if (spent(k)) return stop();
|
|
307
|
+
step(k);
|
|
308
|
+
tick(k, opts.everyMs ?? REPEAT_EVERY_MS);
|
|
309
|
+
}, ms);
|
|
310
|
+
};
|
|
311
|
+
return {
|
|
312
|
+
start(k) {
|
|
313
|
+
stop();
|
|
314
|
+
const mine = hold;
|
|
315
|
+
key = k;
|
|
316
|
+
step(k);
|
|
317
|
+
if (hold === mine) tick(k, opts.delayMs ?? REPEAT_DELAY_MS);
|
|
318
|
+
},
|
|
319
|
+
stop,
|
|
320
|
+
held: () => key
|
|
321
|
+
};
|
|
322
|
+
}
|
|
82
323
|
|
|
83
|
-
export { EMPTY_TYPEAHEAD, TYPEAHEAD_RESET_MS, firstStop, rovingKey, stepStop, tabSort, tabStep, typeaheadMatch, typeaheadStep };
|
|
324
|
+
export { EMPTY_TYPEAHEAD, REPEAT_DELAY_MS, REPEAT_EVERY_MS, TYPEAHEAD_RESET_MS, arrowMove, arrowSteps, attract, clampOrdered, createStepRepeat, editEnd, firstStop, gapByMidpoint, imeKey, isNoopDrop, isNudgeChord, moveItems, movedIndex, nearestGap, nudgeGap, nudgeOf, rangeKey, rovingKey, rowKey, sliderKey, stepStop, tabSort, tabStep, timerScheduler, treeKey, typeaheadMatch, typeaheadStep, usableStops };
|
|
84
325
|
//# sourceMappingURL=index.js.map
|
|
85
326
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/list.ts","../src/typeahead.ts","../src/focus.ts"],"names":[],"mappings":";AAYA,IAAM,SAAS,MAAM,IAAA;AAGd,SAAS,SAAA,CAAa,KAAA,EAAqB,KAAA,GAAsB,EAAC,E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AA,CAAO,iBAAA;AAE5C,EAAA,OAAO,CAAC,GAAG,OAAO,CAAA,CAAE,IAAA,CAAK,CAAC,CAAA,EAAG,CAAA,KAAM,GAAA,CAAI,CAAC,CAAA,GAAI,GAAA,CAAI,CAAC,CAAC,CAAA;AACpD;AAKO,SAAS,OAAA,CACd,KAAA,EAAqB,IAAA,EAAgB,SAAA,EAA4B,QAAA,EACvD;AACV,EAAA,MAAM,UAAU,SAAA,KAAc,MAAA;AAC9B,EAAA,MAAM,OAAO,IAAA,KAAS,IAAA,GAAO,EAAA,GAAK,KAAA,CAAM,QAAQ,IAAI,CAAA;AACpD,EAAA,IAAI,EAAA;AACJ,EAAA,IAAI,IAAA,KAAS,EAAA,EAAI,EAAA,GAAK,IAAA,IAAQ,UAAU,CAAA,GAAI,EAAA,CAAA;AAAA,OAAA,IACnC,IAAA,KAAS,QAAQ,CAAC,QAAA,OAAe,OAAA,GAAU,CAAA,GAAI,MAAM,MAAA,GAAS,CAAA;AAAA,OAClE;AACH,IAAA,MAAM,KAAA,GAAQ,MAAM,SAAA,CAAU,CAAC,MAAM,QAAA,CAAS,IAAA,EAAM,CAAC,CAAC,CAAA;AACtD,IAAA,MAAM,GAAA,GAAM,KAAA,KAAU,EAAA,GAAK,KAAA,CAAM,MAAA,GAAS,KAAA;AAC1C,IAAA,EAAA,GAAK,OAAA,GAAU,MAAM,GAAA,GAAM,CAAA;AAAA,EAC7B;AACA,EAAA,OAAO,KAAA,CAAM,EAAE,CAAA,IAAK,IAAA;AACtB","file":"index.js","sourcesContent":["/** How a list's items are read: which ones the highlight can land on, and which way its arrows run. */\nexport interface ListShape<T> {\n /** Whether the highlight can land on `item`. A separator or a heading can't. Default: every item. */\n canStop?(item: T): boolean;\n /** Which arrow keys move the highlight. `'both'` answers to all four, the way a radio group does. Default `'vertical'`. */\n orientation?: 'vertical' | 'horizontal' | 'both';\n /** The inline direction the items are laid out in. Under `'rtl'` Left steps forward and Right back. Default `'ltr'`. */\n dir?: 'ltr' | 'rtl';\n /** An arrow past either end comes round to the other. Default true. */\n wrap?: boolean;\n}\n\nconst always = () => true;\n\n/** Index of the first item the highlight can land on, or null for a list with none. */\nexport function firstStop<T>(items: readonly T[], shape: ListShape<T> = {}): number | null {\n const i = items.findIndex(shape.canStop ?? always);\n return i < 0 ? null : i;\n}\n\n/** The next item the highlight can land on from `from` in direction `dir`; from `null`, the first or last.\n * Without wrap, an end stays where it is. Null for a list with no such item. */\nexport function stepStop<T>(\n items: readonly T[], from: number | null, dir: 1 | -1, shape: ListShape<T> = {},\n): number | null {\n const canStop = shape.canStop ?? always;\n const wrap = shape.wrap ?? true;\n const n = items.length;\n let i = from ?? (dir === 1 ? -1 : n);\n for (let k = 0; k < n; k++) {\n i += dir;\n if (i < 0 || i >= n) {\n if (!wrap) break;\n i = (i + n) % n;\n }\n if (canStop(items[i]!)) return i;\n }\n return !wrap && from !== null && canStop(items[from]!) ? from : null;\n}\n\n/** What one key does to a list's highlight. `handled` false for a key that is not the list's to move. */\nexport type RovingResult = { handled: false } | { handled: true; at: number | null };\n\n/** Which way an arrow key steps a list of `shape`: 1, -1, or 0 for a key that is not one of its arrows. */\nfunction arrowDir(key: string, shape: ListShape<unknown>): 1 | -1 | 0 {\n const orientation = shape.orientation ?? 'vertical';\n if (orientation !== 'horizontal') {\n if (key === 'ArrowDown') return 1;\n if (key === 'ArrowUp') return -1;\n }\n if (orientation !== 'vertical') {\n const forward = shape.dir === 'rtl' ? -1 : 1;\n if (key === 'ArrowRight') return forward;\n if (key === 'ArrowLeft') return -forward as 1 | -1;\n }\n return 0;\n}\n\n/** Move the highlight `at` by one key: the arrows along the list's orientation, Home and End. */\nexport function rovingKey<T>(items: readonly T[], at: number | null, key: string, shape: ListShape<T> = {}): RovingResult {\n if (key === 'Home') return { handled: true, at: stepStop(items, null, 1, shape) };\n if (key === 'End') return { handled: true, at: stepStop(items, null, -1, shape) };\n const dir = arrowDir(key, shape as ListShape<unknown>);\n return dir === 0 ? { handled: false } : { handled: true, at: stepStop(items, at, dir, shape) };\n}\n","import type { ListShape } from './list';\n\n/** How typeahead reads a list: its shape, and the text each item is found by. */\nexport interface TypeaheadShape<T> extends Pick<ListShape<T>, 'canStop'> {\n label(item: T): string;\n}\n\n/** How long typing can pause before the typeahead buffer starts over, in milliseconds. */\nexport const TYPEAHEAD_RESET_MS = 500;\n\n/** What has been typed so far, and when the last key came. */\nexport interface TypeaheadBuffer {\n text: string;\n at: number;\n}\n\n/** A buffer nothing has been typed into. */\nexport const EMPTY_TYPEAHEAD: TypeaheadBuffer = { text: '', at: -Infinity };\n\n/** The item a typed `query` lands on, searching forward from `from` with\n * wrap. A one-letter query starts after the highlight so repeating a letter\n * cycles; a longer one starts at it so typing on keeps the match. */\nexport function typeaheadMatch<T>(\n items: readonly T[], from: number | null, query: string, shape: TypeaheadShape<T>,\n): number | null {\n const q = query.toLowerCase();\n const n = items.length;\n const start = from == null ? 0 : q.length === 1 ? from + 1 : from;\n for (let k = 0; k < n; k++) {\n const i = (start + k) % n;\n const item = items[i]!;\n if ((shape.canStop?.(item) ?? true) && shape.label(item).toLowerCase().startsWith(q)) return i;\n }\n return null;\n}\n\n/** One typed character: the new buffer and the item it lands on. A key that\n * repeats the whole buffer searches that one letter, so it cycles. */\nexport function typeaheadStep<T>(\n items: readonly T[],\n from: number | null,\n buffer: TypeaheadBuffer,\n key: string,\n now: number,\n shape: TypeaheadShape<T>,\n resetMs: number = TYPEAHEAD_RESET_MS,\n): { buffer: TypeaheadBuffer; hit: number | null } {\n const prev = now - buffer.at < resetMs ? buffer.text : '';\n const k = key.toLowerCase();\n const cycling = prev !== '' && [...prev].every((c) => c.toLowerCase() === k);\n const text = cycling ? key : prev + key;\n return { buffer: { text, at: now }, hit: typeaheadMatch(items, from, text, shape) };\n}\n","/** How a focus change came about. */\nexport interface FocusScopeOptions {\n /** Whether the target shows its focus ring: the scope's `:focus-visible`. Keyboard moves show one, a press does\n * not. Default `false` for {@link FocusScope.focus}, `true` for {@link FocusScope.moveFocus}. */\n readonly visible?: boolean;\n}\n\n/**\n * Keyboard focus over a set of targets, whatever draws them: a HUD's widgets and a DOM subtree's elements both\n * answer it, so code that moves or reads focus is written once for either.\n */\nexport interface FocusScope<T> {\n /** The target holding keyboard focus, or null. One per scope. */\n readonly focused: T | null;\n /** Whether the focused target shows its focus ring. */\n readonly focusVisible: boolean;\n /** Focus `target`, or blur with `null`. Returns whether `target` now holds focus: false when it is not in this\n * scope or cannot take focus, and focus is then unchanged. */\n focus(target: T | null, options?: FocusScopeOptions): boolean;\n /** Move focus one step through {@link tabOrder}. From nothing, `'next'` lands on the first target and `'prev'` on\n * the last; past either end the scope blurs and returns null, which is where focus leaves it. */\n moveFocus(direction: 'next' | 'prev', options?: FocusScopeOptions): T | null;\n /** The targets Tab visits, in order. */\n tabOrder(): T[];\n /** Run `fn` whenever the focused target changes. Returns an unsubscribe. */\n subscribeFocus(fn: (focused: T | null) => void): () => void;\n}\n\n/** Tab order: targets that declare a place come first, ascending, then the rest in the order given. In the DOM a\n * positive `tabindex` declares one and `0` does not. */\nexport function tabSort<T>(targets: readonly T[], declared: (target: T) => number | undefined): T[] {\n const key = (t: T) => declared(t) ?? Number.POSITIVE_INFINITY;\n // Array.prototype.sort is stable, so ties and the undeclared keep the given order.\n return [...targets].sort((a, b) => key(a) - key(b));\n}\n\n/** The target one Tab step from `from` through `order`, or null past either end. From nothing, `'next'` is the\n * first and `'prev'` the last. A `from` that is out of the order (hidden since it took focus, say) steps from where\n * it sits, which `isBefore` says; without it, such a `from` steps as nothing does. */\nexport function tabStep<T>(\n order: readonly T[], from: T | null, direction: 'next' | 'prev', isBefore?: (a: T, b: T) => boolean,\n): T | null {\n const forward = direction === 'next';\n const here = from === null ? -1 : order.indexOf(from);\n let at: number;\n if (here !== -1) at = here + (forward ? 1 : -1);\n else if (from === null || !isBefore) at = forward ? 0 : order.length - 1;\n else {\n const after = order.findIndex((t) => isBefore(from, t));\n const end = after === -1 ? order.length : after;\n at = forward ? end : end - 1;\n }\n return order[at] ?? null;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/tree.ts","../src/list.ts","../src/typeahead.ts","../src/focus.ts","../src/keySteps.ts","../src/slider.ts","../src/textCommit.ts","../src/reorder.ts","../src/scheduler.ts","../src/stepRepeat.ts"],"names":["first"],"mappings":";;;;;AAyBO,SAAS,OAAA,CAAW,KAAA,EAAqB,EAAA,EAAY,GAAA,EAAa,KAAA,EAAoC;AAC3G,EAAA,MAAM,IAAA,GAAO,MAAM,EAAE,CAAA;AACrB,EAAA,IAAI,IAAA,KAAS,MAAA,EAAW,OAAO,EAAE,SAAS,KAAA,EAAM;AAChD,EAAA,MAAM,SAAA,GAAY,KAAA,CAAM,SAAA,GAAY,IAAI,CAAA,IAAK,IAAA;AAC7C,EAAA,MAAM,CAAC,KAAA,EAAO,MAAM,CAAA,GAAI,KAAA,CAAM,GAAA,KAAQ,KAAA,GAAQ,CAAC,WAAA,EAAa,YAAY,CAAA,GAAI,CAAC,cAAc,WAAW,CAAA;AACtG,EAAA,IAAI,QAAQ,KAAA,EAAO;AACjB,IAAA,IAAI,CAAC,MAAM,QAAA,CAAS,IAAI,GAAG,OAAO,EAAE,SAAS,KAAA,EAAM;AACnD,IAAA,IAAI,CAAC,KAAA,CAAM,MAAA,CAAO,IAAI,CAAA,SAAU,SAAA,GAAY,EAAE,OAAA,EAAS,IAAA,EAAM,QAAQ,EAAA,EAAG,GAAI,EAAE,OAAA,EAAS,IAAA,EAAM,IAAI,IAAA,EAAK;AACtG,IAAA,MAAM,IAAA,GAAO,KAAA,CAAM,EAAA,GAAK,CAAC,CAAA;AACzB,IAAA,OAAO,EAAE,OAAA,EAAS,IAAA,EAAM,EAAA,EAAI,IAAA,KAAS,MAAA,IAAa,KAAA,CAAM,SAAA,CAAU,IAAA,EAAM,IAAI,CAAA,GAAI,EAAA,GAAK,IAAI,IAAA,EAAK;AAAA,EAChG;AACA,EAAA,IAAI,GAAA,KAAQ,MAAA,EAAQ,OAAO,EAAE,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U,IAAA,CAAK,SAAA,IAAa,cAAA,EAAgB,GAAA,CAAI,MAAM;AAEpD,MAAA,IAAI,KAAA,CAAM,CAAC,CAAA,EAAG,OAAO,IAAA,EAAK;AAC1B,MAAA,IAAA,CAAK,CAAC,CAAA;AACN,MAAA,IAAA,CAAK,CAAA,EAAG,IAAA,CAAK,OAAA,IAAW,eAAe,CAAA;AAAA,IACzC,GAAG,EAAE,CAAA;AAAA,EACP,CAAA;AACA,EAAA,OAAO;AAAA,IACL,MAAM,CAAA,EAAG;AACP,MAAA,IAAA,EAAK;AACL,MAAA,MAAM,IAAA,GAAO,IAAA;AACb,MAAA,GAAA,GAAM,CAAA;AACN,MAAA,IAAA,CAAK,CAAC,CAAA;AAEN,MAAA,IAAI,SAAS,IAAA,EAAM,IAAA,CAAK,CAAA,EAAG,IAAA,CAAK,WAAW,eAAe,CAAA;AAAA,IAC5D,CAAA;AAAA,IACA,IAAA;AAAA,IACA,MAAM,MAAM;AAAA,GACd;AACF","file":"index.js","sourcesContent":["/** How a list that folds is read: which items are branches, which are open, and which item is whose child. */\nexport interface TreeShape<T> {\n /** Whether `item` can open, its children shown or not. */\n isBranch(item: T): boolean;\n isOpen(item: T): boolean;\n /** Whether `child` is one of `parent`'s own children. */\n isChildOf(child: T, parent: T): boolean;\n /** Whether a branch can open or close now. A disabled one can't. Default: every branch. */\n canToggle?(item: T): boolean;\n /** The inline direction the rows read in. Under `'rtl'` Left opens and Right closes. Default `'ltr'`. */\n dir?: 'ltr' | 'rtl';\n}\n\n/** What Right or Left does on one item of a tree: open or close it, move the highlight, or nothing (`handled` false).\n * `at` null is a key the tree takes without moving. */\nexport type TreeKeyResult =\n | { handled: false }\n | { handled: true; toggle: number }\n | { handled: true; at: number | null };\n\n/**\n * Right and Left on the item at `at`, the WAI-ARIA tree way: Right opens a closed branch, else steps to its first\n * child; Left closes an open branch, else steps to the parent. Mirrored under `dir: 'rtl'`. `items` are the rows\n * shown, in drawn order.\n */\nexport function treeKey<T>(items: readonly T[], at: number, key: string, shape: TreeShape<T>): TreeKeyResult {\n const item = items[at];\n if (item === undefined) return { handled: false };\n const canToggle = shape.canToggle?.(item) ?? true;\n const [opens, closes] = shape.dir === 'rtl' ? ['ArrowLeft', 'ArrowRight'] : ['ArrowRight', 'ArrowLeft'];\n if (key === opens) {\n if (!shape.isBranch(item)) return { handled: false };\n if (!shape.isOpen(item)) return canToggle ? { handled: true, toggle: at } : { handled: true, at: null };\n const next = items[at + 1];\n return { handled: true, at: next !== undefined && shape.isChildOf(next, item) ? at + 1 : null };\n }\n if (key !== closes) return { handled: false };\n if (shape.isOpen(item) && canToggle) return { handled: true, toggle: at };\n const parent = items.findIndex((p) => shape.isChildOf(item, p));\n return parent < 0 ? { handled: false } : { handled: true, at: parent };\n}\n","import { treeKey, type TreeShape } from './tree';\n\n/** How a list's items are read: which ones the highlight can land on, and which way its arrows run. */\nexport interface ListShape<T> {\n /** Whether the highlight can land on `item`. A separator or a heading can't. Default: every item. */\n canStop?(item: T): boolean;\n /** Which arrow keys move the highlight. `'both'` answers to all four, the way a radio group does. Default `'vertical'`. */\n orientation?: 'vertical' | 'horizontal' | 'both';\n /** The inline direction the items are laid out in. Under `'rtl'` Left steps forward and Right back. Default `'ltr'`. */\n dir?: 'ltr' | 'rtl';\n /** An arrow past either end comes round to the other. Default true. */\n wrap?: boolean;\n}\n\nconst always = () => true;\n\n/** Index of the first item the highlight can land on, or null for a list with none. */\nexport function firstStop<T>(items: readonly T[], shape: ListShape<T> = {}): number | null {\n const i = items.findIndex(shape.canStop ?? always);\n return i < 0 ? null : i;\n}\n\n/** The next item the highlight can land on from `from` in direction `dir`; from `null`, the first or last.\n * Without wrap, an end stays where it is. Null for a list with no such item. */\nexport function stepStop<T>(\n items: readonly T[], from: number | null, dir: 1 | -1, shape: ListShape<T> = {},\n): number | null {\n const canStop = shape.canStop ?? always;\n const wrap = shape.wrap ?? true;\n const n = items.length;\n let i = from ?? (dir === 1 ? -1 : n);\n for (let k = 0; k < n; k++) {\n i += dir;\n if (i < 0 || i >= n) {\n if (!wrap) break;\n i = (i + n) % n;\n }\n if (canStop(items[i]!)) return i;\n }\n return !wrap && from !== null && canStop(items[from]!) ? from : null;\n}\n\n/** What one key does to a list's highlight. `handled` false for a key that is not the list's to move. */\nexport type RovingResult = { handled: false } | { handled: true; at: number | null };\n\n/** Which way an arrow key steps a list of `shape`: 1, -1, or 0 for a key that is not one of its arrows. */\nexport function arrowDir(key: string, shape: ListShape<unknown>): 1 | -1 | 0 {\n const orientation = shape.orientation ?? 'vertical';\n if (orientation !== 'horizontal') {\n if (key === 'ArrowDown') return 1;\n if (key === 'ArrowUp') return -1;\n }\n if (orientation !== 'vertical') {\n const forward = shape.dir === 'rtl' ? -1 : 1;\n if (key === 'ArrowRight') return forward;\n if (key === 'ArrowLeft') return -forward as 1 | -1;\n }\n return 0;\n}\n\n/** Move the highlight `at` by one key: the arrows along the list's orientation, Home and End. */\nexport function rovingKey<T>(items: readonly T[], at: number | null, key: string, shape: ListShape<T> = {}): RovingResult {\n if (key === 'Home') return { handled: true, at: stepStop(items, null, 1, shape) };\n if (key === 'End') return { handled: true, at: stepStop(items, null, -1, shape) };\n const dir = arrowDir(key, shape as ListShape<unknown>);\n return dir === 0 ? { handled: false } : { handled: true, at: stepStop(items, at, dir, shape) };\n}\n\n/** A key as {@link rowKey} reads it: the key and the modifiers held. */\nexport interface KeyPress {\n key: string;\n shiftKey?: boolean;\n ctrlKey?: boolean;\n metaKey?: boolean;\n altKey?: boolean;\n}\n\n/** A list of rows, and which of the keys past the arrows its caller answers. */\nexport interface RowKeysShape<T> extends ListShape<T> {\n /** Set for a list that folds: Right and Left open, close, and walk it ({@link treeKey}). */\n tree?: TreeShape<T>;\n /** Cmd/Ctrl+A selects every row. */\n selectAll?: boolean;\n /** Enter and Space activate a row. */\n activate?: boolean;\n /** Typing a character searches the rows' text. */\n typeahead?: boolean;\n}\n\n/** What one key on a row asks for, for the caller to carry out. */\nexport type RowKeyAction =\n /** Select every row. */\n | { kind: 'selectAll' }\n /** Move to row `at`, extending a range from the anchor when `extend`. `at` null takes the key without moving. */\n | { kind: 'move'; at: number | null; extend: boolean }\n /** Open or close the branch at `at`. */\n | { kind: 'toggle'; at: number }\n | { kind: 'activate' }\n /** Hand the key to typeahead. `held` when the key is the list's whether or not the search finds a row. */\n | { kind: 'type'; held: boolean };\n\n/**\n * What a key on row `at` of a list of rows does, in the order the keys are tried: Cmd/Ctrl+A, the arrows with Home\n * and End (Shift to extend), a tree's Right and Left, a space typed mid-search, Enter or Space to activate, then any\n * other character to search. `searching` says a typeahead search is under way. Null for a key that is not the list's.\n */\nexport function rowKey<T>(\n items: readonly T[], at: number, press: KeyPress, shape: RowKeysShape<T> = {}, searching = false,\n): RowKeyAction | null {\n const { key } = press;\n const chord = !!(press.ctrlKey || press.metaKey);\n if (shape.selectAll && key.toLowerCase() === 'a' && chord && !press.altKey && !press.shiftKey) return { kind: 'selectAll' };\n const step = rovingKey(items, at, key, shape);\n if (step.handled) return { kind: 'move', at: step.at, extend: !!press.shiftKey };\n if (shape.tree && (key === 'ArrowRight' || key === 'ArrowLeft')) {\n const t = treeKey(items, at, key, shape.dir && !shape.tree.dir ? { ...shape.tree, dir: shape.dir } : shape.tree);\n if (!t.handled) return null;\n return 'toggle' in t ? { kind: 'toggle', at: t.toggle } : { kind: 'move', at: t.at, extend: false };\n }\n // Mid-search a space is a typed character: \"new f\" must not activate at the space.\n if (key === ' ' && shape.typeahead && searching) return { kind: 'type', held: true };\n if (key === 'Enter' || key === ' ') return shape.activate ? { kind: 'activate' } : null;\n if (shape.typeahead && key.length === 1 && !chord && !press.altKey) return { kind: 'type', held: false };\n return null;\n}\n","import type { ListShape } from './list';\n\n/** How typeahead reads a list: its shape, and the text each item is found by. */\nexport interface TypeaheadShape<T> extends Pick<ListShape<T>, 'canStop'> {\n label(item: T): string;\n}\n\n/** How long typing can pause before the typeahead buffer starts over, in milliseconds. */\nexport const TYPEAHEAD_RESET_MS = 500;\n\n/** What has been typed so far, and when the last key came. */\nexport interface TypeaheadBuffer {\n text: string;\n at: number;\n}\n\n/** A buffer nothing has been typed into. */\nexport const EMPTY_TYPEAHEAD: TypeaheadBuffer = { text: '', at: -Infinity };\n\n/** The item a typed `query` lands on, searching forward from `from` with\n * wrap. A one-letter query starts after the highlight so repeating a letter\n * cycles; a longer one starts at it so typing on keeps the match. */\nexport function typeaheadMatch<T>(\n items: readonly T[], from: number | null, query: string, shape: TypeaheadShape<T>,\n): number | null {\n const q = query.toLowerCase();\n const n = items.length;\n const start = from == null ? 0 : q.length === 1 ? from + 1 : from;\n for (let k = 0; k < n; k++) {\n const i = (start + k) % n;\n const item = items[i]!;\n if ((shape.canStop?.(item) ?? true) && shape.label(item).toLowerCase().startsWith(q)) return i;\n }\n return null;\n}\n\n/** One typed character: the new buffer and the item it lands on. A key that\n * repeats the whole buffer searches that one letter, so it cycles. */\nexport function typeaheadStep<T>(\n items: readonly T[],\n from: number | null,\n buffer: TypeaheadBuffer,\n key: string,\n now: number,\n shape: TypeaheadShape<T>,\n resetMs: number = TYPEAHEAD_RESET_MS,\n): { buffer: TypeaheadBuffer; hit: number | null } {\n const prev = now - buffer.at < resetMs ? buffer.text : '';\n const k = key.toLowerCase();\n const cycling = prev !== '' && [...prev].every((c) => c.toLowerCase() === k);\n const text = cycling ? key : prev + key;\n return { buffer: { text, at: now }, hit: typeaheadMatch(items, from, text, shape) };\n}\n","/** How a focus change came about. */\nexport interface FocusScopeOptions {\n /** Whether the target shows its focus ring: the scope's `:focus-visible`. Keyboard moves show one, a press does\n * not. Default `false` for {@link FocusScope.focus}, `true` for {@link FocusScope.moveFocus}. */\n readonly visible?: boolean;\n}\n\n/**\n * Keyboard focus over a set of targets, whatever draws them: a HUD's widgets and a DOM subtree's elements both\n * answer it, so code that moves or reads focus is written once for either.\n */\nexport interface FocusScope<T> {\n /** The target holding keyboard focus, or null. One per scope. */\n readonly focused: T | null;\n /** Whether the focused target shows its focus ring. */\n readonly focusVisible: boolean;\n /** Focus `target`, or blur with `null`. Returns whether `target` now holds focus: false when it is not in this\n * scope or cannot take focus, and focus is then unchanged. */\n focus(target: T | null, options?: FocusScopeOptions): boolean;\n /** Move focus one step through {@link tabOrder}. From nothing, `'next'` lands on the first target and `'prev'` on\n * the last; past either end the scope blurs and returns null, which is where focus leaves it. */\n moveFocus(direction: 'next' | 'prev', options?: FocusScopeOptions): T | null;\n /** The targets Tab visits, in order. */\n tabOrder(): T[];\n /** Run `fn` whenever the focused target changes. Returns an unsubscribe. */\n subscribeFocus(fn: (focused: T | null) => void): () => void;\n}\n\n/** Tab order: targets that declare a place come first, ascending, then the rest in the order given. In the DOM a\n * positive `tabindex` declares one and `0` does not. */\nexport function tabSort<T>(targets: readonly T[], declared: (target: T) => number | undefined): T[] {\n const key = (t: T) => declared(t) ?? Number.POSITIVE_INFINITY;\n // Array.prototype.sort is stable, so ties and the undeclared keep the given order.\n return [...targets].sort((a, b) => key(a) - key(b));\n}\n\n/** The target one Tab step from `from` through `order`, or null past either end. From nothing, `'next'` is the\n * first and `'prev'` the last. A `from` that is out of the order (hidden since it took focus, say) steps from where\n * it sits, which `isBefore` says; without it, such a `from` steps as nothing does. */\nexport function tabStep<T>(\n order: readonly T[], from: T | null, direction: 'next' | 'prev', isBefore?: (a: T, b: T) => boolean,\n): T | null {\n const forward = direction === 'next';\n const here = from === null ? -1 : order.indexOf(from);\n let at: number;\n if (here !== -1) at = here + (forward ? 1 : -1);\n else if (from === null || !isBefore) at = forward ? 0 : order.length - 1;\n else {\n const after = order.findIndex((t) => isBefore(from, t));\n const end = after === -1 ? order.length : after;\n at = forward ? end : end - 1;\n }\n return order[at] ?? null;\n}\n","import { COARSE_STEPS } from '@weasel-js/quantity';\nimport { arrowDir } from './list';\n\nexport { COARSE_STEPS };\n\n/** Steps an arrow key asks for along one axis: one, or {@link COARSE_STEPS} with Shift, positive\n * toward ArrowRight and ArrowUp. Under `'rtl'` ArrowLeft and ArrowRight swap, as a native range\n * input's do. `null` for any other key. A caller multiplies by its own step. */\nexport function arrowSteps(key: string, shift: boolean, dir: 'ltr' | 'rtl' = 'ltr'): number | null {\n const steps = shift ? COARSE_STEPS : 1;\n if (key === 'ArrowUp') return steps;\n if (key === 'ArrowDown') return -steps;\n const d = arrowDir(key, { orientation: 'horizontal', dir });\n return d === 0 ? null : d * steps;\n}\n\n/** Steps an arrow key asks for on a plane, in screen axes: `dy` is positive toward ArrowDown.\n * One step, or {@link COARSE_STEPS} with Shift; `null` for any other key. */\nexport function arrowMove(key: string, shift: boolean): { dx: number; dy: number } | null {\n const steps = arrowSteps(key, shift);\n if (steps === null) return null;\n return key === 'ArrowRight' || key === 'ArrowLeft' ? { dx: steps, dy: 0 } : { dx: 0, dy: -steps };\n}\n\n/** What a key does along a range: {@link arrowSteps}, the Page keys ({@link COARSE_STEPS} steps),\n * or Home and End (an end of the range). `null` for any other key. */\nexport function rangeKey(key: string, shift: boolean, dir: 'ltr' | 'rtl' = 'ltr'): number | 'home' | 'end' | null {\n switch (key) {\n case 'PageUp': return COARSE_STEPS;\n case 'PageDown': return -COARSE_STEPS;\n case 'Home': return 'home';\n case 'End': return 'end';\n default: return arrowSteps(key, shift, dir);\n }\n}\n","import { clamp } from '@weasel-js/geom';\nimport { snapToStep } from '@weasel-js/quantity';\nimport { rangeKey } from './keySteps';\n\n/** A value on a slider's track the thumb can come to rest on, with whatever label draws under it. */\nexport interface SliderStop<L = unknown> {\n value: number;\n label?: L;\n}\n\n/** The stops that are actually reachable: inside the range, deduped, ascending.\n * A duplicate keeps the first label given for its value. */\nexport function usableStops<L>(\n stops: readonly (number | SliderStop<L>)[] | undefined, min: number, max: number,\n): SliderStop<L>[] {\n if (!stops || stops.length === 0) return [];\n const byValue = new Map<number, SliderStop<L>>();\n for (const stop of stops) {\n const s = typeof stop === 'number' ? { value: stop } : stop;\n if (s.value < min || s.value > max) continue;\n const seen = byValue.get(s.value);\n if (!seen || (seen.label === undefined && s.label !== undefined)) byValue.set(s.value, s);\n }\n return [...byValue.values()].sort((a, b) => a.value - b.value);\n}\n\n/** The stop nearest `v` along the track, or `v` itself when none lies within\n * `tolerance` (a track fraction). Measured on the track rather than in value\n * units so evenly spaced stops attract over the same distance. */\nexport function attract(v: number, stops: readonly number[], tolerance: number, toFraction: (v: number) => number): number {\n const f = toFraction(v);\n let best = v;\n let bestGap = tolerance;\n for (const stop of stops) {\n const gap = Math.abs(toFraction(stop) - f);\n if (gap <= bestGap) {\n best = stop;\n bestGap = gap;\n }\n }\n return best;\n}\n\n/** The stop `count` places past `v` in `direction`, saturating at either end. */\nexport function stepStops(stops: readonly number[], v: number, direction: 1 | -1, count: number): number {\n if (direction > 0) {\n const first = stops.findIndex(stop => stop > v);\n if (first === -1) return stops[stops.length - 1]!;\n return stops[Math.min(stops.length - 1, first + count - 1)]!;\n }\n let first = -1;\n for (let i = stops.length - 1; i >= 0; i--) {\n if (stops[i]! < v) {\n first = i;\n break;\n }\n }\n if (first === -1) return stops[0]!;\n return stops[Math.max(0, first - (count - 1))]!;\n}\n\n/** `step` when it is positive; otherwise a hundredth of the span. */\nexport function defaultStep(step: number | undefined, min: number, max: number): number {\n if (step !== undefined && step > 0) return step;\n return (max - min) / 100;\n}\n\n/** Keep thumb `index` strictly between its neighbors: a step apart, or a thousandth of the span without one. */\nexport function clampOrdered(\n v: number,\n thumbs: readonly { value: number }[],\n index: number,\n min: number,\n max: number,\n step: number | undefined,\n): number {\n const gap = step !== undefined && step > 0 ? step : (max - min) / 1000;\n const lower = index > 0 ? thumbs[index - 1]!.value + gap : min;\n const upper = index < thumbs.length - 1 ? thumbs[index + 1]!.value - gap : max;\n return clamp(v, lower, upper);\n}\n\n/**\n * Where a keystroke of `delta` takes a thumb at `from` when the move meets an\n * end of the slider's finite `range`, or `null` when it stays inside. An\n * endless end's stop lies on the `track` beyond the range, at a distance drawn\n * for the eye; to the keyboard it is one press past the range's end. A move\n * that would overshoot the end lands on the end first.\n */\nexport function crossEnd(\n from: number,\n delta: number,\n stepSize: number,\n range: readonly [number, number],\n track: readonly [number, number],\n): number | null {\n const [lo, hi] = range;\n if (from > hi) return delta < 0 ? hi + delta + stepSize : from;\n if (from < lo) return delta > 0 ? lo + delta - stepSize : from;\n if (from + delta > hi) return from < hi ? hi : track[1];\n if (from + delta < lo) return from > lo ? lo : track[0];\n return null;\n}\n\n/** What a key press on one thumb of a slider reads. */\nexport interface SliderKeyShape {\n thumbs: readonly { value: number }[];\n /** The thumb the key went to. */\n index: number;\n /** The ends of the track. */\n min: number;\n max: number;\n step?: number;\n /** Usable stops, ascending. With any, a keystroke moves by whole stops rather than by value. */\n stops?: readonly number[];\n /** Keep the thumb between its neighbors. */\n ordered?: boolean;\n /** The finite range, where the track runs past it to an endless end's stop. Default the track. */\n range?: readonly [number, number];\n /** Under `'rtl'` the horizontal arrows are mirrored, as the values run from the right end. */\n dir?: 'ltr' | 'rtl';\n /** This thumb's own limits inside the track. Read only for a key the slider answers to. */\n bounds?(): readonly [number, number];\n /** Where the thumb lands `steps` presses away, for a track whose values aren't what the user counts in. */\n keyStep?(steps: number): number;\n}\n\n/** The value one key moves thumb `index` to: arrows by a step (ten with Shift), the Page keys by ten,\n * Home and End to the thumb's ends. `null` for a key the slider does not answer to. */\nexport function sliderKey(key: string, shift: boolean, shape: SliderKeyShape): number | null {\n const move = rangeKey(key, shift, shape.dir);\n if (move === null) return null;\n const { thumbs, index, min, max, step } = shape;\n const from = thumbs[index]!.value;\n const [bLo, bHi] = shape.bounds?.() ?? [min, max];\n const lo = Math.max(min, bLo);\n const hi = Math.min(max, bHi);\n const stops = shape.stops ?? [];\n let v: number;\n if (move === 'home') v = lo;\n else if (move === 'end') v = hi;\n else if (shape.keyStep) v = shape.keyStep(move);\n else if (stops.length > 0) v = stepStops(stops, from, move > 0 ? 1 : -1, Math.abs(move));\n else {\n const range = shape.range ?? [min, max];\n const stepSize = defaultStep(step, range[0], range[1]);\n const delta = move * stepSize;\n v = crossEnd(from, delta, stepSize, range, [min, max]) ?? snapToStep(from + delta, step, min);\n }\n v = clamp(v, lo, hi);\n return shape.ordered ? clampOrdered(v, thumbs, index, lo, hi, step) : v;\n}\n","/** How a text field's edit ends: `'commit'` writes what was typed, `'cancel'` drops it and puts back what was there. */\nexport type EditEnd = 'commit' | 'cancel';\n\n/** What a field varies in how its edit ends: what Escape does (`'cancel'` by default), and whether the field holds\n * more than one line, which leaves Shift+Enter to type a line break rather than commit. */\nexport interface EditEndShape {\n escape?: EditEnd;\n multiline?: boolean;\n}\n\n/** What may end an edit: a key, with whether Shift is held, or the field losing focus. A key {@link imeKey} claims is\n * the caller's to hold back. */\nexport type EditCause = 'blur' | { key: string; shift?: boolean };\n\n/** How `cause` ends a text field's edit, or null when it leaves the edit open. Enter and blur commit, Escape cancels\n * unless `shape` says otherwise. */\nexport function editEnd(cause: EditCause, shape: EditEndShape = {}): EditEnd | null {\n if (cause === 'blur') return 'commit';\n if (cause.key === 'Enter') return shape.multiline && cause.shift ? null : 'commit';\n if (cause.key === 'Escape') return shape.escape ?? 'cancel';\n return null;\n}\n\n/** A keydown as the platform reports it, enough to tell whether an input method owns it. */\nexport interface KeySource {\n key: string;\n isComposing?: boolean;\n keyCode?: number;\n}\n\n/** Whether a keydown belongs to an input method, which a field must leave alone: Enter there confirms a candidate and\n * Escape dismisses them. Safari sends the Enter that ends a composition with `isComposing` already false, but with\n * `keyCode` 229. */\nexport function imeKey(e: KeySource): boolean {\n return e.isComposing === true || e.keyCode === 229 || e.key === 'Process' || e.key === 'Dead';\n}\n","/**\n * Moving items within an ordered list. A destination is a gap, counted before anything is taken out: 0 before the\n * first item, `length` after the last, and `i` between `items[i - 1]` and `items[i]` — the gap a UI marks while it\n * carries something. The items moved are named by their indices, so a selection of several moves in one go.\n */\n\nconst sortedSet = (at: readonly number[]): number[] => [...new Set(at)].sort((a, b) => a - b);\n\n/** `items` with the items at indices `at` taken out and put back, in their order, in gap `to`. */\nexport function moveItems<T>(items: readonly T[], at: readonly number[], to: number): T[] {\n const taken = new Set(at);\n const moved = sortedSet(at).flatMap((i) => (i >= 0 && i < items.length ? [items[i]!] : []));\n const out = items.filter((_, i) => !taken.has(i));\n out.splice(movedIndex(at, to), 0, ...moved);\n return out;\n}\n\n/** The index the first of the items at `at` holds once {@link moveItems} has put them in gap `to`. */\nexport function movedIndex(at: readonly number[], to: number): number {\n return to - sortedSet(at).filter((i) => i < to).length;\n}\n\n/** Whether putting the items at `at` in gap `to` leaves them where they are: a contiguous run dropped in a gap at\n * either of its ends or inside it. False for no items. */\nexport function isNoopDrop(at: readonly number[], to: number): boolean {\n const s = sortedSet(at);\n if (s.length === 0) return false;\n const first = s[0]!;\n const last = s[s.length - 1]!;\n return last - first + 1 === s.length && to >= first && to <= last + 1;\n}\n\n/** The gap a one-place move sends the items at `at` to: before the item ahead of the first (`by` -1), or after the\n * item past the last (`by` 1). Null where that runs off either end of a list `length` long, or for no items. */\nexport function nudgeGap(at: readonly number[], by: -1 | 1, length: number): number | null {\n const s = sortedSet(at);\n if (s.length === 0) return null;\n const to = by < 0 ? s[0]! - 1 : s[s.length - 1]! + 2;\n return to < 0 || to > length ? null : to;\n}\n\n/**\n * The gap a point `p` along a line of items falls in, where each item splits at its middle: how many of the\n * middles `mids`, given in item order, `p` has reached. A line whose middles run backward, as a right-to-left row\n * does along x, counts the other way. For items that sit side by side — rows, tabs, chips.\n */\nexport function gapByMidpoint(mids: readonly number[], p: number): number {\n const backward = mids.length > 1 && mids[mids.length - 1]! < mids[0]!;\n return mids.filter((m) => (backward ? m >= p : m <= p)).length;\n}\n\n/** Which of the gaps at positions `gaps` along a line is nearest `p`, as an index into `gaps`; -1 for none. For\n * gaps drawn at places of their own, between items set apart however the layout put them: picking the nearest\n * keeps the gap chosen the one whose mark is drawn closest. */\nexport function nearestGap(gaps: readonly number[], p: number): number {\n let best = -1;\n for (let k = 0; k < gaps.length; k++) if (best < 0 || Math.abs(gaps[k]! - p) < Math.abs(gaps[best]! - p)) best = k;\n return best;\n}\n\n/** The keys and modifiers a nudge chord reads off a keystroke. */\nexport interface NudgeKey {\n key: string;\n altKey: boolean;\n ctrlKey: boolean;\n metaKey: boolean;\n}\n\n/** Alt with neither Ctrl nor Cmd held: the chord that moves an item instead of moving focus. */\nexport function isNudgeChord(e: NudgeKey): boolean {\n return e.altKey && !e.ctrlKey && !e.metaKey;\n}\n\n/** Which way the nudge chord moves an item: `-1` for Alt+`back`, `1` for Alt+`on`, `null` for any other key.\n * The keys are Up and Down unless named. */\nexport function nudgeOf(e: NudgeKey, back = 'ArrowUp', on = 'ArrowDown'): -1 | 1 | null {\n if (!isNudgeChord(e)) return null;\n if (e.key === back) return -1;\n return e.key === on ? 1 : null;\n}\n","/** Clock and timer, injected so tests and hosts control time. */\nexport interface Scheduler {\n now(): number;\n /** Run `fn` after `ms`; returns a cancel. */\n set(fn: () => void, ms: number): () => void;\n}\n\n/** The real-time scheduler: `Date.now` and `setTimeout`. */\nexport const timerScheduler: Scheduler = {\n now: () => Date.now(),\n set: (fn, ms) => {\n const t = setTimeout(fn, ms);\n return () => clearTimeout(t);\n },\n};\n","import { timerScheduler, type Scheduler } from './scheduler';\n\n/** How long a held stepper waits before it repeats, in milliseconds. */\nexport const REPEAT_DELAY_MS = 400;\n/** How often a held stepper steps once it repeats, in milliseconds. */\nexport const REPEAT_EVERY_MS = 60;\n\n/** The timing of a {@link createStepRepeat}. Each is read when it is next used, so a getter can change it mid-hold. */\nexport interface StepRepeatOptions {\n /** Default {@link REPEAT_DELAY_MS}. */\n delayMs?: number;\n /** Default {@link REPEAT_EVERY_MS}. */\n everyMs?: number;\n /** Default {@link timerScheduler}. */\n scheduler?: Pick<Scheduler, 'set'>;\n}\n\n/** A held stepper: {@link createStepRepeat}. */\nexport interface StepRepeat<K> {\n /** Step `key` once now, then after the delay once every interval until {@link stop}. Replaces any hold. */\n start(key: K): void;\n stop(): void;\n /** The key being held, or null. */\n held(): K | null;\n}\n\n/**\n * Hold-to-repeat stepping, whatever draws the stepper: `step` moves the value by `key`, and `spent` says `key`\n * has nowhere left to go, which ends the hold. Both are called at the moment of each step.\n */\nexport function createStepRepeat<K>(\n step: (key: K) => void, spent: (key: K) => boolean, opts: StepRepeatOptions = {},\n): StepRepeat<K> {\n let cancel: (() => void) | null = null;\n let key: K | null = null;\n let hold = 0;\n const stop = () => {\n cancel?.();\n cancel = null;\n key = null;\n hold++;\n };\n const tick = (k: K, ms: number) => {\n cancel = (opts.scheduler ?? timerScheduler).set(() => {\n // A stepper at its bound may be disabled while still held, and never hear the release that ends the hold.\n if (spent(k)) return stop();\n step(k);\n tick(k, opts.everyMs ?? REPEAT_EVERY_MS);\n }, ms);\n };\n return {\n start(k) {\n stop();\n const mine = hold;\n key = k;\n step(k);\n // `step` may have ended or replaced this hold.\n if (hold === mine) tick(k, opts.delayMs ?? REPEAT_DELAY_MS);\n },\n stop,\n held: () => key,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/headless",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.1",
|
|
4
4
|
"description": "Headless components: list behavior (roving highlight, typeahead) that a DOM component and a canvas-drawn widget share. No React, no DOM.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsup"
|
|
38
38
|
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@weasel-js/geom": "1.14.1",
|
|
41
|
+
"@weasel-js/quantity": "1.14.1"
|
|
42
|
+
},
|
|
39
43
|
"publishConfig": {
|
|
40
44
|
"access": "public",
|
|
41
45
|
"provenance": true
|