// THE BRANCH MAP (umtool/docs/report-video.md, "The branch map"; operator, 4 Nov: "we need a specific visual for // changing thread branches that makes the context obvious"): when a scene moves from one branch of a conversation // to another -- a sibling reply, a new reply to an earlier post, a jump back up the tree -- a small map in the card's // corner shows where we are going: a node for each post on screen, a line for each reply, the post the item before // ended on lit, the light travelling up to the post the two branches share and down the new branch to the post being // read; a label under it says it ("another reply to @X's post"; "back to @X's post"). A manual thread // (`thread.group`) draws its posts' real relations (a post that answers none stands alone). // // render.posts.threadMap = { on?: true, style?: "both" } the cut's: "map" | "label" | "both" // timeline[i].thread.map = false | true | "map" | "label" | "both" // // When: the item's scene changes branch (a post the run had on screen collapses away, post-conversation.mjs) or, in a // manual thread, the post he answers is not the one just above. It shows from the join's end, travels with the // branch's change (its collapse), holds, and fades (MAP_TIMES). // // PURE but the page function (applyMapState, self-contained): threadLayout carries `map` (its nodes, its edges, the // path), threadSchedule its times, the state at a second its progress; the card page draws it (mapHtml). const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const str = (v) => (typeof v === "string" ? v : ""); const r3 = (v) => Math.round(v * 1000) / 1000; export const MAP_DEFAULTS = Object.freeze({ on: true, style: "both" }); export const MAP_STYLES = Object.freeze(["map", "label", "both"]); /** The map's fade in (s), the light's travel (s), the hold after it (s), the fade out (s). */ export const MAP_TIMES = Object.freeze({ in: 0.3, travel: 1.1, hold: 2.2, out: 0.5 }); /** The cut's and an item's map knobs: `{ on, style }`. */ export function mapKnobs(entry, render = undefined) { const r = isObj(render?.posts?.threadMap) ? render.posts.threadMap : {}; const k = { ...MAP_DEFAULTS, ...(typeof r.on === "boolean" ? { on: r.on } : {}), ...(MAP_STYLES.includes(r.style) ? { style: r.style } : {}) }; const v = entry?.thread?.map; if (v === false) return { ...k, on: false }; if (v === true) return { ...k, on: true }; if (MAP_STYLES.includes(v)) return { on: true, style: v }; return k; } /** Every reason `render.posts.threadMap` cannot be built. */ export function validatePostMapRender(render) { const v = render?.posts?.threadMap; if (v === undefined) return []; if (!isObj(v)) return ["render.posts.threadMap must be { on?, style? } (the branch map)"]; const errors = Object.keys(v).filter((k) => !(k in MAP_DEFAULTS)).map((k) => `render.posts.threadMap.${k} is not a branch-map knob (on, style)`); if (v.on !== undefined && typeof v.on !== "boolean") errors.push("render.posts.threadMap.on must be true or false"); if (v.style !== undefined && !MAP_STYLES.includes(v.style)) errors.push(`render.posts.threadMap.style must be one of ${MAP_STYLES.join(", ")}`); return errors; } /** * THE MAP of a card, pure: `{ style, nodes: [{ id, scope, handle, him, x, y }], edges: [[from, to]], path: [ids], * label, from, to }` -- or null when the scene does not change branch. `tiles`: threadLayout's (each with its `id`, * its parent's `parent`, its author's `handle`); `subject`: his handle; `prevId`: the post the item before ended on; * `nowId`: the post being read first (the new branch's). Laid out as a tree: depth across, the tiles' order down. */ export function branchMapOf(tiles, { prevId, nowId, subject = "", style = "both", group = false } = {}) { const list = (tiles ?? []).filter((t) => str(t.id)); if (!prevId || !nowId || prevId === nowId || !list.some((t) => t.id === prevId) || !list.some((t) => t.id === nowId)) return null; const byId = new Map(list.map((t) => [t.id, t])); const parentOf = (id) => { const p = str(byId.get(id)?.parent); return p && byId.has(p) && p !== id ? p : null; }; const up = (id) => { const out = [id]; for (let p = parentOf(id), n = 0; p && n < 20; p = parentOf(p), n += 1) out.push(p); return out; }; const a = up(prevId); const b = up(nowId); const shared = a.find((id) => b.includes(id)) ?? null; // The new post answers the one before it directly (no branch change): no map. if (shared === prevId) return null; // No post shared: through the post the new one answers when it is on screen (a manual thread's reply up the stack). const via = !shared && parentOf(nowId) && parentOf(nowId) !== prevId ? parentOf(nowId) : null; const path = shared ? [...a.slice(0, a.indexOf(shared) + 1), ...b.slice(0, b.indexOf(shared)).reverse()] : via ? [prevId, via, nowId] : [prevId, nowId]; const depth = new Map(); const depthOf = (id) => { if (depth.has(id)) return depth.get(id); depth.set(id, 0); const p = parentOf(id); const d = p ? depthOf(p) + 1 : 0; depth.set(id, d); return d; }; const nodes = list.map((t, row) => ({ id: t.id, scope: t.scope, handle: str(t.handle), him: !!subject && str(t.handle).toLowerCase() === subject.toLowerCase(), x: depthOf(t.id), y: row })); const edges = list.map((t) => [parentOf(t.id), t.id]).filter((e) => e[0]); const sh = shared ? byId.get(shared) : via ? byId.get(via) : null; const isHim = (t) => !!subject && str(t?.handle).toLowerCase() === subject.toLowerCase(); const who = sh ? (isHim(sh) ? "his" : sh.handle ? `@${sh.handle}'s` : "the") : null; // Back up the stack to a post on screen (the post he answers, read again): "back to"; else a sibling's. const nowT = byId.get(nowId); const back = !sh && nowT && nowT.scope !== "post" ? (isHim(nowT) ? "his" : nowT.handle ? `@${nowT.handle}'s` : "the") : null; const label = back ? `back to ${back} post` : !sh ? "a new thread" : shared === nowId ? `back to ${who} post` : `another reply to ${who} post`; return { style, nodes, edges, path, label, from: prevId, to: nowId, shared }; } /** The map's times on the item's clock: in at `t0`, the light travelling from `travel`, out by `t3`. */ export function mapTimes(at) { const t0 = r3(Math.max(0, at)); const travel = r3(t0 + MAP_TIMES.in); const t2 = r3(travel + MAP_TIMES.travel + MAP_TIMES.hold); return { t0, travel, t2, t3: r3(t2 + MAP_TIMES.out) }; } /** The map's state at item second `s`: `{ op, p }` (p: the light's place along its path, 0..1), or null. */ export function mapStateAt(map, s) { if (!map?.times) return null; const { t0, travel, t2, t3 } = map.times; if (s < t0 || s > t3) return null; const op = s < travel ? (s - t0) / Math.max(1e-6, travel - t0) : s > t2 ? 1 - (s - t2) / Math.max(1e-6, t3 - t2) : 1; const x = Math.min(1, Math.max(0, (s - travel) / MAP_TIMES.travel)); const p = x < 0.5 ? 4 * x ** 3 : 1 - (-2 * x + 2) ** 3 / 2; return { op: r3(Math.max(0, Math.min(1, op))), p: r3(p) }; } const esc = (s) => String(s ?? "").replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); /** * The map's markup (the card page's): an SVG of the nodes and lines in the card's top-right corner, the light (a * ring that travels), the label under it -- hidden until applyMapState shows it. `k`: the page's scale (height รท * 1080); `blurred`: handles the card blurs (a label never names one); `accent`: his colour. */ export function mapHtml(map, { k = 1, accent = "#c8752a", fg = "#f6f1e6", muted = "#a2957f", blurred = [] } = {}) { if (!map) return ""; const cell = Math.round(26 * k); const r = Math.round(6 * k); const cols = Math.max(...map.nodes.map((n) => n.x)) + 1; const rows = map.nodes.length; const w = cols * cell + 2 * r + 4; const h = rows * cell + 2 * r; const at = (n) => [r + 2 + n.x * cell + r, r + n.y * cell + r]; const pos = new Map(map.nodes.map((n) => [n.id, at(n)])); const lines = map.edges.map(([a, b]) => { const [x1, y1] = pos.get(a); const [x2, y2] = pos.get(b); return ``; }); const onPath = new Set(map.path); const dots = map.nodes.map((n) => { const [x, y] = pos.get(n.id); return ``; }); const pts = map.path.map((id) => pos.get(id)); const blur = new Set(blurred.map((h) => String(h).replace(/^@/, "").toLowerCase())); const label = /@([A-Za-z0-9_]+)'s/.exec(map.label)?.[1]; const text = label && blur.has(label.toLowerCase()) ? map.label.replace(`@${label}'s`, "a stranger's") : map.label; const showMap = map.style !== "label"; const showLabel = map.style !== "map"; return `
${showMap ? `${lines.join("")}${dots.join("")}` : ""}${showLabel ? `
${esc(text)}
` : ""}
`; } /** IN THE PAGE (self-contained): the map shown at strength `st.op`, its light `st.p` along its path (null: hidden). */ export function applyMapState(st, doc = document) { const el = doc.querySelector("[data-thread-map]"); if (!el) return; if (!st) { el.style.display = "none"; return; } el.style.display = "block"; el.style.opacity = String(st.op); const light = el.querySelector("[data-map-light]"); const pts = JSON.parse(el.getAttribute("data-map-path") || "[]"); if (!light || pts.length < 1) return; // Along the path's legs (each an elbow: down, then across), by length. const legs = []; for (let i = 1; i < pts.length; i += 1) { const [a, b] = [pts[i - 1], pts[i]]; const mid = [a[0], b[1]]; legs.push([a, mid], [mid, b]); } const len = legs.map(([a, b]) => Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1])); const total = len.reduce((x, y) => x + y, 0) || 1; let d = st.p * total; let at = pts[0]; for (let i = 0; i < legs.length; i += 1) { if (d <= len[i] || i === legs.length - 1) { const f = len[i] ? Math.min(1, d / len[i]) : 1; at = [legs[i][0][0] + (legs[i][1][0] - legs[i][0][0]) * f, legs[i][0][1] + (legs[i][1][1] - legs[i][0][1]) * f]; break; } d -= len[i]; } light.setAttribute("cx", String(at[0])); light.setAttribute("cy", String(at[1])); } /** The map's box (px), as mapHtml draws it: its tree, its label under it, its padding. */ export function mapSize(map, k = 1) { if (!map) return { width: 0, height: 0 }; const cell = Math.round(26 * k); const r = Math.round(6 * k); const cols = Math.max(...map.nodes.map((n) => n.x)) + 1; const svgW = map.style === "label" ? 0 : cols * cell + 2 * r + 4; const svgH = map.style === "label" ? 0 : map.nodes.length * cell + 2 * r; const labelW = map.style === "map" ? 0 : Math.round(String(map.label).length * 19 * k * 0.52); const labelH = map.style === "map" ? 0 : Math.round(19 * k * 1.3) + (map.style === "both" ? Math.round(6 * k) : 0); const pad = Math.round(10 * k); return { width: Math.max(svgW, labelW) + 2 * pad, height: svgH + labelH + 2 * pad }; } /** * WHERE THE MAP SITS in the item's box -- never over words being read: over the attachments' slot (its top-left * corner) when there is one, else beside a card alone (right of it, when the box has room), else the card's top-right * corner. `L`: post-item.mjs postLayout's; `size`: mapSize's. */ export function mapPlace(L, box, size, k = 1) { const m = Math.round(16 * k); if (L?.slot && L.mode === "side") return { x: L.slot.x + m, y: L.slot.y + m }; if (L?.card && L.card.x + L.card.width + m + size.width <= box.width - m) return { x: L.card.x + L.card.width + 2 * m, y: L.card.y + m }; return { x: Math.max(0, (L?.card?.x ?? 0) + (L?.card?.width ?? box.width) - size.width - m), y: (L?.card?.y ?? 0) + m }; } /** The map alone on a transparent page (the build shoots its states and lays them over the attachments). */ export function mapPageHtml(map, { width, height, k = 1, accent, fg, muted, blurred = [], fonts = {} }) { const css = (s) => String(s ?? "").replace(/[\\'"\n<>]/g, ""); return `${mapHtml(map, { k, accent, fg, muted, blurred })}`; }