// THE INSPECTOR'S WRITERS (umtool redesign L6, "inspector panels for this week's hand edits"): what agents did by // hand this week, each now one write the UI and its CLI twin share -- the project's provenance (siteOrigin, // channelSlug, channel), its title and subtitle, a teaser or closing card inserted, and the published Rumble embed // ids (`siteVideo`) written onto the clips that cite a URL-slug folder. // // Every one lands in the WORKING DRAFT (video.manifest.json; L3: never a take's patch) through manifest.mjs's // structural door (`structuralEdit`): the mtime token, the read inside the lock, the build's own checks, an undo // point `auto-before-` (every op its own in the app -- the Cut tab's Undo takes exactly it back), the atomic // write. The checks of a value are pure and exported, so the route and the CLI refuse in the same words. // // Plain ESM: the CLI runs it under bare node. import { StructureRefused, entryIndex, freshEntryId, patchClipEntry, structuralEdit } from "./manifest.mjs"; import { TEASER_BEAT, TEASER_LIMITS, validateTeaser } from "umtool-report-to-video/deck"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const round2 = (n) => Math.round(n * 100) / 100; // --------------------------------------------------------------------------- // PROVENANCE: where the cut's clips come from and where its QRs open. // --------------------------------------------------------------------------- /** The provenance keys the inspector writes (the link policy has its own writer, updateLinkPolicy). */ export const PROVENANCE_EDIT_KEYS = Object.freeze(["siteOrigin", "channelSlug", "channel"]); const SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; const CHANNEL_NAME_MAX = 120; /** * A provenance patch checked and normalised: each key a string to set, or null / "" to remove it. siteOrigin is * an http(s) origin (a path, query or hash refused; the trailing slash dropped), channelSlug a corpus slug, * channel the sweep's display name (one line). Throws a sentence naming every problem. * * @param {unknown} patch * @returns {Record} */ export function provenancePatchOf(patch) { if (!isObj(patch)) throw new Error("a provenance patch is an object: { siteOrigin?, channelSlug?, channel? }"); const errors = []; const out = {}; for (const k of Object.keys(patch)) { if (!PROVENANCE_EDIT_KEYS.includes(k)) errors.push(`${k} is not a provenance key this writer sets (${PROVENANCE_EDIT_KEYS.join(", ")}; the link policy: umtool link-policy)`); } for (const k of PROVENANCE_EDIT_KEYS) { if (!(k in patch)) continue; const raw = patch[k]; if (raw === null || raw === undefined || (typeof raw === "string" && !raw.trim())) { out[k] = null; continue; } if (typeof raw !== "string") { errors.push(`${k} must be text`); continue; } const v = raw.trim(); if (k === "siteOrigin") { let u = null; try { u = new URL(v); } catch { /* below */ } if (!u || (u.protocol !== "http:" && u.protocol !== "https:")) errors.push(`siteOrigin must be an http:// or https:// address (got \`${v}\`)`); else if ((u.pathname && u.pathname !== "/") || u.search || u.hash) errors.push(`siteOrigin is the archive's origin, with no path (\`${u.origin}\`, not \`${v}\`)`); else out[k] = u.origin; } else if (k === "channelSlug") { if (!SLUG_RE.test(v)) errors.push(`channelSlug must be a channel's slug (letters, digits, . _ -), not \`${v}\``); else out[k] = v; } else { if (/[\r\n]/.test(v)) errors.push("channel must be one line"); else if (v.length > CHANNEL_NAME_MAX) errors.push(`channel is ${v.length} characters (at most ${CHANNEL_NAME_MAX})`); else out[k] = v; } } if (errors.length) throw new Error(errors.join("; ")); if (!Object.keys(out).length) throw new Error("nothing to change"); return out; } /** The patch applied to a manifest in memory; returns what changed (`{ key: [from, to] }`). */ export function applyProvenancePatch(m, patch) { const p = provenancePatchOf(patch); const prov = isObj(m.provenance) ? m.provenance : {}; const changed = {}; for (const [k, v] of Object.entries(p)) { const was = typeof prov[k] === "string" ? prov[k] : null; if (v === null) delete prov[k]; else prov[k] = v; if (was !== v) changed[k] = [was, v]; } m.provenance = prov; return changed; } /** What the inspector reads: the three keys as written (null when absent). */ export const provenanceView = (m) => Object.fromEntries(PROVENANCE_EDIT_KEYS.map((k) => [k, typeof m?.provenance?.[k] === "string" && m.provenance[k] ? m.provenance[k] : null])); /** * Set the project's provenance keys (one write, one undo point `auto-before-provenance`). * * @param {string} dir * @param {Record} patch * @param {{ token?: string | null }} [opts] */ export async function updateProvenance(dir, patch, { token = null } = {}) { provenancePatchOf(patch); // refused before the lock is taken return structuralEdit(dir, "provenance", token, (m) => { const changed = applyProvenancePatch(m, patch); return { provenance: provenanceView(m), changed }; }); } // --------------------------------------------------------------------------- // THE TITLE AND SUBTITLE (9 Nov: four news projects' title/subtitle were set with jq -- no verb existed). // --------------------------------------------------------------------------- export const TITLE_MAX = 200; export const SUBTITLE_MAX = 300; /** * A title patch checked: `title` one line, not empty, at most TITLE_MAX; `subtitle` one line at most SUBTITLE_MAX, * null or "" removes it. A key not given is kept. * * @param {unknown} patch * @returns {{ title?: string, subtitle?: string | null }} */ export function titlePatchOf(patch) { if (!isObj(patch)) throw new Error("a title patch is an object: { title?, subtitle? }"); const errors = []; const out = {}; for (const k of Object.keys(patch)) if (k !== "title" && k !== "subtitle") errors.push(`${k} is not title or subtitle`); if ("title" in patch) { const v = typeof patch.title === "string" ? patch.title.trim() : null; if (!v) errors.push("title must be words (a project always has one)"); else if (/[\r\n]/.test(v)) errors.push("title must be one line"); else if (v.length > TITLE_MAX) errors.push(`title is ${v.length} characters (at most ${TITLE_MAX})`); else out.title = v; } if ("subtitle" in patch) { const raw = patch.subtitle; if (raw === null || raw === undefined || (typeof raw === "string" && !raw.trim())) out.subtitle = null; else if (typeof raw !== "string") errors.push("subtitle must be text"); else if (/[\r\n]/.test(raw.trim())) errors.push("subtitle must be one line"); else if (raw.trim().length > SUBTITLE_MAX) errors.push(`subtitle is ${raw.trim().length} characters (at most ${SUBTITLE_MAX})`); else out.subtitle = raw.trim(); } if (errors.length) throw new Error(errors.join("; ")); if (!Object.keys(out).length) throw new Error("nothing to change: give a title, a subtitle, or both"); return out; } /** * Set the project's title and/or subtitle (one write, one undo point `auto-before-title`). * * @param {string} dir * @param {{ title?: unknown, subtitle?: unknown }} patch * @param {{ token?: string | null }} [opts] */ export async function updateTitle(dir, patch, { token = null } = {}) { const p = titlePatchOf(patch); return structuralEdit(dir, "title", token, (m) => { if (p.title !== undefined) m.title = p.title; if (p.subtitle === null) delete m.subtitle; else if (p.subtitle !== undefined) m.subtitle = p.subtitle; return { title: m.title ?? null, subtitle: typeof m.subtitle === "string" ? m.subtitle : null }; }); } // --------------------------------------------------------------------------- // A TEASER OR CLOSING CARD INSERTED (a "cold" open, a "close" at the end): `{type: "teaser", id, lines, beat?}`, // checked by the build's validateTeaser before it is written. // --------------------------------------------------------------------------- /** The teaser line keys the inspector writes (the rest -- role, lead, replace, together -- through `timeline teaser`). */ export const TEASER_INSERT_LINE_KEYS = Object.freeze(["text", "break", "hold", "replace", "together", "role", "lead"]); /** * One line as written: a string when it is only its text, else `{ text, break?, hold?, … }` with empty keys left out. * @param {unknown} l */ export function teaserLineOf(l) { if (typeof l === "string") return l.trim(); if (!isObj(l)) throw new Error("a teaser line is its words, or { text, break?, hold? }"); const out = {}; for (const k of Object.keys(l)) if (!TEASER_INSERT_LINE_KEYS.includes(k)) throw new Error(`${k} is not a teaser line key (${TEASER_INSERT_LINE_KEYS.join(", ")})`); out.text = typeof l.text === "string" ? l.text.trim() : l.text; if (typeof l.break === "string" && l.break.trim()) out.break = l.break.trim(); if (l.hold !== undefined && l.hold !== null && l.hold !== "") out.hold = round2(Number(l.hold)); for (const k of ["replace", "together"]) if (l[k] === true) out[k] = true; if (typeof l.role === "string" && l.role) out.role = l.role; if (typeof l.lead === "string" && l.lead.trim()) out.lead = l.lead.trim(); return Object.keys(out).length === 1 ? out.text : out; } /** * Where an insert lands, as an index of the timeline BEFORE the insert: "start" (0), "end" (its length), * `{ after: }` or `{ before: }` (an id the timeline holds once; `at` names it when it repeats). * * @param {Array>} timeline * @param {unknown} position */ export function insertIndexOf(timeline, position) { if (position === "start" || position === 0) return 0; if (position === "end" || position === undefined || position === null) return timeline.length; if (isObj(position) && typeof position.after === "string") return entryIndex(timeline, position.after, position.at ?? null) + 1; if (isObj(position) && typeof position.before === "string") return entryIndex(timeline, position.before, position.at ?? null); throw new Error('a position is "start", "end", { after: } or { before: }'); } /** * The teaser entry a spec makes, its id fresh in the timeline (the spec's, else "close" at the end / "cold" at * the start / "teaser"), checked by the build's validateTeaser (throws StructureRefused with its sentences). * * @param {Array>} timeline * @param {{ id?: unknown, lines?: unknown, beat?: unknown }} spec * @param {number} index where it will be inserted (names the default id) */ export function teaserEntryOf(timeline, spec, index) { if (!isObj(spec)) throw new Error("a teaser is { id?, lines: [...], beat? }"); const lines = Array.isArray(spec.lines) ? spec.lines.map(teaserLineOf).filter((l) => (typeof l === "string" ? l : l.text)) : null; if (!lines || !lines.length) throw new StructureRefused([`a teaser needs ${TEASER_LIMITS.lines[0]} to ${TEASER_LIMITS.lines[1]} lines (the first is its title)`]); const base = typeof spec.id === "string" && spec.id.trim() ? spec.id.trim() : index === 0 ? "cold" : index >= timeline.length ? "close" : "teaser"; if (!/^[A-Za-z0-9_-]{1,64}$/.test(base)) throw new StructureRefused([`id must be letters, digits, dashes or underscores (got \`${base}\`)`]); if (typeof spec.id === "string" && spec.id.trim() && timeline.some((e) => e?.id === base)) { throw new StructureRefused([`the cut already has an entry ${base}: name the card something else`]); } const entry = { type: "teaser", id: freshEntryId(timeline, base), lines }; if (spec.beat !== undefined && spec.beat !== null && spec.beat !== "") { const b = Number(spec.beat); entry.beat = Number.isFinite(b) ? round2(b) : spec.beat; } const errors = validateTeaser(entry, `the new teaser ${entry.id}`); if (errors.length) throw new StructureRefused(errors); return entry; } /** * INSERT A TEASER OR CLOSING CARD (`umtool timeline

