// 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 `