@kgdiff/viewer 0.0.0-stage → 0.2.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.
Files changed (155) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/dist/compute-loader.d.ts +13 -0
  4. package/dist/compute-loader.d.ts.map +1 -0
  5. package/dist/compute-loader.js +46 -0
  6. package/dist/compute-loader.js.map +1 -0
  7. package/dist/compute.d.ts +8 -0
  8. package/dist/compute.d.ts.map +1 -0
  9. package/dist/compute.js +8 -0
  10. package/dist/compute.js.map +1 -0
  11. package/dist/controller.d.ts +199 -0
  12. package/dist/controller.d.ts.map +1 -0
  13. package/dist/controller.js +1254 -0
  14. package/dist/controller.js.map +1 -0
  15. package/dist/dom.d.ts +32 -0
  16. package/dist/dom.d.ts.map +1 -0
  17. package/dist/dom.js +105 -0
  18. package/dist/dom.js.map +1 -0
  19. package/dist/element.d.ts +43 -0
  20. package/dist/element.d.ts.map +1 -0
  21. package/dist/element.js +656 -0
  22. package/dist/element.js.map +1 -0
  23. package/dist/framing.d.ts +45 -0
  24. package/dist/framing.d.ts.map +1 -0
  25. package/dist/framing.js +107 -0
  26. package/dist/framing.js.map +1 -0
  27. package/dist/hash-sync.d.ts +18 -0
  28. package/dist/hash-sync.d.ts.map +1 -0
  29. package/dist/hash-sync.js +45 -0
  30. package/dist/hash-sync.js.map +1 -0
  31. package/dist/i18n.d.ts +222 -0
  32. package/dist/i18n.d.ts.map +1 -0
  33. package/dist/i18n.js +434 -0
  34. package/dist/i18n.js.map +1 -0
  35. package/dist/icons.d.ts +37 -0
  36. package/dist/icons.d.ts.map +1 -0
  37. package/dist/icons.js +36 -0
  38. package/dist/icons.js.map +1 -0
  39. package/dist/index.d.ts +151 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +41 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/keyboard.d.ts +64 -0
  44. package/dist/keyboard.d.ts.map +1 -0
  45. package/dist/keyboard.js +95 -0
  46. package/dist/keyboard.js.map +1 -0
  47. package/dist/list-model.d.ts +35 -0
  48. package/dist/list-model.d.ts.map +1 -0
  49. package/dist/list-model.js +70 -0
  50. package/dist/list-model.js.map +1 -0
  51. package/dist/payload.d.ts +10 -0
  52. package/dist/payload.d.ts.map +1 -0
  53. package/dist/payload.js +138 -0
  54. package/dist/payload.js.map +1 -0
  55. package/dist/pipeline.d.ts +31 -0
  56. package/dist/pipeline.d.ts.map +1 -0
  57. package/dist/pipeline.js +122 -0
  58. package/dist/pipeline.js.map +1 -0
  59. package/dist/prefs.d.ts +18 -0
  60. package/dist/prefs.d.ts.map +1 -0
  61. package/dist/prefs.js +38 -0
  62. package/dist/prefs.js.map +1 -0
  63. package/dist/registry.d.ts +3 -0
  64. package/dist/registry.d.ts.map +1 -0
  65. package/dist/registry.js +3 -0
  66. package/dist/registry.js.map +1 -0
  67. package/dist/renderer-port.d.ts +14 -0
  68. package/dist/renderer-port.d.ts.map +1 -0
  69. package/dist/renderer-port.js +16 -0
  70. package/dist/renderer-port.js.map +1 -0
  71. package/dist/replay.d.ts +55 -0
  72. package/dist/replay.d.ts.map +1 -0
  73. package/dist/replay.js +147 -0
  74. package/dist/replay.js.map +1 -0
  75. package/dist/review-store.d.ts +30 -0
  76. package/dist/review-store.d.ts.map +1 -0
  77. package/dist/review-store.js +120 -0
  78. package/dist/review-store.js.map +1 -0
  79. package/dist/search.d.ts +25 -0
  80. package/dist/search.d.ts.map +1 -0
  81. package/dist/search.js +90 -0
  82. package/dist/search.js.map +1 -0
  83. package/dist/standalone/kgdiff-viewer-compute.js +1 -0
  84. package/dist/standalone/kgdiff-viewer.js +5 -0
  85. package/dist/standalone-compute.d.ts +2 -0
  86. package/dist/standalone-compute.d.ts.map +1 -0
  87. package/dist/standalone-compute.js +8 -0
  88. package/dist/standalone-compute.js.map +1 -0
  89. package/dist/standalone.d.ts +2 -0
  90. package/dist/standalone.d.ts.map +1 -0
  91. package/dist/standalone.js +18 -0
  92. package/dist/standalone.js.map +1 -0
  93. package/dist/styles.d.ts +11 -0
  94. package/dist/styles.d.ts.map +1 -0
  95. package/dist/styles.js +622 -0
  96. package/dist/styles.js.map +1 -0
  97. package/dist/text-diff.d.ts +44 -0
  98. package/dist/text-diff.d.ts.map +1 -0
  99. package/dist/text-diff.js +261 -0
  100. package/dist/text-diff.js.map +1 -0
  101. package/dist/ui/context.d.ts +32 -0
  102. package/dist/ui/context.d.ts.map +1 -0
  103. package/dist/ui/context.js +49 -0
  104. package/dist/ui/context.js.map +1 -0
  105. package/dist/ui/detail.d.ts +9 -0
  106. package/dist/ui/detail.d.ts.map +1 -0
  107. package/dist/ui/detail.js +609 -0
  108. package/dist/ui/detail.js.map +1 -0
  109. package/dist/ui/header.d.ts +14 -0
  110. package/dist/ui/header.d.ts.map +1 -0
  111. package/dist/ui/header.js +195 -0
  112. package/dist/ui/header.js.map +1 -0
  113. package/dist/ui/popovers.d.ts +20 -0
  114. package/dist/ui/popovers.d.ts.map +1 -0
  115. package/dist/ui/popovers.js +180 -0
  116. package/dist/ui/popovers.js.map +1 -0
  117. package/dist/ui/sidebar.d.ts +25 -0
  118. package/dist/ui/sidebar.d.ts.map +1 -0
  119. package/dist/ui/sidebar.js +609 -0
  120. package/dist/ui/sidebar.js.map +1 -0
  121. package/dist/ui/stage.d.ts +18 -0
  122. package/dist/ui/stage.d.ts.map +1 -0
  123. package/dist/ui/stage.js +664 -0
  124. package/dist/ui/stage.js.map +1 -0
  125. package/package.json +58 -3
  126. package/src/compute-loader.ts +58 -0
  127. package/src/compute.ts +7 -0
  128. package/src/controller.ts +1393 -0
  129. package/src/dom.ts +131 -0
  130. package/src/element.ts +697 -0
  131. package/src/framing.ts +150 -0
  132. package/src/hash-sync.ts +54 -0
  133. package/src/i18n.ts +467 -0
  134. package/src/icons.ts +48 -0
  135. package/src/index.ts +171 -0
  136. package/src/keyboard.ts +128 -0
  137. package/src/list-model.ts +86 -0
  138. package/src/payload.ts +165 -0
  139. package/src/pipeline.ts +179 -0
  140. package/src/prefs.ts +55 -0
  141. package/src/registry.ts +2 -0
  142. package/src/renderer-port.ts +31 -0
  143. package/src/replay.ts +163 -0
  144. package/src/review-store.ts +125 -0
  145. package/src/search.ts +113 -0
  146. package/src/standalone-compute.ts +8 -0
  147. package/src/standalone.ts +18 -0
  148. package/src/styles.ts +633 -0
  149. package/src/text-diff.ts +263 -0
  150. package/src/ui/context.ts +70 -0
  151. package/src/ui/detail.ts +929 -0
  152. package/src/ui/header.ts +278 -0
  153. package/src/ui/popovers.ts +251 -0
  154. package/src/ui/sidebar.ts +752 -0
  155. package/src/ui/stage.ts +853 -0
