// THE GENERIC MODULE PANEL'S MODEL (release 39): what umtool's Media tab // draws for a module from its `ui` descriptor, and what a save writes -- // PURE, so the route, the CLI and the tests read one arithmetic. // // A module's `ui` fields are named by dotted keys relative to its BASE: its // configKey, except where the module's fields span its host's object (the // deck shell's and the rail's and the claim checks' fields are the deck's // own keys, `render.chrome.deck`; the blur's are the render block's). A save // is `{ fields: { : value | null } }` -- null removes the key -- // applied over the manifest as it is, then written by updateChrome (a key // under render.chrome) or updateRender (a look key: intro, blur). // // ENABLE / DISABLE: a module whose settings are an object of their own (the // story modules, the finale, the counter, the intro, the claim checks, the // section title) is ON while that object is in the manifest -- or its look // preset -- and OFF when it is removed (or, the section title, `false`). The // deck's other parts are on with the deck. import { MODULES } from "./index.mjs"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** Look keys whose writer merges a patch into what it holds (common look.mjs applyLookPatch): the gap mark's, the channel names. */ const MERGED_LOOK_KEYS = new Set(["gapCue", "channelNames"]); /** Where a module's ui keys are rooted. */ export function uiBase(m) { if (m.id === "shell" || m.id === "rail" || m.id === "claims") return "render.chrome.deck"; if (m.id === "blur") return "render"; return m.configKey ?? null; } /** A module's cut-wide fields: a clip's own (`clip.*`, the clip editor's) are not the panel's. */ export const uiOf = (m) => (m.ui ?? []).filter((f) => !String(f.key).startsWith("clip.")); /** The modules the panel lists: every one with a ui descriptor and a home. */ export const panelModules = () => MODULES.filter((m) => uiOf(m).length && uiBase(m)); /** Modules that are switched on and off by their object being there. */ const TOGGLES = Object.freeze({ pair: { pairs: [] }, chapter: { list: [] }, status: { items: [] }, finale: {}, counter: { series: [{ id: "count", label: "Count" }] }, intro: { style: "boot-dark", site: "auto" }, claims: null, "section-title": {}, // The measure's instruments (instruments.mjs): on while `deck.` is there. dial: {}, odometer: {}, trail: {}, }); /** Is this module toggled by its object's presence, and what does turning it on write? */ export function toggleOf(m) { return Object.hasOwn(TOGGLES, m.id) ? { on: TOGGLES[m.id] } : null; } /** A dotted path's value in an object. */ export function getPath(obj, dotted) { return dotted.split(".").reduce((o, k) => (isObj(o) ? o[k] : undefined), obj); } /** A copy of `obj` with `dotted` set to `value` (null or undefined removes it; emptied objects are pruned). */ export function setPath(obj, dotted, value) { const out = isObj(obj) ? structuredClone(obj) : {}; const keys = dotted.split("."); const trail = [out]; let cur = out; for (const k of keys.slice(0, -1)) { if (!isObj(cur[k])) cur[k] = {}; cur = cur[k]; trail.push(cur); } const last = keys[keys.length - 1]; if (value === null || value === undefined) { delete cur[last]; for (let i = trail.length - 1; i > 0; i -= 1) { if (Object.keys(trail[i]).length) break; delete trail[i - 1][keys[i - 1]]; } } else cur[last] = value; return out; } /** * Each panel module's state for a manifest: `{ id, title, summary, kind, * slot, base, ui, docs, toggle, enabled, fields }` -- `fields` its ui keys' * values as the MANIFEST holds them, `seen` as the build reads them (the * preset under it), `problems` its validator's sentences. * * @param {Record} manifest the manifest as written * @param {Record} seen the manifest with its preset laid under it * @param {Record} problems moduleProblems(seen) */ export function panelState(manifest, seen, problems = {}) { return panelModules().map((m) => { const base = uiBase(m); const rel = base.split(".").slice(1).join("."); const own = rel ? getPath(manifest.render ?? {}, rel) : manifest.render ?? {}; const eff = rel ? getPath(seen.render ?? {}, rel) : seen.render ?? {}; const fields = {}; const effective = {}; for (const f of uiOf(m)) { const v = getPath(own, f.key); const e = getPath(eff, f.key); if (v !== undefined) fields[f.key] = v; if (e !== undefined) effective[f.key] = e; } const toggle = toggleOf(m); const cfgRel = (m.configKey ?? "").split(".").slice(1).join("."); const on = getPath(seen.render ?? {}, cfgRel); const enabled = toggle ? on !== undefined && on !== false : true; return { id: m.id, title: m.title, summary: m.summary ?? "", kind: m.kind, slot: m.slot, z: m.z, configKey: m.configKey, base, ui: uiOf(m), docs: m.docs?.heading ?? null, toggle: !!toggle, enabled, fields, effective, preview: typeof m.preview === "function" || !!m.region || !!m.host, problems: problems[m.id] ?? [], }; }); } /** * A panel save as the writers take it: `{ chrome }` (the whole new * render.chrome, for updateChrome) or `{ render }` (a look patch, for * updateRender). `fields` are dotted keys relative to the module's base; * `enabled: false` removes the module's object, `true` writes its default. * * @param {Record} manifest * @param {string} id * @param {{ fields?: Record, enabled?: boolean }} change * @returns {{ chrome?: Record | null, render?: Record }} */ export function panelWrite(manifest, id, { fields = {}, enabled } = {}) { const m = panelModules().find((x) => x.id === id); if (!m) throw new Error(`no module ${id} in the panel (${panelModules().map((x) => x.id).join(", ")})`); const base = uiBase(m); const known = new Set(uiOf(m).map((f) => f.key)); for (const k of Object.keys(fields)) if (!known.has(k)) throw new Error(`${id}: ${k} is not one of its fields (${[...known].join(", ")})`); const render = structuredClone(manifest.render ?? {}); const cfgRel = (m.configKey ?? "").split(".").slice(1).join("."); let next = render; if (enabled === false) { if (!toggleOf(m)) throw new Error(`${id} is drawn with the deck: it is not switched on and off`); next = setPath(next, cfgRel, null); } else { if (enabled === true) { const t = toggleOf(m); if (!t) throw new Error(`${id} is drawn with the deck: it is not switched on and off`); const was = getPath(next, cfgRel); if (was === undefined || was === false) next = setPath(next, cfgRel, structuredClone(t.on ?? {})); } const rel = base.split(".").slice(1).join("."); for (const [k, v] of Object.entries(fields)) next = setPath(next, rel ? `${rel}.${k}` : k, v === "" ? null : v); } if (base.startsWith("render.chrome") || m.configKey?.startsWith("render.chrome")) { // A chrome left empty is no chrome (updateChrome's null removes the key). return { chrome: next.chrome && Object.keys(next.chrome).length ? next.chrome : null }; } // A look key (intro, blur): the patch of the top-level keys it changed. const patch = {}; for (const k of new Set([...Object.keys(render), ...Object.keys(next)])) { if (k === "chrome") continue; if (JSON.stringify(render[k]) === JSON.stringify(next[k])) continue; if (next[k] === undefined) patch[k] = null; else if (MERGED_LOOK_KEYS.has(k) && isObj(next[k])) { // A look key the writer MERGES (applyLookPatch: each key over what it holds, null drops one): a field // cleared here is sent as null, or it would stay. const was = isObj(render[k]) ? render[k] : {}; patch[k] = { ...Object.fromEntries(Object.keys(was).filter((x) => !(x in next[k])).map((x) => [x, null])), ...next[k] }; } else patch[k] = next[k]; } return { render: patch }; }