~bigbes/sr-ht-compare

ref: 0b9a8233d3735b5c609c92af5f0ac16d95c5e261 sr-ht-compare/frontend/src/app.ts -rw-r--r-- 11.7 KiB
0b9a8233 — bigbes deps: sr-ht-ecore whose middleware reports panics through slog 10 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * compare.sr.ht frontend bundle.
 *
 * Consumes the SSR DOM contract:
 *   <script id="compare-data" type="application/json">{...}</script>
 *   <div id="tree-root">   (file tree sidebar)
 *   <div id="diff-root">   (per-file diff sections)
 *   <button data-diff-layout="split">  / <button data-diff-layout="stacked">
 *   <button data-diff-wrap>  (long-line wrapping toggle)
 *
 * Rendering uses @pierre/diffs (FileDiff vanilla class) for each file and
 * @pierre/trees (FileTree vanilla class) for the sidebar. Both render on the
 * main thread (no worker pool passed) with the pure-JS Shiki engine, so the
 * bundle is fully self-contained (no wasm / no runtime asset fetches).
 */

import { FileDiff, parsePatchFiles } from "@pierre/diffs";
import type { FileDiffMetadata, FileDiffOptions, ThemesType } from "@pierre/diffs";
import { FileTree } from "@pierre/trees";
import type { GitStatus, GitStatusEntry } from "@pierre/trees";

// ---- DOM contract types (produced by the Go web layer) --------------------

type Layout = "split" | "stacked";

interface FileEntry {
  path: string;
  oldPath: string;
  status: string; // "A" | "M" | "D" | "R" | ...
  additions: number;
  deletions: number;
  binary: boolean;
}

interface CompareData {
  mode: "compare" | "commit";
  patch: string;
  truncated: boolean;
  files: FileEntry[];
  spec: { base: string; head: string; threeDot: boolean };
}

// ---- Constants ------------------------------------------------------------

const LAYOUT_KEY = "compare.layout";
const WRAP_KEY = "compare.wrap";
const DEFAULT_LAYOUT: Layout = "split";

// Auto light/dark: FileDiff selects the entry matching `themeType`. With
// themeType "system" it follows prefers-color-scheme at runtime.
const THEME: ThemesType = { light: "github-light", dark: "github-dark" };

// ---- Small helpers --------------------------------------------------------

function el<K extends keyof HTMLElementTagNameMap>(
  tag: K,
  className?: string,
  text?: string,
): HTMLElementTagNameMap[K] {
  const node = document.createElement(tag);
  if (className) node.className = className;
  if (text != null) node.textContent = text;
  return node;
}

function readLayout(): Layout {
  try {
    const v = localStorage.getItem(LAYOUT_KEY);
    if (v === "split" || v === "stacked") return v;
  } catch {
    /* localStorage may be unavailable (private mode); fall through */
  }
  return DEFAULT_LAYOUT;
}

function writeLayout(layout: Layout): void {
  try {
    localStorage.setItem(LAYOUT_KEY, layout);
  } catch {
    /* ignore persistence failures */
  }
}

function readWrap(): boolean {
  try {
    return localStorage.getItem(WRAP_KEY) === "true";
  } catch {
    /* localStorage may be unavailable (private mode); fall through */
  }
  return false;
}

function writeWrap(wrap: boolean): void {
  try {
    localStorage.setItem(WRAP_KEY, String(wrap));
  } catch {
    /* ignore persistence failures */
  }
}

// "stacked" is the user-facing name for the unified diff style.
function diffStyleFor(layout: Layout): "unified" | "split" {
  return layout === "split" ? "split" : "unified";
}

function diffOptionsFor(
  layout: Layout,
  wrap: boolean,
): FileDiffOptions<undefined> {
  return {
    theme: THEME,
    themeType: "system",
    diffStyle: diffStyleFor(layout),
    overflow: wrap ? "wrap" : "scroll",
    // We render our own header row above, so disable the library header.
    disableFileHeader: true,
  };
}

function gitStatusFor(status: string): GitStatus {
  switch (status.toUpperCase()[0]) {
    case "A":
      return "added";
    case "D":
      return "deleted";
    case "R":
      return "renamed";
    case "M":
    default:
      return "modified";
  }
}

