// THE STORY MODULES' SETTINGS (release 39): `pair` (then and now, two clips // with their dates), `chapter` (a section bumper at a named clip) and // `status` (a ledger of items, each announced and then followed up -- or // not). Their defaults and validators -- the one copy, a LEAF like // settings.mjs (deck.mjs imports it through SETTINGS, so it imports nothing // that imports deck.mjs). // // render.chrome.pair { style, position, dateFormat, labels, seconds, pairs: [...] } // render.chrome.chapter { seconds, style, list: [...] } // render.chrome.status { title, position, rows, width, mode, items: [...] } // // A clip is named by its timeline id; whether it is in the cut is checked // where the timeline is known (`storyClipErrors`, the writers), not here. /** A YYYY / YYYY-MM / YYYY-MM-DD date. */ export const DATE_RE = /^\d{4}(?:-(?:0[1-9]|1[0-2])(?:-(?:0[1-9]|[12]\d|3[01]))?)?$/; const CLIP_RE = /^[A-Za-z0-9_.:-]{1,80}$/; export const PAIR_STYLES = Object.freeze(["split", "then→now"]); export const PAIR_POSITIONS = Object.freeze(["bottom", "top"]); export const DATE_FORMATS = Object.freeze(["long", "year", "iso"]); export const PAIR_SIDES = Object.freeze(["then", "now"]); export const PAIR_LIMITS = Object.freeze({ pairs: 60, caption: 200, topic: 60, label: 16, also: 4, seconds: [1, 10] }); export const PAIR_DEFAULTS = Object.freeze({ style: "split", position: "bottom", dateFormat: "long", labels: Object.freeze({ then: "Then", now: "Now" }), seconds: 4, pairs: Object.freeze([]), }); export const CHAPTER_STYLES = Object.freeze(["card", "lower"]); export const CHAPTER_LIMITS = Object.freeze({ list: 40, title: 48, subtitle: 90, seconds: [1, 8] }); export const CHAPTER_DEFAULTS = Object.freeze({ seconds: 3, style: "card", list: Object.freeze([]) }); export const STATUS_POSITIONS = Object.freeze(["top-right", "top-left", "bottom-right", "bottom-left"]); export const STATUS_MODES = Object.freeze(["fill", "each"]); export const STATUS_LIMITS = Object.freeze({ items: 30, item: 140, text: 160, title: 40, rows: [1, 8], width: [360, 900], evidence: 12 }); export const STATUS_DEFAULTS = Object.freeze({ title: "On the record", position: "top-right", rows: 4, width: 640, mode: "fill", items: Object.freeze([]), }); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const numIn = (v, lo, hi) => typeof v === "number" && Number.isFinite(v) && v >= lo && v <= hi; const str = (v) => typeof v === "string" && v.trim().length > 0; function unknown(obj, allowed, where, errors) { for (const k of Object.keys(obj)) if (!allowed.includes(k)) errors.push(`${where}.${k} is not a setting (${allowed.join(", ")})`); } function text(v, max, where, errors, { required = false } = {}) { if (v === undefined) { if (required) errors.push(`${where} is required`); return; } if (typeof v !== "string" || (required && !v.trim())) { errors.push(`${where} must be words`); return; } if ([...v].length > max) errors.push(`${where} is ${[...v].length} characters (at most ${max})`); } function clip(v, where, errors) { if (typeof v !== "string" || !CLIP_RE.test(v)) errors.push(`${where} must name a clip by its timeline id`); } function date(v, where, errors) { if (v === undefined || v === null) return; if (typeof v !== "string" || !DATE_RE.test(v)) errors.push(`${where} must be a date: YYYY, YYYY-MM or YYYY-MM-DD`); } /** Every reason `render.chrome.pair` cannot be drawn (its shape; the clips against the timeline in storyClipErrors). */ export function validatePair(v, where = "render.chrome.pair") { if (v === undefined) return []; if (!isObj(v)) return [`${where} must be an object`]; const errors = []; unknown(v, ["style", "position", "dateFormat", "labels", "seconds", "pairs"], where, errors); if (v.style !== undefined && !PAIR_STYLES.includes(v.style)) errors.push(`${where}.style must be ${PAIR_STYLES.map((s) => `"${s}"`).join(" or ")}`); if (v.position !== undefined && !PAIR_POSITIONS.includes(v.position)) errors.push(`${where}.position must be "bottom" or "top"`); if (v.dateFormat !== undefined && !DATE_FORMATS.includes(v.dateFormat)) errors.push(`${where}.dateFormat must be ${DATE_FORMATS.join(", ")}`); if (v.seconds !== undefined && !numIn(v.seconds, ...PAIR_LIMITS.seconds)) errors.push(`${where}.seconds must be from ${PAIR_LIMITS.seconds[0]} to ${PAIR_LIMITS.seconds[1]}`); if (v.labels !== undefined) { if (!isObj(v.labels)) errors.push(`${where}.labels must be { then, now }`); else { unknown(v.labels, ["then", "now"], `${where}.labels`, errors); for (const s of PAIR_SIDES) text(v.labels[s], PAIR_LIMITS.label, `${where}.labels.${s}`, errors); } } if (v.pairs === undefined) return errors; if (!Array.isArray(v.pairs)) return [...errors, `${where}.pairs must be a list`]; if (v.pairs.length > PAIR_LIMITS.pairs) errors.push(`${where}.pairs has ${v.pairs.length} (at most ${PAIR_LIMITS.pairs})`); v.pairs.forEach((p, i) => { const w = `${where}.pairs[${i}]`; if (!isObj(p)) { errors.push(`${w} must be { then, now, caption? }`); return; } unknown(p, ["then", "now", "caption", "topic", "style", "also"], w, errors); for (const s of PAIR_SIDES) { const side = p[s]; if (!isObj(side)) { errors.push(`${w}.${s} must be { clip, date? }`); continue; } unknown(side, ["clip", "date", "label"], `${w}.${s}`, errors); clip(side.clip, `${w}.${s}.clip`, errors); date(side.date, `${w}.${s}.date`, errors); text(side.label, PAIR_LIMITS.label, `${w}.${s}.label`, errors); } text(p.caption, PAIR_LIMITS.caption, `${w}.caption`, errors); text(p.topic, PAIR_LIMITS.topic, `${w}.topic`, errors); if (p.style !== undefined && !PAIR_STYLES.includes(p.style)) errors.push(`${w}.style must be ${PAIR_STYLES.map((s) => `"${s}"`).join(" or ")}`); if (p.also !== undefined) { if (!Array.isArray(p.also) || p.also.length > PAIR_LIMITS.also) errors.push(`${w}.also must be a list of at most ${PAIR_LIMITS.also} { clip, side, date? }`); else p.also.forEach((a, j) => { const aw = `${w}.also[${j}]`; if (!isObj(a)) { errors.push(`${aw} must be { clip, side, date? }`); return; } unknown(a, ["clip", "side", "date"], aw, errors); clip(a.clip, `${aw}.clip`, errors); if (!PAIR_SIDES.includes(a.side)) errors.push(`${aw}.side must be "then" or "now"`); date(a.date, `${aw}.date`, errors); }); } }); return errors; } /** Every reason `render.chrome.chapter` cannot be drawn. */ export function validateChapter(v, where = "render.chrome.chapter") { if (v === undefined) return []; if (!isObj(v)) return [`${where} must be an object`]; const errors = []; unknown(v, ["seconds", "style", "list"], where, errors); if (v.seconds !== undefined && !numIn(v.seconds, ...CHAPTER_LIMITS.seconds)) errors.push(`${where}.seconds must be from ${CHAPTER_LIMITS.seconds[0]} to ${CHAPTER_LIMITS.seconds[1]}`); if (v.style !== undefined && !CHAPTER_STYLES.includes(v.style)) errors.push(`${where}.style must be "card" or "lower"`); if (v.list === undefined) return errors; if (!Array.isArray(v.list)) return [...errors, `${where}.list must be a list`]; if (v.list.length > CHAPTER_LIMITS.list) errors.push(`${where}.list has ${v.list.length} (at most ${CHAPTER_LIMITS.list})`); const seen = new Set(); v.list.forEach((c, i) => { const w = `${where}.list[${i}]`; if (!isObj(c)) { errors.push(`${w} must be { at, title, subtitle?, date? }`); return; } unknown(c, ["at", "title", "subtitle", "date"], w, errors); clip(c.at, `${w}.at`, errors); if (seen.has(c.at)) errors.push(`${w}.at: ${c.at} already opens a chapter`); seen.add(c.at); text(c.title, CHAPTER_LIMITS.title, `${w}.title`, errors, { required: true }); text(c.subtitle, CHAPTER_LIMITS.subtitle, `${w}.subtitle`, errors); date(c.date, `${w}.date`, errors); }); return errors; } function statusSide(v, where, errors) { if (v === undefined) return; if (typeof v === "string") { text(v, STATUS_LIMITS.text, where, errors); return; } if (!isObj(v)) { errors.push(`${where} must be words or { clip?, date?, text? }`); return; } unknown(v, ["clip", "date", "text"], where, errors); if (v.clip !== undefined) clip(v.clip, `${where}.clip`, errors); date(v.date, `${where}.date`, errors); text(v.text, STATUS_LIMITS.text, `${where}.text`, errors); } /** Every reason `render.chrome.status` cannot be drawn. */ export function validateStatus(v, where = "render.chrome.status") { if (v === undefined) return []; if (!isObj(v)) return [`${where} must be an object`]; const errors = []; unknown(v, ["title", "position", "rows", "width", "mode", "items"], where, errors); text(v.title, STATUS_LIMITS.title, `${where}.title`, errors); if (v.position !== undefined && !STATUS_POSITIONS.includes(v.position)) errors.push(`${where}.position must be ${STATUS_POSITIONS.join(", ")}`); if (v.mode !== undefined && !STATUS_MODES.includes(v.mode)) errors.push(`${where}.mode must be "fill" or "each"`); if (v.rows !== undefined && !(Number.isInteger(v.rows) && numIn(v.rows, ...STATUS_LIMITS.rows))) errors.push(`${where}.rows must be a whole number from ${STATUS_LIMITS.rows[0]} to ${STATUS_LIMITS.rows[1]}`); if (v.width !== undefined && !(Number.isInteger(v.width) && numIn(v.width, ...STATUS_LIMITS.width))) errors.push(`${where}.width must be a whole number of pixels from ${STATUS_LIMITS.width[0]} to ${STATUS_LIMITS.width[1]}`); if (v.items === undefined) return errors; if (!Array.isArray(v.items)) return [...errors, `${where}.items must be a list`]; if (v.items.length > STATUS_LIMITS.items) errors.push(`${where}.items has ${v.items.length} (at most ${STATUS_LIMITS.items})`); v.items.forEach((it, i) => { const w = `${where}.items[${i}]`; if (!isObj(it)) { errors.push(`${w} must be { item, announced, outcome?, evidence? }`); return; } unknown(it, ["item", "announced", "outcome", "evidence"], w, errors); text(it.item, STATUS_LIMITS.item, `${w}.item`, errors, { required: true }); statusSide(it.announced, `${w}.announced`, errors); statusSide(it.outcome, `${w}.outcome`, errors); if (it.evidence !== undefined) { if (!Array.isArray(it.evidence) || it.evidence.length > STATUS_LIMITS.evidence) errors.push(`${w}.evidence must be a list of at most ${STATUS_LIMITS.evidence} clip ids`); else it.evidence.forEach((c, j) => clip(c, `${w}.evidence[${j}]`, errors)); } const clips = [typeof it.announced === "object" ? it.announced?.clip : undefined, typeof it.outcome === "object" ? it.outcome?.clip : undefined, ...(it.evidence ?? [])]; if (!clips.some((c) => typeof c === "string")) errors.push(`${w} names no clip (announced.clip, outcome.clip or evidence): it would never be shown`); }); return errors; } /** The clips a story module's settings name, each with the path that names it. */ export function storyClips(chrome) { const out = []; const c = isObj(chrome) ? chrome : {}; (Array.isArray(c.pair?.pairs) ? c.pair.pairs : []).forEach((p, i) => { for (const s of PAIR_SIDES) if (isObj(p?.[s])) out.push([`render.chrome.pair.pairs[${i}].${s}.clip`, p[s].clip]); (Array.isArray(p?.also) ? p.also : []).forEach((a, j) => out.push([`render.chrome.pair.pairs[${i}].also[${j}].clip`, a?.clip])); }); (Array.isArray(c.chapter?.list) ? c.chapter.list : []).forEach((ch, i) => out.push([`render.chrome.chapter.list[${i}].at`, ch?.at])); (Array.isArray(c.status?.items) ? c.status.items : []).forEach((it, i) => { for (const s of ["announced", "outcome"]) if (isObj(it?.[s]) && it[s].clip !== undefined) out.push([`render.chrome.status.items[${i}].${s}.clip`, it[s].clip]); (Array.isArray(it?.evidence) ? it.evidence : []).forEach((e, j) => out.push([`render.chrome.status.items[${i}].evidence[${j}]`, e])); }); return out.filter(([, id]) => typeof id === "string"); } /** * The clips the story modules name that the timeline does not hold: a * sentence each. What a writer refuses with (a chapter at a clip that is not * in the cut would never be drawn). The build tolerates them -- a variant or * an --only build plays fewer clips -- and draws what is there. */ export function storyClipErrors(manifest) { const ids = new Set((manifest?.timeline ?? []).map((e) => e?.id)); return storyClips(manifest?.render?.chrome) .filter(([, id]) => !ids.has(id)) .map(([where, id]) => `${where}: no timeline entry is ${id}`); }