@weasel-js/prefs 0.0.0-stage → 1.9.4

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,572 @@
1
+ import { Display, UnitEntry, InfinityText, UnitSystem, Unit } from '@weasel-js/quantity';
2
+
3
+ /** What a {@link PrefAction}'s `run` is told. */
4
+ interface PrefActionContext {
5
+ /** The path the action sits at on the surface drawing it. */
6
+ path: string;
7
+ }
8
+ /** How a schema-driven UI should present an action leaf. */
9
+ type PrefActionControl = 'button' | 'icon';
10
+ /**
11
+ * A leaf that holds no value: a button among the rows or in a bar. A store
12
+ * neither reads nor writes one, and its path is not a {@link PrefPath}.
13
+ *
14
+ * It fires one of two things. `action` names an action the host registered,
15
+ * by key, which keeps the leaf plain data: it survives JSON, so a layout
16
+ * holding it can be saved and exported. `run` is code the schema carries.
17
+ * A leaf with both fires `action`. While a promise `run` returned is
18
+ * pending, the button is disabled.
19
+ */
20
+ interface PrefAction extends PrefBase<'action', undefined> {
21
+ /** The key of a registered action to fire: its id, or `id:variant` for one
22
+ * variant of a parametric action. The leaf's `name` and `icon` are what
23
+ * the button shows, over the registered action's own. */
24
+ action?: string;
25
+ /** What to call when no `action` is named. */
26
+ run?: (ctx: PrefActionContext) => void | Promise<void>;
27
+ /** The button's text. Default: the leaf's `name`. */
28
+ label?: string;
29
+ /** `button` reads its label; `icon` is the icon alone, with the label in
30
+ * its tooltip. Unset: a button in a row, an icon in a bar. */
31
+ control?: PrefActionControl;
32
+ }
33
+
34
+ /**
35
+ * A second place for another leaf: its row draws that leaf's control and
36
+ * reads and writes that leaf's value, so one setting can sit on two pages.
37
+ * It holds no value of its own. A store neither reads nor writes one, and
38
+ * its path is not a {@link PrefPath}.
39
+ *
40
+ * `name` and `description` left empty are the target's.
41
+ */
42
+ interface PrefAlias extends PrefBase<'alias', undefined> {
43
+ /** The leaf shown here, by the full path its value is read and written at (`'view.grid'`). */
44
+ of: string;
45
+ }
46
+ /**
47
+ * The leaf an alias stands for and the path its value lives at, following an
48
+ * alias of an alias to its end. `undefined` when a path along the way names
49
+ * no leaf, or the aliases lead back to one already passed.
50
+ */
51
+ declare function prefAliasTarget(alias: PrefAlias, leafAt: (path: string) => PrefLeaf | undefined): {
52
+ path: string;
53
+ leaf: PrefLeaf;
54
+ } | undefined;
55
+ /** A leaf drawn through an alias: the target, under the alias's own name and description where it gives them. */
56
+ declare function prefAliasedLeaf(alias: PrefAlias, target: PrefLeaf): PrefLeaf;
57
+
58
+ /**
59
+ * A line of text among the rows, holding no value: `name` is the line and
60
+ * `description` the note under it. A store neither reads nor writes one, and
61
+ * its path is not a {@link PrefPath}.
62
+ */
63
+ type PrefLabel = PrefBase<'label', undefined>;
64
+ /** A rule drawn between the rows, holding no value and drawing none of its own text. */
65
+ type PrefSeparator = PrefBase<'separator', undefined>;
66
+
67
+ /**
68
+ * A leaf whose value is an array, every entry described by `item`.
69
+ *
70
+ * `item` is an ordinary leaf, built-in or app-defined, so a list of objects,
71
+ * of lists or of a custom kind is declared the way a list of numbers is, and
72
+ * each entry is validated and drawn as that leaf would be on its own. Its
73
+ * `default` is what a new entry starts as, and its `name` names one entry
74
+ * (`'Phase'` under a list named `'Phases'`).
75
+ */
76
+ interface PrefList extends PrefBase<'list', readonly unknown[]> {
77
+ item: PrefLeaf;
78
+ /** A stored list shorter than this reads as the default, and a UI stops removing at it. */
79
+ minItems?: number;
80
+ /** A stored list longer than this reads cut to it, and a UI stops adding at it. */
81
+ maxItems?: number;
82
+ }
83
+
84
+ /**
85
+ * A leaf whose value is an object keyed by strings nobody declared, every
86
+ * value described by `item`: the keyed counterpart of a {@link PrefList}.
87
+ * `item` is an ordinary leaf of any kind; its `default` is what a new entry
88
+ * starts as, and its `name` names one entry's value.
89
+ */
90
+ interface PrefMap extends PrefBase<'map', Readonly<Record<string, unknown>>> {
91
+ item: PrefLeaf;
92
+ }
93
+
94
+ /**
95
+ * A leaf whose value is one of several object shapes, told apart by the
96
+ * string its `tag` field holds: `{ type: 'linear', angle: 90 }` or
97
+ * `{ type: 'radial', radius: 1 }` under `tag: 'type'`.
98
+ *
99
+ * Each variant is an `object` leaf keyed by its tag value. Its `children`
100
+ * are the fields beside the tag, its `name` is what a person chooses, and
101
+ * its `default` (without the tag) is what the value becomes when they choose
102
+ * it. A stored value whose tag names no variant reads as the leaf's default.
103
+ */
104
+ interface PrefUnion extends PrefBase<'union', unknown> {
105
+ tag: string;
106
+ variants: Record<string, PrefObject>;
107
+ }
108
+ /** The variant `value` is, or `undefined` when its tag names none. */
109
+ declare function prefVariantOf(leaf: PrefUnion, value: unknown): [key: string, variant: PrefObject] | undefined;
110
+ /** The value a union takes on switching to the variant at `key`: that variant's default, tagged. */
111
+ declare function prefVariantDefault(leaf: PrefUnion, key: string): Record<string, unknown>;
112
+
113
+ /** The value types a built-in pref leaf can hold. */
114
+ type PrefKind = 'number' | 'boolean' | 'string' | 'enum' | 'color' | 'paint' | 'object' | 'list' | 'map' | 'union' | 'field' | 'action' | 'alias' | 'label' | 'separator';
115
+ /**
116
+ * Leaves that share one row in a compact property UI (weasel-ui
117
+ * `SelectionPanel`, labkit's `ControlPanel`), declared on the first of them.
118
+ * Purely presentational.
119
+ */
120
+ interface PrefPair {
121
+ /** The other leaves on the row, by the full path their values are read and
122
+ * written at (`'pose.y'`, `'camera.type'`), in order. Each must follow the
123
+ * one before it among its siblings. */
124
+ with: string | readonly string[];
125
+ /** What the row reads. Default: the declaring leaf's `name`. */
126
+ label?: string;
127
+ }
128
+ /** The fields every leaf pref shares, keyed by its `kind` and typed by its stored value. */
129
+ interface PrefBase<K extends string, Value> {
130
+ kind: K;
131
+ /** Human-readable label. */
132
+ name: string;
133
+ /** Longer help text — shown in tooltips / a settings pane. */
134
+ description: string;
135
+ /** Fallback when nothing is persisted. */
136
+ default: Value;
137
+ /** Hide from a host app's settings UI by default. */
138
+ hidden?: boolean;
139
+ /** Nothing held where this leaf's value is read means `default`, because
140
+ * whoever reads the value takes it that way (`pose.rotation ?? 0`). A
141
+ * surface over sparse settings reads every leaf so already. One over a
142
+ * scene node, where a missing key otherwise means the node has no such
143
+ * property, shows the default only for a leaf that says this, and still
144
+ * writes nothing until an edit. */
145
+ absentIsDefault?: boolean;
146
+ /** Render full-width with no label row in schema-driven settings UIs
147
+ * (weasel-ui `PrefsForm` honors this for leaves whose control brings
148
+ * its own chrome). */
149
+ block?: boolean;
150
+ /** Glyph naming this leaf in a host UI's icon set (weasel-ui resolves it
151
+ * against `ICON_PATHS`). A plain string because this package ships no
152
+ * icon set and cannot depend on one. Read where a leaf's `name` has nowhere to go — a
153
+ * `pair`ed row is labeled by the pair, so its fields have only the glyph
154
+ * to tell them apart. */
155
+ icon?: string;
156
+ /** Puts this leaf and the ones it names on one row. */
157
+ pair?: PrefPair;
158
+ /** Shorter forms of `name`, longest first. A surface short of room for
159
+ * `name` — a strip of controls on one line, a segment in a `pair`ed row —
160
+ * shows the longest of them that fits. `name` stays the accessible name, so
161
+ * these abbreviate without costing anything. */
162
+ short?: readonly string[];
163
+ /** Never auto. A surface that lets a leaf be left for its owner to decide
164
+ * (weasel-ui `PrefsForm`'s `onAutoChange`, labkit's `ControlPanel`) gives
165
+ * this one no way to. */
166
+ manual?: boolean;
167
+ /** Starts auto, where it would start pinned at `default`. */
168
+ unpinned?: boolean;
169
+ /** What the leaf reads while it is auto, where its owner computes nothing
170
+ * for it. Unset, an auto leaf has no value. */
171
+ autoValue?: Value;
172
+ /** The name of the type this leaf was made from, set by `prefType`. A
173
+ * schema editor shows such a leaf as one closed thing and prints the name
174
+ * in place of its literal. */
175
+ type?: string;
176
+ }
177
+ /** How a schema-driven UI should present a number pref. */
178
+ type PrefNumberControl = 'input' | 'slider';
179
+ /** How a schema-driven UI should present a boolean pref. */
180
+ type PrefBooleanControl = 'checkbox' | 'switch' | 'toggle';
181
+ /** How a schema-driven UI should present a string pref. */
182
+ type PrefStringControl = 'input' | 'textarea';
183
+ /** How a schema-driven UI should present an enum pref. */
184
+ type PrefEnumControl = 'select' | 'radio' | 'toggle' | 'menu';
185
+ /** Display-unit conversion for number leaves whose stored value uses a
186
+ * canonical unit the user shouldn't see (e.g. radians stored, degrees
187
+ * shown). The stored value stays canonical; UIs convert at the edge. */
188
+ interface PrefNumberUnit {
189
+ toDisplay: (stored: number) => number;
190
+ fromDisplay: (display: number) => number;
191
+ /** Shown after the input, e.g. `'°'`. */
192
+ suffix?: string;
193
+ /** Suffixes a person may type, each mapped to the scale that turns a
194
+ * number in that unit into a display number: `{ mm: 0.1, cm: 1 }` for a
195
+ * field showing centimeters. A unit that disagrees with the display unit
196
+ * about zero carries an `offset` as well. */
197
+ accepts?: Readonly<Record<string, UnitEntry>>;
198
+ /** The stored value as display text, suffix included. */
199
+ format?: (stored: number) => string;
200
+ }
201
+ /**
202
+ * A display unit built from a {@link UnitSystem}: values are stored in the
203
+ * system's base and shown in `display`, rounded to `precision` places when
204
+ * given, and every unit in the system is accepted as typed text. `suffix`
205
+ * defaults to the unit's name and is accepted too.
206
+ */
207
+ declare function prefUnit(system: UnitSystem, display: Unit, opts?: {
208
+ precision?: number;
209
+ suffix?: string;
210
+ }): PrefNumberUnit;
211
+ /** A numeric pref, optionally bounded and stepped, and optionally stored in a
212
+ * different unit from the one shown. */
213
+ interface PrefNumber extends PrefBase<'number', number> {
214
+ min?: number;
215
+ max?: number;
216
+ step?: number;
217
+ control?: PrefNumberControl;
218
+ /** How the value shows, speaks and reads back when typed — `compact()`
219
+ * reads `2.00M`. Presentation only: the stored value stays a number. */
220
+ display?: Display;
221
+ unit?: PrefNumberUnit;
222
+ /** Which end of a slider gets a stop for ±Infinity, one step beyond the range. */
223
+ endless?: 'min' | 'max' | 'both';
224
+ /** The word infinity shows as — `'never'`, `'uncapped'`. Default `∞`. */
225
+ infinity?: InfinityText;
226
+ }
227
+ /**
228
+ * Stored-value bridge for a boolean leaf whose field is not a boolean — the
229
+ * flag counterpart of {@link PrefEnumEncoding}. `TextStyle.fontStyle` is
230
+ * the case: it stores `'italic'`, and the control is an Italic toggle.
231
+ */
232
+ interface PrefBooleanEncoding {
233
+ /** Whether `stored` reads as on. */
234
+ read: (stored: unknown, siblings: Record<string, unknown> | undefined) => boolean;
235
+ /** What to store for `on`. `undefined` removes the field. */
236
+ write: (on: boolean, siblings: Record<string, unknown> | undefined) => unknown;
237
+ }
238
+ /** An on/off pref. */
239
+ interface PrefBoolean extends PrefBase<'boolean', boolean> {
240
+ control?: PrefBooleanControl;
241
+ encoding?: PrefBooleanEncoding;
242
+ }
243
+ /** A free-text pref. */
244
+ interface PrefString extends PrefBase<'string', string> {
245
+ control?: PrefStringControl;
246
+ }
247
+ /**
248
+ * Stored-value bridge for an enum leaf whose value is not the option string —
249
+ * the counterpart of {@link PrefNumberUnit}, which does the same for a
250
+ * number stored in a canonical unit.
251
+ *
252
+ * A dash array is the case that needs it: `Stroke.dash` stores lengths, and
253
+ * the thing a person chooses is a style. The presets scale by the stroke's
254
+ * width, so both directions are given the object's other fields — a style is
255
+ * meaningless without the width it is a multiple of.
256
+ */
257
+ interface PrefEnumEncoding<T extends string = string> {
258
+ /**
259
+ * The option `stored` reads as, or `undefined` for none — which a UI shows
260
+ * the way it shows a mixed selection, by selecting nothing.
261
+ *
262
+ * `siblings` is the object the leaf is a field of, or `undefined` when the
263
+ * node does not hold that object (and for a top-level leaf, which has none).
264
+ */
265
+ read: (stored: unknown, siblings: Record<string, unknown> | undefined) => T | undefined;
266
+ /** What to store for `option`. `undefined` removes the field. */
267
+ write: (option: T, siblings: Record<string, unknown> | undefined) => unknown;
268
+ }
269
+ /** What every enum leaf carries; {@link PrefEnum} adds `clearable`. */
270
+ interface PrefEnumBase<T extends string = string> extends PrefBase<'enum', T | undefined> {
271
+ /** `short` holds shorter forms of `label`, longest first, for a segmented
272
+ * control too narrow for the full one — down to a capital or two. `icon`
273
+ * names a glyph in the host UI's set (weasel-ui resolves it against
274
+ * `ICON_PATHS`), drawn in place of any text form where it resolves. It is
275
+ * a plain string because this package ships no icon set and cannot
276
+ * depend on one. The full `label` stays the accessible name, so neither the abbreviation nor the glyph becomes the only thing naming
277
+ * the option.
278
+ *
279
+ * `disabled` marks an option a control reports but cannot author — the
280
+ * value a stored form reads as when it matches nothing offered. Dropping it
281
+ * from the list instead would leave the control selecting nothing and
282
+ * claiming the field is unset. */
283
+ options: readonly {
284
+ value: T;
285
+ label: string;
286
+ short?: readonly string[];
287
+ icon?: string;
288
+ disabled?: boolean;
289
+ }[];
290
+ control?: PrefEnumControl;
291
+ encoding?: PrefEnumEncoding<T>;
292
+ }
293
+ /**
294
+ * A pref with a fixed set of labeled choices.
295
+ *
296
+ * `clearable` says the field may hold none of the options, and that absence
297
+ * is a state of its own rather than a missing value — a script that is
298
+ * neither super nor sub. A toggle control then lets the chosen segment be
299
+ * clicked off, which removes the field. Such a leaf's `default` is
300
+ * `undefined`: a missing value is the only stored form of cleared, so a
301
+ * default would bring an option back the moment the field was cleared.
302
+ */
303
+ type PrefEnum<T extends string = string> = PrefEnumBase<T> & ({
304
+ clearable?: false;
305
+ } | {
306
+ clearable: true;
307
+ default: undefined;
308
+ });
309
+ /** A single color, stored as a hex string. For a value that may also be a
310
+ * gradient or a pattern, use {@link PrefPaint} instead. */
311
+ interface PrefColor extends PrefBase<'color', string> {
312
+ /** Value is `#rrggbb`, or `#rrggbbaa` when `alpha` is set (UIs then
313
+ * offer an opacity control). */
314
+ alpha?: boolean;
315
+ }
316
+ /**
317
+ * Open leaf: any node with a `kind` outside the built-ins. Schema-driven
318
+ * UIs (weasel-ui `PrefsForm` / `SelectionPanel`) dispatch it to an
319
+ * app-supplied renderer. Deliberately NOT index-signatured so concrete
320
+ * app interfaces stay assignable. Mirrors weasel-ui's `PrefCustom`.
321
+ */
322
+ type PrefCustom = PrefBase<string, unknown>;
323
+ /**
324
+ * A whole `FillStyle`, not a color inside one. Use it wherever the value is
325
+ * the tagged paint union — a solid color, a pattern, a gradient — rather
326
+ * than a hex string.
327
+ *
328
+ * Addressing `…fill.color` instead reads `undefined` off a gradient (so the
329
+ * control shows its default and claims the text is black) and writes a
330
+ * hybrid `{ fill: 'gradient', stops, color }` that the renderer's structural
331
+ * `'color' in paint` checks then paint flat solid. The union has to be
332
+ * edited as a union.
333
+ */
334
+ interface PrefPaint extends PrefBase<'paint', unknown> {
335
+ /** Offer an opacity control alongside the color. */
336
+ alpha?: boolean;
337
+ }
338
+ /**
339
+ * A leaf whose value is one object, with its own fields hanging off it.
340
+ *
341
+ * A compound value — a stroke, a shadow, a pattern spec — could be described
342
+ * as several sibling leaves addressing into it (`data.stroke.width`,
343
+ * `data.stroke.cap`). It shouldn't be: each control would then write one field
344
+ * of a value it can only half see, and writing into something that is not an
345
+ * object yet corrupts it. Here the fields are `children` of one leaf, and
346
+ * every edit commits the parent object whole.
347
+ *
348
+ * `children` paths are relative to the object. They are ordinary leaves, so a
349
+ * field that is itself a union (a stroke's `paint`) declares the kind that
350
+ * edits that union. A child may also be a {@link PrefSection}, which
351
+ * organizes the fields under a heading without contributing to the path. A
352
+ * `TextStyle` needs it: its character and paragraph fields belong to one
353
+ * value but read as two lists.
354
+ */
355
+ interface PrefObject extends PrefBase<'object', unknown> {
356
+ children: Record<string, PrefLeaf | PrefSection>;
357
+ /**
358
+ * Lift a non-object value into the object form, for a consumer field that
359
+ * may also be held as a scalar. Called before a child edit is applied;
360
+ * without it a scalar-valued leaf shows its children empty, and an edit
361
+ * starts from the leaf's `default`.
362
+ */
363
+ fromScalar?: (value: unknown) => Record<string, unknown>;
364
+ }
365
+ /**
366
+ * A reference to another field of the same schema, held as the full path its
367
+ * value is read and written at (`'camera.type'`) — the currency fields refer
368
+ * to each other in. A surface offers the fields it draws as the choices.
369
+ */
370
+ interface PrefField extends PrefBase<'field', string> {
371
+ /** Only fields of these kinds may be named. Default: any leaf. */
372
+ kinds?: readonly string[];
373
+ }
374
+ /** One built-in pref leaf. `PrefLeaf` widens this to include
375
+ * app-defined kinds. */
376
+ type BuiltinPref = PrefNumber | PrefBoolean | PrefString | PrefEnum | PrefColor | PrefPaint | PrefObject | PrefList | PrefMap | PrefUnion | PrefField | PrefAction | PrefAlias | PrefLabel | PrefSeparator;
377
+ /**
378
+ * The built-in kinds, as a table. A kind added to {@link PrefKind} is a
379
+ * compile error here, and from here it is one in every renderer's `never`
380
+ * guard — the only thing standing between a new kind and rendering as
381
+ * nothing in four places at once.
382
+ */
383
+ declare const PREF_KINDS: Record<PrefKind, true>;
384
+ /** Which row each pairing among `leaves` puts a path on: a `key` the row's
385
+ * members share (the declaring leaf's path) and the `label` it reads. A
386
+ * path no `pair` names is absent. `leaves` are given by the path each
387
+ * surface reads and writes them at, so references resolve in its terms. */
388
+ declare function pairRowsOf(leaves: Iterable<readonly [path: string, leaf: PrefLeaf]>): Map<string, {
389
+ key: string;
390
+ label: string;
391
+ }>;
392
+ /**
393
+ * Narrows a leaf to the built-in union, so a renderer's switch discriminates
394
+ * on {@link PrefKind} instead of the open `string` that
395
+ * {@link PrefCustom} widens `kind` to. An app-defined kind answers false
396
+ * and belongs to the renderer's custom-renderer path.
397
+ */
398
+ declare function isBuiltinPref(leaf: PrefLeaf): leaf is BuiltinPref;
399
+ /** Built-in or app-defined leaf. */
400
+ type PrefLeaf = BuiltinPref | PrefCustom;
401
+
402
+ /**
403
+ * How a {@link PrefGroup} is drawn: `page` gets an entry in a form's rail and
404
+ * a pane of its own; `tab` shares a tab strip with the `tab` groups beside it;
405
+ * `panel` is a bordered, titled box; `section` is a heading over its rows;
406
+ * `fragment` draws nothing of its own, so its rows sit among its neighbors'
407
+ * as though they were written there. A fragment exists to hold things
408
+ * together: one key for their values to nest under, one node to move.
409
+ */
410
+ type PrefGroupAs = 'page' | 'tab' | 'panel' | 'section' | 'fragment' | 'bar';
411
+ /** How a {@link PrefSection} is drawn. */
412
+ type PrefSectionAs = Exclude<PrefGroupAs, 'page'>;
413
+ /**
414
+ * A group whose key is a path segment: `view` > `gridDensity` is the leaf at
415
+ * `view.gridDensity`, and its value nests the same way. The shape of a
416
+ * preferences schema — what `openPrefs` stores, `PrefsForm` renders, and a
417
+ * tool declares its options as. A key holds no `.`.
418
+ */
419
+ interface PrefGroup {
420
+ /** Heading for the group's rows. **Empty means no heading** — for a group
421
+ * that exists to organize, not to name: one whose children are themselves
422
+ * groups carrying the labels a reader needs. Give it a name whenever the
423
+ * name is the referent (a `Border` group over `Top` / `Right` / `Bottom`
424
+ * reads as nothing without it). */
425
+ name: string;
426
+ description?: string;
427
+ /** How the group is drawn. Unset, its depth decides: a top-level group is a
428
+ * `page`, and one inside another is a `section`. */
429
+ as?: PrefGroupAs;
430
+ children: Record<string, PrefLeaf | PrefGroup>;
431
+ }
432
+ /**
433
+ * A heading over leaves that adds nothing to their paths: each member is
434
+ * addressed by its own key alone, and the section's key only identifies the
435
+ * section. The shape of a node's property schema, where a leaf's key is the
436
+ * whole dotted node path (`pose.x`, `data.fill`), and of the headings inside
437
+ * a {@link PrefObject}, where a key is relative to the object.
438
+ */
439
+ interface PrefSection {
440
+ /** Heading for the section's rows. Empty means no heading, as for a
441
+ * {@link PrefGroup}. */
442
+ name: string;
443
+ description?: string;
444
+ /** How the section is drawn, as a {@link PrefGroup}'s `as` — without
445
+ * `page`, since nothing that draws sections has a rail. Unset: a heading
446
+ * over its rows. */
447
+ as?: PrefSectionAs;
448
+ members: Record<string, PrefLeaf | PrefSection>;
449
+ }
450
+ /** Whether a top-level group gets a page of its own, and not a place on the page of the root's own leaves. */
451
+ declare function prefGroupIsPage(group: PrefGroup): boolean;
452
+
453
+ type Join<A extends string, B extends string> = A extends '' ? B : `${A}.${B}`;
454
+ /** Every value's dotted path in a `PrefGroup` schema, where a group's key is a
455
+ * segment; an `object` leaf is one path, its fields are not, and an `action`,
456
+ * `alias`, `label`, or `separator` leaf, holding no value, has none. A `PrefSection`
457
+ * has no such paths: each of its leaves is addressed by its own key. */
458
+ type PrefPath<G, Prefix extends string = ''> = G extends {
459
+ children: infer C;
460
+ } ? string extends keyof C ? Join<Prefix, string> : {
461
+ [K in keyof C & string]: C[K] extends {
462
+ kind: string;
463
+ } ? C[K] extends {
464
+ kind: 'action' | 'alias' | 'label' | 'separator';
465
+ } ? never : Join<Prefix, K> : C[K] extends {
466
+ children: Record<string, unknown>;
467
+ } ? PrefPath<C[K], Join<Prefix, K>> : never;
468
+ }[keyof C & string] : never;
469
+ /** The leaf at `P`. */
470
+ type PrefAtPath<G, P extends string> = G extends {
471
+ children: infer C;
472
+ } ? P extends `${infer H}.${infer Rest}` ? H extends keyof C ? C[H] extends {
473
+ kind: string;
474
+ } ? never : PrefAtPath<C[H], Rest> : never : P extends keyof C ? C[P] extends {
475
+ kind: string;
476
+ } ? C[P] : never : never : never;
477
+ type AllOf<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
478
+ /** The fields an `object` leaf's `children` describe, sections folded in. Each is optional: an object may omit any. */
479
+ type PrefFieldsOf<C> = {
480
+ [K in keyof C as C[K] extends {
481
+ kind: string;
482
+ } ? K : never]?: PrefValueOf<C[K]>;
483
+ } & AllOf<{
484
+ [K in keyof C]: C[K] extends {
485
+ members: infer M;
486
+ } ? PrefFieldsOf<M> : never;
487
+ }[keyof C]>;
488
+ /** What a leaf holds: its kind's value type for a built-in kind, the type of
489
+ * its `default` for an app-defined one. A list holds an array of what its
490
+ * `item` holds, a map a record of it, and a union one variant's fields
491
+ * under that variant's tag — where the schema keeps `tag` a literal
492
+ * (`tag: 'type' as const`); one widened to `string` is typed by its default. */
493
+ type PrefValueOf<L> = L extends {
494
+ kind: 'number';
495
+ } ? number : L extends {
496
+ kind: 'boolean';
497
+ } ? boolean : L extends {
498
+ kind: 'string' | 'color' | 'field';
499
+ } ? string : L extends {
500
+ kind: 'paint';
501
+ } ? unknown : L extends {
502
+ kind: 'enum';
503
+ options: readonly {
504
+ value: infer T;
505
+ }[];
506
+ } ? L extends {
507
+ clearable: true;
508
+ } ? T | undefined : T : L extends {
509
+ kind: 'list';
510
+ item: infer I;
511
+ } ? PrefValueOf<I>[] : L extends {
512
+ kind: 'map';
513
+ item: infer I;
514
+ } ? Record<string, PrefValueOf<I>> : L extends {
515
+ kind: 'union';
516
+ tag: infer T extends string;
517
+ variants: infer V;
518
+ default: infer D;
519
+ } ? string extends keyof V | T ? D : {
520
+ [K in keyof V & string]: {
521
+ [P in T]: K;
522
+ } & (V[K] extends {
523
+ children: infer C;
524
+ } ? PrefFieldsOf<C> : unknown);
525
+ }[keyof V & string] : L extends {
526
+ default: infer D;
527
+ } ? D : unknown;
528
+ /** What the leaf at `P` holds. */
529
+ type PrefValueAt<G, P extends string> = PrefValueOf<PrefAtPath<G, P>>;
530
+
531
+ /** One leaf changing, as a store subscriber hears it: the value readers now
532
+ * see, and whether this store or another writer made the change. */
533
+ interface PrefChange {
534
+ path: string;
535
+ value: unknown;
536
+ origin: 'local' | 'remote';
537
+ }
538
+ /** Values for one schema, held in memory and persisted behind. */
539
+ interface PrefsStore<S extends PrefGroup> {
540
+ readonly schema: S;
541
+ /** The leaf's value, repaired against the schema; its default when unset. */
542
+ get<P extends PrefPath<S>>(path: P): PrefValueAt<S, P>;
543
+ set<P extends PrefPath<S>>(path: P, value: PrefValueAt<S, P>): void;
544
+ /** Unset every leaf at or under `path`; with no path, all. */
545
+ reset(path?: string): void;
546
+ /** Whether the leaf has a value of its own rather than following its default. */
547
+ isSet(path: PrefPath<S>): boolean;
548
+ /** Every leaf's value as a nested tree. The same object until something changes. */
549
+ values(): Record<string, unknown>;
550
+ /** The leaves following their default. The same set until something changes. */
551
+ unset(): ReadonlySet<string>;
552
+ /** Every stored record as a nested tree, unrepaired, including records no
553
+ * leaf describes. The same object until something changes. */
554
+ stored(): Record<string, unknown>;
555
+ /** `changes` is empty when only records no leaf describes changed. */
556
+ subscribe(listener: (changes: PrefChange[]) => void): () => void;
557
+ /** False while the store cannot persist: storage has not been read yet, or
558
+ * was written by a newer schema. Changes still apply for this session, and
559
+ * ones made before a late read lands are persisted when it does. */
560
+ readonly writable: boolean;
561
+ /** Try again now to read storage that could not be read at open, ahead of
562
+ * the store's own retries. Resolves whether storage has been read. */
563
+ read(): Promise<boolean>;
564
+ /** Resolves once storage has been read: at once for a store that opened
565
+ * with its values, and when a later read lands for one that could not. */
566
+ whenRead(): Promise<void>;
567
+ /** Resolves true when every queued write reached storage. */
568
+ flush(): Promise<boolean>;
569
+ close(): Promise<void>;
570
+ }
571
+
572
+ export { type PrefList as A, type BuiltinPref as B, type PrefMap as C, type PrefNumberControl as D, type PrefNumberUnit as E, type PrefPaint as F, type PrefPair as G, type PrefPath as H, type PrefSectionAs as I, type PrefSeparator as J, type PrefString as K, type PrefStringControl as L, type PrefUnion as M, type PrefValueAt as N, type PrefValueOf as O, type PrefLeaf as P, isBuiltinPref as Q, pairRowsOf as R, prefAliasTarget as S, prefAliasedLeaf as T, prefGroupIsPage as U, prefUnit as V, prefVariantDefault as W, prefVariantOf as X, type PrefGroup as a, type PrefSection as b, type PrefNumber as c, type PrefObject as d, type PrefsStore as e, type PrefKind as f, PREF_KINDS as g, type PrefAction as h, type PrefActionContext as i, type PrefActionControl as j, type PrefAlias as k, type PrefAtPath as l, type PrefBase as m, type PrefBoolean as n, type PrefBooleanControl as o, type PrefBooleanEncoding as p, type PrefChange as q, type PrefColor as r, type PrefCustom as s, type PrefEnum as t, type PrefEnumBase as u, type PrefEnumControl as v, type PrefEnumEncoding as w, type PrefField as x, type PrefGroupAs as y, type PrefLabel as z };
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "@weasel-js/prefs",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.9.4",
4
+ "description": "A preferences schema, a store that loads, validates, migrates and persists values against it, and React hooks over the store.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ },
16
+ "./react": {
17
+ "import": "./dist/react.js",
18
+ "types": "./dist/react.d.ts"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "author": "orochi235",
23
+ "homepage": "https://orochi235.github.io/weasel/",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/orochi235/weasel.git",
27
+ "directory": "packages/prefs"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/orochi235/weasel/issues"
31
+ },
32
+ "engines": {
33
+ "node": ">=22"
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "README.md",
38
+ "LICENSE"
39
+ ],
40
+ "scripts": {
41
+ "build": "tsup"
42
+ },
43
+ "dependencies": {
44
+ "@weasel-js/quantity": "1.9.4",
45
+ "@weasel-js/registry": "1.9.4",
46
+ "@weasel-js/storage": "1.9.4"
47
+ },
48
+ "peerDependencies": {
49
+ "react": ">=18"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "react": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "provenance": true
59
+ }
60
+ }