Each file of a commit folds under its name
Misha Kaletsky, 2026-11-24 20:48 UTC · 0ad00e151f51dab433a711480ae224b5c538622d · parent 9ddbacf
2 files changed
M app.ts
@@ -359,7 +359,8 @@ return page(view, 200, "commits", `Commits · ${nameOf(view.repo)}`, main, null); } -/** One commit: its message, and each file it changed against its first parent, as a diff. */ +/** One commit: its message, and each file it changed against its first parent, as a diff that + * folds away under the file's name. */ async function commitPage(view: View, handle: Repo, itx: Itx, wanted: string) { const { repo } = view; if (!/^[0-9a-f]{7,40}$/.test(wanted)) @@ -415,7 +416,8 @@ const shown = change.sides ? `<div class="diff" data-diff="${index}"></div>` : note(change.note || ""); - return `<section id="diff-${index}"><h2><span class="status ${change.status}">${letter[change.status]}</span> ${esc(change.path)}</h2>${shown}</section>`; + // each file folds: the reader closes the ones they have read + return `<details id="diff-${index}" open><summary><span class="status ${change.status}">${letter[change.status]}</span> ${esc(change.path)}</summary>${shown}</details>`; }); const count = `${commit.changes.length} file${commit.changes.length === 1 ? "" : "s"} changed`; const main = [ @@ -613,13 +615,17 @@ .commit .message { margin-bottom: 0.75rem; white-space: pre-wrap; } .commit > p { margin-bottom: 0.5rem; } .changes { margin-bottom: 1.5rem; } - .changes li, .commit h2 { font-family: var(--font-mono, ui-monospace, monospace); + .changes li, .commit summary { font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.8125rem; } - .commit section { margin-top: 1.25rem; border: 1px solid var(--border); - border-radius: 0.5rem; overflow: hidden; } - .commit h2 { padding: 0.4rem 0.75rem; border-bottom: 1px solid var(--border); } - .commit section .note { padding: 0.5rem 0.75rem; } - .diff { min-height: 4rem; } + .commit details { margin-top: 1.25rem; border: 1px solid var(--border); + border-radius: 0.5rem; } + /* the file's name stays at the top of the window while its diff is read */ + .commit summary { position: sticky; top: 0; z-index: 1; padding: 0.4rem 0.75rem; + cursor: pointer; background: var(--background); border-radius: 0.5rem; } + .commit details[open] > summary { border-bottom: 1px solid var(--border); + border-radius: 0.5rem 0.5rem 0 0; } + .commit details .note { padding: 0.5rem 0.75rem; } + .diff { min-height: 4rem; overflow: hidden; border-radius: 0 0 0.5rem 0.5rem; } .status { font-weight: 600; } .status.added { color: light-dark(oklch(0.5 0.15 150), oklch(0.75 0.15 150)); } .status.deleted { color: var(--destructive); } @@ -693,6 +699,12 @@ async function drawDiffs() { const places = document.querySelectorAll("[data-diff]"); if (places.length === 0) return; + // a file's name in the list of changes opens that file, if the reader had folded it + document.addEventListener("click", (event) => { + const link = event.target.closest(".changes a"); + const file = link && document.querySelector(link.getAttribute("href")); + if (file) file.open = true; + }); const { MultiFileDiff } = await import("@pierre/diffs/react"); const near = new IntersectionObserver( (entries) => {
M tests/pages.test.mjs
@@ -121,7 +121,7 @@ assert.ok(!html.includes("ada@example.com")); }); -test("a commit's page names each file it changed and carries both texts of each, and the second reader costs the repo nothing", async () => { +test("a commit's page names each file it changed, each under a name that folds it, and carries both texts of each; the second reader costs the repo nothing", async () => { using kernel = repoWith([ { message: "Start", files: { "kept.ts": "same\n", "changed.ts": "old\n", "gone.ts": "bye\n" } }, { @@ -146,6 +146,12 @@ ["#diff-1", "changed.ts"], ["#diff-2", "gone.ts"], ]); + // each file is open, under its name: a click on the name folds it, with no script + assert.ok( + html.includes( + `<details id="diff-1" open><summary><span class="status modified">M</span> changed.ts</summary><div class="diff" data-diff="1"></div></details>`, + ), + ); assert.deepEqual(dataOf(html), { diffs: [ { path: "came.ts", before: "", after: "hello\n" },