teaser-insert`, the inspector's Insert a card): one * structural write, one undo point `auto-before-teaser-insert`. * * @param {string} dir * @param {{ id?: unknown, lines?: unknown, beat?: unknown }} spec * @param {unknown} position "start" | "end" | { after } | { before } * @param {{ token?: string | null }} [opts] */ export async function insertTeaser(dir, spec, position, { token = null } = {}) { return structuralEdit(dir, "teaser-insert", token, (m) => { const i = insertIndexOf(m.timeline, position); const entry = teaserEntryOf(m.timeline, spec, i); m.timeline.splice(i, 0, entry); return { id: entry.id, at: i, entry }; }); } /** The teaser's limits, for a form's hints (the build's numbers). */ export const teaserLimits = () => ({ lines: [...TEASER_LIMITS.lines], rows: TEASER_LIMITS.rows, chars: TEASER_LIMITS.chars, hold: TEASER_LIMITS.hold, beat: { ...TEASER_BEAT } }); // --------------------------------------------------------------------------- // THE PUBLISHED EMBED ID (a Rumble clip whose `video` is the URL-slug folder the corpus keeps it under, while the // archive serves the EMBED id: shareLine.mjs siteVideoFor). `siteVideo` written on each, a citeUrl that named the // folder id moved with it -- every clip in ONE write, one undo point `auto-before-embed-ids`. // --------------------------------------------------------------------------- /** * @param {string} dir * @param {Array<{ clip: string, siteVideo: string, citeUrl?: string | null }>} fixes * @param {{ token?: string | null }} [opts] */ export async function setSiteVideos(dir, fixes, { token = null } = {}) { if (!Array.isArray(fixes) || !fixes.length) throw new Error("nothing to fix"); for (const f of fixes) { if (!isObj(f) || typeof f.clip !== "string" || typeof f.siteVideo !== "string" || !f.siteVideo) throw new Error("a fix is { clip, siteVideo, citeUrl? }"); } return structuralEdit(dir, "embed-ids", token, (m) => { const done = []; for (const f of fixes) { const e = m.timeline[entryIndex(m.timeline, f.clip)]; if (e?.type !== "clip") throw new Error(`${f.clip} is not a clip`); const patch = { siteVideo: f.siteVideo, ...(typeof f.citeUrl === "string" && f.citeUrl ? { citeUrl: f.citeUrl } : {}) }; const { entry } = patchClipEntry(m, f.clip, patch); done.push({ clip: f.clip, video: String(entry.video), siteVideo: entry.siteVideo, ...(patch.citeUrl ? { citeUrl: entry.citeUrl } : {}) }); } return { fixed: done }; }); }