/** * The review page's outline: the section list in the sidebar, the one rule that says which section * the reader is in, and the one that says which section a source line belongs to. * * Written as ordinary TypeScript and exported as source — `WEB_OUTLINE_JS` — because the * review client is a template literal no compiler sees. The functions are given what * they read rather than closing over the bundle's variables, which is what makes them * testable here: `renderOutline` returns the per-line counters it built instead of * assigning the client's `outlineItems`, `sectionFor` takes the outline it searches, and * `watchSections` takes the pane it listens to. * * `el` is the client runtime's builder, declared here because this source is spliced into * the same bundle it lives in: the declaration emits nothing, so the body calls the one * builder. The nodes are reached through a structural type because this project compiles * without a DOM lib; the annotations are erased before the source is spliced. */ import { pageSource } from "../../lib/format.ts"; import type { ClientNode } from "../../lib/web/client-runtime.ts"; /** One entry of the page's `outline` island: a heading, its line, and whether it moved. */ export interface OutlineEntry { id: string; level: number; line: number; text: string; changed?: boolean; /** Content blocks this section no longer holds, when the baseline can say so. */ removed?: number; } /** Where a heading sits in the viewport, as the scroll pass reads it. */ export interface SectionPosition { id: string; top: number; } /** What the pane's scroll pass leaves the client to hold: one handle, and dropping it. */ export interface SectionWatch { disconnect(): void; } /** The one member the pass reads off a heading. */ interface Boxed { getBoundingClientRect(): { top: number }; } /** The pane, which is both a box to measure and the element that scrolls. */ interface ScrollBox extends Boxed { clientHeight: number; addEventListener(type: string, listener: () => void): void; removeEventListener(type: string, listener: () => void): void; } /** What `el` has to answer for an outline. Spliced beside it; declared for the checker. */ type Build = (tag: string, className?: string, text?: string | null) => ClientNode; declare const el: Build; /** * The section a source line belongs to: the last heading at or before it. * * `null` when the line sits above the first heading, which is how a preamble renders * without a section of its own. */ export function sectionFor(entries: readonly OutlineEntry[], line: number): OutlineEntry | null { let found: OutlineEntry | null = null; for (const item of entries) if (item.line <= line) found = item; return found; } /** * Draw the section list and return the counter each line owns. * * The caller keeps the returned map because the counters are updated later, when the * comments are counted per section; returning it is what lets this function be a pure * build with no handle on the client's state. * * A repaint is not a reset: the mark saying which section the reader is in is read off the * list being replaced and put back on the link for the same heading, because the page * rebuilds the rail whenever the plan does. A heading that moved has a new id, so the mark * goes with the section it was on rather than landing on whatever now sits at its line. * * `jump` is the caller's arrival hook, called with the entry's id once the mark has moved. * A link scrolls the pane, and a section that scrolled into view has announced nothing, so * the page puts the keyboard on it; the hook is optional because the outline is also drawn * where there is no plan frame to focus. */ export function renderOutline( list: ClientNode | null, entries: readonly OutlineEntry[], jump?: (id: string) => void, ): Record { const counters: Record = {}; if (!list) return counters; const current = list.querySelector("[aria-current]")?.getAttribute("data-id") ?? ""; list.replaceChildren(); for (const item of entries) { const li = el("li", "lvl-" + item.level); const link = el("a"); link.href = "#" + item.id; link.setAttribute("data-id", item.id); if (item.id === current) link.setAttribute("aria-current", "true"); const label = el("span", "review-nav__link"); label.textContent = item.text; link.appendChild(label); if (item.changed) { const dot = el("span", "review-changed"); dot.setAttribute("role", "img"); dot.setAttribute("aria-label", "Changed since your last review"); dot.title = "Changed since your last review"; link.appendChild(dot); } if (item.removed !== undefined && item.removed > 0) { // A block that left cannot be drawn where it was, so the count is the fact the // page can still give: how much shorter this section has become. It is drawn rather // than left in a tooltip, which a touch reader and a printed page never see. const sentence = item.removed === 1 ? "1 block removed since your last review" : item.removed + " blocks removed since your last review"; link.title = sentence; const removed = el("span", "review-nav__removed"); removed.setAttribute("role", "img"); removed.setAttribute("aria-label", sentence); removed.title = sentence; removed.textContent = "\u2212" + item.removed; link.appendChild(removed); } const count = el("span", "review-nav__count"); count.hidden = true; link.appendChild(count); // A jump is the reader naming the section they want, so the mark moves to it at once; the // scroll pass still owns it from the next scroll on. link.addEventListener("click", () => { const links = list.querySelectorAll("a"); for (let i = 0; i < links.length; i++) links[i]?.removeAttribute("aria-current"); link.setAttribute("aria-current", "true"); if (jump) jump(item.id); }); li.appendChild(link); list.appendChild(li); counters[item.line] = count; } return counters; } /** * The section the reader is in: the last heading whose top is at or above the reading line. * * An empty id when every heading is below the line, which is how a preamble longer than the top * two-fifths of the pane renders without a section of its own. */ export function sectionAtLine(positions: readonly SectionPosition[], line: number): string { let found = ""; for (const position of positions) if (position.top <= line) found = position.id; return found; } /** * Mark the section the pane's reading line is in, on every scroll it makes. * * The pane is the box that scrolls, so its own scroll event is the whole input: a step, End, a * scrollbar dragged to the bottom, and a bare `scrollTop` write all move it, and a position read * afterwards answers which heading is above the line. An observer rooted in the same pane was the * previous owner and could not answer that: a jump delivers no entry that intersects, so the mark * stayed on whatever section was marked before it. * * One pass per frame: a burst of scroll events asks for one animation frame between them, so a drag * costs a pass a frame rather than a pass an event. The pass runs once when it is installed, because * a repaint rebuilds the rail and restores the scroll position the pass reads. * * The watch is returned rather than stored, so the client owns the one handle it has to drop and * this function owns the rule: the last heading above the line wins, and a page with no headings * needs no watch at all. */ export function watchSections( planEl: ClientNode | null, list: ClientNode | null, previous: SectionWatch | null, ): SectionWatch | null { if (previous) previous.disconnect(); if (!planEl || !list) return null; const headings = planEl.querySelectorAll(".bf-block[id]"); if (headings.length === 0) return null; const pane = planEl as unknown as ScrollBox; const frames = globalThis as unknown as { requestAnimationFrame(callback: () => void): number; cancelAnimationFrame(handle: number): void; }; let pending = 0; /** Mark one section, or none: the one attribute the rail reads, written in one place. */ const mark = (id: string): void => { const links = list.querySelectorAll("a"); for (let i = 0; i < links.length; i++) { const link = links[i]; if (!link) continue; if (id !== "" && link.getAttribute("data-id") === id) link.setAttribute("aria-current", "true"); else link.removeAttribute("aria-current"); } }; const pass = (): void => { pending = 0; // The top two-fifths of the pane is the line a reader's eye sits on. A jump lands its heading at // `scroll-margin-top: 88px`, above the line at every width the suite draws, so a click's own mark // and the next pass agree rather than taking turns. const line = pane.getBoundingClientRect().top + pane.clientHeight * 0.4; const positions: SectionPosition[] = []; for (let i = 0; i < headings.length; i++) { const heading = headings[i]; if (!heading) continue; positions.push({ id: heading.id, top: (heading as unknown as Boxed).getBoundingClientRect().top }); } mark(sectionAtLine(positions, line)); }; const schedule = (): void => { if (pending === 0) pending = frames.requestAnimationFrame(pass); }; pane.addEventListener("scroll", schedule); pass(); return { disconnect(): void { pane.removeEventListener("scroll", schedule); if (pending !== 0) frames.cancelAnimationFrame(pending); pending = 0; }, }; } /** One review comment, as the facts need it: the line it is anchored to. */ export interface AnchoredComment { anchor: number; /** A settled note counts in the section's thread but not among the open ones. */ resolved?: true; } /** What a plan's step list says: the numbers the server counted. */ export interface StepProgress { done: number; total: number; } /** * Paint the review facts: the plan's step progress, the comment total, and one count per * section in the outline. * * The counters are the ones `renderOutline` returned, so the two functions agree by * construction; a section a comment cannot be placed in is skipped rather than counted * against the first heading, which is what "not attached to a current block" means. */ export function renderFacts( steps: StepProgress, comments: readonly AnchoredComment[], counters: Record, entries: readonly OutlineEntry[], ): void { const doc = ( globalThis as unknown as { document: { getElementById(id: string): ClientNode | null }; } ).document; // A plan with no Steps section has no progress to show; 0/0 is noise. const fact = doc.getElementById("step-fact"); if (fact) fact.hidden = steps.total === 0; const fill = doc.getElementById("step-fill"); if (fill) fill.style.width = (steps.total > 0 ? Math.round((steps.done / steps.total) * 100) : 0) + "%"; const count = doc.getElementById("step-count"); // Named for what it counts: before approval these ticks are the plan's ambition, not // work done, and after it completion belongs to `todo`. if (count) count.textContent = steps.total === 0 ? "" : steps.done + "/" + steps.total + " ticked"; const total = doc.getElementById("comment-total"); if (total) { // Open first, because a settled note is history: "3 open · 2 resolved" says what is // left to answer, and the resolved half stays because the file keeps it. const settled = comments.filter((comment) => comment.resolved === true).length; const open = comments.length - settled; total.textContent = settled === 0 ? String(open) : open + " open · " + settled + " resolved"; } for (const line of Object.keys(counters)) { const node = counters[Number(line)]; if (node) node.hidden = true; } for (const comment of comments) { const section = sectionFor(entries, comment.anchor); if (!section) continue; const node = counters[section.line]; if (!node) continue; const value = node.hidden ? 0 : Number(node.textContent); node.textContent = String(value + 1); node.hidden = false; } } /** The section list, the facts beside it, and the pass that marks where the reader is. */ export const WEB_OUTLINE_JS = [sectionFor, sectionAtLine, renderOutline, watchSections, renderFacts] .map(pageSource) .join("\n\n");