@weasel-js/prefs-schema 0.0.0-stage → 1.19.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/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-SGUOG5QO.js +570 -0
- package/dist/chunk-SGUOG5QO.js.map +1 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.js +434 -0
- package/dist/index.js.map +1 -0
- package/dist/internal-Ad6SCtbG.d.ts +1032 -0
- package/dist/internal.d.ts +3 -0
- package/dist/internal.js +3 -0
- package/dist/internal.js.map +1 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 orochi235
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @weasel-js/prefs-schema
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The declaration half of [`@weasel-js/prefs`](../prefs/README.md): the `pref`
|
|
4
|
+
builders, the leaf, group and section types, units, conditions and the status
|
|
5
|
+
they give a leaf, and repairing a stored value against a leaf. No store, no
|
|
6
|
+
storage, no React.
|
|
7
|
+
|
|
8
|
+
Part of [weasel](https://github.com/orochi235/weasel), a domain-agnostic
|
|
9
|
+
scene-graph engine for React. See the
|
|
10
|
+
[API reference](https://orochi235.github.io/weasel/#pkg/prefs-schema).
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @weasel-js/prefs-schema
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { pref, type PrefGroup } from '@weasel-js/prefs-schema';
|
|
18
|
+
|
|
19
|
+
const params: PrefGroup = pref.group('Offset', {
|
|
20
|
+
distance: pref.number('Distance', 4, { min: 0, unit: { scale: 1, suffix: 'px' } }),
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
A package that declares settings and leaves storing them to its host (an
|
|
25
|
+
action's `params`, a tool's options, a node's properties) depends on this one.
|
|
26
|
+
`@weasel-js/prefs` re-exports all of it beside the store, so an app that opens
|
|
27
|
+
a store imports from there and never needs this package by name.
|
|
28
|
+
|
|
29
|
+
- `@weasel-js/prefs-schema/internal` — not public API. The helpers
|
|
30
|
+
`@weasel-js/prefs` builds its store and edit model on; it changes without
|
|
31
|
+
notice.
|
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { unitSymbol, decimal, formatUnit, unitScale, decimalPlaces } from '@weasel-js/quantity';
|
|
2
|
+
import { setOwn, matchQuery, isPlainObject } from '@weasel-js/registry';
|
|
3
|
+
|
|
4
|
+
// src/alias.ts
|
|
5
|
+
function prefAliasTarget(alias, leafAt) {
|
|
6
|
+
const passed = /* @__PURE__ */ new Set();
|
|
7
|
+
let path = alias.of;
|
|
8
|
+
for (; ; ) {
|
|
9
|
+
if (passed.has(path)) return void 0;
|
|
10
|
+
passed.add(path);
|
|
11
|
+
const leaf = leafAt(path);
|
|
12
|
+
if (leaf === void 0) return void 0;
|
|
13
|
+
if (leaf.kind !== "alias") return { path, leaf };
|
|
14
|
+
path = leaf.of;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function prefAliasName(alias, target) {
|
|
18
|
+
if (alias.name !== "") return alias.name;
|
|
19
|
+
return target === void 0 ? "Alias" : target.name;
|
|
20
|
+
}
|
|
21
|
+
function prefAliasedLeaf(alias, target) {
|
|
22
|
+
return {
|
|
23
|
+
...target,
|
|
24
|
+
name: prefAliasName(alias, target),
|
|
25
|
+
description: alias.description === "" ? target.description : alias.description,
|
|
26
|
+
...alias.access ? { access: alias.access } : {},
|
|
27
|
+
...alias.tags ? { tags: [...target.tags ?? [], ...alias.tags] } : {}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function prefUnitAccepts(unit) {
|
|
31
|
+
return unit.suffix === void 0 ? { ...unit.accepts } : { [unit.suffix]: 1, ...unit.accepts };
|
|
32
|
+
}
|
|
33
|
+
function isPrefLinearUnit(unit) {
|
|
34
|
+
return typeof unit.toDisplay !== "function";
|
|
35
|
+
}
|
|
36
|
+
var SHOWN = "shown";
|
|
37
|
+
var DECIMAL_PLACES = 12;
|
|
38
|
+
function storedOf(shown, scale, offset) {
|
|
39
|
+
const raw = shown * scale + offset;
|
|
40
|
+
if (!Number.isFinite(raw)) return raw;
|
|
41
|
+
const places = Math.max(decimalPlaces(shown) + decimalPlaces(scale), decimalPlaces(offset));
|
|
42
|
+
return places > DECIMAL_PLACES ? raw : Number(raw.toFixed(places));
|
|
43
|
+
}
|
|
44
|
+
function resolvePrefUnit(unit) {
|
|
45
|
+
const suffix = unit.suffix === void 0 ? void 0 : unitSymbol(unit.suffix, { compact: unit.compact }).text;
|
|
46
|
+
if (!isPrefLinearUnit(unit)) {
|
|
47
|
+
return {
|
|
48
|
+
...unit,
|
|
49
|
+
...suffix === void 0 ? {} : { suffix },
|
|
50
|
+
toDisplayExact: unit.toDisplay,
|
|
51
|
+
format: unit.format ?? ((stored) => `${unit.toDisplay(stored)}${suffix ?? ""}`)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const { scale, offset = 0, precision, accepts } = unit;
|
|
55
|
+
const system = { base: "stored", units: { [SHOWN]: { factor: scale, offset } } };
|
|
56
|
+
const toDisplayExact = (stored) => (stored - offset) / scale;
|
|
57
|
+
return {
|
|
58
|
+
toDisplay: (stored) => {
|
|
59
|
+
const shown = toDisplayExact(stored);
|
|
60
|
+
return precision === void 0 ? shown : Number(shown.toFixed(precision));
|
|
61
|
+
},
|
|
62
|
+
toDisplayExact,
|
|
63
|
+
fromDisplay: (shown) => storedOf(shown, scale, offset),
|
|
64
|
+
format: (stored) => `${formatUnit(stored, SHOWN, system, { precision: precision ?? 2, suffix: false, pad: precision !== void 0 })}${suffix ?? ""}`,
|
|
65
|
+
...precision === void 0 ? {} : { precision, display: decimal({ places: precision, grouping: false }) },
|
|
66
|
+
...suffix === void 0 ? {} : { suffix },
|
|
67
|
+
...accepts === void 0 ? {} : { accepts }
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function prefUnit(system, display, opts) {
|
|
71
|
+
let self;
|
|
72
|
+
try {
|
|
73
|
+
self = unitScale(system, display);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
throw new Error(`prefUnit: ${e instanceof Error ? e.message : String(e)}`);
|
|
76
|
+
}
|
|
77
|
+
const suffix = opts?.suffix ?? display;
|
|
78
|
+
const accepts = { [suffix]: 1 };
|
|
79
|
+
for (const name of Object.keys(system.units)) {
|
|
80
|
+
const other = unitScale(system, name);
|
|
81
|
+
const factor = other.factor / self.factor;
|
|
82
|
+
const offset = (other.offset - self.offset) / self.factor;
|
|
83
|
+
accepts[name] = offset === 0 ? factor : { factor, offset };
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
suffix,
|
|
87
|
+
scale: self.factor,
|
|
88
|
+
...self.offset === 0 ? {} : { offset: self.offset },
|
|
89
|
+
accepts,
|
|
90
|
+
...opts?.precision === void 0 ? {} : { precision: opts.precision },
|
|
91
|
+
...opts?.compact ? { compact: true } : {}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
var PX_UNIT = { scale: 1, suffix: "px" };
|
|
95
|
+
function prefDisplayBounds(p) {
|
|
96
|
+
const ends = p.endless === void 0 ? {} : { endless: p.endless };
|
|
97
|
+
if (!p.unit) return { min: p.min, max: p.max, step: p.step ?? 1, ...ends, ...p.stops ? { stops: p.stops } : {} };
|
|
98
|
+
const { toDisplay, toDisplayExact, precision } = resolvePrefUnit(p.unit);
|
|
99
|
+
const backward = toDisplay(1) < toDisplay(0);
|
|
100
|
+
const lo = p.min === void 0 ? void 0 : toDisplay(p.min);
|
|
101
|
+
const hi = p.max === void 0 ? void 0 : toDisplay(p.max);
|
|
102
|
+
const step = p.step === void 0 ? 1 : displayStep(Math.abs(toDisplayExact(p.step) - toDisplayExact(0)), precision);
|
|
103
|
+
const flip = { min: "max", max: "min", both: "both" };
|
|
104
|
+
const endless = backward && p.endless !== void 0 ? flip[p.endless] : p.endless;
|
|
105
|
+
const stops = p.stops?.map(toDisplay);
|
|
106
|
+
return {
|
|
107
|
+
min: backward ? hi : lo,
|
|
108
|
+
max: backward ? lo : hi,
|
|
109
|
+
step,
|
|
110
|
+
...endless === void 0 ? {} : { endless },
|
|
111
|
+
...stops ? { stops } : {}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function displayStep(exact, precision) {
|
|
115
|
+
if (precision === void 0) return exact;
|
|
116
|
+
const quantum = 10 ** -precision;
|
|
117
|
+
const quanta = Math.max(1, Math.ceil(Number((exact / quantum).toFixed(9))));
|
|
118
|
+
return Number((quanta * quantum).toFixed(precision));
|
|
119
|
+
}
|
|
120
|
+
function isPrefLeaf(node) {
|
|
121
|
+
return "kind" in node;
|
|
122
|
+
}
|
|
123
|
+
var VALUELESS = /* @__PURE__ */ new Set(["action", "actions", "alias", "label", "separator"]);
|
|
124
|
+
function prefHoldsValue(leaf) {
|
|
125
|
+
return !VALUELESS.has(leaf.kind);
|
|
126
|
+
}
|
|
127
|
+
function isPrefSection(node) {
|
|
128
|
+
return !isPrefLeaf(node) && "members" in node;
|
|
129
|
+
}
|
|
130
|
+
function prefSectionLeaves(members) {
|
|
131
|
+
return Object.entries(members).flatMap(
|
|
132
|
+
([key, child]) => isPrefLeaf(child) ? [[key, child]] : prefSectionLeaves(child.members)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
function prefLeafAt(schema, path) {
|
|
136
|
+
if (isPrefSection(schema)) return prefSectionLeaves(schema.members).find(([key]) => key === path)?.[1];
|
|
137
|
+
let node = schema;
|
|
138
|
+
for (const seg of path.split(".")) {
|
|
139
|
+
if (isPrefLeaf(node) || !Object.hasOwn(node.children, seg)) return void 0;
|
|
140
|
+
node = node.children[seg];
|
|
141
|
+
}
|
|
142
|
+
return isPrefLeaf(node) ? node : void 0;
|
|
143
|
+
}
|
|
144
|
+
function prefValueAtPath(values, path) {
|
|
145
|
+
let cur = values;
|
|
146
|
+
for (const seg of path.split(".")) {
|
|
147
|
+
if (cur == null || typeof cur !== "object" || !Object.hasOwn(cur, seg)) return void 0;
|
|
148
|
+
cur = cur[seg];
|
|
149
|
+
}
|
|
150
|
+
return cur;
|
|
151
|
+
}
|
|
152
|
+
function visibleFields(fields, keepEmpty) {
|
|
153
|
+
const out = {};
|
|
154
|
+
for (const [key, child] of Object.entries(fields)) {
|
|
155
|
+
if (isPrefLeaf(child)) {
|
|
156
|
+
const kept = visiblePrefSubtree(child, false, keepEmpty);
|
|
157
|
+
if (kept) setOwnPrefValue(out, key, kept);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const members = visibleFields(child.members, keepEmpty);
|
|
161
|
+
if (Object.keys(members).length > 0 || keepEmpty) setOwnPrefValue(out, key, { ...child, members });
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
function visiblePrefSubtree(node, showHidden, keepEmpty = false) {
|
|
166
|
+
if (isPrefLeaf(node)) {
|
|
167
|
+
if (node.hidden && !showHidden) return null;
|
|
168
|
+
if (showHidden || node.kind !== "object") return node;
|
|
169
|
+
return { ...node, children: visibleFields(node.children, keepEmpty) };
|
|
170
|
+
}
|
|
171
|
+
const group = node;
|
|
172
|
+
const children = {};
|
|
173
|
+
for (const [key, child] of Object.entries(group.children)) {
|
|
174
|
+
const kept = visiblePrefSubtree(child, showHidden, keepEmpty);
|
|
175
|
+
if (kept) setOwnPrefValue(children, key, kept);
|
|
176
|
+
}
|
|
177
|
+
if (Object.keys(children).length === 0 && !keepEmpty) return null;
|
|
178
|
+
return { ...node, children };
|
|
179
|
+
}
|
|
180
|
+
var VERSION_RECORD = "$version";
|
|
181
|
+
function prefLeaves(schema) {
|
|
182
|
+
const out = /* @__PURE__ */ new Map();
|
|
183
|
+
const walk = (group, prefix) => {
|
|
184
|
+
for (const [key, child] of Object.entries(group.children)) {
|
|
185
|
+
if (key.includes(".")) {
|
|
186
|
+
throw new Error(`[prefs] schema key "${key}" contains "."; group keys are path segments`);
|
|
187
|
+
}
|
|
188
|
+
if (prefix === "" && key === VERSION_RECORD) {
|
|
189
|
+
throw new Error(`[prefs] schema key "${VERSION_RECORD}" is reserved`);
|
|
190
|
+
}
|
|
191
|
+
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
192
|
+
if (isPrefLeaf(child)) out.set(path, child);
|
|
193
|
+
else walk(child, path);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
walk(schema, "");
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
function setOwnPrefValue(tree, key, value) {
|
|
200
|
+
setOwn(tree, key, value);
|
|
201
|
+
}
|
|
202
|
+
function assignPrefValueAtPath(root, path, value) {
|
|
203
|
+
const parts = path.split(".");
|
|
204
|
+
let cursor = root;
|
|
205
|
+
for (const part of parts.slice(0, -1)) {
|
|
206
|
+
if (!Object.hasOwn(cursor, part) || cursor[part] == null) setOwnPrefValue(cursor, part, {});
|
|
207
|
+
cursor = cursor[part];
|
|
208
|
+
}
|
|
209
|
+
setOwnPrefValue(cursor, parts[parts.length - 1], value);
|
|
210
|
+
}
|
|
211
|
+
function nestPrefValues(values) {
|
|
212
|
+
const tree = {};
|
|
213
|
+
for (const [path, value] of Object.entries(values ?? {})) assignPrefValueAtPath(tree, path, value);
|
|
214
|
+
return tree;
|
|
215
|
+
}
|
|
216
|
+
function flattenPrefValues(schema, tree) {
|
|
217
|
+
const out = [];
|
|
218
|
+
for (const path of prefLeaves(schema).keys()) {
|
|
219
|
+
const value = prefValueAtPath(tree, path);
|
|
220
|
+
if (value !== void 0) out.push([path, value]);
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
function prefLeafMatches(pref, path, query) {
|
|
225
|
+
return matchQuery(query, { text: [pref.name, pref.description, path], tags: pref.tags }) !== null;
|
|
226
|
+
}
|
|
227
|
+
function filterPrefSubtree(node, query, path = "", over) {
|
|
228
|
+
const q = query.trim().toLowerCase();
|
|
229
|
+
if (q === "") return node;
|
|
230
|
+
const within = over ?? (isPrefLeaf(node) ? void 0 : node);
|
|
231
|
+
const find = (p) => within && prefLeafAt(within, p);
|
|
232
|
+
if (isPrefLeaf(node)) {
|
|
233
|
+
const target = node.kind === "alias" ? prefAliasTarget(node, find) : void 0;
|
|
234
|
+
const drawn = target ? prefAliasedLeaf(node, target.leaf) : node;
|
|
235
|
+
return prefLeafMatches(drawn, path, q) ? node : null;
|
|
236
|
+
}
|
|
237
|
+
const group = node;
|
|
238
|
+
if (matchQuery(q, { text: [group.name], tags: group.tags }) !== null) return node;
|
|
239
|
+
const children = {};
|
|
240
|
+
for (const [key, child] of Object.entries(group.children)) {
|
|
241
|
+
const kept = filterPrefSubtree(child, q, path === "" ? key : `${path}.${key}`, within);
|
|
242
|
+
if (kept) setOwnPrefValue(children, key, kept);
|
|
243
|
+
}
|
|
244
|
+
if (Object.keys(children).length === 0) return null;
|
|
245
|
+
return { ...node, children };
|
|
246
|
+
}
|
|
247
|
+
var ORDERINGS = ["above", "below", "atLeast", "atMost"];
|
|
248
|
+
var TEST_KEYS = /* @__PURE__ */ new Set(["path", "from", "is", "isNot", "in", ...ORDERINGS]);
|
|
249
|
+
var isValue = (v) => v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
250
|
+
function formOf(value) {
|
|
251
|
+
if (!isPlainObject(value)) return void 0;
|
|
252
|
+
const keys = Object.keys(value);
|
|
253
|
+
if (keys.length === 1 && (keys[0] === "all" || keys[0] === "any")) return Array.isArray(value[keys[0]]) ? keys[0] : void 0;
|
|
254
|
+
if (keys.length === 1 && keys[0] === "not") return "not";
|
|
255
|
+
if (typeof value.path !== "string" || !keys.every((key) => TEST_KEYS.has(key))) return void 0;
|
|
256
|
+
if (value.from !== void 0 && value.from !== "item") return void 0;
|
|
257
|
+
if (value.is !== void 0 && !isValue(value.is)) return void 0;
|
|
258
|
+
if (value.isNot !== void 0 && !isValue(value.isNot)) return void 0;
|
|
259
|
+
if (value.in !== void 0 && !(Array.isArray(value.in) && value.in.every(isValue))) return void 0;
|
|
260
|
+
return ORDERINGS.every((op) => value[op] === void 0 || typeof value[op] === "number") ? "test" : void 0;
|
|
261
|
+
}
|
|
262
|
+
function partsOf(condition, form) {
|
|
263
|
+
const held = condition;
|
|
264
|
+
return form === "test" ? [] : form === "not" ? [held.not] : held[form];
|
|
265
|
+
}
|
|
266
|
+
function isPrefCondition(value) {
|
|
267
|
+
const form = formOf(value);
|
|
268
|
+
return form !== void 0 && partsOf(value, form).every(isPrefCondition);
|
|
269
|
+
}
|
|
270
|
+
var same = (held, value) => value === null ? held == null : held === value;
|
|
271
|
+
function isSet(held) {
|
|
272
|
+
if (Array.isArray(held)) return held.length > 0;
|
|
273
|
+
if (isPlainObject(held)) return Object.keys(held).length > 0;
|
|
274
|
+
return Boolean(held);
|
|
275
|
+
}
|
|
276
|
+
function testHolds(test, held) {
|
|
277
|
+
const ops = Object.keys(test).filter((key) => key !== "path" && key !== "from" && test[key] !== void 0);
|
|
278
|
+
if (ops.length === 0) return isSet(held);
|
|
279
|
+
if (test.is !== void 0 && !same(held, test.is)) return false;
|
|
280
|
+
if (test.isNot !== void 0 && same(held, test.isNot)) return false;
|
|
281
|
+
if (test.in !== void 0 && !test.in.some((value) => same(held, value))) return false;
|
|
282
|
+
if (!ORDERINGS.some((op) => test[op] !== void 0)) return true;
|
|
283
|
+
if (typeof held !== "number") return false;
|
|
284
|
+
return (test.above === void 0 || held > test.above) && (test.below === void 0 || held < test.below) && (test.atLeast === void 0 || held >= test.atLeast) && (test.atMost === void 0 || held <= test.atMost);
|
|
285
|
+
}
|
|
286
|
+
function prefConditionHolds(condition, read) {
|
|
287
|
+
if ("all" in condition) return condition.all.every((part) => prefConditionHolds(part, read));
|
|
288
|
+
if ("any" in condition) return condition.any.some((part) => prefConditionHolds(part, read));
|
|
289
|
+
if ("not" in condition) return !prefConditionHolds(condition.not, read);
|
|
290
|
+
return testHolds(condition, read(condition.path, condition.from));
|
|
291
|
+
}
|
|
292
|
+
function prefIsShown(node, read) {
|
|
293
|
+
return node.showIf === void 0 || prefConditionHolds(node.showIf, read);
|
|
294
|
+
}
|
|
295
|
+
function prefIsEnabled(node, read) {
|
|
296
|
+
return node.enableIf === void 0 || prefConditionHolds(node.enableIf, read);
|
|
297
|
+
}
|
|
298
|
+
function prefConditionPaths(condition, from) {
|
|
299
|
+
const out = /* @__PURE__ */ new Set();
|
|
300
|
+
const walk = (c) => {
|
|
301
|
+
if ("all" in c) c.all.forEach(walk);
|
|
302
|
+
else if ("any" in c) c.any.forEach(walk);
|
|
303
|
+
else if ("not" in c) walk(c.not);
|
|
304
|
+
else if (c.from === from) out.add(c.path);
|
|
305
|
+
};
|
|
306
|
+
walk(condition);
|
|
307
|
+
return [...out];
|
|
308
|
+
}
|
|
309
|
+
function prefConditionReadsItem(condition) {
|
|
310
|
+
if ("all" in condition) return condition.all.some(prefConditionReadsItem);
|
|
311
|
+
if ("any" in condition) return condition.any.some(prefConditionReadsItem);
|
|
312
|
+
if ("not" in condition) return prefConditionReadsItem(condition.not);
|
|
313
|
+
return condition.from === "item";
|
|
314
|
+
}
|
|
315
|
+
function mapPrefConditionPaths(condition, move) {
|
|
316
|
+
const each = (parts) => {
|
|
317
|
+
const next = parts.map((part) => mapPrefConditionPaths(part, move));
|
|
318
|
+
return next.every((part, i) => part === parts[i]) ? parts : next;
|
|
319
|
+
};
|
|
320
|
+
if ("all" in condition) {
|
|
321
|
+
const all = each(condition.all);
|
|
322
|
+
return all === condition.all ? condition : { all };
|
|
323
|
+
}
|
|
324
|
+
if ("any" in condition) {
|
|
325
|
+
const any = each(condition.any);
|
|
326
|
+
return any === condition.any ? condition : { any };
|
|
327
|
+
}
|
|
328
|
+
if ("not" in condition) {
|
|
329
|
+
const not = mapPrefConditionPaths(condition.not, move);
|
|
330
|
+
return not === condition.not ? condition : { not };
|
|
331
|
+
}
|
|
332
|
+
const path = move(condition.path, condition.from);
|
|
333
|
+
return path === condition.path ? condition : { ...condition, path };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// src/schema.ts
|
|
337
|
+
var PREF_KINDS = {
|
|
338
|
+
number: true,
|
|
339
|
+
boolean: true,
|
|
340
|
+
string: true,
|
|
341
|
+
enum: true,
|
|
342
|
+
color: true,
|
|
343
|
+
paint: true,
|
|
344
|
+
object: true,
|
|
345
|
+
list: true,
|
|
346
|
+
map: true,
|
|
347
|
+
union: true,
|
|
348
|
+
field: true,
|
|
349
|
+
action: true,
|
|
350
|
+
actions: true,
|
|
351
|
+
alias: true,
|
|
352
|
+
label: true,
|
|
353
|
+
separator: true
|
|
354
|
+
};
|
|
355
|
+
function pairRowsOf(leaves) {
|
|
356
|
+
const rows = /* @__PURE__ */ new Map();
|
|
357
|
+
for (const [path, leaf] of leaves) {
|
|
358
|
+
if (!leaf.pair || rows.has(path)) continue;
|
|
359
|
+
const row = { key: path, label: leaf.pair.label ?? leaf.name };
|
|
360
|
+
rows.set(path, row);
|
|
361
|
+
const partners = typeof leaf.pair.with === "string" ? [leaf.pair.with] : leaf.pair.with;
|
|
362
|
+
for (const partner of partners) if (!rows.has(partner)) rows.set(partner, row);
|
|
363
|
+
}
|
|
364
|
+
return rows;
|
|
365
|
+
}
|
|
366
|
+
function isBuiltinPref(leaf) {
|
|
367
|
+
return Object.hasOwn(PREF_KINDS, leaf.kind);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// src/holding.ts
|
|
371
|
+
var UNSEEN = /* @__PURE__ */ Symbol("unseen");
|
|
372
|
+
var under = (at, key) => at === "" ? key : `${at}.${key}`;
|
|
373
|
+
function amongFields(fields, path, at) {
|
|
374
|
+
const exact = fields.find(([key]) => key === path);
|
|
375
|
+
if (exact) return [{ leaf: exact[1], ...at === void 0 ? {} : { within: at } }];
|
|
376
|
+
const outer = fields.find(([key]) => path.startsWith(`${key}.`));
|
|
377
|
+
return outer && insideLeaf(outer[1], path.slice(outer[0].length + 1), under(at ?? "", outer[0]));
|
|
378
|
+
}
|
|
379
|
+
function insideLeaf(leaf, rest, at) {
|
|
380
|
+
if (leaf.kind === "object") return amongFields(prefSectionLeaves(leaf.children), rest, at);
|
|
381
|
+
if (leaf.kind === "union") {
|
|
382
|
+
const { tag, variants } = leaf;
|
|
383
|
+
if (rest === tag) {
|
|
384
|
+
const options = Object.keys(variants).map((value) => ({ value, label: variants[value].name }));
|
|
385
|
+
return [{ leaf: { kind: "enum", name: tag, description: "", default: void 0, options }, within: at }];
|
|
386
|
+
}
|
|
387
|
+
const out = [];
|
|
388
|
+
for (const [key, variant] of Object.entries(variants)) {
|
|
389
|
+
const found = amongFields(prefSectionLeaves(variant.children), rest, at);
|
|
390
|
+
if (found === UNSEEN) return UNSEEN;
|
|
391
|
+
for (const held of found ?? []) out.push({ ...held, variants: [{ at, tag, key }, ...held.variants ?? []] });
|
|
392
|
+
}
|
|
393
|
+
return out.length ? out : void 0;
|
|
394
|
+
}
|
|
395
|
+
return !isBuiltinPref(leaf) || leaf.kind === "list" || leaf.kind === "map" || leaf.kind === "paint" ? UNSEEN : void 0;
|
|
396
|
+
}
|
|
397
|
+
function prefLeavesHolding(schema, path) {
|
|
398
|
+
if (isPrefSection(schema)) return amongFields(prefSectionLeaves(schema.members), path, void 0);
|
|
399
|
+
const steps = path.split(".");
|
|
400
|
+
let node = schema;
|
|
401
|
+
for (let i = 0; i < steps.length; i++) {
|
|
402
|
+
if (isPrefLeaf(node)) return insideLeaf(node, steps.slice(i).join("."), steps.slice(0, i).join("."));
|
|
403
|
+
if (!Object.hasOwn(node.children, steps[i])) return void 0;
|
|
404
|
+
node = node.children[steps[i]];
|
|
405
|
+
}
|
|
406
|
+
return isPrefLeaf(node) ? [{ leaf: node }] : void 0;
|
|
407
|
+
}
|
|
408
|
+
function prefLeavesHoldingIn(item, path) {
|
|
409
|
+
return path === "" ? [{ leaf: item }] : insideLeaf(item, path, "");
|
|
410
|
+
}
|
|
411
|
+
function prefSchemaHolding(schema, path, over) {
|
|
412
|
+
return over === void 0 || prefLeavesHolding(schema, path) !== void 0 ? schema : over;
|
|
413
|
+
}
|
|
414
|
+
var PREF_ELSE = "$else";
|
|
415
|
+
var named = (overrides) => Array.isArray(overrides);
|
|
416
|
+
function overrideOptions(overrides) {
|
|
417
|
+
return named(overrides) ? overrides : Object.keys(overrides);
|
|
418
|
+
}
|
|
419
|
+
var hasRest = (scope) => Object.hasOwn(scope, PREF_ELSE);
|
|
420
|
+
var restOr = (scope, absent) => hasRest(scope) ? scope[PREF_ELSE] : absent;
|
|
421
|
+
var withRest = (scope, rest) => ({ ...scope, [PREF_ELSE]: rest });
|
|
422
|
+
function isPrefScope(value) {
|
|
423
|
+
if (!isPlainObject(value)) return false;
|
|
424
|
+
const keys = Object.keys(value).length - (Object.hasOwn(value, PREF_ELSE) ? 1 : 0);
|
|
425
|
+
return keys === 2 && typeof value.$by === "string" && isPlainObject(value.$in);
|
|
426
|
+
}
|
|
427
|
+
function upgradeScope(value) {
|
|
428
|
+
if (isPrefScope(value)) return mapMembers(value, upgradeScope);
|
|
429
|
+
const old = isPlainObject(value) && Object.keys(value).length === 2 && typeof value.$by === "string" && isPlainObject(value.$of);
|
|
430
|
+
return old ? mapMembers({ $by: value.$by, $in: value.$of }, upgradeScope) : value;
|
|
431
|
+
}
|
|
432
|
+
function selectionWeights(selection) {
|
|
433
|
+
return Object.fromEntries(Object.entries(selection).map(([path, option]) => [path, { [option]: 1 }]));
|
|
434
|
+
}
|
|
435
|
+
var shares = (scope, option, overridesOf) => {
|
|
436
|
+
const overrides = overridesOf?.(scope.$by);
|
|
437
|
+
return overrides !== void 0 && !overrideOptions(overrides).includes(option);
|
|
438
|
+
};
|
|
439
|
+
function scopeOptionValue(scope, option, absent, overridesOf) {
|
|
440
|
+
if (option === void 0 || shares(scope, option, overridesOf)) return restOr(scope, absent);
|
|
441
|
+
if (Object.hasOwn(scope.$in, option)) return scope.$in[option];
|
|
442
|
+
const overrides = overridesOf?.(scope.$by);
|
|
443
|
+
return overrides && !named(overrides) && Object.hasOwn(overrides, option) ? overrides[option] : restOr(scope, absent);
|
|
444
|
+
}
|
|
445
|
+
function weighScope(stored, weights, absent, overridesOf) {
|
|
446
|
+
const out = [];
|
|
447
|
+
const walk = (value, weight) => {
|
|
448
|
+
if (!isPrefScope(value)) {
|
|
449
|
+
out.push({ value, weight });
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const given = weights[value.$by];
|
|
453
|
+
if (given === void 0) {
|
|
454
|
+
if (hasRest(value)) walk(value[PREF_ELSE], weight);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
for (const [option, share] of Object.entries(given)) {
|
|
458
|
+
if (share > 0) walk(scopeOptionValue(value, option, absent, overridesOf), weight * share);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
walk(stored, 1);
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
function memberAt(stored, selection, absent, overridesOf) {
|
|
465
|
+
let value = stored;
|
|
466
|
+
while (isPrefScope(value)) value = scopeOptionValue(value, selection[value.$by], absent, overridesOf);
|
|
467
|
+
return value;
|
|
468
|
+
}
|
|
469
|
+
function dropUnlisted(stored, overridesOf) {
|
|
470
|
+
if (!overridesOf || !isPrefScope(stored)) return stored;
|
|
471
|
+
const overrides = overridesOf(stored.$by);
|
|
472
|
+
const list = overrides && overrideOptions(overrides);
|
|
473
|
+
const stale = list ? Object.keys(stored.$in).filter((option) => !list.includes(option)) : [];
|
|
474
|
+
const kept = stale.length ? { ...stored, $in: Object.fromEntries(Object.entries(stored.$in).filter(([option]) => list.includes(option))) } : stored;
|
|
475
|
+
return mapMembers(kept, (inner) => dropUnlisted(inner, overridesOf));
|
|
476
|
+
}
|
|
477
|
+
function writeDeclared(stored, selectors, under2, now, next, absent, overridesOf, held = true) {
|
|
478
|
+
const [by, ...inner] = selectors;
|
|
479
|
+
if (by === void 0) return next;
|
|
480
|
+
let scope;
|
|
481
|
+
if (isPrefScope(stored) && stored.$by === by) scope = stored;
|
|
482
|
+
else {
|
|
483
|
+
const shared = isPrefScope(stored) ? memberAt(stored, now, absent, overridesOf) : stored;
|
|
484
|
+
scope = held ? { $by: by, $in: {}, [PREF_ELSE]: shared } : { $by: by, $in: {} };
|
|
485
|
+
}
|
|
486
|
+
const option = under2[by];
|
|
487
|
+
if (option === void 0 || shares(scope, option, overridesOf)) {
|
|
488
|
+
return withRest(scope, writeDeclared(scope[PREF_ELSE], inner, under2, now, next, absent, overridesOf, hasRest(scope)));
|
|
489
|
+
}
|
|
490
|
+
const own = scope.$in[option];
|
|
491
|
+
const kept = isPrefScope(own) && own.$by === inner[0] ? writeDeclared(own, inner, under2, now, next, absent, overridesOf) : next;
|
|
492
|
+
return { ...scope, $in: { ...scope.$in, [option]: kept } };
|
|
493
|
+
}
|
|
494
|
+
function mapMembers(scope, fn) {
|
|
495
|
+
let out;
|
|
496
|
+
for (const [option, value] of Object.entries(scope.$in)) {
|
|
497
|
+
const next = fn(value);
|
|
498
|
+
if (next !== value) (out ??= { ...scope.$in })[option] = next;
|
|
499
|
+
}
|
|
500
|
+
const mapped = out ? { ...scope, $in: out } : scope;
|
|
501
|
+
if (!hasRest(scope)) return mapped;
|
|
502
|
+
const rest = fn(scope[PREF_ELSE]);
|
|
503
|
+
return rest === scope[PREF_ELSE] ? mapped : withRest(mapped, rest);
|
|
504
|
+
}
|
|
505
|
+
function mapScopeValues(stored, fn) {
|
|
506
|
+
return isPrefScope(stored) ? mapMembers(stored, (inner) => mapScopeValues(inner, fn)) : fn(stored);
|
|
507
|
+
}
|
|
508
|
+
function scopeSelectors(stored) {
|
|
509
|
+
const out = /* @__PURE__ */ new Set();
|
|
510
|
+
const walk = (value) => {
|
|
511
|
+
if (!isPrefScope(value)) return;
|
|
512
|
+
out.add(value.$by);
|
|
513
|
+
for (const inner of Object.values(value.$in)) walk(inner);
|
|
514
|
+
if (hasRest(value)) walk(value[PREF_ELSE]);
|
|
515
|
+
};
|
|
516
|
+
walk(stored);
|
|
517
|
+
return out;
|
|
518
|
+
}
|
|
519
|
+
function repairScope(stored, allowed, optionsOf, repairMember, overridesOf) {
|
|
520
|
+
const BAD = /* @__PURE__ */ Symbol("bad");
|
|
521
|
+
const walk = (value, met) => {
|
|
522
|
+
if (!isPrefScope(value)) return repairMember(value);
|
|
523
|
+
const options = allowed.includes(value.$by) && !met.includes(value.$by) ? optionsOf(value.$by) : void 0;
|
|
524
|
+
if (options === void 0) return BAD;
|
|
525
|
+
const inner = [...met, value.$by];
|
|
526
|
+
const kept = [];
|
|
527
|
+
for (const option of options ?? Object.keys(value.$in)) {
|
|
528
|
+
if (!Object.hasOwn(value.$in, option) || shares(value, option, overridesOf)) continue;
|
|
529
|
+
const read2 = walk(value.$in[option], inner);
|
|
530
|
+
if (read2 === BAD) return BAD;
|
|
531
|
+
kept.push([option, read2]);
|
|
532
|
+
}
|
|
533
|
+
const out = { $by: value.$by, $in: Object.fromEntries(kept) };
|
|
534
|
+
if (!hasRest(value)) return out;
|
|
535
|
+
const rest = walk(value[PREF_ELSE], inner);
|
|
536
|
+
return rest === BAD ? BAD : withRest(out, rest);
|
|
537
|
+
};
|
|
538
|
+
const read = walk(stored, []);
|
|
539
|
+
return read === BAD ? void 0 : read;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/scopeSpec.ts
|
|
543
|
+
function prefScopesOf(pref) {
|
|
544
|
+
return pref.scopes ?? [];
|
|
545
|
+
}
|
|
546
|
+
function prefOverridesOf(pref, selector) {
|
|
547
|
+
return pref.scopes?.find((scope) => scope.selector === selector)?.overrides;
|
|
548
|
+
}
|
|
549
|
+
function scopeSelectorsOf(pref) {
|
|
550
|
+
return pref.scopes?.length ? pref.scopes.map((scope) => scope.selector) : void 0;
|
|
551
|
+
}
|
|
552
|
+
var lookups = /* @__PURE__ */ new WeakMap();
|
|
553
|
+
function overridesOfPref(pref) {
|
|
554
|
+
if (!lookups.has(pref)) {
|
|
555
|
+
lookups.set(pref, pref.scopes?.some((scope) => scope.overrides !== void 0) ? (selector) => prefOverridesOf(pref, selector) : void 0);
|
|
556
|
+
}
|
|
557
|
+
return lookups.get(pref);
|
|
558
|
+
}
|
|
559
|
+
var withElse = (by, shared) => ({ $by: by, $in: {}, [PREF_ELSE]: shared });
|
|
560
|
+
function prefScopedDefault(pref) {
|
|
561
|
+
if (!pref.scopes?.length) return void 0;
|
|
562
|
+
if (pref.defaultScope !== void 0) return pref.defaultScope;
|
|
563
|
+
let inner;
|
|
564
|
+
for (const scope of [...pref.scopes].reverse()) inner = inner === void 0 ? { $by: scope.selector, $in: {} } : withElse(scope.selector, inner);
|
|
565
|
+
return inner;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export { ORDERINGS, PREF_ELSE, PREF_KINDS, PX_UNIT, UNSEEN, VERSION_RECORD, assignPrefValueAtPath, dropUnlisted, filterPrefSubtree, flattenPrefValues, formOf, isBuiltinPref, isPrefCondition, isPrefLeaf, isPrefLinearUnit, isPrefScope, isPrefSection, mapPrefConditionPaths, mapScopeValues, memberAt, nestPrefValues, overrideOptions, overridesOfPref, pairRowsOf, partsOf, prefAliasName, prefAliasTarget, prefAliasedLeaf, prefConditionHolds, prefConditionPaths, prefConditionReadsItem, prefDisplayBounds, prefHoldsValue, prefIsEnabled, prefIsShown, prefLeafAt, prefLeaves, prefLeavesHolding, prefLeavesHoldingIn, prefOverridesOf, prefSchemaHolding, prefScopedDefault, prefScopesOf, prefSectionLeaves, prefUnit, prefUnitAccepts, prefValueAtPath, repairScope, resolvePrefUnit, scopeOptionValue, scopeSelectors, scopeSelectorsOf, selectionWeights, setOwnPrefValue, upgradeScope, visiblePrefSubtree, weighScope, writeDeclared };
|
|
569
|
+
//# sourceMappingURL=chunk-SGUOG5QO.js.map
|
|
570
|
+
//# sourceMappingURL=chunk-SGUOG5QO.js.map
|