// SECTIONS OF A DECK CUT, edited as sections (UI notes 2026-10-30, item 1). // // Under the deck a cut's sections ARE its markers (`render.chrome.deck.markers`, // report-to-video deck.mjs `railSections`): section 0 runs from the start to // the first marker's boundary, and a marker `{ after: , label?, color? }` // opens the next section right after that clip -- its label and colour are the // NEW section's. So "move this clip to section 3" is two edits that must land // together: the clip moves in the timeline, and every marker whose boundary the // move crosses follows, or the clip lands in the wrong section (or a section // ends up empty, or two markers cross). Doing that by `timeline move --to` // index arithmetic went wrong twice on the Ember cut. // // Everything here is PURE: each function mutates the manifest it is handed and // returns what it did in words. manifest.mjs runs them inside a structural // write (the lock, the token, the build's checks, the undo point), and syncs // the take patches' markers after (patchMarkers.mjs syncSectionMarkers). // // The rules, each refused in words rather than repaired silently: // - a section never ends up with no clip (two markers on one boundary); // - a boundary never moves past its neighbours (markers never cross); // - a marker never follows the last clip (a section after it is empty); // - markers stay within the deck's own limits (validateChrome says so). // // A SUBSECTION (a marker with `level: 2`) is a section to every rule here: a // boundary like any other, never left empty, never crossed, its clips moved // by the same ops -- so "§4" means the same thing to a drop, the Cut tab and // the CLI whatever the levels. Only the deck draws it inside its parent (the // rail's colour, the section title's "Section › Sub"), and the build refuses // a cut whose first marker is one (validateDeckMarkers): an op that would // leave one first is refused in those words. const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); // A FOOTAGE entry: a clip, or a post item (post-item.mjs) -- what a section holds and a marker follows. const isClip = (e) => !!e && ((e.kind ?? e.type) === "clip" || (e.kind ?? e.type) === "post"); /** The deck's markers as written (an array, maybe empty), or null without a deck. */ function deckMarkers(m) { const chrome = m?.render?.chrome; if (!isObj(chrome)) return null; const d = isObj(chrome.deck) ? chrome.deck : null; return Array.isArray(d?.markers) ? d.markers : []; } function needDeck(m) { const chrome = m?.render?.chrome; if (!isObj(chrome)) throw new Error("sections are the on-screen deck's markers, and this manifest has no deck — turn the deck on first"); if (!isObj(chrome.deck)) chrome.deck = {}; if (!Array.isArray(chrome.deck.markers)) chrome.deck.markers = []; return chrome.deck.markers; } /** * The cut's sections, read from its markers. * * `sections[i]`: `{ index, label, color, marker, level, clips, first, last }` * -- `marker` is the index into `deck.markers` that opens it (null for section * 0), `level` 1 or 2 (a subsection, which also carries `parent` and `within`, * its section's index and label), `clips` its clip ids in order, * `first`/`last` its first and last clip. * `sectionOf` maps a clip id to its section; `entrySection[i]` is the section * of timeline entry i (a card belongs to the section it stands in). Markers * naming no clip of the timeline are left out (`stray`: their indexes). * * @param {Record} m */ export function sectionModel(m) { const tl = Array.isArray(m?.timeline) ? m.timeline : []; const markers = deckMarkers(m) ?? []; const pos = new Map(); tl.forEach((e, i) => { if (isClip(e) && !pos.has(e.id)) pos.set(e.id, i); }); const placed = []; const stray = []; markers.forEach((mk, k) => { const p = isObj(mk) ? pos.get(String(mk.after ?? "").trim()) : undefined; if (p === undefined) stray.push(k); else placed.push({ k, mk, pos: p }); }); placed.sort((a, b) => a.pos - b.pos || a.k - b.k); const pal = m?.render?.palette ?? {}; const key = m?.render?.chrome?.deck?.pip?.sectionColor0; const color0 = typeof key === "string" && /^#[0-9a-f]{3,8}$/i.test(key) ? key : (key && pal[key]) || pal.accent || null; const sections = [{ index: 0, label: null, color: color0, marker: null, level: 1, clips: [], first: null, last: null }]; for (const [i, p] of placed.entries()) { const label = typeof p.mk.label === "string" && p.mk.label.trim() ? p.mk.label.trim() : null; const own = typeof p.mk.color === "string" && p.mk.color.trim() ? p.mk.color.trim() : null; // A subsection (`level: 2`) is a section here like any other -- clips // move into and out of it, its boundary drags, it is never left empty -- // but it sits inside the section before it: `parent` is that section's // index (0, the opening, when no level-1 marker comes first), `within` // its label, and its colour is the parent's, as the rail draws it. if (p.mk.level === 2) { const parent = [...sections].reverse().find((x) => x.level === 1) ?? sections[0]; sections.push({ index: i + 1, label, color: parent.color, marker: p.k, level: 2, parent: parent.index, within: parent.label, clips: [], first: null, last: null }); } else { sections.push({ index: i + 1, label, color: own, marker: p.k, level: 1, clips: [], first: null, last: null }); } } const sectionOf = new Map(); const entrySection = []; let s = 0; let next = 0; tl.forEach((e, i) => { entrySection[i] = s; if (isClip(e)) { if (!sectionOf.has(e.id)) sectionOf.set(e.id, s); sections[s].clips.push(e.id); sections[s].first ??= e.id; sections[s].last = e.id; } while (next < placed.length && placed[next].pos === i) { next += 1; s = next; } }); return { sections, sectionOf, entrySection, stray, placed }; } /** "§3 (Leaves it open)" -- how a sentence names a section; a subsection "§4 (Leaves it open › Why)". */ export const sectionWords = (s) => { const words = s.level === 2 ? [s.within, s.label].filter(Boolean).join(" › ") : s.label; return `§${s.index}${words ? ` (${words})` : s.index === 0 ? " (the opening)" : ""}`; }; /** * Each marker's `after` again, from what section each clip is in: marker * opening section i follows the last clip of section i-1. `membership` maps a * clip id to its section; the timeline is in section order already. Refuses a * section left with no clip. Returns one sentence per marker that moved. */ export function reanchorMarkers(m, model, membership) { const tl = m.timeline; const markers = deckMarkers(m) ?? []; const lastIn = new Map(); const count = new Map(); for (const e of tl) { if (!isClip(e) || !membership.has(e.id)) continue; const s = membership.get(e.id); lastIn.set(s, e.id); count.set(s, (count.get(s) ?? 0) + 1); } const notes = []; for (const sec of model.sections) { if (!count.get(sec.index)) { throw new Error(`${sectionWords(sec)} would have no clip left — remove its boundary first (umtool timeline section-remove ${sec.marker ?? 0}), or move a clip into it`); } } for (const sec of model.sections.slice(1)) { const mk = markers[sec.marker]; const after = lastIn.get(sec.index - 1); if (mk.after !== after) { notes.push(`marker ${sec.marker}${mk.label ? ` ("${mk.label}")` : ""} moved from after ${mk.after} to after ${after}`); mk.after = after; } } return notes; } /** * MOVE TO SECTION: clips `ids` (in the order they stand in the cut) to the end * of section `target` -- or its start with `position: "start"` -- with every * marker following so each section keeps the clips it had, plus these. * * @param {Record} m * @param {string[]} ids * @param {number} target * @param {{ position?: "start" | "end", allowNoop?: boolean }} [opts] `allowNoop`: a clip already there is no refusal */ export function moveToSectionIn(m, ids, target, { position = "end", allowNoop = false } = {}) { const model = sectionModel(m); if (deckMarkers(m) === null) needDeck(m); const t = Number(target); if (!Number.isInteger(t) || t < 0 || t >= model.sections.length) { throw new Error(`section must be 0–${model.sections.length - 1} (§0 is before the first marker)`); } if (position !== "start" && position !== "end") throw new Error('position is "start" or "end"'); const want = [...new Set((Array.isArray(ids) ? ids : [ids]).map((x) => String(x ?? "").trim()).filter(Boolean))]; if (!want.length) throw new Error("name one or more clips to move"); const tl = m.timeline; for (const id of want) { const e = tl.find((x) => x?.id === id); if (!e) throw new Error(`no timeline entry with id ${id}`); if (!isClip(e)) throw new Error(`${id} is a ${e.type ?? "non-clip"} entry: a section move takes clips (a card stays where it stands)`); if (tl.filter((x) => x?.id === id).length > 1) throw new Error(`${id} is in the timeline more than once — move one twin with \`timeline move\``); } const membership = new Map(model.sectionOf); const from = new Map(want.map((id) => [id, membership.get(id)])); for (const id of want) membership.set(id, t); const moving = new Set(want); const moved = tl.filter((e) => isClip(e) && moving.has(e.id)); const rest = tl.filter((e) => !(isClip(e) && moving.has(e.id))); // The remaining clips are still in section order: the end of `t` is just // after the last clip at or before it, its start just after the last clip // before it (a card opening the next section stays after the moved clips). let at = 0; rest.forEach((e, i) => { if (!isClip(e)) return; const s = membership.get(e.id); if (position === "end" ? s <= t : s < t) at = i + 1; }); rest.splice(at, 0, ...moved); const before = tl.map((e) => e?.id).join("\n"); m.timeline.splice(0, m.timeline.length, ...rest); const reanchored = reanchorMarkers(m, model, membership); const changed = before !== m.timeline.map((e) => e?.id).join("\n"); if (!changed && !reanchored.length && !allowNoop) throw new Error(`${want.join(", ")} already ${want.length === 1 ? "stands" : "stand"} at the ${position} of ${sectionWords(model.sections[t])}`); return { ids: moved.map((e) => e.id), section: t, label: model.sections[t].label, from: Object.fromEntries(from), position, reanchored, }; } /** * PLACE (a drop on Play's reel strip, 1 Nov 2026): entries `ids` -- in the * order they stand in the cut -- land right before entry `before` (null: at * the end), and the CLIPS among them join section `section`; every other clip * keeps the section it is in, and the markers are re-anchored so that holds. * One edit for "drag c04 between o01 and o02" whatever the boundaries do -- * `timeline move --to` index arithmetic moved a boundary with the clip it * followed. * * `section` defaults to the section of the clip the drop lands next to (the * one before it, else the one after). It must be one the slot can belong to: * between a clip of §a and a clip of §b the moved clips join §a, §b, or a * section between them that has no other clip left (a section's only clip * moved within its own seam). Anything else is refused in words, as an emptied * section is. A card (not a clip) is moved to the slot and belongs to no * section of its own. * * The section model is the deck's markers (sectionModel): nothing here * assumes a flat list of at most N sections, nor knows the limit. * * @param {Record} m * @param {string[] | string} ids * @param {{ before?: string | null, section?: number | null }} [opts] */ export function placeIn(m, ids, { before = null, section = null } = {}) { const tl = m.timeline; if (!Array.isArray(tl)) throw new Error("the manifest has no timeline"); const want = [...new Set((Array.isArray(ids) ? ids : [ids]).map((x) => String(x ?? "").trim()).filter(Boolean))]; if (!want.length) throw new Error("name one or more entries to place"); for (const id of want) { const n = tl.filter((x) => x?.id === id).length; if (!n) throw new Error(`no timeline entry with id ${id}`); if (n > 1) throw new Error(`${id} is in the timeline more than once — move one twin with \`timeline move\``); } const b = before === null || before === undefined || before === "" ? null : String(before).trim(); if (b !== null && want.includes(b)) throw new Error(`${b} is one of the entries being placed: name the entry they land before`); if (b !== null && !tl.some((x) => x?.id === b)) throw new Error(`no timeline entry with id ${b} to place before`); const model = sectionModel(m); const hasDeck = deckMarkers(m) !== null; const moving = new Set(want); const moved = tl.filter((e) => moving.has(e?.id)); const rest = tl.filter((e) => !moving.has(e?.id)); const at = b === null ? rest.length : rest.findIndex((e) => e?.id === b); const clipsMoved = moved.filter(isClip).map((e) => e.id); const fromIndex = tl.findIndex((e) => e?.id === want[0]); // The sections either side of the slot, among the clips that stay. const prevClip = [...rest.slice(0, at)].reverse().find(isClip) ?? null; const nextClip = rest.slice(at).find(isClip) ?? null; const lo = prevClip ? model.sectionOf.get(prevClip.id) : 0; const hi = nextClip ? model.sectionOf.get(nextClip.id) : model.sections.length - 1; let target = section === null || section === undefined || section === "" ? (prevClip ? lo : hi) : Number(section); if (!hasDeck || !clipsMoved.length) target = prevClip ? lo : hi; if (!Number.isInteger(target) || target < 0 || target >= model.sections.length) { throw new Error(`section must be 0–${model.sections.length - 1} (§0 is before the first marker)`); } if (clipsMoved.length && hasDeck && (target < lo || target > hi)) { const where = prevClip && nextClip ? `between ${prevClip.id} (${sectionWords(model.sections[lo])}) and ${nextClip.id} (${sectionWords(model.sections[hi])})` : prevClip ? `after ${prevClip.id} (${sectionWords(model.sections[lo])})` : `before ${nextClip.id} (${sectionWords(model.sections[hi])})`; throw new Error(`a clip dropped ${where} cannot join ${sectionWords(model.sections[target])} — drop it inside that section, or move it there (section-move)`); } const membership = new Map(model.sectionOf); const fromSections = Object.fromEntries(clipsMoved.map((id) => [id, membership.get(id)])); for (const id of clipsMoved) membership.set(id, target); // A section strictly between lo and hi other than the target would be left with no clip. rest.splice(at, 0, ...moved); const beforeOrder = tl.map((e) => e?.id).join("\n"); m.timeline.splice(0, m.timeline.length, ...rest); const reanchored = hasDeck ? reanchorMarkers(m, model, membership) : []; const changed = beforeOrder !== m.timeline.map((e) => e?.id).join("\n"); if (!changed && !reanchored.length) { throw new Error(`${want.join(", ")} already ${want.length === 1 ? "stands" : "stand"} there${hasDeck && clipsMoved.length ? ` in ${sectionWords(model.sections[target])}` : ""}`); } return { ids: moved.map((e) => e.id), before: b, from: fromIndex, to: m.timeline.findIndex((e) => e?.id === want[0]), section: hasDeck && clipsMoved.length ? target : null, label: hasDeck && clipsMoved.length ? model.sections[target].label : null, fromSections, reanchored, }; } /** * NEW SECTION HERE: a marker right after clip `afterId`, so the clips after it * (to the next boundary) are a section of their own, labelled and coloured. * * @param {Record} m * @param {string} afterId * @param {{ label?: string | null, color?: string | null }} [spec] */ export function addSectionIn(m, afterId, { label = null, color = null, level = null } = {}) { const markers = needDeck(m); const model = sectionModel(m); const id = String(afterId ?? "").trim(); const tl = m.timeline; const i = tl.findIndex((e) => e?.id === id); if (i < 0) throw new Error(`no timeline entry with id ${id}`); if (!isClip(tl[i])) throw new Error(`${id} is a ${tl[i].type ?? "non-clip"} entry: a section boundary follows a clip`); const lastClip = [...tl].reverse().find(isClip); if (lastClip?.id === id) throw new Error(`${id} is the last clip: a section after it would be empty`); const already = model.placed.find((p) => p.pos === i); if (already) throw new Error(`a section already starts after ${id} (marker ${already.k}${already.mk.label ? `, "${already.mk.label}"` : ""}) — edit that one`); const mk = { after: id }; const l = typeof label === "string" ? label.trim() : ""; if (l) mk.label = l; const c = typeof color === "string" ? color.trim() : ""; if (c) mk.color = c; const lv = levelOf(level); if (lv !== 1) mk.level = lv; // In the cut's order in the list too: the Markers editor lists them so. const k = model.placed.filter((p) => p.pos < i).reduce((n, p) => Math.max(n, p.k + 1), 0); markers.splice(k, 0, mk); const opened = model.sections[model.entrySection[i]]; const nextClip = tl.slice(i + 1).find(isClip); return { marker: k, after: id, label: l || null, color: c || null, ...(lv === 2 ? { level: 2 } : {}), opens: nextClip?.id ?? null, splitFrom: sectionWords(opened) }; } /** A level as an op is handed it: 1 (or nothing) a section, 2 a subsection; the word "2" is 2; anything else kept for the deck's check. */ function levelOf(v) { if (v === null || v === undefined || v === "") return 1; return typeof v === "string" && /^\s*\d+\s*$/.test(v) ? Number(v) : v; } /** * DRAG A BOUNDARY: marker `k` follows clip `afterId` now -- somewhere between * the boundaries either side of it, so no section empties and none cross. * * @param {Record} m * @param {number} k * @param {string} afterId */ export function moveBoundaryIn(m, k, afterId) { const markers = needDeck(m); const model = sectionModel(m); const n = Number(k); if (!Number.isInteger(n) || !markers[n]) throw new Error(`no marker ${k} (0–${markers.length - 1})`); const sec = model.sections.find((s) => s.marker === n); if (!sec) throw new Error(`marker ${n} names ${markers[n]?.after ?? "nothing"}, which is not a clip of the cut`); const tl = m.timeline; const id = String(afterId ?? "").trim(); const i = tl.findIndex((e) => e?.id === id); if (i < 0) throw new Error(`no timeline entry with id ${id}`); if (!isClip(tl[i])) throw new Error(`${id} is a ${tl[i].type ?? "non-clip"} entry: a section boundary follows a clip`); const me = model.placed.findIndex((p) => p.k === n); const lo = me > 0 ? model.placed[me - 1].pos : -1; const hi = me + 1 < model.placed.length ? model.placed[me + 1].pos : tl.length; const clipsIn = (a, b) => tl.slice(a + 1, b + 1).filter(isClip).length; if (i <= lo || clipsIn(lo, i) < 1) throw new Error(`after ${id} the boundary would cross or touch the one before it (${model.placed[me - 1]?.mk.after}): ${sectionWords(model.sections[sec.index - 1])} would have no clip`); if (i >= hi || tl.slice(i + 1, hi + 1).filter(isClip).length < 1) { throw new Error(`after ${id} ${sectionWords(sec)} would have no clip${hi < tl.length ? ` (the next boundary is after ${model.placed[me + 1].mk.after})` : " (it is the last clip)"}`); } const was = markers[n].after; if (was === id) throw new Error(`marker ${n} already follows ${id}`); markers[n].after = id; const p0 = model.placed[me].pos; const crossed = tl.slice(Math.min(p0, i) + 1, Math.max(p0, i) + 1).filter(isClip).map((e) => e.id); return { marker: n, from: was, after: id, // Which clips changed section, and into which. moved: crossed, into: i > p0 ? sectionWords(model.sections[sec.index - 1]) : sectionWords(sec), }; } /** REMOVE A BOUNDARY: marker `k` goes, and its section joins the one before it. */ export function removeBoundaryIn(m, k) { const markers = needDeck(m); const n = Number(k); if (!Number.isInteger(n) || !markers[n]) throw new Error(`no marker ${k} (0–${markers.length - 1})`); const [gone] = markers.splice(n, 1); return { marker: n, removed: gone }; } /** A section's label, colour and/or level (its marker's): null or "" removes a label or colour; level 1 (or null) a section, 2 a subsection. */ export function setSectionIn(m, k, patch) { const markers = needDeck(m); const n = Number(k); if (!Number.isInteger(n) || !markers[n]) throw new Error(`no marker ${k} (0–${markers.length - 1})`); if (!isObj(patch) || (!("label" in patch) && !("color" in patch) && !("level" in patch))) throw new Error("send label, color and/or level"); const mk = markers[n]; for (const key of ["label", "color"]) { if (!(key in patch)) continue; const v = typeof patch[key] === "string" ? patch[key].trim() : ""; if (v) mk[key] = v; else delete mk[key]; } if ("level" in patch) { const lv = levelOf(patch.level); if (lv === 1) delete mk.level; else mk.level = lv; } return { marker: n, entry: { ...mk } }; } /** * After clip `firstId` was split in two (the second half `secondId` right * after it): a marker that followed the first half follows the second -- the * same boundary, or the second half falls into the next section. */ export function followSplitMarkers(m, firstId, secondId) { const markers = deckMarkers(m) ?? []; const notes = []; markers.forEach((mk, k) => { if (isObj(mk) && String(mk.after ?? "").trim() === firstId) { mk.after = secondId; notes.push(`marker ${k}${mk.label ? ` ("${mk.label}")` : ""} follows ${secondId} now (the same boundary)`); } }); return notes; }