function sectionId(index: number): string {
  return `file-${index}`;
}

// ---- Main -----------------------------------------------------------------

function readData(): CompareData | null {
  const script = document.getElementById("compare-data");
  if (!script || !script.textContent) return null;
  try {
    return JSON.parse(script.textContent) as CompareData;
  } catch (err) {
    console.error("compare: failed to parse #compare-data", err);
    return null;
  }
}

function buildFileHeader(index: number, file: FileEntry): HTMLElement {
  const header = el("div", "diff-file-header");

  const path = el("span", "diff-file-path");
  if (file.oldPath && file.oldPath !== file.path) {
    path.append(
      el("span", "diff-file-oldpath", file.oldPath),
      el("span", "diff-file-arrow", " → "),
      el("span", "diff-file-newpath", file.path),
    );
  } else {
    path.textContent = file.path;
  }
  header.append(path);

  const stats = el("span", "diff-file-stats");
  if (file.additions > 0) {
    stats.append(el("span", "diff-file-add", `+${file.additions}`));
  }
  if (file.deletions > 0) {
    stats.append(el("span", "diff-file-del", `-${file.deletions}`));
  }
  header.append(stats);

  if (file.binary) {
    header.append(el("span", "diff-file-badge diff-file-binary", "BIN"));
  }

  header.dataset.fileIndex = String(index);
  return header;
}

function renderDiffs(
  root: HTMLElement,
  data: CompareData,
  metaByPath: Map<string, FileDiffMetadata>,
  layout: Layout,
  wrap: boolean,
): FileDiff[] {
  root.textContent = "";
  const instances: FileDiff[] = [];

  if (data.files.length === 0) {
    root.append(el("div", "diff-empty", "No changes to display."));
    return instances;
  }

  data.files.forEach((file, index) => {
    const section = el("section", "diff-file");
    section.id = sectionId(index);
    section.dataset.path = file.path;
    section.append(buildFileHeader(index, file));

    const body = el("div", "diff-file-body");
    section.append(body);
    root.append(section);

    if (file.binary) {
      body.append(el("div", "diff-file-note", "Binary file not shown."));
      return;
    }

    const meta = metaByPath.get(file.path);
    if (!meta) {
      body.append(el("div", "diff-file-note", "No textual diff available."));
      return;
    }

    const instance = new FileDiff(diffOptionsFor(layout, wrap));
    instance.render({ fileDiff: meta, containerWrapper: body });
    instances.push(instance);
  });

  return instances;
}

function buildTree(
  root: HTMLElement,
  data: CompareData,
): FileTree | null {
  root.textContent = "";
  if (data.files.length === 0) return null;

  const paths = data.files.map((f) => f.path);
  const gitStatus: GitStatusEntry[] = data.files.map((f) => ({
    path: f.path,
    status: gitStatusFor(f.status),
  }));

  const indexByPath = new Map(data.files.map((f, i) => [f.path, i]));

  const tree = new FileTree({
    paths,
    gitStatus,
    initialExpansion: "open",
    // "compact" preset: 24px rows + 0.8 density factor (tighter level-gap,
    // row-gap and padding) so the sidebar reads as a dense file index rather
    // than an airy list — matching the reference diff UIs.
    density: "compact",
    onSelectionChange: (selected) => {
      const path = selected[0];
      if (path == null) return;
      const index = indexByPath.get(String(path));
      if (index == null) return;
      const section = document.getElementById(sectionId(index));
      section?.scrollIntoView({ behavior: "smooth", block: "start" });
    },
  });
  tree.render({ containerWrapper: root });
  sizeTreeRoot(root, tree);
  return tree;
}