@@ -0,0 +1,929 @@
1
+ /**
2
+ * Detail panel (docs/interaction.md §Detail panel): for the selected hunk (or node) — title,
3
+ * family pill, prev / next, reviewed toggle; then what changed, first: one row per changed
4
+ * facet, before → after (character diff for names, word diff for text, set changes paired into
5
+ * edits); merge / split composition, relations as sentences and match evidence; the unchanged
6
+ * rest last, its attributes folded. Added, removed and inspected nodes show their overview,
7
+ * attributes and relations.
8
+ */
9
+ import type {
10
+ AttrChange,
11
+ DiffResult,
12
+ EdgeFacet,
13
+ MatchEvidence,
14
+ NodeFacet,
15
+ UnionEdge,
16
+ UnionId,
17
+ UnionNode,
18
+ } from '@kgdiff/core';
19
+ import type { Family, Scene, SceneHunk } from '@kgdiff/scene';
20
+ import type { Topic } from '../controller.js';
21
+ import { attr, h, icon, iconButton, replaceKeepingFocus } from '../dom.js';
22
+ import { formatPercent, type Translator } from '../i18n.js';
23
+ import { type DiffSegment, diffChars, diffWords, pairItems } from '../text-diff.js';
24
+ import { formatValue, listItems, sceneHunkGlyph, type UiContext } from './context.js';
25
+
26
+ const MAX_NODES = 12;
27
+ const MAX_RELATIONS = 12;
28
+ const CLAMP_CHARS = 180;
29
+ /** Up to this many characters (both sides together) a value change reads `before → after`. */
30
+ const INLINE_CHARS = 60;
31
+
32
+ type Child = Node | string | null | false | undefined;
33
+
34
+ function section(title: string, count: number | null, ...children: Child[]): HTMLElement {
35
+ return h(
36
+ 'section',
37
+ { class: 'section' },
38
+ h('h3', null, title, count !== null ? h('span', { class: 'n', text: String(count) }) : null),
39
+ ...children,
40
+ );
41
+ }
42
+
43
+ function kv(rows: readonly (readonly [string, Child | Child[]])[]): HTMLElement {
44
+ const dl = h('dl', { class: 'kv' });
45
+ for (const [k, v] of rows) dl.append(h('dt', { text: k }), h('dd', null, ...[v].flat()));
46
+ return dl;
47
+ }
48
+
49
+ /** Render one side of a diff: equal text plus that side's changes marked. */
50
+ function marked(segs: readonly DiffSegment[], side: 'before' | 'after'): (string | HTMLElement)[] {
51
+ const out: (string | HTMLElement)[] = [];
52
+ for (const s of segs) {
53
+ if (s.op === 'equal') out.push(s.text);
54
+ else if (s.op === 'delete' && side === 'before')
55
+ out.push(h('del', { class: 'hl', text: s.text }));
56
+ else if (s.op === 'insert' && side === 'after')
57
+ out.push(h('ins', { class: 'hl', text: s.text }));
58
+ }
59
+ return out;
60
+ }
61
+
62
+ function chip(text: string, kind: 'add' | 'del' | 'same'): HTMLElement {
63
+ const sign = kind === 'add' ? '+' : kind === 'del' ? '−' : '';
64
+ return h(
65
+ 'span',
66
+ { class: `vchip${kind === 'same' ? '' : ` ${kind}`}` },
67
+ sign ? h('span', { class: 'sign', 'aria-hidden': 'true', text: sign }) : null,
68
+ text,
69
+ );
70
+ }
71
+
72
+ function chips(
73
+ added: readonly string[],
74
+ removed: readonly string[],
75
+ same: readonly string[] = [],
76
+ ): HTMLElement {
77
+ return h(
78
+ 'div',
79
+ { class: 'chips' },
80
+ removed.map((x) => chip(x, 'del')),
81
+ added.map((x) => chip(x, 'add')),
82
+ same.map((x) => chip(x, 'same')),
83
+ );
84
+ }
85
+
86
+ /** `before → after` on one line, wrapping when long. */
87
+ function pair(before: Child[], after: Child[], tr: Translator): HTMLElement {
88
+ return h(
89
+ 'div',
90
+ { class: 'pair' },
91
+ h('span', { class: 'b' }, before),
92
+ h('span', { class: 'arr', 'aria-hidden': 'true', text: '→' }),
93
+ h('span', { class: 'sr-only', text: tr.t('detail.changedTo') }),
94
+ h('span', null, after),
95
+ );
96
+ }
97
+
98
+ /** `before → after` with the changed characters marked. */
99
+ function textPair(before: string, after: string, tr: Translator): HTMLElement {
100
+ const segs = diffChars(before, after);
101
+ return pair(marked(segs, 'before'), marked(segs, 'after'), tr);
102
+ }
103
+
104
+ /** One side of a change on a line of its own: `− before` or `+ after`. */
105
+ function signed(side: 'old' | 'new', content: Child): HTMLElement {
106
+ return h(
107
+ 'div',
108
+ { class: `line ${side}` },
109
+ h('span', { class: 'sign', 'aria-hidden': 'true', text: side === 'old' ? '−' : '+' }),
110
+ h('div', null, content),
111
+ );
112
+ }
113
+
114
+ /** A set change (synonyms, types, parents, list values): edits as pairs, the rest as chips. */
115
+ function items(
116
+ added: readonly string[],
117
+ removed: readonly string[],
118
+ tr: Translator,
119
+ same: readonly string[] = [],
120
+ ): Child[] {
121
+ const p = pairItems(removed, added);
122
+ const out: Child[] = p.pairs.map(([b, a]) => textPair(b, a, tr));
123
+ if (p.added.length > 0 || p.removed.length > 0 || same.length > 0) {
124
+ out.push(chips(p.added, p.removed, same));
125
+ }
126
+ return out;
127
+ }
128
+
129
+ /** Key · value rows. */
130
+ function table(rows: readonly (readonly [string, Child | Child[]])[], cls = 'attrs'): HTMLElement {
131
+ return h(
132
+ 'table',
133
+ { class: cls },
134
+ h('colgroup', null, h('col', { class: 'k' }), h('col')),
135
+ h(
136
+ 'tbody',
137
+ null,
138
+ rows.map(([k, v]) => h('tr', null, h('td', { class: 'k', text: k }), h('td', null, v))),
139
+ ),
140
+ );
141
+ }
142
+
143
+ function familyOfEdge(e: UnionEdge): Family {
144
+ if (e.status === 'added') return 'added';
145
+ if (e.status === 'removed') {
146
+ return e.facets.some((f) => f.kind === 'collapsed') ? 'identity' : 'removed';
147
+ }
148
+ for (const f of e.facets) {
149
+ if (f.kind === 'retarget')
150
+ return f.cause === 'merge' || f.cause === 'split' ? 'identity' : 'structure';
151
+ if (f.kind === 'predicate') return 'structure';
152
+ }
153
+ return e.status === 'modified' ? 'content' : 'neutral';
154
+ }
155
+
156
+ export class Detail {
157
+ readonly el: HTMLElement;
158
+ readonly #body: HTMLElement;
159
+ #showUnchangedAttrs = false;
160
+ #showAllRelations = false;
161
+ #showAllNodes = false;
162
+ readonly #expandedValues = new Set<string>();
163
+ #lastKey = '';
164
+
165
+ readonly #ctx: UiContext;
166
+
167
+ constructor(ctx: UiContext) {
168
+ this.#ctx = ctx;
169
+ this.#body = h('div', { class: 'detail-scroll scroll', tabindex: '-1' });
170
+ this.el = h('section', { class: 'detail', 'data-empty': 'true' }, this.#body);
171
+ }
172
+
173
+ update(topics: ReadonlySet<Topic>): void {
174
+ if (
175
+ topics.has('selection') ||
176
+ topics.has('data') ||
177
+ topics.has('locale') ||
178
+ topics.has('review') ||
179
+ topics.has('status') ||
180
+ topics.has('filters')
181
+ ) {
182
+ this.#render();
183
+ }
184
+ }
185
+
186
+ #render(): void {
187
+ const c = this.#ctx.controller;
188
+ const tr = this.#ctx.tr();
189
+ attr(this.el, 'aria-label', tr.t('detail.label'));
190
+ const scene = c.scene;
191
+ const diff = c.diff;
192
+ this.el.hidden =
193
+ c.status !== 'ready' || !scene || !diff || (scene.hunks.length === 0 && c.selectedNode < 0);
194
+ if (this.el.hidden || !scene || !diff) return;
195
+ const key = `${c.selectedHunk}|${c.selectedNode}`;
196
+ if (key !== this.#lastKey) {
197
+ this.#lastKey = key;
198
+ this.#showUnchangedAttrs = false;
199
+ this.#showAllRelations = false;
200
+ this.#showAllNodes = false;
201
+ this.#expandedValues.clear();
202
+ this.#body.scrollTop = 0;
203
+ }
204
+ if (c.selectedHunk < 0 && c.selectedNode < 0) {
205
+ attr(this.el, 'data-empty', 'true');
206
+ replaceKeepingFocus(
207
+ this.#ctx.root,
208
+ this.#body,
209
+ this.#body,
210
+ h(
211
+ 'div',
212
+ { class: 'detail-empty' },
213
+ icon('changes'),
214
+ h(
215
+ 'div',
216
+ null,
217
+ h('div', { text: tr.t('detail.empty') }),
218
+ h('div', { class: 'sub', text: tr.t('detail.emptyHint') }),
219
+ ),
220
+ ),
221
+ );
222
+ return;
223
+ }
224
+ attr(this.el, 'data-empty', 'false');
225
+ const hunk = c.selectedHunk >= 0 ? scene.hunks[c.selectedHunk] : undefined;
226
+ if (hunk) this.#renderHunk(hunk, scene, diff, tr);
227
+ else this.#renderNode(scene, diff, tr);
228
+ }
229
+
230
+ #nav(tr: Translator, hunk: SceneHunk | undefined): HTMLElement {
231
+ const c = this.#ctx.controller;
232
+ const items: Child[] = [];
233
+ if (hunk) {
234
+ const reviewed = c.isReviewed(hunk.id);
235
+ items.push(
236
+ h(
237
+ 'button',
238
+ {
239
+ type: 'button',
240
+ class: 'rv-btn',
241
+ 'data-focus-key': 'review',
242
+ 'aria-pressed': String(reviewed),
243
+ title: `${tr.t(reviewed ? 'review.unmark' : 'review.mark')} (X)`,
244
+ onclick: () => c.toggleReviewed(hunk.id),
245
+ },
246
+ h('span', { class: 'ring' }, icon('check')),
247
+ tr.t('review.toggle'),
248
+ ),
249
+ );
250
+ }
251
+ const rank = hunk ? c.list.rank.get(hunk.index) : undefined;
252
+ const prev = iconButton('chevron-up', `${tr.t('detail.prev')} (K)`, () => c.prev(), {
253
+ class: 'icon-btn sm',
254
+ 'data-focus-key': 'prev',
255
+ });
256
+ const next = iconButton('chevron-down', `${tr.t('detail.next')} (J)`, () => c.next(), {
257
+ class: 'icon-btn sm',
258
+ 'data-focus-key': 'next',
259
+ });
260
+ prev.disabled = rank === 0 || c.list.order.length === 0;
261
+ next.disabled =
262
+ (rank !== undefined && rank >= c.list.order.length - 1) || c.list.order.length === 0;
263
+ items.push(prev, next);
264
+ return h('div', { class: 'd-nav' }, ...items);
265
+ }
266
+
267
+ #renderHunk(hunk: SceneHunk, scene: Scene, diff: DiffResult, tr: Translator): void {
268
+ const c = this.#ctx.controller;
269
+ const core = diff.index.hunk(hunk.id);
270
+ const focusIndex =
271
+ c.selectedNode >= 0 && hunk.nodes.includes(c.selectedNode) ? c.selectedNode : hunk.anchor;
272
+ const focusId = focusIndex >= 0 ? scene.nodes[focusIndex]?.id : core?.anchor;
273
+ const focus = focusId !== undefined ? diff.index.node(focusId) : undefined;
274
+ const parts: Child[] = [
275
+ h(
276
+ 'div',
277
+ { class: 'd-top' },
278
+ h(
279
+ 'span',
280
+ { class: 'fam-pill', 'data-family': hunk.family },
281
+ h('span', { class: 'g', 'aria-hidden': 'true', text: sceneHunkGlyph(hunk) }),
282
+ tr.t(`kind.${hunk.kind}`),
283
+ ),
284
+ this.#nav(tr, hunk),
285
+ ),
286
+ h('h2', { class: 'd-title', text: hunk.title }),
287
+ hunk.subtitle ? h('div', { class: 'd-sub', text: hunk.subtitle }) : null,
288
+ ];
289
+ const nodeIds = core?.nodes ?? [];
290
+ if (nodeIds.length > 1) parts.push(this.#nodeList(nodeIds, focusId, scene, diff, tr));
291
+ const [lead, rest] = focus
292
+ ? this.#nodeSections(focus, diff, tr, nodeIds.length > 1, true)
293
+ : [[], []];
294
+ const edges = (core?.edges ?? [])
295
+ .map((id) => diff.index.edge(id))
296
+ .filter((e): e is UnionEdge => e !== undefined);
297
+ parts.push(...lead);
298
+ if (edges.length > 0) parts.push(this.#relations(edges, focusId, scene, diff, tr, true));
299
+ parts.push(...rest);
300
+ replaceKeepingFocus(this.#ctx.root, this.#body, this.#body, ...parts);
301
+ }
302
+
303
+ #renderNode(scene: Scene, diff: DiffResult, tr: Translator): void {
304
+ const c = this.#ctx.controller;
305
+ const sn = scene.nodes[c.selectedNode];
306
+ const node = sn ? diff.index.node(sn.id) : undefined;
307
+ if (!sn || !node) {
308
+ replaceKeepingFocus(this.#ctx.root, this.#body, this.#body);
309
+ return;
310
+ }
311
+ const edges = diff.index.edgesOf(node.id);
312
+ const [lead, rest] = this.#nodeSections(node, diff, tr, false, false);
313
+ replaceKeepingFocus(
314
+ this.#ctx.root,
315
+ this.#body,
316
+ this.#body,
317
+ h(
318
+ 'div',
319
+ { class: 'd-top' },
320
+ h(
321
+ 'span',
322
+ { class: 'fam-pill', 'data-family': sn.family },
323
+ h('span', { class: 'g', 'aria-hidden': 'true', text: '·' }),
324
+ tr.t(node.status === 'unchanged' ? 'detail.unchangedNode' : `status.${node.status}`),
325
+ ),
326
+ this.#nav(tr, undefined),
327
+ ),
328
+ h('h2', { class: 'd-title', text: node.label }),
329
+ ...lead,
330
+ edges.length > 0 ? this.#relations(edges, node.id, scene, diff, tr, false) : null,
331
+ ...rest,
332
+ );
333
+ }
334
+
335
+ // ─── Sections ────────────────────────────────────────────────────────────
336
+
337
+ #nodeList(
338
+ ids: readonly UnionId[],
339
+ focusId: UnionId | undefined,
340
+ scene: Scene,
341
+ diff: DiffResult,
342
+ tr: Translator,
343
+ ): HTMLElement {
344
+ const c = this.#ctx.controller;
345
+ const shown = this.#showAllNodes ? ids : ids.slice(0, MAX_NODES);
346
+ const list = h('div', { class: 'nodes' });
347
+ for (const id of shown) {
348
+ const n = diff.index.node(id);
349
+ const i = scene.nodeIndex.get(id);
350
+ const fam = i !== undefined ? (scene.nodes[i]?.family ?? 'neutral') : 'neutral';
351
+ list.appendChild(
352
+ h(
353
+ 'button',
354
+ {
355
+ type: 'button',
356
+ class: 'node-link',
357
+ 'data-focus-key': `node:${id}`,
358
+ 'data-family': fam,
359
+ 'aria-current': String(id === focusId),
360
+ onclick: () => {
361
+ if (i !== undefined) c.selectNode(i, { fly: true });
362
+ },
363
+ },
364
+ h('span', { class: 'sdot', 'aria-hidden': 'true' }),
365
+ h('span', { class: 'lbl', text: n?.label ?? id, title: n?.label ?? id }),
366
+ h('span', { class: 'meta', text: n ? tr.t(`status.${n.status}`) : '' }),
367
+ ),
368
+ );
369
+ }
370
+ return section(
371
+ tr.t('detail.nodes'),
372
+ ids.length,
373
+ list,
374
+ ids.length > MAX_NODES
375
+ ? this.#moreToggle(tr, this.#showAllNodes, () => {
376
+ this.#showAllNodes = !this.#showAllNodes;
377
+ this.#render();
378
+ })
379
+ : null,
380
+ );
381
+ }
382
+
383
+ #moreToggle(tr: Translator, expanded: boolean, onClick: () => void): HTMLElement {
384
+ return h(
385
+ 'button',
386
+ {
387
+ type: 'button',
388
+ class: 'disclosure',
389
+ 'data-focus-key': 'nodes-more',
390
+ 'aria-expanded': String(expanded),
391
+ onclick: onClick,
392
+ },
393
+ tr.t(expanded ? 'detail.showLess' : 'detail.showMore'),
394
+ icon('chevron-down'),
395
+ );
396
+ }
397
+
398
+ /**
399
+ * The node's sections, split around the relations. A modified node (or an unchanged one a hunk
400
+ * points at) leads with what changed and ends with its unchanged rest; added, removed and
401
+ * inspected nodes lead with what they are.
402
+ */
403
+ #nodeSections(
404
+ node: UnionNode,
405
+ diff: DiffResult,
406
+ tr: Translator,
407
+ showName: boolean,
408
+ inHunk: boolean,
409
+ ): [HTMLElement[], HTMLElement[]] {
410
+ const label = (id: UnionId): string => diff.index.node(id)?.label ?? id;
411
+ const fold = node.status === 'modified' || (inHunk && node.status === 'unchanged');
412
+ const changes = this.#changes(node, diff, tr, label);
413
+ const comp = this.#composition(node, diff, tr, label);
414
+ const evidence = this.#evidence(node, diff, tr, label);
415
+ const overview = this.#overview(node, tr, showName, fold);
416
+ const present = (xs: readonly (HTMLElement | null)[]): HTMLElement[] =>
417
+ xs.filter((x): x is HTMLElement => x !== null);
418
+ return fold
419
+ ? [present([comp, changes]), present([evidence, ...overview])]
420
+ : [present([...overview, changes, comp, evidence]), []];
421
+ }
422
+
423
+ /** What changed on the node: one row per facet (per key for attributes), in facet order. */
424
+ #changes(
425
+ node: UnionNode,
426
+ diff: DiffResult,
427
+ tr: Translator,
428
+ label: (id: UnionId) => string,
429
+ ): HTMLElement | null {
430
+ // Replacement links hold ids as written: name the node they point at when there is one.
431
+ const named = (ids: readonly string[]): string[] =>
432
+ ids.map((id) => (diff.index.nodeByAfter(id) ?? diff.index.nodeByBefore(id))?.label ?? id);
433
+ const rows: [string, Child | Child[]][] = [];
434
+ for (const f of node.facets) {
435
+ switch (f.kind) {
436
+ case 'rekey':
437
+ rows.push([
438
+ tr.t('detail.id'),
439
+ [
440
+ h('div', { class: 'id-text' }, textPair(f.from, f.to, tr)),
441
+ f.score < 1
442
+ ? h('div', {
443
+ class: 'note-line',
444
+ text: tr.t('detail.matchScore', { p: formatPercent(f.score) }),
445
+ })
446
+ : null,
447
+ ],
448
+ ]);
449
+ break;
450
+ case 'rename':
451
+ rows.push([tr.t('detail.name'), textPair(f.from, f.to, tr)]);
452
+ break;
453
+ case 'types':
454
+ case 'aliases':
455
+ rows.push([tr.t(`detail.${f.kind}`), items(f.added, f.removed, tr)]);
456
+ break;
457
+ case 'attrs':
458
+ for (const ch of f.changes) rows.push([ch.key, this.#attrChange(node.id, ch, tr)]);
459
+ break;
460
+ case 'move':
461
+ rows.push([tr.t('detail.hierarchy'), items(f.to.map(label), f.from.map(label), tr)]);
462
+ break;
463
+ case 'deprecate':
464
+ rows.push([
465
+ tr.t('detail.status'),
466
+ h('strong', {
467
+ text: tr.t(f.deprecated ? 'detail.deprecated' : 'detail.undeprecated'),
468
+ }),
469
+ ]);
470
+ break;
471
+ case 'replacement':
472
+ for (const [key, links] of [
473
+ ['detail.replacedBy', f.replacedBy],
474
+ ['detail.replaces', f.replaces],
475
+ ] as const) {
476
+ if (links.added.length > 0 || links.removed.length > 0) {
477
+ rows.push([tr.t(key), items(named(links.added), named(links.removed), tr)]);
478
+ }
479
+ }
480
+ break;
481
+ default:
482
+ // Merges and splits: the composition section.
483
+ break;
484
+ }
485
+ }
486
+ return rows.length > 0
487
+ ? section(tr.t('detail.changes'), rows.length, table(rows, 'attrs changes'))
488
+ : null;
489
+ }
490
+
491
+ /** One attribute's change: lists as set changes, short values inline, long ones a line a side. */
492
+ #attrChange(nodeId: string, ch: AttrChange, tr: Translator): Child[] {
493
+ const beforeList = listItems(ch.before);
494
+ const afterList = listItems(ch.after);
495
+ if (beforeList && afterList) {
496
+ const b = new Set(beforeList);
497
+ const a = new Set(afterList);
498
+ return items(
499
+ afterList.filter((x) => !b.has(x)),
500
+ beforeList.filter((x) => !a.has(x)),
501
+ tr,
502
+ afterList.filter((x) => b.has(x)),
503
+ );
504
+ }
505
+ const key = `${nodeId}|${ch.key}`;
506
+ const before = formatValue(ch.before);
507
+ const after = formatValue(ch.after);
508
+ if (ch.before === undefined) {
509
+ const ins = h('ins', { class: 'hl', text: after });
510
+ return [signed('new', this.#value(`${key}|a`, after, tr, [ins]))];
511
+ }
512
+ if (ch.after === undefined) {
513
+ const del = h('del', { class: 'hl', text: before });
514
+ return [signed('old', this.#value(`${key}|b`, before, tr, [del]))];
515
+ }
516
+ const segs: readonly DiffSegment[] =
517
+ typeof ch.before === 'string' && typeof ch.after === 'string'
518
+ ? diffWords(before, after)
519
+ : [
520
+ { op: 'delete', text: before },
521
+ { op: 'insert', text: after },
522
+ ];
523
+ const b = marked(segs, 'before');
524
+ const a = marked(segs, 'after');
525
+ return before.length + after.length <= INLINE_CHARS && !/\n/.test(before + after)
526
+ ? [pair(b, a, tr)]
527
+ : [
528
+ signed('old', this.#value(`${key}|b`, before, tr, b)),
529
+ signed('new', this.#value(`${key}|a`, after, tr, a)),
530
+ ];
531
+ }
532
+
533
+ /**
534
+ * Identity and attributes, without what the changes section shows. Folded, the attributes
535
+ * (all unchanged) hide behind a toggle; otherwise they get a section of their own.
536
+ */
537
+ #overview(node: UnionNode, tr: Translator, showName: boolean, fold: boolean): HTMLElement[] {
538
+ const has = (kind: NodeFacet['kind']): boolean => node.facets.some((f) => f.kind === kind);
539
+ const current = node.after ?? node.before;
540
+ const rows: [string, Child | Child[]][] = [];
541
+ if (showName && !has('rename')) rows.push([tr.t('detail.name'), node.label]);
542
+ if (!has('rekey')) {
543
+ rows.push([
544
+ tr.t('detail.id'),
545
+ h('span', { class: 'id-text', text: node.afterId ?? node.beforeId ?? node.id }),
546
+ ]);
547
+ }
548
+ if (!has('types') && node.types.length > 0) {
549
+ rows.push([tr.t('detail.types'), chips([], [], node.types)]);
550
+ }
551
+ if (!has('aliases') && current && current.aliases.length > 0) {
552
+ rows.push([tr.t('detail.aliases'), chips([], [], current.aliases)]);
553
+ }
554
+ const status: Child[] = [tr.t(`status.${node.status}`)];
555
+ if (current?.deprecated && !has('deprecate')) status.push(` · ${tr.t('detail.deprecated')}`);
556
+ if (current?.implicit) status.push(` · ${tr.t('detail.implicit')}`);
557
+ rows.push([tr.t('detail.status'), status]);
558
+
559
+ const all = current?.attrs ?? {};
560
+ const changed = new Set<string>();
561
+ for (const f of node.facets) {
562
+ if (f.kind === 'attrs') for (const c of f.changes) changed.add(c.key);
563
+ }
564
+ const keys = Object.keys(all)
565
+ .filter((k) => !changed.has(k))
566
+ .sort();
567
+ const attrs = (): HTMLElement =>
568
+ table(keys.map((k) => [k, this.#value(`${node.id}|u|${k}`, formatValue(all[k]), tr)]));
569
+ if (!fold) {
570
+ const out = [section(tr.t('detail.identity'), null, kv(rows))];
571
+ if (keys.length > 0) out.push(section(tr.t('detail.attributes'), null, attrs()));
572
+ return out;
573
+ }
574
+ return [
575
+ section(
576
+ tr.t('detail.identity'),
577
+ null,
578
+ kv(rows),
579
+ keys.length > 0
580
+ ? h(
581
+ 'button',
582
+ {
583
+ type: 'button',
584
+ class: 'disclosure',
585
+ 'data-focus-key': 'attrs-more',
586
+ 'aria-expanded': String(this.#showUnchangedAttrs),
587
+ onclick: () => {
588
+ this.#showUnchangedAttrs = !this.#showUnchangedAttrs;
589
+ this.#render();
590
+ },
591
+ },
592
+ tr.plural('detail.unchangedAttrs', keys.length),
593
+ icon('chevron-down'),
594
+ )
595
+ : null,
596
+ keys.length > 0 && this.#showUnchangedAttrs ? attrs() : null,
597
+ ),
598
+ ];
599
+ }
600
+
601
+ /** A value cell, clamped to 4 lines with “Show more” when long. */
602
+ #value(
603
+ key: string,
604
+ text: string,
605
+ tr: Translator,
606
+ content?: (string | HTMLElement)[],
607
+ ): HTMLElement {
608
+ const long = text.length > CLAMP_CHARS;
609
+ const open = this.#expandedValues.has(key);
610
+ const v = h('div', { class: `val${long && !open ? ' clamped' : ''}` }, ...(content ?? [text]));
611
+ if (!long) return v;
612
+ return h(
613
+ 'div',
614
+ null,
615
+ v,
616
+ h('button', {
617
+ type: 'button',
618
+ class: 'link-btn more',
619
+ 'data-focus-key': `value:${key}`,
620
+ text: tr.t(open ? 'detail.showLess' : 'detail.showMore'),
621
+ onclick: () => {
622
+ if (open) this.#expandedValues.delete(key);
623
+ else this.#expandedValues.add(key);
624
+ this.#render();
625
+ },
626
+ }),
627
+ );
628
+ }
629
+
630
+ #composition(
631
+ node: UnionNode,
632
+ diff: DiffResult,
633
+ tr: Translator,
634
+ label: (id: UnionId) => string,
635
+ ): HTMLElement | null {
636
+ const rows: HTMLElement[] = [];
637
+ const via = (v: 'hint' | 'heuristic', confidence: number): string =>
638
+ v === 'hint'
639
+ ? tr.t('detail.viaHint')
640
+ : `${tr.t('detail.viaHeuristic')} · ${tr.t('detail.confidence', { p: formatPercent(confidence) })}`;
641
+ for (const f of node.facets) {
642
+ switch (f.kind) {
643
+ case 'mergedInto':
644
+ rows.push(
645
+ h(
646
+ 'div',
647
+ { class: 'comp-row' },
648
+ chip(node.label, 'del'),
649
+ h('span', { class: 'arrow', 'aria-hidden': 'true', text: '→' }),
650
+ chip(label(f.target), 'same'),
651
+ h('span', { class: 'note', text: via(f.via, f.confidence) }),
652
+ ),
653
+ );
654
+ break;
655
+ case 'mergeTarget': {
656
+ const notes = f.sources.map((s) => {
657
+ const src = diff.index.node(s);
658
+ const m = src?.facets.find((x) => x.kind === 'mergedInto');
659
+ return m && m.kind === 'mergedInto' ? via(m.via, m.confidence) : '';
660
+ });
661
+ rows.push(
662
+ h(
663
+ 'div',
664
+ { class: 'comp-row' },
665
+ h('span', { class: 'note', text: tr.t('detail.mergedFrom') }),
666
+ ),
667
+ );
668
+ f.sources.forEach((s, i) => {
669
+ rows.push(
670
+ h(
671
+ 'div',
672
+ { class: 'comp-row' },
673
+ chip(label(s), 'del'),
674
+ h('span', { class: 'arrow', 'aria-hidden': 'true', text: '→' }),
675
+ chip(node.label, 'same'),
676
+ notes[i] ? h('span', { class: 'note', text: notes[i] }) : null,
677
+ ),
678
+ );
679
+ });
680
+ break;
681
+ }
682
+ case 'splitFrom':
683
+ rows.push(
684
+ h(
685
+ 'div',
686
+ { class: 'comp-row' },
687
+ chip(label(f.source), 'same'),
688
+ h('span', { class: 'arrow', 'aria-hidden': 'true', text: '→' }),
689
+ chip(node.label, 'add'),
690
+ h('span', { class: 'note', text: via(f.via, f.confidence) }),
691
+ ),
692
+ );
693
+ break;
694
+ case 'splitInto':
695
+ rows.push(
696
+ h(
697
+ 'div',
698
+ { class: 'comp-row' },
699
+ h('span', { class: 'note', text: tr.t('detail.splitInto') }),
700
+ ),
701
+ h(
702
+ 'div',
703
+ { class: 'comp-row' },
704
+ chip(node.label, 'same'),
705
+ h('span', { class: 'arrow', 'aria-hidden': 'true', text: '→' }),
706
+ f.targets.map((t) => chip(label(t), 'add')),
707
+ ),
708
+ );
709
+ break;
710
+ default:
711
+ break;
712
+ }
713
+ }
714
+ return rows.length
715
+ ? section(tr.t('detail.composition'), null, h('div', { class: 'comp' }, rows))
716
+ : null;
717
+ }
718
+
719
+ #evidence(
720
+ node: UnionNode,
721
+ diff: DiffResult,
722
+ tr: Translator,
723
+ label: (id: UnionId) => string,
724
+ ): HTMLElement | null {
725
+ const parts: Child[] = [];
726
+ const rekey = node.facets.find((f) => f.kind === 'rekey');
727
+ if (rekey && node.afterId !== undefined) {
728
+ const m = diff.matches.find((x) => x.after === node.afterId);
729
+ if (m?.method === 'hint')
730
+ parts.push(h('div', { class: 'note-line', text: tr.t('detail.matchHint') }));
731
+ else if (m) {
732
+ parts.push(
733
+ h('div', {
734
+ class: 'note-line',
735
+ text: tr.t('detail.matchScore', { p: formatPercent(m.score) }),
736
+ }),
737
+ );
738
+ if (m.evidence) parts.push(this.#bars(m.evidence, tr));
739
+ }
740
+ }
741
+ const suggestions = diff.suggestions.filter(
742
+ (s) =>
743
+ (node.beforeId !== undefined && node.status === 'removed' && s.before === node.beforeId) ||
744
+ (node.afterId !== undefined && node.status === 'added' && s.after === node.afterId),
745
+ );
746
+ for (const s of suggestions) {
747
+ const other =
748
+ node.status === 'removed'
749
+ ? diff.index.nodeByAfter(s.after)
750
+ : diff.index.nodeByBefore(s.before);
751
+ parts.push(
752
+ h(
753
+ 'div',
754
+ { class: 'comp-row', style: 'margin-top: 8px' },
755
+ h('span', { class: 'note', text: tr.t('detail.suggestion') }),
756
+ chip(other ? label(other.id) : node.status === 'removed' ? s.after : s.before, 'same'),
757
+ h('span', {
758
+ class: 'note',
759
+ text: `${tr.t('detail.matchScore', { p: formatPercent(s.score) })} · ${tr.t(
760
+ s.reason === 'ambiguous' ? 'detail.suggestion.ambiguous' : 'detail.suggestion.below',
761
+ )}`,
762
+ }),
763
+ ),
764
+ this.#bars(s.evidence, tr),
765
+ );
766
+ }
767
+ return parts.length ? section(tr.t('detail.evidence'), null, ...parts) : null;
768
+ }
769
+
770
+ #bars(e: MatchEvidence, tr: Translator): HTMLElement {
771
+ const rows: [string, number][] = [
772
+ [tr.t('detail.evidence.label'), e.label],
773
+ [tr.t('detail.evidence.neighbors'), e.neighbors],
774
+ [tr.t('detail.evidence.types'), e.types],
775
+ [tr.t('detail.evidence.attrs'), e.attrs],
776
+ ];
777
+ return h(
778
+ 'div',
779
+ { class: 'bars', style: 'margin-top: 8px' },
780
+ rows.map(([k, v]) =>
781
+ h(
782
+ 'div',
783
+ { class: 'bar-row' },
784
+ h('span', { class: 'lbl', text: k }),
785
+ h(
786
+ 'span',
787
+ {
788
+ class: 'track',
789
+ role: 'meter',
790
+ 'aria-label': k,
791
+ 'aria-valuemin': '0',
792
+ 'aria-valuemax': '100',
793
+ 'aria-valuenow': String(Math.round(v * 100)),
794
+ },
795
+ h('span', {
796
+ class: 'fill',
797
+ style: `display:block; width: ${Math.round(Math.max(0, Math.min(1, v)) * 100)}%`,
798
+ }),
799
+ ),
800
+ h('span', { class: 'v', text: formatPercent(v) }),
801
+ ),
802
+ ),
803
+ );
804
+ }
805
+
806
+ // ─── Relations ───────────────────────────────────────────────────────────
807
+
808
+ #relations(
809
+ edges: readonly UnionEdge[],
810
+ focusId: UnionId | undefined,
811
+ scene: Scene,
812
+ diff: DiffResult,
813
+ tr: Translator,
814
+ changed: boolean,
815
+ ): HTMLElement {
816
+ const sorted = changed
817
+ ? edges
818
+ : [...edges].sort((a, b) => Number(a.source !== focusId) - Number(b.source !== focusId));
819
+ const shown = this.#showAllRelations ? sorted : sorted.slice(0, MAX_RELATIONS);
820
+ const list = h('div', { class: 'rels' });
821
+ for (const e of shown) list.appendChild(this.#relation(e, focusId, scene, diff, tr));
822
+ const rest = sorted.length - shown.length;
823
+ return section(
824
+ tr.t('detail.relations'),
825
+ edges.length,
826
+ list,
827
+ rest > 0 || this.#showAllRelations
828
+ ? h(
829
+ 'button',
830
+ {
831
+ type: 'button',
832
+ class: 'disclosure',
833
+ 'data-focus-key': 'rels-more',
834
+ 'aria-expanded': String(this.#showAllRelations),
835
+ onclick: () => {
836
+ this.#showAllRelations = !this.#showAllRelations;
837
+ this.#render();
838
+ },
839
+ },
840
+ this.#showAllRelations
841
+ ? tr.t('detail.showLess')
842
+ : tr.plural('detail.moreRelations', rest),
843
+ icon('chevron-down'),
844
+ )
845
+ : null,
846
+ );
847
+ }
848
+
849
+ #relation(
850
+ e: UnionEdge,
851
+ focusId: UnionId | undefined,
852
+ scene: Scene,
853
+ diff: DiffResult,
854
+ tr: Translator,
855
+ ): HTMLElement {
856
+ const c = this.#ctx.controller;
857
+ const label = (id: UnionId): string => diff.index.node(id)?.label ?? id;
858
+ const node = (id: UnionId): HTMLElement => h('span', { class: 'node', text: label(id) });
859
+ const was = (id: UnionId): HTMLElement =>
860
+ h('span', { class: 'was', text: ` (${tr.t('detail.was', { x: tr.quote(label(id)) })})` });
861
+ const retarget = e.facets.find(
862
+ (f): f is Extract<EdgeFacet, { kind: 'retarget' }> => f.kind === 'retarget',
863
+ );
864
+ const predicate = e.facets.find(
865
+ (f): f is Extract<EdgeFacet, { kind: 'predicate' }> => f.kind === 'predicate',
866
+ );
867
+ const relabel = e.facets.find(
868
+ (f): f is Extract<EdgeFacet, { kind: 'label' }> => f.kind === 'label',
869
+ );
870
+ const pred = h('span', { class: 'pred', text: ` —${e.label}→ ` });
871
+ const outgoing = focusId !== undefined && e.source === focusId && e.target !== focusId;
872
+ const incoming = focusId !== undefined && e.target === focusId && e.source !== focusId;
873
+ const s: Child[] = [];
874
+ if (outgoing) {
875
+ s.push(h('span', { class: 'pred', text: `→ ${e.label} → ` }), node(e.target));
876
+ if (retarget?.end === 'target') s.push(was(retarget.from));
877
+ } else if (incoming) {
878
+ s.push(h('span', { class: 'pred', text: `← ${e.label} ← ` }), node(e.source));
879
+ if (retarget?.end === 'source') s.push(was(retarget.from));
880
+ } else {
881
+ s.push(node(e.source));
882
+ if (retarget?.end === 'source') s.push(was(retarget.from));
883
+ s.push(pred, node(e.target));
884
+ if (retarget?.end === 'target') s.push(was(retarget.from));
885
+ }
886
+ const prevLabel = predicate
887
+ ? (e.before?.label ?? predicate.from)
888
+ : relabel
889
+ ? relabel.from
890
+ : undefined;
891
+ if (prevLabel !== undefined) {
892
+ s.push(
893
+ h('span', { class: 'was', text: ` (${tr.t('detail.was', { x: tr.quote(prevLabel) })})` }),
894
+ );
895
+ }
896
+ if (e.facets.some((f) => f.kind === 'attrs')) {
897
+ s.push(h('span', { class: 'was', text: ` · ${tr.t('detail.edgeAttrs')}` }));
898
+ }
899
+ if (e.facets.some((f) => f.kind === 'collapsed')) {
900
+ s.push(h('span', { class: 'was', text: ` · ${tr.t('detail.collapsed')}` }));
901
+ }
902
+ const sign =
903
+ e.status === 'added'
904
+ ? '+'
905
+ : e.status === 'removed'
906
+ ? '−'
907
+ : e.status === 'modified'
908
+ ? '~'
909
+ : '·';
910
+ const j = scene.edgeIndex.get(e.id);
911
+ const family = familyOfEdge(e);
912
+ return h(
913
+ 'button',
914
+ {
915
+ type: 'button',
916
+ class: `rel ${e.status}`,
917
+ 'data-focus-key': `rel:${e.id}`,
918
+ 'data-family': family,
919
+ title: tr.t('detail.showOnCanvas'),
920
+ disabled: j === undefined,
921
+ onclick: () => {
922
+ if (j !== undefined) c.pinEdge(j);
923
+ },
924
+ },
925
+ h('span', { class: 'sign', 'aria-hidden': 'true', text: sign }),
926
+ h('span', { class: 's' }, ...s),
927
+ );
928
+ }
929
+ }