// THE COUNTER (release 39): the deck's running tally, generalised from the // fact-check verdicts to arbitrary NAMED SERIES. // // render.chrome.deck.counter: { // "series": [ { "id": "seizures", "label": "Seizures", "color": "#e5534b", // "stampLabel": "Seizure", "tallyLabel": "Fits", "showAt": "first", // "unit": "seizures", "shape": "diamond" }, … ], // "show": true, "position": "right", "showAt": "always" // } // // A clip's MARK names the series it counts by id (`claim.verdict`, or its // alias `claim.counter`), and the counter steps as its stamp lands // (factcheck.mjs `stampSchedule`, `tallySteps`). The series are the tally's // cells, in the order listed; a deck claim (`deck.claims[].tally`) names one // by id or label. // // BACK-COMPAT: without `counter`, the series are the fact-check verdicts // (common's VERDICTS, in that order) with `render.chrome.factcheck.verdicts`'s // relabels and `factcheck.tally`'s show/position/showAt -- exactly what the // tally always drew. `migrateManifest` (modules/migrate.mjs) writes that as // a `counter` the bytes of whose render are the same. A series whose id is a // verdict takes that verdict's default label, colour and shape. // // PURE: no fs. factcheck.mjs, deck.mjs and the pages read `resolveCounter`; // modules/counter.mjs validates and documents it. import { VERDICT_DEFAULTS, VERDICT_COLOR_RE, VERDICT_LABEL_MAX, VERDICTS } from "yt-dlp-transcript-common/lib/report/verdicts.mjs"; /** A series' cell shape, so a viewer who cannot tell the colours apart can tell the series apart. */ export const COUNTER_SHAPES = Object.freeze(["circle", "diamond", "square", "triangle", "ring"]); /** Each verdict's shape (VERDICTS order): what a verdict series takes by default. */ export const VERDICT_SHAPES = Object.freeze(Object.fromEntries(VERDICTS.map((v, i) => [v, COUNTER_SHAPES[i] ?? "circle"]))); /** When a cell is on screen: from the first frame (dim at 0), or from its first count. */ export const COUNTER_SHOW_AT = Object.freeze(["always", "first"]); /** Which end of the deck's text column the counter takes. */ export const COUNTER_POSITIONS = Object.freeze(["right", "left"]); /** How a series' words are set in its cell: capitalised (as the verdicts always were), or as written. */ export const COUNTER_LABELS = Object.freeze(["caps", "as-written"]); /** * The counter's limits: how many series, an id's and a unit's characters, a * label's (`writtenLabel`: a label or cell word set as written, which may take * two lines). Past 8 series the cells narrow into one row (design.mjs * counterCell, lane leaflit-labels): 12 is the most whose cells still hold a * short word at the deck's own label size. */ export const COUNTER_LIMITS = Object.freeze({ series: 12, id: 32, unit: 24, label: VERDICT_LABEL_MAX, writtenLabel: 32 }); /** A series id: what a mark names. A letter, then letters, digits, `_` or `-`. */ export const SERIES_ID_RE = /^[A-Za-z][A-Za-z0-9_-]{0,31}$/; /** A custom series' colour when it names none, by its place in the list. */ export const SERIES_COLORS = Object.freeze(["#e5534b", "#e3b23c", "#3fbf7f", "#7d8fd6", "#c55f9c", "#22ab83", "#d2732f", "#8b93a7"]); /** The keys a series may carry. */ export const SERIES_KEYS = Object.freeze(["id", "label", "color", "stampLabel", "tallyLabel", "showAt", "unit", "shape"]); /** The keys `deck.counter` may carry. */ export const COUNTER_KEYS = Object.freeze(["series", "show", "position", "showAt", "labels"]); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** Is `render` counting named series (`deck.counter`) rather than the fact-check verdicts? */ export const counterOn = (render) => isObj(render?.chrome?.deck?.counter); /** * The counter resolved: `series` (each `{ id, label, color, stampLabel?, * tallyLabel?, showAt, shape, unit? }`, in the tally's order), `order` (their * ids), `byId` (each `{ label, color, stampLabel?, tallyLabel? }` -- the shape * `resolveVerdicts` has always given, so every reader of a verdict's label and * colour reads a series' the same way), `shapes`, `showAt` (id → "always" | * "first"), `show`, `position`, and `custom` (a `deck.counter` was given). * A shape no validator would pass reads as what it can. */ export function resolveCounter(render) { const c = render?.chrome?.deck?.counter; const f = isObj(render?.chrome?.factcheck) ? render.chrome.factcheck : {}; const one = (v) => (COUNTER_SHOW_AT.includes(v) ? v : "always"); if (isObj(c)) { const list = (Array.isArray(c.series) ? c.series : []).filter((s) => isObj(s) && typeof s.id === "string" && SERIES_ID_RE.test(s.id)); const series = list.map((s, i) => { const { id, showAt, unit, shape, ...look } = s; const base = VERDICT_DEFAULTS[id] ?? { label: id, color: SERIES_COLORS[i % SERIES_COLORS.length] }; return { id, ...base, ...look, showAt: one(showAt ?? c.showAt), shape: COUNTER_SHAPES.includes(shape) ? shape : (VERDICT_SHAPES[id] ?? COUNTER_SHAPES[i % COUNTER_SHAPES.length]), ...(typeof unit === "string" && unit.trim() ? { unit: unit.trim() } : {}), }; }); return { custom: true, series, order: series.map((s) => s.id), byId: Object.fromEntries(series.map(({ id, showAt: _a, shape: _s, unit: _u, ...look }) => [id, look])), shapes: Object.fromEntries(series.map((s) => [s.id, s.shape])), showAt: Object.fromEntries(series.map((s) => [s.id, s.showAt])), show: c.show !== false, position: COUNTER_POSITIONS.includes(c.position) ? c.position : "right", labels: COUNTER_LABELS.includes(c.labels) ? c.labels : "caps", }; } // The fact-check verdicts, as the tally always counted them. const given = isObj(f.verdicts) ? f.verdicts : {}; const tally = isObj(f.tally) ? f.tally : {}; const byId = {}; for (const v of VERDICTS) byId[v] = { ...VERDICT_DEFAULTS[v], ...(isObj(given[v]) ? given[v] : {}) }; const w = tally.showAt; const showAt = Object.fromEntries(VERDICTS.map((v) => [v, isObj(w) ? one(w[v]) : one(w)])); return { custom: false, series: VERDICTS.map((id) => ({ id, ...byId[id], showAt: showAt[id], shape: VERDICT_SHAPES[id] })), order: [...VERDICTS], byId, shapes: { ...VERDICT_SHAPES }, showAt, show: tally.show !== false, position: COUNTER_POSITIONS.includes(tally.position) ? tally.position : "right", labels: "caps", }; } /** The series ids a mark may name: the counter's, else the verdicts. */ export const counterOrder = (render) => resolveCounter(render).order; /** * Every reason a `deck.counter` cannot be drawn, as sentences (empty: it can). * Unknown keys are refused. `factcheck` is the cut's `render.chrome.factcheck`: * a counter beside the legacy `verdicts` or `tally` is refused (one home for * the series). `labelFits(series)` is the tally cell's fit check * (factcheck.mjs `tallyLabelOf`), handed in so this file stays free of it. * * @returns {string[]} */ export function validateCounter(c, { factcheck = undefined, labelFits = null, where = "render.chrome.deck.counter" } = {}) { if (c === undefined) return []; if (!isObj(c)) return [`${where} must be an object { series, show?, position?, showAt? }`]; const errors = []; for (const k of Object.keys(c)) if (!COUNTER_KEYS.includes(k)) errors.push(`${where}.${k} is not a counter setting (${COUNTER_KEYS.join(", ")})`); if (isObj(factcheck) && (factcheck.verdicts !== undefined || factcheck.tally !== undefined)) { errors.push(`${where} replaces render.chrome.factcheck.verdicts and .tally: set the series and the cells in one place`); } if (c.show !== undefined && typeof c.show !== "boolean") errors.push(`${where}.show must be true or false`); if (c.position !== undefined && !COUNTER_POSITIONS.includes(c.position)) { errors.push(`${where}.position must be ${COUNTER_POSITIONS.map((p) => `"${p}"`).join(" or ")}`); } if (c.showAt !== undefined && !COUNTER_SHOW_AT.includes(c.showAt)) { errors.push(`${where}.showAt must be ${COUNTER_SHOW_AT.map((p) => `"${p}"`).join(" or ")}`); } if (c.labels !== undefined && !COUNTER_LABELS.includes(c.labels)) { errors.push(`${where}.labels must be ${COUNTER_LABELS.map((p) => `"${p}"`).join(" or ")}`); } const written = c.labels === "as-written"; if (!Array.isArray(c.series) || !c.series.length) { errors.push(`${where}.series must list at least one series { id, label }`); return errors; } if (c.series.length > COUNTER_LIMITS.series) errors.push(`${where}.series holds ${c.series.length} (at most ${COUNTER_LIMITS.series})`); const seen = new Set(); c.series.forEach((s, i) => { const w = `${where}.series[${i}]`; if (!isObj(s)) { errors.push(`${w} must be { id, label, color? }`); return; } for (const k of Object.keys(s)) if (!SERIES_KEYS.includes(k)) errors.push(`${w}.${k} is not a series setting (${SERIES_KEYS.join(", ")})`); if (typeof s.id !== "string" || !SERIES_ID_RE.test(s.id)) { errors.push(`${w}.id must be a letter then letters, digits, _ or - (at most ${COUNTER_LIMITS.id})`); } else if (seen.has(s.id)) errors.push(`${w}.id ${s.id} is listed twice -- one series, one id`); else seen.add(s.id); if (!VERDICTS.includes(s.id) && s.label === undefined) errors.push(`${w}.label is needed: only a verdict series has a default label`); for (const key of ["label", "stampLabel", "tallyLabel"]) { const word = s[key]; if (word === undefined) continue; if (typeof word !== "string" || !word.trim()) errors.push(`${w}.${key} must be words, not empty`); else if (/[\r\n]/.test(word)) errors.push(`${w}.${key} must be one line`); else { // A label or cell word set as written may run to two lines; a stamp's word is one line, as ever. const max = written && key !== "stampLabel" ? COUNTER_LIMITS.writtenLabel : COUNTER_LIMITS.label; if (word.trim().length > max) errors.push(`${w}.${key} is ${word.trim().length} characters (at most ${max})`); } } if (s.color !== undefined && !(typeof s.color === "string" && VERDICT_COLOR_RE.test(s.color))) errors.push(`${w}.color must be a hex colour, #rgb or #rrggbb`); if (s.showAt !== undefined && !COUNTER_SHOW_AT.includes(s.showAt)) errors.push(`${w}.showAt must be ${COUNTER_SHOW_AT.map((p) => `"${p}"`).join(" or ")}`); if (s.shape !== undefined && !COUNTER_SHAPES.includes(s.shape)) errors.push(`${w}.shape must be one of ${COUNTER_SHAPES.join(", ")}`); if (s.unit !== undefined && !(typeof s.unit === "string" && s.unit.trim() && s.unit.trim().length <= COUNTER_LIMITS.unit)) { errors.push(`${w}.unit must be a word, 1 to ${COUNTER_LIMITS.unit} characters`); } if (labelFits && !errors.some((e) => e.startsWith(`${w}.`))) { const problem = labelFits(s, w, { n: c.series.length, labels: written ? "as-written" : "caps" }); if (problem) errors.push(problem); } }); return errors; } /** * The series a mark counts: its `counter`, else its `verdict` -- one name for * the same thing, `counter` the toolkit's, `verdict` the fact-check's. */ export const markSeries = (m) => (isObj(m) ? (m.counter ?? m.verdict) : undefined);