// The @pierre/trees FileTree virtualizes its rows into a scroll viewport, so the
// mount needs a *definite* height or it collapses to zero and renders nothing
// (its README sets `mount.style.height` for exactly this reason — the inner
// container flexes to fill the mount, so a mount of height 0 shows nothing).
//
// Sizing the mount to the full viewport (the naive fix) leaves a tall band of
// the tree's own background below the last row whenever the tree is shorter than
// the screen. Instead we size the mount to the tree's *content* height, capped at
// the viewport: a short tree hugs its rows, a long tree scrolls within the cap.
//
// The content height can't be read directly (the inner container flexes to fill
// whatever height the mount has, so its scrollHeight just echoes the mount). We
// momentarily pin the mount to 1px so the virtualizer's scroll spacer reports the
// true total content height, read it, then apply the capped value. This is
// synchronous — no paint happens between the pin and the restore, so it doesn't
// flicker. Re-runs on window resize and on every tree change (expand/collapse).
function sizeTreeRoot(root: HTMLElement, tree: FileTree): void {
  const cap = () => Math.max(160, window.innerHeight - 24);

  const contentHeight = (): number | null => {
    const container = root.firstElementChild as HTMLElement | null;
    const scroll = container?.shadowRoot?.querySelector<HTMLElement>(
      '[data-file-tree-virtualized-scroll]',
    );
    if (!scroll) return null;
    const prev = root.style.height;
    root.style.height = "1px";
    const content = scroll.scrollHeight;
    root.style.height = prev;
    return content;
  };

  const apply = () => {
    const content = contentHeight();
    root.style.height = `${Math.min(cap(), content ?? cap())}px`;
  };

  apply();
  window.addEventListener("resize", apply);
  // Fires on expand/collapse and any other tree mutation, so the mount keeps
  // hugging its content as rows appear and disappear.
  tree.subscribe(apply);
}

function wireDiffControls(
  layoutButtons: HTMLButtonElement[],
  wrapButton: HTMLButtonElement | null,
  getInstances: () => FileDiff[],
  initialLayout: Layout,
  initialWrap: boolean,
): void {
  let layout = initialLayout;
  let wrap = initialWrap;

  const reflectControls = () => {
    for (const btn of layoutButtons) {
      btn.classList.toggle("active", btn.dataset.diffLayout === layout);
    }
    wrapButton?.classList.toggle("active", wrap);
    wrapButton?.setAttribute("aria-pressed", String(wrap));
  };

  const rerender = () => {
    for (const instance of getInstances()) {
      instance.setOptions(diffOptionsFor(layout, wrap));
      instance.rerender();
    }
  };

  for (const btn of layoutButtons) {
    btn.addEventListener("click", (ev) => {
      ev.preventDefault();
      const next = btn.dataset.diffLayout;
      if (next !== "split" && next !== "stacked") return;
      layout = next;
      writeLayout(layout);
      reflectControls();
      rerender();
    });
  }

  wrapButton?.addEventListener("click", (ev) => {
    ev.preventDefault();
    wrap = !wrap;
    writeWrap(wrap);
    reflectControls();
    rerender();
  });

  // Reflect the initial (possibly persisted) options on the controls.
  reflectControls();
}

function main(): void {
  const data = readData();
  if (!data) return;

  const diffRoot = document.getElementById("diff-root");
  const treeRoot = document.getElementById("tree-root");
  if (!diffRoot) return;

  const layout = readLayout();
  const wrap = readWrap();

  // parsePatchFiles returns one ParsedPatch per commit; each has a `files`
  // array of FileDiffMetadata. Flatten across patches and index by path.
  const metaByPath = new Map<string, FileDiffMetadata>();
  if (data.patch) {
    try {
      for (const patch of parsePatchFiles(data.patch)) {
        for (const meta of patch.files) {
          metaByPath.set(meta.name, meta);
        }
      }
    } catch (err) {
      console.error("compare: failed to parse patch", err);
    }
  }

  const instances = renderDiffs(diffRoot, data, metaByPath, layout, wrap);
  if (treeRoot) buildTree(treeRoot, data);

  const buttons = Array.from(
    document.querySelectorAll<HTMLButtonElement>("button[data-diff-layout]"),
  );
  const wrapButton = document.querySelector<HTMLButtonElement>(
    "button[data-diff-wrap]",
  );
  if (buttons.length > 0 || wrapButton) {
    wireDiffControls(buttons, wrapButton, () => instances, layout, wrap);
  }
}

if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", main);
} else {
  main();
}