@jxsuite/schema 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Kevin Shenk
3
+ Copyright (c) 2026 Avunu LLC and Jx Suite contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -42,6 +42,13 @@ const schema = await generateSchema(); // component meta-schema object
42
42
  const valid = await validateDocument(doc); // validate a Jx document
43
43
  ```
44
44
 
45
+ ## Documents as data
46
+
47
+ Two subpaths let any host edit and write a Jx document the way Studio does, with no DOM, no reactive store and no yjs:
48
+
49
+ - **`@jxsuite/schema/doc-ops`** is the document-op vocabulary. A `JxDocOp` (`set-key`, `insert-child`, `remove-child`, `set-child`, `move-child`) is a value-carrying mutation addressed by `JxPath`. `applyDocOpToDoc(doc, op)` applies one to a plain tree; `inverseOf(doc, op)` computes the op that undoes it, against the document as it stands before the op, in the coordinates of the document after it; `applyDocOpsWithInverse(doc, ops)` applies a sequence and answers each forward/inverse pair. Studio's history, the canvas iframe's shadow document and the collab bridge all replay through this one implementation, and `@jxsuite/collab/ops` re-exports it. Undoing an insert or move into a node that had no `children` leaves `children: []` behind, as Studio's history always has.
50
+ - **`@jxsuite/schema/json-layout`** is the layout-preserving JSON serializer behind every save. `parseJsonDocument(text)` answers the document and the facts about its text the formatter keeps; `serializeJson(document, layout)` writes it back with those honoured, so a one-value edit is a one-line diff.
51
+
45
52
  ## Component schema coverage
46
53
 
47
54
  - **`tagName`**: all standard HTML element names from `@webref/elements`
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "dependencies": {
3
- "@webref/css": "^8.7.4",
4
- "@webref/elements": "^2.8.0",
5
- "@webref/idl": "^3.83.1",
3
+ "@webref/css": "^8.7.5",
4
+ "@webref/elements": "^2.9.0",
5
+ "@webref/idl": "^3.85.0",
6
6
  "ajv": "^8.20.0",
7
7
  "ajv-formats": "^3.0.1"
8
8
  },
@@ -25,6 +25,8 @@
25
25
  "./a11y": "./src/a11y.ts",
26
26
  "./ijson": "./src/ijson.ts",
27
27
  "./parse": "./src/parse.ts",
28
+ "./doc-ops": "./src/doc-ops.ts",
29
+ "./json-layout": "./src/json-layout.ts",
28
30
  "./defs": "./defs/index.ts",
29
31
  "./schema.json": "./schema.json",
30
32
  "./project-schema.json": "./project-schema.json",
@@ -58,7 +60,7 @@
58
60
  "upgrade": "bun update --latest && bun install"
59
61
  },
60
62
  "type": "module",
61
- "version": "2.2.0",
63
+ "version": "3.0.0",
62
64
  "devDependencies": {
63
65
  "json-schema-to-ts": "^3.1.1"
64
66
  }
package/schema.json CHANGED
@@ -3578,6 +3578,20 @@
3578
3578
  }
3579
3579
  ]
3580
3580
  },
3581
+ "oninstallresult": {
3582
+ "description": "Event handler for the \"installresult\" event.",
3583
+ "oneOf": [
3584
+ {
3585
+ "$ref": "#/$defs/RefObject"
3586
+ },
3587
+ {
3588
+ "$ref": "#/$defs/ExpressionEntry"
3589
+ },
3590
+ {
3591
+ "$ref": "#/$defs/FunctionDef"
3592
+ }
3593
+ ]
3594
+ },
3581
3595
  "oninvalid": {
3582
3596
  "description": "Event handler for the \"invalid\" event.",
3583
3597
  "oneOf": [
@@ -5636,6 +5650,34 @@
5636
5650
  }
5637
5651
  ]
5638
5652
  },
5653
+ "ontoolactivated": {
5654
+ "description": "Event handler for the \"toolactivated\" event.",
5655
+ "oneOf": [
5656
+ {
5657
+ "$ref": "#/$defs/RefObject"
5658
+ },
5659
+ {
5660
+ "$ref": "#/$defs/ExpressionEntry"
5661
+ },
5662
+ {
5663
+ "$ref": "#/$defs/FunctionDef"
5664
+ }
5665
+ ]
5666
+ },
5667
+ "ontoolcancel": {
5668
+ "description": "Event handler for the \"toolcancel\" event.",
5669
+ "oneOf": [
5670
+ {
5671
+ "$ref": "#/$defs/RefObject"
5672
+ },
5673
+ {
5674
+ "$ref": "#/$defs/ExpressionEntry"
5675
+ },
5676
+ {
5677
+ "$ref": "#/$defs/FunctionDef"
5678
+ }
5679
+ ]
5680
+ },
5639
5681
  "ontoolchange": {
5640
5682
  "description": "Event handler for the \"toolchange\" event.",
5641
5683
  "oneOf": [
@@ -27922,6 +27964,7 @@
27922
27964
  "img",
27923
27965
  "input",
27924
27966
  "ins",
27967
+ "install",
27925
27968
  "kbd",
27926
27969
  "label",
27927
27970
  "legend",
@@ -77,6 +77,16 @@ export interface AssetMount {
77
77
  urlPrefix: string;
78
78
  /** Absolute directory the prefix maps onto. */
79
79
  dir: string;
80
+ /**
81
+ * Whether the mount serves a file, asked with the file's path relative to {@link dir},
82
+ * `/`-separated. Absent means every file under the directory. A mount over a folder of notes sets
83
+ * it so that what the collection leaves out (`exclude`, hidden files, unpublished documents) is
84
+ * never reachable through the mount either: {@link assetUrlFor} gives a refused file no URL and
85
+ * {@link resolveAssetUrl} resolves a refused URL to nothing, so no host serves or copies it. It is
86
+ * a function, so it exists only where the mount was declared; a host that rebuilds a mount from
87
+ * its two strings (a project-relative copy for Studio) gets none.
88
+ */
89
+ filter?: (path: string) => boolean;
80
90
  }
81
91
 
82
92
  /** Normalize a path or prefix for comparison: forward slashes, no trailing slash. */
@@ -117,6 +127,10 @@ export function assetUrlFor(mounts: readonly AssetMount[], absolutePath: string)
117
127
  continue;
118
128
  }
119
129
  const rest = path.slice(dir.length + 1);
130
+ const { filter: serves } = mount;
131
+ if (serves && !serves(rest)) {
132
+ return null;
133
+ }
120
134
  const encoded = rest
121
135
  .split("/")
122
136
  .map((segment) => encodeURIComponent(segment))
@@ -160,6 +174,10 @@ export function resolveAssetUrl(mounts: readonly AssetMount[], url: string): str
160
174
  if (segments.some((s) => s === "" || s === "." || s === "..")) {
161
175
  return null;
162
176
  }
177
+ const { filter: serves } = mount;
178
+ if (serves && !serves(segments.join("/"))) {
179
+ return null;
180
+ }
163
181
  return `${normalizeDir(mount.dir)}/${segments.join("/")}`;
164
182
  }
165
183
  return null;
package/src/doc-ops.ts ADDED
@@ -0,0 +1,348 @@
1
+ /**
2
+ * The canonical Jx document-op vocabulary, its pure applier, and its inverse. A `JxDocOp` is a
3
+ * value-carrying, replayable mutation of a plain-JSON Jx document tree, addressed by `JxPath`
4
+ * (alternating "children" / index segments for structure, plus node keys). Studio's transaction
5
+ * pipeline records forward/inverse pairs of these for history replay; the collab bridge mirrors
6
+ * them into a shared Y.Doc.
7
+ *
8
+ * This module is deliberately yjs-free and imports types only: the slim canvas-iframe bundle
9
+ * imports the applier for its non-reactive shadow doc, and it MUST stay byte-identical in behavior
10
+ * to the parent's history replay — both sides import this one implementation. It lives in
11
+ * `@jxsuite/schema`, beside the document types, so any host can edit a document the way Studio does
12
+ * without taking yjs: a Durable Object, a headless tool, a test. `@jxsuite/collab/ops` re-exports
13
+ * it.
14
+ *
15
+ * @module @jxsuite/schema/doc-ops
16
+ */
17
+
18
+ import type { JxMutableNode, JxPath } from "../types.ts";
19
+
20
+ /** Value-carrying document mutation, replayable in either direction. */
21
+ export type JxDocOp =
22
+ /** Set (or delete, when value is undefined) a key on the node at path. */
23
+ | { op: "set-key"; path: JxPath; key: string; value?: unknown }
24
+ /** Insert a node at parentPath.children[index]. */
25
+ | { op: "insert-child"; parentPath: JxPath; index: number; node: unknown }
26
+ /** Remove parentPath.children[index]. */
27
+ | { op: "remove-child"; parentPath: JxPath; index: number }
28
+ /** Replace parentPath.children[index] with node. */
29
+ | { op: "set-child"; parentPath: JxPath; index: number; node: unknown }
30
+ /** Raw two-splice move: remove fromParent.children[fromIndex], insert at toParent[toIndex]. */
31
+ | {
32
+ op: "move-child";
33
+ fromParentPath: JxPath;
34
+ fromIndex: number;
35
+ toParentPath: JxPath;
36
+ toIndex: number;
37
+ };
38
+
39
+ export interface JxDocOpPair {
40
+ forward: JxDocOp;
41
+ inverse: JxDocOp;
42
+ }
43
+
44
+ /** JSON round-trip clone — also normalizes away reactive proxies / functions / undefined. */
45
+ function jsonClone<T>(value: T): T {
46
+ // oxlint-disable-next-line unicorn/prefer-structured-clone -- structuredClone throws on reactive proxies; JSON normalization is the point
47
+ return JSON.parse(JSON.stringify(value)) as T;
48
+ }
49
+
50
+ /** Deep-clone a recorded value (undefined/null pass through; reactive proxies are read through). */
51
+ export function cloneValue<T>(v: T): T {
52
+ return v === undefined || v === null ? v : (jsonClone(v as object) as T);
53
+ }
54
+
55
+ /**
56
+ * Walk a plain document tree by path. Paths address nodes by construction; missing segments and
57
+ * non-node leaves surface as undefined (typed as node for caller ergonomics, matching Studio's
58
+ * historical helper).
59
+ */
60
+ export function getNodeAtPath(doc: JxMutableNode, path: JxPath): JxMutableNode {
61
+ let node = doc;
62
+ for (const key of path) {
63
+ if (node == null) {
64
+ return undefined as unknown as JxMutableNode;
65
+ }
66
+ node = node[key] as JxMutableNode;
67
+ }
68
+ return node;
69
+ }
70
+
71
+ /** The node's children array, lazily created; throws if `node` is itself a children array or mapped. */
72
+ export function childArray(node: JxMutableNode): (JxMutableNode | string)[] {
73
+ // Defense-in-depth: a path that resolves to a children array (rather than a node) would
74
+ // Otherwise get a bogus `.children` property tacked on here, silently storing the insert where
75
+ // Nothing renders. Callers must pass a node; fail loudly if they don't.
76
+ if (Array.isArray(node)) {
77
+ throw new TypeError("Cannot insert into a children array; parentPath must point at a node");
78
+ }
79
+ if (!node.children) {
80
+ node.children = [];
81
+ }
82
+ if (!Array.isArray(node.children)) {
83
+ throw new TypeError("Cannot insert into mapped-array children; edit the map template instead");
84
+ }
85
+ return node.children;
86
+ }
87
+
88
+ /**
89
+ * Apply a replayable doc op to a bare document tree. Values/nodes are cloned in, so the tree never
90
+ * aliases the op object. Throws (with a machine-readable reason) when a target path is missing.
91
+ */
92
+ export function applyDocOpToDoc(doc: JxMutableNode, op: JxDocOp): void {
93
+ switch (op.op) {
94
+ case "set-key": {
95
+ const node = getNodeAtPath(doc, op.path);
96
+ if (!node) {
97
+ throw new Error(`doc-op-node-not-found:${op.path.join("/")}`);
98
+ }
99
+ const target = node as Record<string, unknown>;
100
+ if (op.value === undefined) {
101
+ delete target[op.key];
102
+ } else {
103
+ target[op.key] = cloneValue(op.value);
104
+ }
105
+ return;
106
+ }
107
+ case "insert-child": {
108
+ const parent = getNodeAtPath(doc, op.parentPath);
109
+ childArray(parent).splice(op.index, 0, cloneValue(op.node) as JxMutableNode);
110
+ return;
111
+ }
112
+ case "remove-child": {
113
+ const parent = getNodeAtPath(doc, op.parentPath);
114
+ childArray(parent).splice(op.index, 1);
115
+ return;
116
+ }
117
+ case "set-child": {
118
+ const parent = getNodeAtPath(doc, op.parentPath);
119
+ childArray(parent).splice(op.index, 1, cloneValue(op.node) as JxMutableNode);
120
+ return;
121
+ }
122
+ case "move-child": {
123
+ assertNotIntoOwnSubtree(op);
124
+ const fromParent = getNodeAtPath(doc, op.fromParentPath);
125
+ const toParent = getNodeAtPath(doc, op.toParentPath);
126
+ /* Both arrays are resolved BEFORE the source is spliced, so a move whose target cannot receive
127
+ children throws with the document untouched rather than having detached the node it could
128
+ not place. */
129
+ const from = childArray(fromParent);
130
+ const to = childArray(toParent);
131
+ const [node] = from.splice(op.fromIndex, 1);
132
+ to.splice(op.toIndex, 0, node!);
133
+ return;
134
+ }
135
+ default: {
136
+ break;
137
+ }
138
+ }
139
+ throw new Error(`unknown-doc-op:${(op as JxDocOp).op}`);
140
+ }
141
+
142
+ /**
143
+ * Translate `path` into post-splice coordinates when it descends through `parentPath`'s children at
144
+ * or beyond the spliced index: a removal (delta -1) at the index shifts deeper paths down; an
145
+ * insertion (delta +1, inclusive) shifts them up. Returns `path` unchanged when the splice does not
146
+ * affect it — including when `path` IS `parentPath`, since a splice moves a node's children, never
147
+ * the node.
148
+ */
149
+ function shiftThroughSplice(
150
+ parentPath: JxPath,
151
+ path: JxPath,
152
+ spliceIndex: number,
153
+ delta: number,
154
+ inclusive: boolean,
155
+ ): JxPath {
156
+ const depth = parentPath.length;
157
+ const isProperPrefix = depth < path.length && parentPath.every((seg, i) => seg === path[i]);
158
+ if (!isProperPrefix || path[depth] !== "children" || typeof path[depth + 1] !== "number") {
159
+ return path;
160
+ }
161
+ const index = path[depth + 1] as number;
162
+ if (inclusive ? index < spliceIndex : index <= spliceIndex) {
163
+ return path;
164
+ }
165
+ const shifted = [...path];
166
+ shifted[depth + 1] = index + delta;
167
+ return shifted;
168
+ }
169
+
170
+ /**
171
+ * Refuse a move into the moved node itself or into its own subtree. Splicing the node out first
172
+ * would detach the very parent the insert was to land in, so the subtree would vanish and no
173
+ * inverse could bring it back.
174
+ */
175
+ function assertNotIntoOwnSubtree(op: Extract<JxDocOp, { op: "move-child" }>): void {
176
+ const moved = [...op.fromParentPath, "children", op.fromIndex];
177
+ const target = op.toParentPath;
178
+ if (target.length >= moved.length && moved.every((seg, i) => seg === target[i])) {
179
+ throw new Error(`doc-op-move-into-self:${target.join("/")}`);
180
+ }
181
+ }
182
+
183
+ /**
184
+ * The index a `splice(index, 0, …)` into `length` elements actually inserts at: a negative index
185
+ * counts from the end and an index past the end appends. The applier inserts with `splice`, so an
186
+ * inverse must name the slot the node really landed in, not the one the op asked for.
187
+ */
188
+ function insertionSlot(index: number, length: number): number {
189
+ return index < 0 ? Math.max(length + index, 0) : Math.min(index, length);
190
+ }
191
+
192
+ /**
193
+ * The op that undoes `op`, computed against `doc` as it stands BEFORE `op` is applied.
194
+ *
195
+ * Every inverse is expressed in the coordinates of the document after `op` ran, because that is the
196
+ * document it will be applied to. For four kinds that is the op's own coordinates. `move-child` is
197
+ * the one that is not: removing the node shifts every sibling path after it under the source
198
+ * parent, and inserting it shifts every sibling path at or after the target index under the
199
+ * destination parent, so each parent path of the inverse is translated through the other splice.
200
+ * The rule is the one Studio's own move has always recorded.
201
+ *
202
+ * **One residue is not undone, deliberately.** Inserting or moving a node into a parent with no
203
+ * `children` creates the array (the applier's `childArray` does), and undoing it removes the node
204
+ * but leaves `children: []`. A single op cannot both move a node back and delete a key, and
205
+ * Studio's own history has always recorded exactly these inverses, so matching it is the contract;
206
+ * an empty `children` array renders as nothing and validates as the absent key does.
207
+ *
208
+ * An index the applier would clamp (`splice` appends past the end and counts a negative index from
209
+ * it) is recorded as the slot the node actually lands in.
210
+ *
211
+ * Throws when the op cannot be applied: its target does not resolve, an insert or move targets
212
+ * something that cannot hold children, or a move targets the moved node's own subtree. An op that
213
+ * cannot be applied has nothing to undo, and a caller that computed an inverse for it would record
214
+ * a history entry for an edit that never happened.
215
+ *
216
+ * @param {JxMutableNode} doc - The document the op is about to be applied to
217
+ * @param {JxDocOp} op
218
+ * @returns {JxDocOp}
219
+ */
220
+ export function inverseOf(doc: JxMutableNode, op: JxDocOp): JxDocOp {
221
+ switch (op.op) {
222
+ case "set-key": {
223
+ const node = getNodeAtPath(doc, op.path);
224
+ if (!node) {
225
+ throw new Error(`doc-op-node-not-found:${op.path.join("/")}`);
226
+ }
227
+ const before = cloneValue((node as Record<string, unknown>)[op.key]);
228
+ return before === undefined
229
+ ? { key: op.key, op: "set-key", path: op.path }
230
+ : { key: op.key, op: "set-key", path: op.path, value: before };
231
+ }
232
+ case "insert-child": {
233
+ const { length } = insertableChildren(doc, op.parentPath);
234
+ return {
235
+ index: insertionSlot(op.index, length),
236
+ op: "remove-child",
237
+ parentPath: op.parentPath,
238
+ };
239
+ }
240
+ case "remove-child": {
241
+ const removed = existingChild(doc, op.parentPath, op.index);
242
+ return { index: op.index, node: removed, op: "insert-child", parentPath: op.parentPath };
243
+ }
244
+ case "set-child": {
245
+ const replaced = existingChild(doc, op.parentPath, op.index);
246
+ return { index: op.index, node: replaced, op: "set-child", parentPath: op.parentPath };
247
+ }
248
+ case "move-child": {
249
+ assertNotIntoOwnSubtree(op);
250
+ const source = childrenAt(doc, op.fromParentPath);
251
+ assertIndex(source, op.fromParentPath, op.fromIndex);
252
+ const target = insertableChildren(doc, op.toParentPath);
253
+ // The target's length after the removal: the same array when the move stays in one parent.
254
+ const toIndex = insertionSlot(op.toIndex, target.length - (target === source ? 1 : 0));
255
+ return {
256
+ fromIndex: toIndex,
257
+ // The removal shifted paths under the source parent; the insertion, under the target.
258
+ fromParentPath: shiftThroughSplice(
259
+ op.fromParentPath,
260
+ op.toParentPath,
261
+ op.fromIndex,
262
+ -1,
263
+ false,
264
+ ),
265
+ op: "move-child",
266
+ toIndex: op.fromIndex,
267
+ toParentPath: shiftThroughSplice(op.toParentPath, op.fromParentPath, toIndex, 1, true),
268
+ };
269
+ }
270
+ default: {
271
+ break;
272
+ }
273
+ }
274
+ throw new Error(`unknown-doc-op:${(op as JxDocOp).op}`);
275
+ }
276
+
277
+ /** The node's existing children array, or null when it has none (or is not a node). */
278
+ function childrenAt(doc: JxMutableNode, parentPath: JxPath): (JxMutableNode | string)[] | null {
279
+ const parent = getNodeAtPath(doc, parentPath);
280
+ return parent && Array.isArray(parent.children) ? parent.children : null;
281
+ }
282
+
283
+ /** Throw the applier's reason unless `children` holds an element at `index`. */
284
+ function assertIndex(
285
+ children: (JxMutableNode | string)[] | null,
286
+ parentPath: JxPath,
287
+ index: number,
288
+ ): asserts children is (JxMutableNode | string)[] {
289
+ if (!children || index < 0 || index >= children.length) {
290
+ throw new Error(`doc-op-node-not-found:${[...parentPath, "children", index].join("/")}`);
291
+ }
292
+ }
293
+
294
+ /** A clone of `parentPath`'s child at `index`, or the applier's reason when there is none. */
295
+ function existingChild(doc: JxMutableNode, parentPath: JxPath, index: number): unknown {
296
+ const children = childrenAt(doc, parentPath);
297
+ assertIndex(children, parentPath, index);
298
+ return cloneValue(children[index]);
299
+ }
300
+
301
+ /**
302
+ * The children array an insert into `parentPath` lands in — an empty stand-in when the node has no
303
+ * `children` yet, since the applier creates one — or the reason it cannot land: no node there, a
304
+ * children array addressed as if it were a node, or a mapped-array `children`.
305
+ */
306
+ function insertableChildren(doc: JxMutableNode, parentPath: JxPath): (JxMutableNode | string)[] {
307
+ const node = getNodeAtPath(doc, parentPath) as unknown as { children?: unknown } | null;
308
+ if (!node || typeof node !== "object" || Array.isArray(node)) {
309
+ throw new Error(`doc-op-node-not-found:${parentPath.join("/")}`);
310
+ }
311
+ if (node.children === undefined) {
312
+ return [];
313
+ }
314
+ if (!Array.isArray(node.children)) {
315
+ throw new TypeError(`doc-op-mapped-children:${parentPath.join("/")}`);
316
+ }
317
+ return node.children as (JxMutableNode | string)[];
318
+ }
319
+
320
+ /**
321
+ * Apply `ops` to `doc` in order, answering the forward/inverse pair for each — the record a history
322
+ * entry, an undo stack or a journal needs. Each inverse is computed against the document as it
323
+ * stands just before its own op, so a sequence whose later ops address what earlier ones created is
324
+ * recorded correctly. Undo applies the inverses in REVERSE order.
325
+ *
326
+ * Stops at the first op that does not apply and rethrows its reason; the ops before it have been
327
+ * applied, and their pairs are on the error as `applied`, so a caller can roll them back.
328
+ *
329
+ * @param {JxMutableNode} doc
330
+ * @param {readonly JxDocOp[]} ops
331
+ * @returns {JxDocOpPair[]}
332
+ */
333
+ export function applyDocOpsWithInverse(doc: JxMutableNode, ops: readonly JxDocOp[]): JxDocOpPair[] {
334
+ const pairs: JxDocOpPair[] = [];
335
+ for (const forward of ops) {
336
+ let inverse: JxDocOp;
337
+ try {
338
+ inverse = inverseOf(doc, forward);
339
+ applyDocOpToDoc(doc, forward);
340
+ } catch (error) {
341
+ throw Object.assign(error instanceof Error ? error : new Error(String(error)), {
342
+ applied: pairs,
343
+ });
344
+ }
345
+ pairs.push({ forward, inverse });
346
+ }
347
+ return pairs;
348
+ }
@@ -0,0 +1,474 @@
1
+ /**
2
+ * A JSON document's layout — the facts about a file's text that `JSON.parse` throws away and a save
3
+ * has to put back.
4
+ *
5
+ * Studio's native JSON branch used to be `JSON.stringify(document, null, 2)`, and that is not the
6
+ * layout the repository is kept in. The formatter (`oxfmt`, Prettier-compatible for JSON) keeps a
7
+ * short object on the one line its author wrote it on, so every
8
+ * `packages/studio/src/surfaces/*.json` and `packages/ui/components/*.json` — files Studio now
9
+ * opens as projects — carries hundreds of `{ "part": "bar" }`s that a save expanded to three lines
10
+ * each. One edit became a whole-file diff, and the line the author actually changed was buried in
11
+ * it (issue 308).
12
+ *
13
+ * Two halves. {@link deriveJsonLayout} reads the source text once, when a file is read into a tab,
14
+ * and records the things the formatter would preserve; {@link serializeJson} writes a document back
15
+ * with those facts honoured and everything else laid out the way the formatter lays it out. The
16
+ * test that proves the pair is byte-identity over every formatted document in this repository.
17
+ *
18
+ * **What is recorded, and why nothing else.** The formatter preserves three authored choices and
19
+ * relays everything else. (1) Whether an object's `{` is followed by a line break: an object
20
+ * written on one line stays on one line while it fits the print width, and one whose first key sits
21
+ * on its own line stays expanded however short it is. (2) A blank line between two members of an
22
+ * object or an array — one, however many were written, and never one against a bracket. (3) A
23
+ * non-ASCII character an author spelt as a `\uXXXX` escape, which `JSON.stringify` would otherwise
24
+ * turn into the literal character and so touch a line the edit never reached. Arrays are relaid
25
+ * from scratch every time — `[\n 1, 2 ]` comes back as `[1, 2]` — so an array's own line break is a
26
+ * fact the serializer would then have to ignore, and it is not recorded.
27
+ *
28
+ * **Keyed by JSON Pointer**, so the record survives every edit that leaves a node where it was: a
29
+ * changed value, a new sibling, a removed one. An object with no entry — one the edit created —
30
+ * takes its parent's answer, so a new `{ "$ref": … }` inside an inline `attributes` lands inline
31
+ * beside its siblings rather than blowing the object open; at an expanded level, or in a document
32
+ * that has no layout at all, it expands, which is what the formatter does with `JSON.stringify`
33
+ * output.
34
+ *
35
+ * The width rules are measured against `oxfmt`, not recalled from Prettier: a container is inline
36
+ * when the whole line, trailing comma included, is at most `printWidth` columns; a container with
37
+ * an expanded object or a blank line anywhere inside it expands (a break propagates outward); an
38
+ * array of two or more elements that are all objects with more than one key, or all arrays with
39
+ * more than one element, always expands one element per line; and an array of two or more numbers
40
+ * fills its lines instead of taking one per line. Width is DISPLAY width, as the formatter measures
41
+ * it, not string length: a CJK or fullwidth character and an emoji are two columns, a combining
42
+ * mark or a format character is none ({@link displayWidth}; probed against oxfmt, which uses the
43
+ * `unicode-width` tables — the ranges here are Prettier's, and agree with those on everything a
44
+ * document is likely to hold). Scalars are `JSON.stringify`'s, so `1e5` reads back as `100000` and
45
+ * `1.50` as `1.5` — the parsed value has already lost the spelling, and no document in this
46
+ * repository writes a number either way. `package.json` is the one file the formatter lays out as
47
+ * `JSON.stringify` does, every array expanded; it is not a document and nothing here writes one.
48
+ *
49
+ * @docs studio/interface/tabs
50
+ */
51
+
52
+ /** A JSON Pointer (RFC 6901) segment: `~` and `/` are the two characters that need escaping. */
53
+ function segment(key: string): string {
54
+ return key.replaceAll("~", "~0").replaceAll("/", "~1");
55
+ }
56
+
57
+ /**
58
+ * What a source text said about its own layout, by JSON Pointer.
59
+ *
60
+ * `inline` maps an object to whether its `{` and first key shared a line; an empty object has no
61
+ * entry because `{}` is `{}` either way. `blankAfter` holds the members a blank line followed.
62
+ * `escaped` maps a string to the UTF-16 code units its source spelt as `\uXXXX` — the units rather
63
+ * than a flag, because `"— Julio Cortázar"` with only the dash escaped is a string this repository
64
+ * holds, and a flag would have had to choose which half of it to rewrite.
65
+ */
66
+ export interface JsonLayout {
67
+ inline: Map<string, boolean>;
68
+ blankAfter: Set<string>;
69
+ escaped: Map<string, Set<number>>;
70
+ }
71
+
72
+ /** The print width the repository formats to — `.oxfmtrc.json` leaves oxfmt's default in place. */
73
+ export const JSON_PRINT_WIDTH = 100;
74
+
75
+ /**
76
+ * Read the layout facts out of a JSON text.
77
+ *
78
+ * A scanner rather than a parser: the text has already been through `JSON.parse` by the time this
79
+ * runs (see {@link parseJsonDocument}), so it is known to be well-formed and the scanner only has to
80
+ * find its way from one container to the next. Keys are decoded with `JSON.parse` so the pointer
81
+ * matches the key the document actually holds, escapes and all.
82
+ */
83
+ export function deriveJsonLayout(text: string): JsonLayout {
84
+ const layout: JsonLayout = { blankAfter: new Set(), escaped: new Map(), inline: new Map() };
85
+ let i = 0;
86
+
87
+ /** Skip whitespace; report how many line breaks were among it. */
88
+ const space = (): number => {
89
+ let newlines = 0;
90
+ while (
91
+ i < text.length &&
92
+ (text[i] === " " || text[i] === "\t" || text[i] === "\r" || text[i] === "\n")
93
+ ) {
94
+ if (text[i] === "\n") {
95
+ newlines += 1;
96
+ }
97
+ i += 1;
98
+ }
99
+ return newlines;
100
+ };
101
+
102
+ /** Consume the string starting at `i`; return its raw text and the code units it escaped. */
103
+ const string = (): { raw: string; escaped: Set<number> } => {
104
+ const start = i;
105
+ const escaped = new Set<number>();
106
+ i += 1;
107
+ while (i < text.length && text[i] !== '"') {
108
+ if (text[i] === "\\") {
109
+ if (text[i + 1] === "u") {
110
+ const unit = Number.parseInt(text.slice(i + 2, i + 6), 16);
111
+ if (unit >= 0x80) {
112
+ escaped.add(unit);
113
+ }
114
+ i += 6;
115
+ } else {
116
+ i += 2;
117
+ }
118
+ } else {
119
+ i += 1;
120
+ }
121
+ }
122
+ i += 1;
123
+ return { escaped, raw: text.slice(start, i) };
124
+ };
125
+
126
+ /**
127
+ * After a member: consume its separator and note a blank line before the next one. Returns
128
+ * whether the container closed instead.
129
+ */
130
+ const separator = (memberPath: string, close: string): boolean => {
131
+ let newlines = space();
132
+ if (text[i] === close) {
133
+ i += 1;
134
+ return true;
135
+ }
136
+ i += 1; // ","
137
+ newlines += space();
138
+ if (newlines >= 2) {
139
+ layout.blankAfter.add(memberPath);
140
+ }
141
+ return false;
142
+ };
143
+
144
+ const value = (path: string): void => {
145
+ space();
146
+ const c = text[i];
147
+ if (c === "{") {
148
+ i += 1;
149
+ const broken = space() > 0;
150
+ if (text[i] === "}") {
151
+ i += 1;
152
+ return;
153
+ }
154
+ layout.inline.set(path, !broken);
155
+ while (i < text.length) {
156
+ const key = JSON.parse(string().raw) as string;
157
+ space();
158
+ i += 1; // ":"
159
+ const member = `${path}/${segment(key)}`;
160
+ value(member);
161
+ if (separator(member, "}")) {
162
+ return;
163
+ }
164
+ }
165
+ } else if (c === "[") {
166
+ i += 1;
167
+ space();
168
+ if (text[i] === "]") {
169
+ i += 1;
170
+ return;
171
+ }
172
+ for (let index = 0; i < text.length; index += 1) {
173
+ const member = `${path}/${index}`;
174
+ value(member);
175
+ if (separator(member, "]")) {
176
+ return;
177
+ }
178
+ }
179
+ } else if (c === '"') {
180
+ const { escaped } = string();
181
+ if (escaped.size > 0) {
182
+ layout.escaped.set(path, escaped);
183
+ }
184
+ } else {
185
+ // A number or a literal: runs to the next delimiter.
186
+ while (i < text.length && !",]} \t\r\n".includes(text[i]!)) {
187
+ i += 1;
188
+ }
189
+ }
190
+ };
191
+
192
+ value("");
193
+ return layout;
194
+ }
195
+
196
+ /** Parse a JSON file into the document it holds and the layout it was written in. */
197
+ export function parseJsonDocument(text: string): {
198
+ document: Record<string, unknown>;
199
+ layout: JsonLayout;
200
+ } {
201
+ const document = JSON.parse(text) as Record<string, unknown>;
202
+ return { document, layout: deriveJsonLayout(text) };
203
+ }
204
+
205
+ // ─── Writing ──────────────────────────────────────────────────────────────────
206
+
207
+ /** Anything outside printable ASCII: the one case {@link displayWidth} has to count. */
208
+ const NOT_ASCII = /[^\u0020-\u007E]/;
209
+
210
+ /** A mark or a format character (a zero-width joiner, say) takes no column. */
211
+ const ZERO_WIDTH = /^[\p{Mn}\p{Me}\p{Cf}]$/u;
212
+
213
+ /** A character the formatter draws two columns wide: East Asian Wide/Fullwidth, or an emoji. */
214
+ const EMOJI = /^\p{Emoji_Presentation}$/u;
215
+
216
+ /**
217
+ * East Asian Wide and Fullwidth (`is-fullwidth-code-point`'s table, which is what Prettier's
218
+ * `getStringWidth` consults): Hangul Jamo, CJK and its punctuation, Hangul syllables, the
219
+ * compatibility ideographs, vertical and small forms, the fullwidth Latin block, and the
220
+ * supplementary ideographic planes.
221
+ */
222
+ function isWide(cp: number): boolean {
223
+ return (
224
+ cp >= 0x11_00 &&
225
+ (cp <= 0x11_5f ||
226
+ cp === 0x23_29 ||
227
+ cp === 0x23_2a ||
228
+ (cp >= 0x2e_80 && cp <= 0x32_47 && cp !== 0x30_3f) ||
229
+ (cp >= 0x32_50 && cp <= 0x4d_bf) ||
230
+ (cp >= 0x4e_00 && cp <= 0xa4_c6) ||
231
+ (cp >= 0xa9_60 && cp <= 0xa9_7c) ||
232
+ (cp >= 0xac_00 && cp <= 0xd7_a3) ||
233
+ (cp >= 0xf9_00 && cp <= 0xfa_ff) ||
234
+ (cp >= 0xfe_10 && cp <= 0xfe_19) ||
235
+ (cp >= 0xfe_30 && cp <= 0xfe_6b) ||
236
+ (cp >= 0xff_01 && cp <= 0xff_60) ||
237
+ (cp >= 0xff_e0 && cp <= 0xff_e6) ||
238
+ (cp >= 0x1_b0_00 && cp <= 0x1_b0_01) ||
239
+ (cp >= 0x1_f2_00 && cp <= 0x1_f2_51) ||
240
+ (cp >= 0x2_00_00 && cp <= 0x3_ff_fd))
241
+ );
242
+ }
243
+
244
+ /**
245
+ * How many columns a line of output occupies — what the formatter compares against the print width.
246
+ * `String.length` counts UTF-16 units, and the two part company exactly where a document holds
247
+ * non-Latin text: `"漢"` is one unit and two columns, an emoji is two units and two columns, `"é"`
248
+ * written as `e` plus a combining acute is two units and one column. Measured with the length, a
249
+ * line of CJK could sit "inline" at 62 units and 106 columns, and the formatter would break it open
250
+ * on the next run — a save that is not formatter-stable.
251
+ */
252
+ export function displayWidth(text: string): number {
253
+ if (!NOT_ASCII.test(text)) {
254
+ return text.length;
255
+ }
256
+ let width = 0;
257
+ for (const c of text) {
258
+ if (ZERO_WIDTH.test(c)) {
259
+ continue;
260
+ }
261
+ width += isWide(c.codePointAt(0)!) || EMOJI.test(c) ? 2 : 1;
262
+ }
263
+ return width;
264
+ }
265
+
266
+ type Entry = [key: string, value: unknown];
267
+
268
+ /** Whether `JSON.stringify` would drop this value from an object. */
269
+ function omitted(value: unknown): boolean {
270
+ return value === undefined || typeof value === "function" || typeof value === "symbol";
271
+ }
272
+
273
+ function entriesOf(value: object): Entry[] {
274
+ return Object.entries(value).filter(([, v]) => !omitted(v));
275
+ }
276
+
277
+ function isObject(value: unknown): value is Record<string, unknown> {
278
+ return typeof value === "object" && value !== null && !Array.isArray(value);
279
+ }
280
+
281
+ /**
282
+ * Whether the formatter forces this array open: two or more elements, all objects with more than
283
+ * one key or all arrays with more than one element (Prettier's "concisely printed" rule,
284
+ * inverted).
285
+ */
286
+ function forcedOpen(items: unknown[]): boolean {
287
+ if (items.length < 2) {
288
+ return false;
289
+ }
290
+ const kind = Array.isArray(items[0]) ? "array" : isObject(items[0]) ? "object" : null;
291
+ if (kind === null) {
292
+ return false;
293
+ }
294
+ return items.every((item) =>
295
+ kind === "array"
296
+ ? Array.isArray(item) && item.length > 1
297
+ : isObject(item) && entriesOf(item).length > 1,
298
+ );
299
+ }
300
+
301
+ /** Two or more numbers: the formatter fills lines with them rather than stacking one per line. */
302
+ function isNumberRun(items: unknown[]): boolean {
303
+ return items.length > 1 && items.every((item) => typeof item === "number");
304
+ }
305
+
306
+ /** A scalar, as `JSON.stringify` spells it — with the code units the source escaped, escaped. */
307
+ function scalar(value: unknown, escaped?: Set<number>): string {
308
+ const text = JSON.stringify(value) ?? "null";
309
+ return escaped === undefined
310
+ ? text
311
+ : text.replaceAll(/[\u0080-\uFFFF]/g, (c) => {
312
+ const unit = c.codePointAt(0)!;
313
+ return escaped.has(unit) ? `\\u${unit.toString(16).padStart(4, "0")}` : c;
314
+ });
315
+ }
316
+
317
+ /**
318
+ * Write a document in the layout the formatter would keep it in.
319
+ *
320
+ * `layout` is what {@link deriveJsonLayout} read from the file this document came from, or `null`
321
+ * for a document that has no file yet (or whose file was not JSON) — every object then expands and
322
+ * every array is laid by fit, which is the formatter's own answer for `JSON.stringify` output. The
323
+ * result ends with exactly one newline, as every formatted file does.
324
+ */
325
+ export function serializeJson(
326
+ value: unknown,
327
+ layout: JsonLayout | null,
328
+ printWidth = JSON_PRINT_WIDTH,
329
+ ): string {
330
+ const inline = layout?.inline ?? new Map<string, boolean>();
331
+ const blankAfter = layout?.blankAfter ?? new Set<string>();
332
+ const escaped = layout?.escaped ?? new Map<string, Set<number>>();
333
+
334
+ /**
335
+ * Whether an object is a candidate for one line: its own record, or its parent's when it has
336
+ * none.
337
+ */
338
+ const soft = (path: string, inherited: boolean): boolean => inline.get(path) ?? inherited;
339
+
340
+ /**
341
+ * The one-line rendering, or `null` when something inside is forced open — an expanded object, a
342
+ * blank line, or an array the formatter always breaks — so that the break propagates outward the
343
+ * way the formatter's does.
344
+ */
345
+ const flat = (v: unknown, path: string, inherited: boolean): string | null => {
346
+ if (Array.isArray(v)) {
347
+ if (v.length === 0) {
348
+ return "[]";
349
+ }
350
+ if (forcedOpen(v)) {
351
+ return null;
352
+ }
353
+ const parts: string[] = [];
354
+ for (const [index, item] of v.entries()) {
355
+ const member = `${path}/${index}`;
356
+ const part = blankAfter.has(member) ? null : flat(item, member, inherited);
357
+ if (part === null) {
358
+ return null;
359
+ }
360
+ parts.push(part);
361
+ }
362
+ return `[${parts.join(", ")}]`;
363
+ }
364
+ if (isObject(v)) {
365
+ const entries = entriesOf(v);
366
+ if (entries.length === 0) {
367
+ return "{}";
368
+ }
369
+ if (!soft(path, inherited)) {
370
+ return null;
371
+ }
372
+ const parts: string[] = [];
373
+ for (const [key, item] of entries) {
374
+ const member = `${path}/${segment(key)}`;
375
+ const part = blankAfter.has(member) ? null : flat(item, member, true);
376
+ if (part === null) {
377
+ return null;
378
+ }
379
+ parts.push(`${JSON.stringify(key)}: ${part}`);
380
+ }
381
+ return `{ ${parts.join(", ")} }`;
382
+ }
383
+ return scalar(v, escaped.get(path));
384
+ };
385
+
386
+ /** Members one per line, with the blank lines the source had between them. */
387
+ const stack = (members: { path: string; text: string }[]): string =>
388
+ members
389
+ .map(({ path, text }, index) =>
390
+ index === members.length - 1 ? text : `${text},\n${blankAfter.has(path) ? "\n" : ""}`,
391
+ )
392
+ .join("");
393
+
394
+ /**
395
+ * A value at `column`, followed on its last line by `trailing` more characters (the comma a
396
+ * parent adds, which counts toward the width the formatter measures).
397
+ */
398
+ const render = (
399
+ v: unknown,
400
+ path: string,
401
+ indent: number,
402
+ column: number,
403
+ inherited: boolean,
404
+ trailing: number,
405
+ ): string => {
406
+ const line = flat(v, path, inherited);
407
+ // A scalar is never broken, however long: only a container has a second layout to fall to.
408
+ const container = Array.isArray(v) || isObject(v);
409
+ if (line !== null && (!container || column + displayWidth(line) + trailing <= printWidth)) {
410
+ return line;
411
+ }
412
+ const pad = " ".repeat(indent + 2);
413
+ const close = `\n${" ".repeat(indent)}`;
414
+ if (Array.isArray(v)) {
415
+ if (isNumberRun(v)) {
416
+ const blank = (index: number) => blankAfter.has(`${path}/${index}`);
417
+ return `[\n${fill(v, pad, printWidth, blank)}${close}]`;
418
+ }
419
+ const items = v.map((item, index) => {
420
+ const member = `${path}/${index}`;
421
+ const last = index === v.length - 1;
422
+ const text = render(item, member, indent + 2, pad.length, inherited, last ? 0 : 1);
423
+ return { path: member, text: `${pad}${text}` };
424
+ });
425
+ return `[\n${stack(items)}${close}]`;
426
+ }
427
+ const entries = entriesOf(v as object);
428
+ const own = soft(path, inherited);
429
+ const members = entries.map(([key, item], index) => {
430
+ const member = `${path}/${segment(key)}`;
431
+ const prefix = `${JSON.stringify(key)}: `;
432
+ const last = index === entries.length - 1;
433
+ const at = pad.length + displayWidth(prefix);
434
+ const text = render(item, member, indent + 2, at, own, last ? 0 : 1);
435
+ return { path: member, text: `${pad}${prefix}${text}` };
436
+ });
437
+ return `{\n${stack(members)}${close}}`;
438
+ };
439
+
440
+ return `${render(value, "", 0, 0, false, 0)}\n`;
441
+ }
442
+
443
+ /**
444
+ * Numbers packed onto lines greedily: the next one joins the line when it fits with its comma, and
445
+ * opens a new line otherwise. Each line carries `pad`. A blank line the source had after a number
446
+ * (`blank`) ends its line and stays, as it does between any other two members. Lengths, not
447
+ * {@link displayWidth}: a line of numbers and spaces is ASCII, so the two agree.
448
+ */
449
+ function fill(
450
+ numbers: unknown[],
451
+ pad: string,
452
+ printWidth: number,
453
+ blank: (index: number) => boolean,
454
+ ): string {
455
+ const lines: string[] = [];
456
+ let line = "";
457
+ for (const [index, n] of numbers.entries()) {
458
+ const item = `${scalar(n)}${index === numbers.length - 1 ? "" : ","}`;
459
+ if (line === "") {
460
+ line = `${pad}${item}`;
461
+ } else if (line.length + 1 + item.length <= printWidth) {
462
+ line = `${line} ${item}`;
463
+ } else {
464
+ lines.push(line);
465
+ line = `${pad}${item}`;
466
+ }
467
+ if (blank(index) && index < numbers.length - 1) {
468
+ lines.push(line, "");
469
+ line = "";
470
+ }
471
+ }
472
+ lines.push(line);
473
+ return lines.join("\n");
474
+ }
package/types.ts CHANGED
@@ -857,6 +857,26 @@ export interface ContentLoaderEntry {
857
857
  mtime?: string;
858
858
  /** Authored text of a field the date-coercion pass rewrote, keyed by field name. */
859
859
  rawDates?: Record<string, unknown>;
860
+ /**
861
+ * The entry's source file, relative to its content type's source directory, `/`-separated and
862
+ * with its extension (`Frappe/Bench Operations.md`). Stamped by the content loader for every
863
+ * entry it reads from a directory, so a message or an "edit this page" link can name the file
864
+ * an entry came from.
865
+ */
866
+ path?: string;
867
+ /**
868
+ * The entry's route: the site-absolute path its content type's `route` template gives it,
869
+ * without a locale prefix or a trailing slash (`/kb/frappe/bench-operations`). Present only
870
+ * when the content type declares a `route`. Everything that needs a URL for the entry (links
871
+ * between entries, `$paths`, search, feeds) reads this, so they cannot disagree.
872
+ */
873
+ route?: string;
874
+ /**
875
+ * The entry's URL as a link target: {@link route} percent-encoded, with the locale prefix of a
876
+ * `{locale}` collection and the site's `build.trailingSlash` applied
877
+ * (`/kb/frappe/bench-operations/`).
878
+ */
879
+ url?: string;
860
880
  /**
861
881
  * The locale this entry was loaded for, set only when its content type's `source` carried a
862
882
  * `{locale}` placeholder. It is what lets a `[slug]` route under `/fr/` expand the French