@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,853 @@
1
+ /**
2
+ * The canvas area and its floating chrome: replay caption and transport, swipe divider,
3
+ * changes-only context stepper, legend, zoom controls + minimap, hover tooltip and the
4
+ * loading / identical / error / empty states. Overlays never cover the current hunk: the
5
+ * element reserves their space through the renderer's safe area.
6
+ */
7
+ import { KgdiffError, type Rect } from '@kgdiff/core';
8
+ import type { ChangeFamily, Family } from '@kgdiff/scene';
9
+ import type { Topic } from '../controller.js';
10
+ import {
11
+ attr,
12
+ h,
13
+ icon,
14
+ iconButton,
15
+ relabel,
16
+ replace,
17
+ replaceKeepingFocus,
18
+ setText,
19
+ } from '../dom.js';
20
+ import type { ChromeLayout } from '../framing.js';
21
+ import type { Translator } from '../i18n.js';
22
+ import { FAMILIES } from '../styles.js';
23
+ import { sceneHunkGlyph, type UiContext } from './context.js';
24
+
25
+ const TICK_W = 1000;
26
+
27
+ /** Legend swatch: the node cue of each family (docs/visual-language.md §Meaning families). */
28
+ function swatch(family: Family): SVGSVGElement {
29
+ const v = (name: string) => `var(--kg-${name})`;
30
+ const c = family === 'neutral' ? v('neutral') : v(family);
31
+ let body: string;
32
+ switch (family) {
33
+ case 'added':
34
+ body = `<circle cx="11" cy="11" r="8.5" fill="none" stroke="${c}" stroke-opacity=".35" stroke-width="2"/><circle cx="11" cy="11" r="5" fill="${c}"/>`;
35
+ break;
36
+ case 'removed':
37
+ body = `<circle cx="11" cy="11" r="5.25" fill="none" stroke="${c}" stroke-width="1.5" stroke-dasharray="3 2.5"/>`;
38
+ break;
39
+ case 'identity':
40
+ body = `<circle cx="11" cy="11" r="9.5" fill="none" stroke="${c}" stroke-opacity=".3" stroke-width="1.5"/><circle cx="11" cy="11" r="7.25" fill="none" stroke="${c}" stroke-width="1.5"/><circle cx="11" cy="11" r="4.25" fill="${c}"/>`;
41
+ break;
42
+ case 'content':
43
+ case 'structure':
44
+ body = `<circle cx="11" cy="11" r="7.75" fill="none" stroke="${c}" stroke-width="1.5"/><circle cx="11" cy="11" r="4.5" fill="${c}"/>`;
45
+ break;
46
+ default:
47
+ body = `<circle cx="11" cy="11" r="3.5" fill="${c}"/>`;
48
+ }
49
+ const tpl = document.createElement('template');
50
+ tpl.innerHTML = `<svg class="swatch" viewBox="0 0 22 22" aria-hidden="true" focusable="false">${body}</svg>`;
51
+ return tpl.content.firstElementChild as SVGSVGElement;
52
+ }
53
+
54
+ export class Stage {
55
+ readonly el: HTMLElement;
56
+ readonly canvasHost: HTMLElement;
57
+ readonly minimapHost: HTMLElement;
58
+ readonly #chrome: HTMLElement;
59
+ readonly #liveCanvasLabel: HTMLElement;
60
+ // Replay
61
+ readonly #caption: HTMLElement;
62
+ readonly #transport: HTMLElement;
63
+ readonly #playButton: HTMLButtonElement;
64
+ readonly #prevButton: HTMLButtonElement;
65
+ readonly #nextButton: HTMLButtonElement;
66
+ readonly #scrubber: HTMLElement;
67
+ readonly #fill: HTMLElement;
68
+ readonly #knob: HTMLElement;
69
+ readonly #ticks: SVGSVGElement;
70
+ readonly #scrubTip: HTMLElement;
71
+ readonly #pos: HTMLElement;
72
+ readonly #speed: HTMLButtonElement;
73
+ readonly #finished: HTMLElement;
74
+ // Swipe
75
+ readonly #swipe: HTMLElement;
76
+ readonly #divider: HTMLElement;
77
+ readonly #chipBefore: HTMLElement;
78
+ readonly #chipAfter: HTMLElement;
79
+ // Changes
80
+ readonly #stepper: HTMLElement;
81
+ readonly #hopButtons: HTMLButtonElement[] = [];
82
+ readonly #expandButton: HTMLButtonElement;
83
+ // Chrome
84
+ readonly #legend: HTMLElement;
85
+ readonly #legendHead: HTMLButtonElement;
86
+ readonly #legendBody: HTMLElement;
87
+ readonly #zoomIn: HTMLButtonElement;
88
+ readonly #zoomOut: HTMLButtonElement;
89
+ readonly #fitButton: HTMLButtonElement;
90
+ readonly #mapCluster: HTMLElement;
91
+ readonly tooltip: HTMLElement;
92
+ readonly #state: HTMLElement;
93
+ #pointer: { x: number; y: number } | null = null;
94
+ #scrubbing: number | null = null;
95
+ #dragging: number | null = null;
96
+ #legendOpen: boolean | null = null;
97
+ #stateKey = '';
98
+
99
+ readonly #ctx: UiContext;
100
+
101
+ constructor(ctx: UiContext) {
102
+ this.#ctx = ctx;
103
+ const c = ctx.controller;
104
+ this.canvasHost = h('div', { class: 'stage' });
105
+ this.#liveCanvasLabel = h('div', { class: 'sr-only' });
106
+
107
+ // Replay caption + transport
108
+ this.#caption = h('div', {
109
+ class: 'caption card',
110
+ 'aria-hidden': 'true',
111
+ 'data-empty': 'true',
112
+ });
113
+ this.#prevButton = iconButton('skip-back', '', () => c.stepReplay(-1), { class: 'icon-btn' });
114
+ this.#playButton = iconButton('play', '', () => c.togglePlay(), { class: 'icon-btn play' });
115
+ this.#nextButton = iconButton('skip-forward', '', () => c.stepReplay(1), { class: 'icon-btn' });
116
+ this.#fill = h('div', { class: 'fill' });
117
+ this.#knob = h('div', { class: 'knob' });
118
+ this.#ticks = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
119
+ this.#ticks.setAttribute('class', 'ticks');
120
+ this.#ticks.setAttribute('viewBox', `0 0 ${TICK_W} 14`);
121
+ this.#ticks.setAttribute('preserveAspectRatio', 'none');
122
+ this.#ticks.setAttribute('aria-hidden', 'true');
123
+ this.#scrubTip = h('div', { class: 'scrub-tip card', hidden: true });
124
+ this.#scrubber = h(
125
+ 'div',
126
+ {
127
+ class: 'scrubber',
128
+ role: 'slider',
129
+ tabindex: '0',
130
+ 'aria-valuemin': '0',
131
+ },
132
+ h('div', { class: 'track' }, this.#fill),
133
+ this.#ticks,
134
+ this.#knob,
135
+ this.#scrubTip,
136
+ );
137
+ this.#bindScrubber();
138
+ this.#pos = h('span', { class: 'pos', 'aria-hidden': 'true' });
139
+ this.#speed = h('button', {
140
+ type: 'button',
141
+ class: 'speed',
142
+ onclick: () => c.cycleSpeed(),
143
+ });
144
+ this.#transport = h(
145
+ 'div',
146
+ { class: 'transport card', role: 'toolbar' },
147
+ this.#prevButton,
148
+ this.#playButton,
149
+ this.#nextButton,
150
+ this.#scrubber,
151
+ this.#pos,
152
+ this.#speed,
153
+ );
154
+ this.#finished = h('div', { class: 'finished card', role: 'status', hidden: true });
155
+
156
+ // Swipe
157
+ this.#chipBefore = h('div', { class: 'side-chip before chip card' });
158
+ this.#chipAfter = h('div', { class: 'side-chip after chip card' });
159
+ this.#divider = h(
160
+ 'div',
161
+ {
162
+ class: 'divider',
163
+ role: 'slider',
164
+ tabindex: '0',
165
+ 'aria-valuemin': '0',
166
+ 'aria-valuemax': '100',
167
+ 'aria-orientation': 'horizontal',
168
+ },
169
+ h(
170
+ 'span',
171
+ { class: 'handle', 'aria-hidden': 'true' },
172
+ icon('chevron-left'),
173
+ icon('chevron-right'),
174
+ ),
175
+ );
176
+ this.#bindDivider();
177
+ this.#swipe = h(
178
+ 'div',
179
+ { class: 'swipe', hidden: true },
180
+ this.#chipBefore,
181
+ this.#chipAfter,
182
+ this.#divider,
183
+ );
184
+
185
+ // Changes-only context stepper
186
+ const opts = h('span', { class: 'opts', role: 'radiogroup' });
187
+ for (let n = 0; n <= 2; n++) {
188
+ const b = h('button', {
189
+ type: 'button',
190
+ class: 'opt',
191
+ role: 'radio',
192
+ text: String(n),
193
+ onclick: () => c.setHops(n),
194
+ });
195
+ this.#hopButtons.push(b);
196
+ opts.appendChild(b);
197
+ }
198
+ opts.addEventListener('keydown', (e) => {
199
+ if (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') return;
200
+ e.preventDefault();
201
+ e.stopPropagation();
202
+ const next = Math.max(0, Math.min(2, c.hops + (e.key === 'ArrowRight' ? 1 : -1)));
203
+ c.setHops(next);
204
+ this.#hopButtons[next]?.focus();
205
+ });
206
+ this.#expandButton = h(
207
+ 'button',
208
+ { type: 'button', class: 'expand', onclick: () => c.expandNearest() },
209
+ icon('plus'),
210
+ h('span', { class: 'n' }),
211
+ );
212
+ this.#stepper = h(
213
+ 'div',
214
+ { class: 'stepper card float top-left', hidden: true },
215
+ h('span', { class: 'label' }),
216
+ opts,
217
+ this.#expandButton,
218
+ );
219
+
220
+ // Legend
221
+ this.#legendHead = h(
222
+ 'button',
223
+ {
224
+ type: 'button',
225
+ class: 'legend-head',
226
+ onclick: () => this.#setLegend(!(this.#legendOpen ?? true)),
227
+ },
228
+ h('span', { class: 'lbl' }),
229
+ icon('chevron-down'),
230
+ );
231
+ this.#legendBody = h('div', { class: 'legend-body', id: 'kg-legend' });
232
+ attr(this.#legendHead, 'aria-controls', 'kg-legend');
233
+ this.#legend = h(
234
+ 'div',
235
+ { class: 'legend card float bottom-left', 'data-open': 'true' },
236
+ this.#legendHead,
237
+ this.#legendBody,
238
+ );
239
+
240
+ // Zoom + minimap
241
+ this.#zoomIn = iconButton('plus', '', () => c.zoom(1));
242
+ this.#zoomOut = iconButton('minus', '', () => c.zoom(-1));
243
+ this.#fitButton = iconButton('fit', '', () => void c.fit());
244
+ this.minimapHost = h('div', { class: 'minimap-host' });
245
+ this.#mapCluster = h(
246
+ 'div',
247
+ { class: 'map-cluster float bottom-right' },
248
+ h(
249
+ 'div',
250
+ { class: 'zoom-bar card', role: 'toolbar' },
251
+ this.#zoomIn,
252
+ this.#zoomOut,
253
+ this.#fitButton,
254
+ ),
255
+ this.minimapHost,
256
+ );
257
+
258
+ this.tooltip = h('div', { class: 'tooltip card', role: 'tooltip', 'data-open': 'false' });
259
+ this.#state = h('div', { class: 'state', hidden: true });
260
+ this.#chrome = h(
261
+ 'div',
262
+ { class: 'chrome' },
263
+ this.#swipe,
264
+ this.#caption,
265
+ this.#stepper,
266
+ this.#legend,
267
+ this.#mapCluster,
268
+ this.#finished,
269
+ this.#transport,
270
+ this.tooltip,
271
+ );
272
+ this.el = h(
273
+ 'main',
274
+ { class: 'stage-wrap' },
275
+ this.canvasHost,
276
+ this.#liveCanvasLabel,
277
+ this.#chrome,
278
+ this.#state,
279
+ );
280
+ this.el.addEventListener('pointermove', (e) => {
281
+ const r = this.el.getBoundingClientRect();
282
+ this.#pointer = { x: e.clientX - r.left, y: e.clientY - r.top };
283
+ if (this.tooltip.dataset.open === 'true') this.#placeTooltip();
284
+ });
285
+ this.el.addEventListener('pointerleave', () => {
286
+ this.#pointer = null;
287
+ c.clearHover();
288
+ });
289
+ }
290
+
291
+ /**
292
+ * The canvas size and the floating chrome currently drawn over it (canvas-relative rects),
293
+ * for framing fits and flights around it.
294
+ */
295
+ chromeLayout(): ChromeLayout {
296
+ const r = this.el.getBoundingClientRect();
297
+ const obstacles: Rect[] = [];
298
+ const parts = [
299
+ this.#legend,
300
+ this.#mapCluster,
301
+ this.#transport,
302
+ this.#finished,
303
+ this.#stepper,
304
+ this.#chipBefore,
305
+ this.#chipAfter,
306
+ ];
307
+ if (this.#caption.dataset.empty !== 'true') parts.push(this.#caption);
308
+ for (const el of parts) {
309
+ if (el.hidden) continue;
310
+ const b = el.getBoundingClientRect();
311
+ if (b.width <= 0 || b.height <= 0) continue;
312
+ obstacles.push({ x: b.left - r.left, y: b.top - r.top, width: b.width, height: b.height });
313
+ }
314
+ return { width: r.width, height: r.height, obstacles };
315
+ }
316
+
317
+ /** Whether the stage is tall enough to show the legend expanded by default. */
318
+ #defaultLegendOpen(): boolean {
319
+ const hgt = this.el.clientHeight;
320
+ return hgt === 0 || hgt >= 560;
321
+ }
322
+
323
+ #setLegend(open: boolean): void {
324
+ this.#legendOpen = open;
325
+ attr(this.#legend, 'data-open', String(open));
326
+ attr(this.#legendHead, 'aria-expanded', String(open));
327
+ }
328
+
329
+ update(topics: ReadonlySet<Topic>): void {
330
+ const c = this.#ctx.controller;
331
+ const tr = this.#ctx.tr();
332
+ const all = topics.has('status') || topics.has('data') || topics.has('locale');
333
+ if (all) {
334
+ this.#renderLabels(tr);
335
+ this.#renderState(tr);
336
+ this.#renderTicks();
337
+ this.#renderCanvasLabel(tr);
338
+ if (this.#legendOpen === null) this.#setLegend(this.#defaultLegendOpen());
339
+ }
340
+ if (all || topics.has('mode') || topics.has('focus') || topics.has('selection')) {
341
+ this.#renderMode(tr);
342
+ }
343
+ if (all || topics.has('mode') || topics.has('replay')) this.#renderReplay(tr);
344
+ if (all || topics.has('mode') || topics.has('swipe')) this.#renderSwipe();
345
+ if (all || topics.has('hover') || topics.has('selection') || topics.has('focus')) {
346
+ this.#renderTooltip(tr);
347
+ }
348
+ if (topics.has('theme')) this.#renderTicks();
349
+ const ready = c.status === 'ready';
350
+ this.#chrome.hidden = !ready;
351
+ }
352
+
353
+ #renderLabels(tr: Translator): void {
354
+ relabel(this.#prevButton, `${tr.t('detail.prev')} (←)`);
355
+ relabel(this.#nextButton, `${tr.t('detail.next')} (→)`);
356
+ attr(this.#transport, 'aria-label', tr.t('replay.label'));
357
+ attr(this.#scrubber, 'aria-label', tr.t('replay.scrubber'));
358
+ setText(this.#chipBefore, tr.t('detail.before'));
359
+ setText(this.#chipAfter, tr.t('detail.after'));
360
+ attr(this.#divider, 'aria-label', tr.t('swipe.divider'));
361
+ setText(this.#stepper.querySelector('.label') as Element, tr.t('focus.context'));
362
+ attr(this.#stepper.querySelector('.opts') as Element, 'aria-label', tr.t('focus.hops'));
363
+ setText(this.#legendHead.querySelector('.lbl') as Element, tr.t('legend.title'));
364
+ attr(this.#legendHead, 'aria-label', tr.t('legend.toggle'));
365
+ relabel(this.#zoomIn, `${tr.t('action.zoomIn')} (+)`);
366
+ relabel(this.#zoomOut, `${tr.t('action.zoomOut')} (−)`);
367
+ relabel(this.#fitButton, `${tr.t('action.fit')} (F)`);
368
+ attr(this.minimapHost, 'aria-label', tr.t('minimap.label'));
369
+ attr(this.minimapHost, 'title', tr.t('minimap.label'));
370
+ const fams: Family[] = [...FAMILIES, 'neutral'];
371
+ replace(
372
+ this.#legendBody,
373
+ fams.map((f) =>
374
+ h(
375
+ 'div',
376
+ { class: 'legend-row', 'data-family': f, title: tr.t(`familyHint.${f}`) },
377
+ swatch(f),
378
+ h('span', { class: 'name', text: tr.t(`family.${f}`) }),
379
+ ),
380
+ ),
381
+ );
382
+ }
383
+
384
+ #renderCanvasLabel(tr: Translator): void {
385
+ const stats = this.#ctx.controller.diff?.stats;
386
+ const canvas = this.canvasHost.querySelector('canvas');
387
+ if (!stats) return;
388
+ const label = tr.t('aria.canvas', {
389
+ nb: stats.before.nodes,
390
+ eb: stats.before.edges,
391
+ na: stats.after.nodes,
392
+ ea: stats.after.edges,
393
+ changes: tr.plural('aria.changes', this.#ctx.controller.hunkCount),
394
+ });
395
+ if (canvas) attr(canvas, 'aria-label', label);
396
+ setText(this.#liveCanvasLabel, '');
397
+ }
398
+
399
+ #renderMode(tr: Translator): void {
400
+ const c = this.#ctx.controller;
401
+ const mode = c.mode;
402
+ this.#stepper.hidden = mode !== 'changes';
403
+ this.#hopButtons.forEach((b, n) => {
404
+ attr(b, 'aria-checked', String(n === c.hops));
405
+ b.tabIndex = n === c.hops ? 0 : -1;
406
+ attr(b, 'aria-label', `${tr.t('focus.hops')}: ${n}`);
407
+ });
408
+ const group = c.focus?.collapsed[c.nearestGroup()];
409
+ this.#expandButton.hidden = !group;
410
+ if (group) {
411
+ const usable = c.canExpand(c.nearestGroup()) || group.nodes.length > 0;
412
+ this.#expandButton.disabled = !usable;
413
+ setText(this.#expandButton.querySelector('.n') as Element, tr.num(group.count));
414
+ relabel(
415
+ this.#expandButton,
416
+ `${usable ? tr.t('focus.expandNearest') : tr.t('focus.expandUnavailable')}: ${group.label} (E)`,
417
+ );
418
+ }
419
+ this.#transport.hidden = mode !== 'replay';
420
+ this.#caption.hidden = mode !== 'replay';
421
+ this.#swipe.hidden = mode !== 'swipe';
422
+ }
423
+
424
+ #renderReplay(tr: Translator): void {
425
+ const c = this.#ctx.controller;
426
+ const clock = c.clock;
427
+ const scene = c.scene;
428
+ if (c.mode !== 'replay' || !clock || !scene) {
429
+ this.#finished.hidden = true;
430
+ return;
431
+ }
432
+ const p = clock.progress;
433
+ this.#fill.style.transform = `scaleX(${p})`;
434
+ this.#knob.style.left = `${p * 100}%`;
435
+ const position = clock.position();
436
+ const total = clock.total;
437
+ setText(this.#pos, tr.t('replay.position', { i: position, n: total }));
438
+ attr(this.#scrubber, 'aria-valuemax', String(Math.round(clock.duration)));
439
+ attr(this.#scrubber, 'aria-valuenow', String(Math.round(clock.t)));
440
+ attr(this.#scrubber, 'aria-valuetext', tr.t('replay.position', { i: position, n: total }));
441
+ const playing = clock.playing;
442
+ const playLabel = `${tr.t(playing ? 'replay.pause' : 'replay.play')} (Space)`;
443
+ if (this.#playButton.dataset.state !== String(playing)) {
444
+ this.#playButton.dataset.state = String(playing);
445
+ replace(this.#playButton, icon(playing ? 'pause' : 'play'));
446
+ }
447
+ relabel(this.#playButton, playLabel);
448
+ setText(this.#speed, `${clock.speed}×`);
449
+ relabel(this.#speed, tr.t('replay.speed', { s: `${clock.speed}×` }));
450
+ this.#prevButton.disabled = clock.t <= 0;
451
+ this.#nextButton.disabled = clock.t >= clock.duration;
452
+
453
+ // Caption: the playing hunk (or chapter), else the last one played.
454
+ let key = '';
455
+ let content: (Node | string)[] = [];
456
+ if (clock.t <= 0) {
457
+ // Nothing has played yet: the canvas shows the before version.
458
+ } else if (clock.timeline.mode === 'chapters') {
459
+ const target = clock.activeTarget();
460
+ const ch = target.index >= 0 ? clock.timeline.chapters[target.index] : undefined;
461
+ if (ch) {
462
+ key = `c${target.index}`;
463
+ const first = scene.hunks[ch.hunks[0] ?? -1];
464
+ content = [
465
+ h('span', {
466
+ class: 'glyph-box',
467
+ 'data-family': first?.family ?? 'neutral',
468
+ text: first ? sceneHunkGlyph(first) : '·',
469
+ }),
470
+ h('span', { class: 'text', text: tr.t(`phase.${ch.phase}`) }),
471
+ h('span', { class: 'count', text: tr.num(ch.hunks.length) }),
472
+ ];
473
+ }
474
+ } else {
475
+ let hIndex = clock.activeHunk();
476
+ if (hIndex < 0 && position > 0) hIndex = clock.timeline.windows[position - 1]?.hunk ?? -1;
477
+ const hunk = hIndex >= 0 ? scene.hunks[hIndex] : undefined;
478
+ if (hunk) {
479
+ key = `h${hIndex}|${position}`;
480
+ content = [
481
+ h('span', { class: 'glyph-box', 'data-family': hunk.family, text: sceneHunkGlyph(hunk) }),
482
+ h('span', { class: 'text', text: hunk.title, title: hunk.title }),
483
+ h('span', { class: 'count', text: `${position} / ${total}` }),
484
+ ];
485
+ }
486
+ }
487
+ if (this.#caption.dataset.key !== key) {
488
+ this.#caption.dataset.key = key;
489
+ attr(this.#caption, 'data-empty', String(key === ''));
490
+ replace(this.#caption, ...content);
491
+ }
492
+ const finished = clock.finished;
493
+ this.#finished.hidden = !finished;
494
+ if (finished && this.#finished.childElementCount === 0) {
495
+ replace(
496
+ this.#finished,
497
+ icon('check-circle'),
498
+ h('span', { text: tr.t('replay.finished') }),
499
+ h(
500
+ 'button',
501
+ { type: 'button', class: 'btn', onclick: () => c.restart() },
502
+ icon('restart'),
503
+ tr.t('replay.restart'),
504
+ ),
505
+ );
506
+ } else if (!finished && this.#finished.childElementCount > 0) {
507
+ // Restart was pressed from the pill: keep focus in the transport.
508
+ replaceKeepingFocus(this.#ctx.root, this.#finished, this.#playButton);
509
+ }
510
+ }
511
+
512
+ #renderTicks(): void {
513
+ const c = this.#ctx.controller;
514
+ const clock = c.clock;
515
+ const scene = c.scene;
516
+ while (this.#ticks.firstChild) this.#ticks.removeChild(this.#ticks.firstChild);
517
+ if (!clock || !scene || clock.duration <= 0) return;
518
+ const paths = new Map<ChangeFamily, string[]>();
519
+ for (const w of clock.timeline.windows) {
520
+ const fam = scene.hunks[w.hunk]?.family;
521
+ if (!fam) continue;
522
+ const x = ((w.start / clock.duration) * TICK_W).toFixed(1);
523
+ let list = paths.get(fam);
524
+ if (!list) {
525
+ list = [];
526
+ paths.set(fam, list);
527
+ }
528
+ list.push(`M${x} 2V12`);
529
+ }
530
+ for (const [fam, d] of paths) {
531
+ const p = document.createElementNS('http://www.w3.org/2000/svg', 'path');
532
+ p.setAttribute('d', d.join(''));
533
+ p.setAttribute('stroke', `var(--kg-${fam})`);
534
+ p.setAttribute('stroke-width', '2');
535
+ p.setAttribute('stroke-linecap', 'round');
536
+ p.setAttribute('vector-effect', 'non-scaling-stroke');
537
+ this.#ticks.appendChild(p);
538
+ }
539
+ }
540
+
541
+ #bindScrubber(): void {
542
+ const timeAt = (clientX: number): number => {
543
+ const clock = this.#ctx.controller.clock;
544
+ const r = this.#scrubber.getBoundingClientRect();
545
+ if (!clock || r.width <= 0) return 0;
546
+ return Math.max(0, Math.min(1, (clientX - r.left) / r.width)) * clock.duration;
547
+ };
548
+ this.#scrubber.addEventListener('pointerdown', (e) => {
549
+ this.#scrubbing = e.pointerId;
550
+ this.#scrubber.setPointerCapture?.(e.pointerId);
551
+ this.#ctx.controller.pause();
552
+ this.#ctx.controller.seek(timeAt(e.clientX));
553
+ });
554
+ this.#scrubber.addEventListener('pointermove', (e) => {
555
+ if (this.#scrubbing === e.pointerId) this.#ctx.controller.seek(timeAt(e.clientX));
556
+ this.#showScrubTip(e.clientX, timeAt(e.clientX));
557
+ });
558
+ const end = (e: PointerEvent): void => {
559
+ if (this.#scrubbing === e.pointerId) this.#scrubbing = null;
560
+ };
561
+ this.#scrubber.addEventListener('pointerup', end);
562
+ this.#scrubber.addEventListener('pointercancel', end);
563
+ this.#scrubber.addEventListener('pointerleave', () => {
564
+ this.#scrubTip.hidden = true;
565
+ });
566
+ this.#scrubber.addEventListener('keydown', (e) => {
567
+ const c = this.#ctx.controller;
568
+ if (e.key === 'Home') c.seek(0);
569
+ else if (e.key === 'End') c.seek(c.clock?.duration ?? 0);
570
+ else return;
571
+ e.preventDefault();
572
+ e.stopPropagation();
573
+ });
574
+ }
575
+
576
+ #showScrubTip(clientX: number, t: number): void {
577
+ const c = this.#ctx.controller;
578
+ const clock = c.clock;
579
+ const scene = c.scene;
580
+ if (!clock || !scene) return;
581
+ // Nearest window start: the tick under the pointer.
582
+ let best = -1;
583
+ let bestD = Number.POSITIVE_INFINITY;
584
+ for (const w of clock.timeline.windows) {
585
+ const d = Math.abs(w.start - t);
586
+ if (d < bestD) {
587
+ bestD = d;
588
+ best = w.hunk;
589
+ }
590
+ }
591
+ const hunk = scene.hunks[best];
592
+ if (!hunk) {
593
+ this.#scrubTip.hidden = true;
594
+ return;
595
+ }
596
+ const r = this.#scrubber.getBoundingClientRect();
597
+ this.#scrubTip.hidden = false;
598
+ this.#scrubTip.style.left = `${clientX - r.left}px`;
599
+ if (this.#scrubTip.dataset.hunk !== hunk.id) {
600
+ this.#scrubTip.dataset.hunk = hunk.id;
601
+ replace(
602
+ this.#scrubTip,
603
+ h('span', { class: 'glyph-box', 'data-family': hunk.family, text: sceneHunkGlyph(hunk) }),
604
+ h('span', { text: hunk.title }),
605
+ );
606
+ }
607
+ }
608
+
609
+ #renderSwipe(): void {
610
+ const c = this.#ctx.controller;
611
+ if (c.mode !== 'swipe') return;
612
+ const pct = c.split * 100;
613
+ this.#divider.style.left = `${pct}%`;
614
+ attr(this.#divider, 'aria-valuenow', String(Math.round(pct)));
615
+ attr(this.#divider, 'aria-valuetext', `${Math.round(pct)}%`);
616
+ }
617
+
618
+ #bindDivider(): void {
619
+ const splitAt = (clientX: number): number => {
620
+ const r = this.el.getBoundingClientRect();
621
+ return r.width > 0 ? (clientX - r.left) / r.width : 0.5;
622
+ };
623
+ this.#divider.addEventListener('pointerdown', (e) => {
624
+ e.preventDefault();
625
+ this.#dragging = e.pointerId;
626
+ this.#divider.setPointerCapture?.(e.pointerId);
627
+ this.#divider.classList.add('dragging');
628
+ this.#divider.focus({ preventScroll: true });
629
+ });
630
+ this.#divider.addEventListener('pointermove', (e) => {
631
+ if (this.#dragging === e.pointerId) this.#ctx.controller.setSplit(splitAt(e.clientX));
632
+ });
633
+ const end = (e: PointerEvent): void => {
634
+ if (this.#dragging !== e.pointerId) return;
635
+ this.#dragging = null;
636
+ this.#divider.classList.remove('dragging');
637
+ };
638
+ this.#divider.addEventListener('pointerup', end);
639
+ this.#divider.addEventListener('pointercancel', end);
640
+ this.#divider.addEventListener('dblclick', () => this.#ctx.controller.setSplit(0.5));
641
+ this.#divider.addEventListener('keydown', (e) => {
642
+ const c = this.#ctx.controller;
643
+ const to: Record<string, number> = {
644
+ Home: 0,
645
+ End: 1,
646
+ PageUp: c.split - 0.1,
647
+ PageDown: c.split + 0.1,
648
+ };
649
+ const next = to[e.key];
650
+ if (next === undefined) return;
651
+ e.preventDefault();
652
+ e.stopPropagation();
653
+ c.setSplit(next);
654
+ });
655
+ }
656
+
657
+ // ─── Tooltip ─────────────────────────────────────────────────────────────
658
+
659
+ #renderTooltip(tr: Translator): void {
660
+ const c = this.#ctx.controller;
661
+ const scene = c.scene;
662
+ const diff = c.diff;
663
+ if (!scene || !diff || this.#dragging !== null) {
664
+ attr(this.tooltip, 'data-open', 'false');
665
+ return;
666
+ }
667
+ const parts: (Node | null)[] = [];
668
+ let family: Family = 'neutral';
669
+ if (c.hoverNode >= 0) {
670
+ const sn = scene.nodes[c.hoverNode];
671
+ const un = sn ? diff.index.node(sn.id) : undefined;
672
+ if (!sn || !un) return;
673
+ family = sn.family;
674
+ const hunk = sn.hunk >= 0 ? scene.hunks[sn.hunk] : undefined;
675
+ const rekey = un.facets.find((f) => f.kind === 'rekey');
676
+ const degree = Math.max(un.degree.before, un.degree.after);
677
+ parts.push(
678
+ h('div', { class: 't-label', text: sn.label }),
679
+ sn.labelBefore ? h('div', { class: 't-old', text: sn.labelBefore }) : null,
680
+ h('div', {
681
+ class: 't-id',
682
+ text: rekey && rekey.kind === 'rekey' ? `${rekey.from} → ${rekey.to}` : sn.id,
683
+ }),
684
+ h(
685
+ 'div',
686
+ { class: 't-status', 'data-family': family },
687
+ h('span', { class: 'dot' }),
688
+ sn.status === 'unchanged'
689
+ ? tr.t('status.unchanged')
690
+ : `${tr.t(`status.${sn.status}`)} · ${tr.t(`family.${family}`)}`,
691
+ ),
692
+ h('div', {
693
+ class: 't-sum',
694
+ text: hunk ? hunk.title : tr.plural('tooltip.relations', degree),
695
+ }),
696
+ h('div', { class: 't-hint', text: tr.t('tooltip.select') }),
697
+ );
698
+ } else if (c.hoverEdge >= 0) {
699
+ const se = scene.edges[c.hoverEdge];
700
+ if (!se) return;
701
+ family = se.family;
702
+ const s = scene.nodes[se.s]?.label ?? '';
703
+ const t = scene.nodes[se.t]?.label ?? '';
704
+ const hunk = se.hunk >= 0 ? scene.hunks[se.hunk] : undefined;
705
+ parts.push(
706
+ h('div', { class: 't-label', text: `${s} —${se.label}→ ${t}` }),
707
+ se.labelBefore ? h('div', { class: 't-old', text: se.labelBefore }) : null,
708
+ h(
709
+ 'div',
710
+ { class: 't-status', 'data-family': family },
711
+ h('span', { class: 'dot' }),
712
+ tr.t(`status.${se.status}`),
713
+ ),
714
+ hunk ? h('div', { class: 't-sum', text: hunk.title }) : null,
715
+ );
716
+ } else if (c.hoverGroup >= 0) {
717
+ const g = c.focus?.collapsed[c.hoverGroup];
718
+ if (!g) return;
719
+ parts.push(
720
+ h('div', { class: 't-label', text: g.label }),
721
+ h('div', {
722
+ class: 't-hint',
723
+ text: tr.t(c.canExpand(c.hoverGroup) ? 'focus.expand' : 'focus.expandUnavailable'),
724
+ }),
725
+ );
726
+ }
727
+ if (parts.length === 0) {
728
+ attr(this.tooltip, 'data-open', 'false');
729
+ return;
730
+ }
731
+ replace(this.tooltip, ...parts);
732
+ attr(this.tooltip, 'data-open', 'true');
733
+ this.#placeTooltip();
734
+ }
735
+
736
+ #placeTooltip(): void {
737
+ const p = this.#pointer;
738
+ if (!p) {
739
+ attr(this.tooltip, 'data-open', 'false');
740
+ return;
741
+ }
742
+ const w = this.tooltip.offsetWidth;
743
+ const hgt = this.tooltip.offsetHeight;
744
+ const W = this.el.clientWidth;
745
+ const H = this.el.clientHeight;
746
+ let x = p.x + 16;
747
+ let y = p.y + 18;
748
+ if (x + w > W - 8) x = Math.max(8, p.x - w - 12);
749
+ if (y + hgt > H - 8) y = Math.max(8, p.y - hgt - 12);
750
+ this.tooltip.style.transform = `translate(${Math.round(x)}px, ${Math.round(y)}px)`;
751
+ }
752
+
753
+ // ─── States ──────────────────────────────────────────────────────────────
754
+
755
+ #renderState(tr: Translator): void {
756
+ const c = this.#ctx.controller;
757
+ const stats = c.diff?.stats;
758
+ let key: string = c.status;
759
+ if (c.status === 'loading') key += `|${c.step}|${c.steps.join(',')}`;
760
+ if (c.status === 'ready') key += stats?.identical ? '|identical' : '';
761
+ key += `|${tr.locale}`;
762
+ if (c.status === 'error') key += `|${c.error?.message}`;
763
+ if (key === this.#stateKey) return;
764
+ this.#stateKey = key;
765
+ this.#state.classList.remove('blocking');
766
+ if (c.status === 'ready' && !stats?.identical) {
767
+ this.#state.hidden = true;
768
+ replace(this.#state);
769
+ return;
770
+ }
771
+ this.#state.hidden = false;
772
+ if (c.status === 'loading') {
773
+ this.#state.classList.add('blocking');
774
+ const current = c.step ? c.steps.indexOf(c.step) : -1;
775
+ replace(
776
+ this.#state,
777
+ h('div', { class: 'skeleton-grid', 'aria-hidden': 'true' }),
778
+ h(
779
+ 'div',
780
+ { class: 'state-card card', role: 'status', 'aria-live': 'polite' },
781
+ h('h2', { text: tr.t('state.loading') }),
782
+ h(
783
+ 'ol',
784
+ { class: 'steps' },
785
+ c.steps.map((s, i) =>
786
+ h(
787
+ 'li',
788
+ { 'data-state': i < current ? 'done' : i === current ? 'active' : 'pending' },
789
+ h(
790
+ 'span',
791
+ { class: 'mark', 'aria-hidden': 'true' },
792
+ i < current ? icon('check') : null,
793
+ ),
794
+ h('span', { text: `${tr.t(`step.${s}`)}${i === current ? '…' : ''}` }),
795
+ ),
796
+ ),
797
+ ),
798
+ ),
799
+ );
800
+ } else if (c.status === 'error') {
801
+ this.#state.classList.add('blocking');
802
+ const err = c.error;
803
+ const code = err instanceof KgdiffError ? err.code : err?.name;
804
+ const details = [code ? `[${code}]` : '', err?.message ?? '', err?.stack ?? '']
805
+ .filter(Boolean)
806
+ .join('\n');
807
+ replace(
808
+ this.#state,
809
+ h(
810
+ 'div',
811
+ { class: 'state-card card error', role: 'alert' },
812
+ h('div', { class: 'state-icon' }, icon('alert')),
813
+ h('h2', { text: tr.t('state.error.title') }),
814
+ h('p', { text: err?.message ?? '' }),
815
+ h(
816
+ 'details',
817
+ null,
818
+ h('summary', { text: tr.t('state.error.details') }),
819
+ h('pre', { text: details }),
820
+ ),
821
+ ),
822
+ );
823
+ } else if (c.status === 'ready' && stats?.identical) {
824
+ replace(
825
+ this.#state,
826
+ h(
827
+ 'div',
828
+ { class: 'state-card card ok', role: 'status' },
829
+ h('div', { class: 'state-icon' }, icon('check-circle')),
830
+ h('h2', { text: tr.t('state.identical.title') }),
831
+ h('p', {
832
+ text: tr.t('state.identical.body', {
833
+ nodes: tr.plural('count.nodes', stats.after.nodes),
834
+ edges: tr.plural('count.edges', stats.after.edges),
835
+ }),
836
+ }),
837
+ ),
838
+ );
839
+ } else {
840
+ this.#state.classList.add('blocking');
841
+ replace(
842
+ this.#state,
843
+ h(
844
+ 'div',
845
+ { class: 'state-card' },
846
+ h('div', { class: 'state-icon' }, icon('overlay')),
847
+ h('h2', { text: tr.t('state.empty.title') }),
848
+ h('p', { text: tr.t('state.empty.body') }),
849
+ ),
850
+ );
851
+ }
852
+ }
853
+ }