@keemakr/ui-sdk 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -1
- package/catalog.json +7 -19
- package/dist/build.js +48 -2
- package/dist/check-cli.d.ts +9 -0
- package/dist/check-cli.js +145 -0
- package/dist/check.d.ts +166 -0
- package/dist/check.js +408 -0
- package/dist/cli.js +5 -1
- package/dist/define.d.ts +7 -1
- package/dist/define.js +3 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -2
- package/dist/media.d.ts +12 -0
- package/dist/media.js +19 -1
- package/dist/negotiate.d.ts +11 -2
- package/dist/negotiate.js +24 -1
- package/dist/node-limits.d.ts +3 -3
- package/dist/node-limits.js +2 -2
- package/dist/page.d.ts +307 -0
- package/dist/page.js +482 -0
- package/dist/reach.d.ts +66 -0
- package/dist/reach.js +151 -0
- package/dist/schema.d.ts +46 -0
- package/dist/schema.js +80 -8
- package/dist/stories.d.ts +63 -0
- package/dist/stories.js +297 -0
- package/dist/testkit.d.ts +13 -2
- package/dist/testkit.js +8 -2
- package/dist/ui-build.d.ts +46 -0
- package/dist/ui-build.js +61 -0
- package/dist/ui-nodes.d.ts +195 -0
- package/dist/ui-nodes.js +197 -0
- package/dist/ui.d.ts +61 -0
- package/dist/ui.js +22 -0
- package/package.json +22 -2
package/dist/page.js
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page format (WS28-S13): the one shape a builder page has, wherever it is made or read. Kee's
|
|
3
|
+
* page skill, installed agents, the builder's editor, export and import all write exactly this, and
|
|
4
|
+
* core stores it as an operator artifact of the kind `page`.
|
|
5
|
+
*
|
|
6
|
+
* import { pageDocumentSchema, stampNodeIds } from '@keemakr/ui-sdk/page';
|
|
7
|
+
*
|
|
8
|
+
* A page is a thin envelope over a screen tree:
|
|
9
|
+
*
|
|
10
|
+
* { format: 'keemakr.page', formatVersion: 1, catalogId, kind: 'page' | 'template', title,
|
|
11
|
+
* tree, bindings }
|
|
12
|
+
*
|
|
13
|
+
* - `catalogId` names the catalog the page was built against (`catalog.json`'s `catalogId`). The
|
|
14
|
+
* checker flags a page built against another one.
|
|
15
|
+
* - `tree` is the screen tree, as `screenTreeSchema` defines it. The envelope does not parse it:
|
|
16
|
+
* a page that does not pass the schema yet is still a page, and saving it keeps the person's
|
|
17
|
+
* work. What is wrong with the tree is the checker's to say (`@keemakr/ui-sdk/check`), and only a
|
|
18
|
+
* push or a pack submission is refused for it.
|
|
19
|
+
* - `bindings` are what the page may read and call: an installed dataset by name, a declared
|
|
20
|
+
* action by its stable id. Empty until the builder slices that bind a page fill it.
|
|
21
|
+
*
|
|
22
|
+
* Node ids. Every node of a stored page carries a stable `id`, so an edit names the node it
|
|
23
|
+
* changes, never its position: a move or an insert above it does not send the edit to another
|
|
24
|
+
* node. `stampNodeIds` gives an id to each node that has none (or a duplicate, or one that is not
|
|
25
|
+
* an id), and leaves every other id as it is. The store stamps on every save. The screen tree's own
|
|
26
|
+
* node schemas do not list `id`: they keep unknown keys out of what a renderer is handed, so an id
|
|
27
|
+
* stays in the stored page and never reaches a client.
|
|
28
|
+
*/
|
|
29
|
+
import { z } from 'zod';
|
|
30
|
+
import { placedNodes } from './check.js';
|
|
31
|
+
export const PAGE_FORMAT = 'keemakr.page';
|
|
32
|
+
export const PAGE_FORMAT_VERSION = 1;
|
|
33
|
+
export const PAGE_KINDS = ['page', 'template'];
|
|
34
|
+
/** The caps on the envelope. The tree's own caps are NODE_LIMITS. */
|
|
35
|
+
export const PAGE_LIMITS = { titleLength: 120, catalogIdLength: 100, bindings: 50 };
|
|
36
|
+
/** A node id: short, URL-safe, unique within its page. */
|
|
37
|
+
export const PAGE_NODE_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
38
|
+
export const pageNodeIdSchema = z
|
|
39
|
+
.string()
|
|
40
|
+
.regex(PAGE_NODE_ID, 'a node id: 1-64 of A-Z a-z 0-9 _ -');
|
|
41
|
+
const bindingName = z.string().min(1).max(128);
|
|
42
|
+
/** One thing the page may read or call. */
|
|
43
|
+
export const pageBindingSchema = z.discriminatedUnion('kind', [
|
|
44
|
+
/** An installed dataset a `dataset` node may show, by name. */
|
|
45
|
+
z.strictObject({ kind: z.literal('dataset'), dataset: bindingName }),
|
|
46
|
+
/** An action an installed Pack declares, by its stable id, that a press or a form may call. */
|
|
47
|
+
z.strictObject({ kind: z.literal('action'), action: bindingName }),
|
|
48
|
+
]);
|
|
49
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
50
|
+
export const pageDocumentSchema = z
|
|
51
|
+
.strictObject({
|
|
52
|
+
format: z.literal(PAGE_FORMAT),
|
|
53
|
+
formatVersion: z.literal(PAGE_FORMAT_VERSION),
|
|
54
|
+
catalogId: z.string().min(1).max(PAGE_LIMITS.catalogIdLength),
|
|
55
|
+
kind: z.enum(PAGE_KINDS),
|
|
56
|
+
title: z.string().min(1).max(PAGE_LIMITS.titleLength),
|
|
57
|
+
/** The screen tree. Checked by `checkPage`, not parsed here: see the header. */
|
|
58
|
+
tree: z.record(z.string(), z.unknown()),
|
|
59
|
+
bindings: z.array(pageBindingSchema).max(PAGE_LIMITS.bindings).default([]),
|
|
60
|
+
})
|
|
61
|
+
.superRefine((page, ctx) => {
|
|
62
|
+
// A node id, where one is given, is an id and is the only one of its kind on the page.
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
for (const { node, path } of placedNodes(page.tree, 'tree')) {
|
|
65
|
+
if (!('id' in node))
|
|
66
|
+
continue;
|
|
67
|
+
const id = node.id;
|
|
68
|
+
const at = `${path}.id`;
|
|
69
|
+
if (typeof id !== 'string' || !PAGE_NODE_ID.test(id)) {
|
|
70
|
+
ctx.addIssue({ code: 'custom', path: [at], message: 'not a node id' });
|
|
71
|
+
}
|
|
72
|
+
else if (seen.has(id)) {
|
|
73
|
+
ctx.addIssue({ code: 'custom', path: [at], message: `node id "${id}" is used twice` });
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
seen.add(id);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* A copy of the document in which every node has a stable id: a node with none, with one that is
|
|
82
|
+
* not an id, or with one an earlier node already holds gets a new one from `newId`. Every other id
|
|
83
|
+
* is kept, so ids stay the same from one save to the next. The input is never changed. Anything
|
|
84
|
+
* that is not a page (no `tree` object) comes back as a copy, untouched.
|
|
85
|
+
*
|
|
86
|
+
* `newId` is the caller's (core passes a random one): this package reads no clock and no random
|
|
87
|
+
* source. An id it returns that is taken, or is not an id, is asked for again, a bounded number
|
|
88
|
+
* of times.
|
|
89
|
+
*/
|
|
90
|
+
export function stampNodeIds(document, newId) {
|
|
91
|
+
const copy = structuredClone(document);
|
|
92
|
+
if (!isObject(copy) || !isObject(copy.tree))
|
|
93
|
+
return { document: copy, stamped: 0 };
|
|
94
|
+
const nodes = placedNodes(copy.tree, 'tree');
|
|
95
|
+
const taken = new Set();
|
|
96
|
+
// First pass: every valid id is taken by the first node that holds it.
|
|
97
|
+
const keeps = new Set();
|
|
98
|
+
nodes.forEach(({ node }, index) => {
|
|
99
|
+
const id = node.id;
|
|
100
|
+
if (typeof id === 'string' && PAGE_NODE_ID.test(id) && !taken.has(id)) {
|
|
101
|
+
taken.add(id);
|
|
102
|
+
keeps.add(index);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
let stamped = 0;
|
|
106
|
+
nodes.forEach(({ node }, index) => {
|
|
107
|
+
if (keeps.has(index))
|
|
108
|
+
return;
|
|
109
|
+
let id = '';
|
|
110
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
111
|
+
const candidate = newId();
|
|
112
|
+
if (PAGE_NODE_ID.test(candidate) && !taken.has(candidate)) {
|
|
113
|
+
id = candidate;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!id)
|
|
118
|
+
throw new Error('stampNodeIds: newId gave no free node id');
|
|
119
|
+
taken.add(id);
|
|
120
|
+
node.id = id;
|
|
121
|
+
stamped++;
|
|
122
|
+
});
|
|
123
|
+
return { document: copy, stamped };
|
|
124
|
+
}
|
|
125
|
+
// ── Edits (WS28-S16) ────────────────────────────────────────────────────────────────────────────
|
|
126
|
+
/**
|
|
127
|
+
* An edit to a stored page is a list of patch ops, applied in order to the page's newest version.
|
|
128
|
+
* Kee, an installed agent and the editor all send these, and core applies them in one place
|
|
129
|
+
* (`applyPageEdit`). Every op names the node it changes by its stable id, never its position, so
|
|
130
|
+
* an edit made on top of a person's version changes only what it names.
|
|
131
|
+
*
|
|
132
|
+
* { op: 'add', node, at?: { parent?, tab?, index? } } a new block (it may hold children)
|
|
133
|
+
* { op: 'update', id, set } change a node's fields; `null` removes one
|
|
134
|
+
* { op: 'move', id, to: { parent?, tab?, index? } } the node and everything in it
|
|
135
|
+
* { op: 'remove', id } the node and everything in it
|
|
136
|
+
* { op: 'add_binding', binding } declare what the page may read or call
|
|
137
|
+
* { op: 'update_binding', binding, to } swap one declared binding for another
|
|
138
|
+
* { op: 'remove_binding', binding }
|
|
139
|
+
*
|
|
140
|
+
* There is no op that replaces the whole tree or the whole page: a list of ops that tries one
|
|
141
|
+
* (`replace`, `regenerate`, a `tree` or a page where an op belongs) is `regenerate_refused`. A
|
|
142
|
+
* changed page is made by naming each change, so the person's own work around it stays theirs.
|
|
143
|
+
*
|
|
144
|
+
* A place (`at`, `to`) is a container's list of children: the page's top level when `parent` is
|
|
145
|
+
* omitted, a `section` by its id, or one tab of a `tabs` node (`tab` is that tab's key). `index`
|
|
146
|
+
* is the position in that list, counted after a moved node has been taken out; omitted or past the
|
|
147
|
+
* end, the node goes last. A table row's `expand` holds nodes too: they can be updated, moved out
|
|
148
|
+
* and removed there, but nothing is added or moved into one.
|
|
149
|
+
*
|
|
150
|
+
* `update` changes a node's own fields. It never changes `id` or `type`, and it never adds, moves
|
|
151
|
+
* or drops a child node: a `section`'s `children` are only changed by add, move and remove, and a
|
|
152
|
+
* `tabs` node's `tabs` or a `table`'s `rows` may be set only when every node inside them stays the
|
|
153
|
+
* same node in the same place, unchanged field for field (a tab renamed, a row's values changed).
|
|
154
|
+
* A node inside is changed by an op on its own id.
|
|
155
|
+
*
|
|
156
|
+
* An added node keeps an `id` it brings when that id is free on the page, so a later op in the same
|
|
157
|
+
* edit can name it (add a section, then add into it); a node without one is given one. Ids are
|
|
158
|
+
* checked again when core stores the page.
|
|
159
|
+
*
|
|
160
|
+
* The edit is all or nothing: the first op that cannot apply refuses the whole edit, with its
|
|
161
|
+
* index and a stable code.
|
|
162
|
+
*/
|
|
163
|
+
/** The caps on one edit. The page's own caps still apply to what it makes. */
|
|
164
|
+
export const PAGE_EDIT_LIMITS = { ops: 100 };
|
|
165
|
+
/** The op names. */
|
|
166
|
+
export const PAGE_EDIT_OPS = [
|
|
167
|
+
'add',
|
|
168
|
+
'update',
|
|
169
|
+
'move',
|
|
170
|
+
'remove',
|
|
171
|
+
'add_binding',
|
|
172
|
+
'update_binding',
|
|
173
|
+
'remove_binding',
|
|
174
|
+
];
|
|
175
|
+
/** Names of ops that would replace the tree or the page whole. Refused, never applied. */
|
|
176
|
+
export const REGENERATE_OPS = [
|
|
177
|
+
'replace',
|
|
178
|
+
'replace_tree',
|
|
179
|
+
'replace_page',
|
|
180
|
+
'set_tree',
|
|
181
|
+
'set_page',
|
|
182
|
+
'regenerate',
|
|
183
|
+
];
|
|
184
|
+
/** Where a node goes. */
|
|
185
|
+
export const pageEditPlaceSchema = z.strictObject({
|
|
186
|
+
/** A `section` or `tabs` node by id. Omitted: the page's top level. */
|
|
187
|
+
parent: pageNodeIdSchema.optional(),
|
|
188
|
+
/** The tab's key, when `parent` is a `tabs` node. Refused anywhere else. */
|
|
189
|
+
tab: z.string().min(1).max(64).optional(),
|
|
190
|
+
/** The position in that list. Omitted or past the end: last. */
|
|
191
|
+
index: z.number().int().min(0).optional(),
|
|
192
|
+
});
|
|
193
|
+
const editNodeSchema = z
|
|
194
|
+
.record(z.string(), z.unknown())
|
|
195
|
+
.refine((node) => typeof node.type === 'string' && node.type.length > 0, 'a node with a `type`');
|
|
196
|
+
export const pageEditOpSchema = z.discriminatedUnion('op', [
|
|
197
|
+
z.strictObject({
|
|
198
|
+
op: z.literal('add'),
|
|
199
|
+
node: editNodeSchema,
|
|
200
|
+
at: pageEditPlaceSchema.optional(),
|
|
201
|
+
}),
|
|
202
|
+
z.strictObject({
|
|
203
|
+
op: z.literal('update'),
|
|
204
|
+
id: pageNodeIdSchema,
|
|
205
|
+
/** The fields to set. A `null` value removes that field. */
|
|
206
|
+
set: z.record(z.string(), z.unknown()),
|
|
207
|
+
}),
|
|
208
|
+
z.strictObject({ op: z.literal('move'), id: pageNodeIdSchema, to: pageEditPlaceSchema }),
|
|
209
|
+
z.strictObject({ op: z.literal('remove'), id: pageNodeIdSchema }),
|
|
210
|
+
z.strictObject({ op: z.literal('add_binding'), binding: pageBindingSchema }),
|
|
211
|
+
z.strictObject({
|
|
212
|
+
op: z.literal('update_binding'),
|
|
213
|
+
binding: pageBindingSchema,
|
|
214
|
+
to: pageBindingSchema,
|
|
215
|
+
}),
|
|
216
|
+
z.strictObject({ op: z.literal('remove_binding'), binding: pageBindingSchema }),
|
|
217
|
+
]);
|
|
218
|
+
export const pageEditOpsSchema = z.array(pageEditOpSchema).min(1).max(PAGE_EDIT_LIMITS.ops);
|
|
219
|
+
const refusal = (code, message, index) => ({
|
|
220
|
+
ok: false,
|
|
221
|
+
code,
|
|
222
|
+
message,
|
|
223
|
+
...(index === undefined ? {} : { index }),
|
|
224
|
+
});
|
|
225
|
+
const looksLikeAWholeTree = (value) => 'tree' in value || 'document' in value || 'page' in value || 'children' in value;
|
|
226
|
+
/**
|
|
227
|
+
* Read an edit: a list of ops, or the refusal. Anything that would replace the tree or the page
|
|
228
|
+
* whole (a page or a tree in place of the list, an op named `replace` or `regenerate`, an op of no
|
|
229
|
+
* known name that carries a tree) is `regenerate_refused`, never `invalid_ops`, so the caller is
|
|
230
|
+
* told to name its changes rather than to fix a field.
|
|
231
|
+
*/
|
|
232
|
+
export function parsePageEditOps(input) {
|
|
233
|
+
if (!Array.isArray(input)) {
|
|
234
|
+
if (isObject(input) && (looksLikeAWholeTree(input) || 'format' in input)) {
|
|
235
|
+
return refusal('regenerate_refused', 'An edit is a list of ops, never a whole page or tree.');
|
|
236
|
+
}
|
|
237
|
+
return refusal('invalid_ops', 'An edit is a list of ops.');
|
|
238
|
+
}
|
|
239
|
+
for (const [index, op] of input.entries()) {
|
|
240
|
+
if (!isObject(op))
|
|
241
|
+
continue;
|
|
242
|
+
const name = op.op;
|
|
243
|
+
const known = typeof name === 'string' && PAGE_EDIT_OPS.includes(name);
|
|
244
|
+
if ((typeof name === 'string' && REGENERATE_OPS.includes(name)) ||
|
|
245
|
+
(!known && looksLikeAWholeTree(op))) {
|
|
246
|
+
return refusal('regenerate_refused', 'An edit names each change by node id; it never replaces the whole tree.', index);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const parsed = pageEditOpsSchema.safeParse(input);
|
|
250
|
+
if (!parsed.success) {
|
|
251
|
+
const issue = parsed.error.issues[0];
|
|
252
|
+
const at = typeof issue?.path[0] === 'number' ? issue.path[0] : undefined;
|
|
253
|
+
return refusal('invalid_ops', `${issue?.path.join('.') || 'ops'}: ${issue?.message ?? 'not an edit'}`, at);
|
|
254
|
+
}
|
|
255
|
+
return { ok: true, ops: parsed.data };
|
|
256
|
+
}
|
|
257
|
+
/** The fields of a container that hold its child nodes. */
|
|
258
|
+
const CHILD_FIELDS = ['children', 'tabs', 'rows'];
|
|
259
|
+
/** Every node of the tree that has an id, by id: the first node that holds it. */
|
|
260
|
+
function locate(tree) {
|
|
261
|
+
const out = new Map();
|
|
262
|
+
for (const placed of placedNodes(tree, 'tree')) {
|
|
263
|
+
const id = placed.node.id;
|
|
264
|
+
if (typeof id === 'string' && !out.has(id))
|
|
265
|
+
out.set(id, placed);
|
|
266
|
+
}
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
/** Every node inside `node`, not the node itself. */
|
|
270
|
+
const nodesInside = (node) => placedNodes({ children: [node] }, '')
|
|
271
|
+
.slice(1)
|
|
272
|
+
.map((p) => p.node);
|
|
273
|
+
/** Two JSON values are the same value. */
|
|
274
|
+
function sameJson(a, b) {
|
|
275
|
+
if (a === b)
|
|
276
|
+
return true;
|
|
277
|
+
if (Array.isArray(a)) {
|
|
278
|
+
return Array.isArray(b) && a.length === b.length && a.every((v, i) => sameJson(v, b[i]));
|
|
279
|
+
}
|
|
280
|
+
if (isObject(a) && isObject(b)) {
|
|
281
|
+
const keys = Object.keys(a);
|
|
282
|
+
return (keys.length === Object.keys(b).length &&
|
|
283
|
+
keys.every((k) => Object.hasOwn(b, k) && sameJson(a[k], b[k])));
|
|
284
|
+
}
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Every node inside `before` is still in `after`, unchanged, at the same place: the same path
|
|
289
|
+
* (which tab, which row, which position) and the same node, field for field.
|
|
290
|
+
*/
|
|
291
|
+
function sameNodesInside(before, after) {
|
|
292
|
+
const was = placedNodes({ children: [before] }, '').slice(1);
|
|
293
|
+
const now = placedNodes({ children: [after] }, '').slice(1);
|
|
294
|
+
return (was.length === now.length &&
|
|
295
|
+
was.every((p, i) => p.path === now[i].path && sameJson(p.node, now[i].node)));
|
|
296
|
+
}
|
|
297
|
+
class EditRefused extends Error {
|
|
298
|
+
code;
|
|
299
|
+
constructor(code, message) {
|
|
300
|
+
super(message);
|
|
301
|
+
this.code = code;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** The list a place names, made when a container has none yet. */
|
|
305
|
+
function placeList(tree, nodes, at) {
|
|
306
|
+
if (!at?.parent) {
|
|
307
|
+
if (at?.tab !== undefined)
|
|
308
|
+
throw new EditRefused('tab_not_found', 'The top level has no tabs.');
|
|
309
|
+
if (!Array.isArray(tree.children))
|
|
310
|
+
tree.children = [];
|
|
311
|
+
return tree.children;
|
|
312
|
+
}
|
|
313
|
+
const found = nodes.get(at.parent);
|
|
314
|
+
if (!found)
|
|
315
|
+
throw new EditRefused('node_not_found', `No node "${at.parent}" on the page.`);
|
|
316
|
+
if (found.inExpand) {
|
|
317
|
+
throw new EditRefused('not_a_container', 'Nothing is added to a table row’s expand.');
|
|
318
|
+
}
|
|
319
|
+
const parent = found.node;
|
|
320
|
+
if (parent.type === 'section') {
|
|
321
|
+
if (at.tab !== undefined)
|
|
322
|
+
throw new EditRefused('tab_not_found', 'A section has no tabs.');
|
|
323
|
+
if (!Array.isArray(parent.children))
|
|
324
|
+
parent.children = [];
|
|
325
|
+
return parent.children;
|
|
326
|
+
}
|
|
327
|
+
if (parent.type === 'tabs') {
|
|
328
|
+
const tab = Array.isArray(parent.tabs)
|
|
329
|
+
? parent.tabs.find((t) => isObject(t) && t.key === at.tab)
|
|
330
|
+
: undefined;
|
|
331
|
+
if (!tab)
|
|
332
|
+
throw new EditRefused('tab_not_found', `No tab "${at.tab ?? ''}" on "${at.parent}".`);
|
|
333
|
+
if (!Array.isArray(tab.children))
|
|
334
|
+
tab.children = [];
|
|
335
|
+
return tab.children;
|
|
336
|
+
}
|
|
337
|
+
throw new EditRefused('not_a_container', `"${at.parent}" is not a section or tabs node.`);
|
|
338
|
+
}
|
|
339
|
+
function insertAt(list, node, index) {
|
|
340
|
+
const at = index === undefined ? list.length : Math.min(index, list.length);
|
|
341
|
+
list.splice(at, 0, node);
|
|
342
|
+
}
|
|
343
|
+
const bindingKey = (b) => b.kind === 'dataset' ? `dataset:${b.dataset}` : `action:${b.action}`;
|
|
344
|
+
const sameBinding = (a, b) => bindingKey(a) === bindingKey(b);
|
|
345
|
+
const describeBinding = (b) => b.kind === 'dataset' ? `dataset "${b.dataset}"` : `action "${b.action}"`;
|
|
346
|
+
function applyOne(page, op, newId) {
|
|
347
|
+
const tree = page.tree;
|
|
348
|
+
const bindings = (Array.isArray(page.bindings) ? page.bindings : []);
|
|
349
|
+
page.bindings = bindings;
|
|
350
|
+
const nodes = locate(tree);
|
|
351
|
+
const find = (id) => {
|
|
352
|
+
const found = nodes.get(id);
|
|
353
|
+
if (!found)
|
|
354
|
+
throw new EditRefused('node_not_found', `No node "${id}" on the page.`);
|
|
355
|
+
return found;
|
|
356
|
+
};
|
|
357
|
+
switch (op.op) {
|
|
358
|
+
case 'add': {
|
|
359
|
+
const node = structuredClone(op.node);
|
|
360
|
+
const added = [node, ...nodesInside(node)];
|
|
361
|
+
const taken = new Set(nodes.keys());
|
|
362
|
+
for (const n of added) {
|
|
363
|
+
if (typeof n.id !== 'string')
|
|
364
|
+
continue;
|
|
365
|
+
if (taken.has(n.id))
|
|
366
|
+
throw new EditRefused('id_taken', `The page already has "${n.id}".`);
|
|
367
|
+
taken.add(n.id);
|
|
368
|
+
}
|
|
369
|
+
// Ids for the nodes that bring none (or one that is not an id), so the next op of the same
|
|
370
|
+
// edit can find them; the store stamps the same way, and keeps these.
|
|
371
|
+
for (const n of added) {
|
|
372
|
+
if (typeof n.id === 'string' && PAGE_NODE_ID.test(n.id))
|
|
373
|
+
continue;
|
|
374
|
+
let id = '';
|
|
375
|
+
for (let attempt = 0; attempt < 8 && !id; attempt++) {
|
|
376
|
+
const candidate = newId();
|
|
377
|
+
if (PAGE_NODE_ID.test(candidate) && !taken.has(candidate))
|
|
378
|
+
id = candidate;
|
|
379
|
+
}
|
|
380
|
+
if (!id)
|
|
381
|
+
throw new Error('applyPageEditOps: newId gave no free node id');
|
|
382
|
+
taken.add(id);
|
|
383
|
+
n.id = id;
|
|
384
|
+
}
|
|
385
|
+
insertAt(placeList(tree, nodes, op.at), node, op.at?.index);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
case 'update': {
|
|
389
|
+
const { node } = find(op.id);
|
|
390
|
+
for (const key of ['id', 'type']) {
|
|
391
|
+
if (key in op.set) {
|
|
392
|
+
throw new EditRefused('structural_update', `An update never changes a node's ${key}.`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (node.type === 'section' && 'children' in op.set) {
|
|
396
|
+
throw new EditRefused('structural_update', 'A section’s children change by add, move and remove, never by update.');
|
|
397
|
+
}
|
|
398
|
+
const next = { ...node };
|
|
399
|
+
for (const [key, value] of Object.entries(op.set)) {
|
|
400
|
+
if (value === null)
|
|
401
|
+
delete next[key];
|
|
402
|
+
else
|
|
403
|
+
next[key] = structuredClone(value);
|
|
404
|
+
}
|
|
405
|
+
// A tab renamed or a row's values changed are this node's own; the nodes inside are not.
|
|
406
|
+
if (CHILD_FIELDS.some((field) => field in op.set) && !sameNodesInside(node, next)) {
|
|
407
|
+
throw new EditRefused('structural_update', 'An update leaves every node inside this one unchanged, in its place: change those ' +
|
|
408
|
+
'with update, add, move or remove on their own ids.');
|
|
409
|
+
}
|
|
410
|
+
for (const key of Object.keys(node))
|
|
411
|
+
delete node[key];
|
|
412
|
+
Object.assign(node, next);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
case 'move': {
|
|
416
|
+
const from = find(op.id);
|
|
417
|
+
if (op.to.parent !== undefined) {
|
|
418
|
+
const inside = nodesInside(from.node).some((n) => n.id === op.to.parent);
|
|
419
|
+
if (op.to.parent === op.id || inside) {
|
|
420
|
+
throw new EditRefused('move_into_itself', `"${op.id}" cannot go inside itself.`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
// Resolve the destination before taking the node out, so a refused move changes nothing.
|
|
424
|
+
const list = placeList(tree, nodes, op.to);
|
|
425
|
+
from.list.splice(from.index, 1);
|
|
426
|
+
insertAt(list, from.node, op.to.index);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
case 'remove': {
|
|
430
|
+
const from = find(op.id);
|
|
431
|
+
from.list.splice(from.index, 1);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
case 'add_binding': {
|
|
435
|
+
if (bindings.some((b) => sameBinding(b, op.binding))) {
|
|
436
|
+
throw new EditRefused('binding_exists', `The page already declares ${describeBinding(op.binding)}.`);
|
|
437
|
+
}
|
|
438
|
+
bindings.push(structuredClone(op.binding));
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
case 'update_binding':
|
|
442
|
+
case 'remove_binding': {
|
|
443
|
+
const at = bindings.findIndex((b) => sameBinding(b, op.binding));
|
|
444
|
+
if (at < 0) {
|
|
445
|
+
throw new EditRefused('binding_not_found', `The page does not declare ${describeBinding(op.binding)}.`);
|
|
446
|
+
}
|
|
447
|
+
if (op.op === 'remove_binding') {
|
|
448
|
+
bindings.splice(at, 1);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (bindings.some((b, i) => i !== at && sameBinding(b, op.to))) {
|
|
452
|
+
throw new EditRefused('binding_exists', `The page already declares ${describeBinding(op.to)}.`);
|
|
453
|
+
}
|
|
454
|
+
bindings[at] = structuredClone(op.to);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Apply an edit to a page: a new document, the input unchanged. All or nothing: the first op that
|
|
461
|
+
* cannot apply refuses the edit with its index. `newId` gives ids to the nodes an `add` brings
|
|
462
|
+
* without one (core passes a random one; this package reads no random source).
|
|
463
|
+
*
|
|
464
|
+
* The result is not checked here. Core bounds it, stamps it and parses it as a page when it
|
|
465
|
+
* stores it, and runs the checker on it as on any save.
|
|
466
|
+
*/
|
|
467
|
+
export function applyPageEditOps(document, ops, newId) {
|
|
468
|
+
const page = structuredClone(document);
|
|
469
|
+
if (!isObject(page.tree))
|
|
470
|
+
return refusal('invalid_ops', 'The page has no tree.');
|
|
471
|
+
for (const [index, op] of ops.entries()) {
|
|
472
|
+
try {
|
|
473
|
+
applyOne(page, op, newId);
|
|
474
|
+
}
|
|
475
|
+
catch (error) {
|
|
476
|
+
if (error instanceof EditRefused)
|
|
477
|
+
return refusal(error.code, error.message, index);
|
|
478
|
+
throw error;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return { ok: true, document: page };
|
|
482
|
+
}
|
package/dist/reach.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type ChannelBuild, type ClientRelease, type ClientTarget, type UiFeature, type UiSdkSnapshot } from './negotiate.js';
|
|
2
|
+
/** Core's own module-screen renderer (dash), which draws this package's schema. */
|
|
3
|
+
export declare const CORE_RENDERER: "dash";
|
|
4
|
+
export type ReachTarget = typeof CORE_RENDERER | ClientTarget;
|
|
5
|
+
/**
|
|
6
|
+
* How one client shows a node:
|
|
7
|
+
* - `drawn`: its renderer draws the node (and the feature, when one was asked about);
|
|
8
|
+
* - `without_feature`: it draws the node, but not the feature asked about. Negotiation folds the
|
|
9
|
+
* feature into something the client draws, or drops the feature and keeps the node;
|
|
10
|
+
* - `replaced`: it would draw the node, but a node that carries the feature asked about is
|
|
11
|
+
* replaced or dropped when the client lacks it (`FEATURE_REPLACES`): a form with a confirm
|
|
12
|
+
* becomes one line, a disabled or chat-press body button becomes muted text, a focus-press
|
|
13
|
+
* control is dropped;
|
|
14
|
+
* - `fallback`: it does not draw the node; negotiation sends a stand-in (`fallbackNode`);
|
|
15
|
+
* - `core_expands`: core replaces the node (a dataset) with plain blocks before the client sees
|
|
16
|
+
* it, so what the client draws depends on those blocks (a board, a table), not on this node.
|
|
17
|
+
* Ask about the blocks it expands into.
|
|
18
|
+
*/
|
|
19
|
+
export type ReachLevel = 'drawn' | 'without_feature' | 'replaced' | 'fallback' | 'core_expands';
|
|
20
|
+
export interface ClientReach {
|
|
21
|
+
client: ReachTarget;
|
|
22
|
+
/**
|
|
23
|
+
* Where this reach was read from: `current` for core's renderer, the release row's `since`
|
|
24
|
+
* ("0.2.3"), or `dev channel` for a client build that takes the current schema.
|
|
25
|
+
*/
|
|
26
|
+
since: string;
|
|
27
|
+
/** The schema that client parses with. */
|
|
28
|
+
parses: UiSdkSnapshot;
|
|
29
|
+
level: ReachLevel;
|
|
30
|
+
/**
|
|
31
|
+
* What the stand-in is, when `level` is `fallback`: `text` is a line of text or markdown, and
|
|
32
|
+
* `table` is a table of the same numbers (a heatmap; one wider than a table can hold comes as
|
|
33
|
+
* text). tests/ui-catalogue/internal-reach.test.ts pins this against `fallbackNode` on every
|
|
34
|
+
* catalogue sample.
|
|
35
|
+
*/
|
|
36
|
+
fallbackAs?: 'text' | 'table';
|
|
37
|
+
/** One line for a check message or the preview: "drawn", "not drawn, fallback text". */
|
|
38
|
+
label: string;
|
|
39
|
+
}
|
|
40
|
+
/** Which client builds to read: the stable release rows, or the dev channel where one exists. */
|
|
41
|
+
export type ReachChannel = 'stable' | 'dev';
|
|
42
|
+
export interface ReachOptions {
|
|
43
|
+
/** Also ask about one feature of the node. It must be the node's own or a cross-node one. */
|
|
44
|
+
feature?: UiFeature;
|
|
45
|
+
/**
|
|
46
|
+
* A client version to read instead of that client's newest release ("mobile": "0.2.0"): the
|
|
47
|
+
* row negotiation would pick for a client sending that version.
|
|
48
|
+
*/
|
|
49
|
+
versions?: Partial<Record<ClientTarget, string>>;
|
|
50
|
+
/**
|
|
51
|
+
* `dev` reads a client's dev-channel build (CHANNEL_BUILDS) where it has one, and that build
|
|
52
|
+
* wins over a `versions` entry for the same client. Default stable.
|
|
53
|
+
*/
|
|
54
|
+
channel?: ReachChannel;
|
|
55
|
+
releases?: readonly ClientRelease[];
|
|
56
|
+
builds?: readonly ChannelBuild[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What core's renderer and each client draw of `nodeType` (and, when given, of one feature of
|
|
60
|
+
* it). By default each client is read at its newest stable release; `versions` reads an older
|
|
61
|
+
* one and `channel: 'dev'` reads the dev-channel build. Core's renderer comes first, then the
|
|
62
|
+
* clients in the order CLIENT_RELEASES first names them. Throws on a node type this package does
|
|
63
|
+
* not know, or a feature of another node: a caller asking about a typo must not be told
|
|
64
|
+
* "fallback" as if the question were real.
|
|
65
|
+
*/
|
|
66
|
+
export declare function reachByClient(nodeType: string, options?: ReachOptions): ClientReach[];
|