// THE SHELF'S SETTINGS (weight video, variant A): a strip of the frame cleared // beside the footage -- under it, or over it -- for whatever the video wants in // view without standing on the picture: the diet's dated tags, the streak // chain, a goal stack, an entry's note. `render.chrome.deck.shelf`: // // { show, side: "below" | "above", items: [...], gap, finale: "hide" | "show" } // // Absent (or false), there is no shelf and the footage stands where it always // did. On, deck.mjs deckGeometry moves the footage to the frame's top (`below`) // or down onto the deck (`above`), and the strip it leaves is the shelf; its // occupants (measure-extras.mjs shelfLayout) stand in it left to right in // `items` order, each at its own width scaled to the shelf's height. // // A LEAF: no imports. deck.mjs and measure-extras-settings.mjs read it, and both // are read by files it must not cycle through. /** Where the shelf stands: under the footage (the footage at the frame's top), or over it (the footage on the deck). */ export const SHELF_SIDES = Object.freeze(["below", "above"]); /** What the shelf does over the finale's window: hidden under it (its occupants with it), or drawn over it. */ export const SHELF_FINALE = Object.freeze(["hide", "show"]); /** What may stand on the shelf: the measure extras that take `position: "shelf"`, and an entry's `shelfNote`. */ export const SHELF_ITEMS = Object.freeze(["diet", "streak", "goals", "peak", "note"]); /** The measure extras among them. */ export const SHELF_EXTRAS = Object.freeze(SHELF_ITEMS.filter((i) => i !== "note")); /** The least room the shelf needs, frame px (at 1080): less, and the footage must shrink. */ export const SHELF_MIN = 72; /** An entry's note on the shelf (`shelfNote`): at most this many characters. */ export const SHELF_NOTE_MAX = 80; /** Bounds: the air between two occupants, design px. */ export const SHELF_LIMITS = Object.freeze({ gap: Object.freeze([0, 64]) }); /** The shelf's settings and their defaults (`items` null: the extras whose position is "shelf", then the note). */ export const SHELF_DEFAULTS = Object.freeze({ show: true, side: "below", items: null, gap: 16, finale: "hide" }); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** * The shelf with its defaults filled, or null when there is none (absent, * `false`, `show: false`). `items` resolved: the list as written, or -- unset * -- the measure extras whose own `position` is "shelf" (in SHELF_ITEMS order), * then `note`. * * @returns {{ show: true, side: "below" | "above", items: string[], gap: number, finale: "hide" | "show" } | null} */ export function resolveShelf(render) { const deck = render?.chrome?.deck; const v = isObj(deck) ? deck.shelf : undefined; if (v === undefined || v === null || v === false) return null; const own = isObj(v) ? v : {}; if (own.show === false) return null; const items = Array.isArray(own.items) ? own.items.filter((i) => SHELF_ITEMS.includes(i)) : [...SHELF_EXTRAS.filter((id) => isObj(deck[id]) && deck[id].position === "shelf"), "note"]; return { show: true, side: SHELF_SIDES.includes(own.side) ? own.side : SHELF_DEFAULTS.side, items, gap: typeof own.gap === "number" && Number.isFinite(own.gap) ? own.gap : SHELF_DEFAULTS.gap, finale: SHELF_FINALE.includes(own.finale) ? own.finale : SHELF_DEFAULTS.finale, }; } /** Is `id` (a measure extra) on the shelf: the shelf on and its items naming it. */ export const onShelf = (id, render) => !!resolveShelf(render)?.items.includes(id); /** * Every reason `render.chrome.deck.shelf` cannot be drawn, as sentences: its * own keys, and against the deck it sits in -- an item that puts a measure * extra on the shelf while the extra's `position` names somewhere else, or one * whose position is "shelf" that an explicit `items` leaves out. (The room it * needs is the deck's to check: deck.mjs validateChrome, with the geometry.) * * @returns {string[]} */ export function validateShelf(v, where = "render.chrome.deck.shelf", chrome = {}) { if (v === undefined || typeof v === "boolean") return []; const keys = Object.keys(SHELF_DEFAULTS); if (!isObj(v)) return [`${where} must be true, false or { ${keys.join(", ")} }`]; const errors = []; for (const k of Object.keys(v)) if (!keys.includes(k)) errors.push(`${where}.${k} is not a shelf setting (${keys.join(", ")})`); if (v.show !== undefined && typeof v.show !== "boolean") errors.push(`${where}.show must be true or false`); if (v.side !== undefined && !SHELF_SIDES.includes(v.side)) errors.push(`${where}.side must be "below" or "above"`); if (v.finale !== undefined && !SHELF_FINALE.includes(v.finale)) errors.push(`${where}.finale must be "hide" or "show"`); const [lo, hi] = SHELF_LIMITS.gap; if (v.gap !== undefined && !(typeof v.gap === "number" && Number.isFinite(v.gap) && v.gap >= lo && v.gap <= hi)) { errors.push(`${where}.gap must be from ${lo} to ${hi} (design px between two items)`); } if (v.items !== undefined && v.items !== null) { if (!Array.isArray(v.items) || !v.items.every((i) => SHELF_ITEMS.includes(i)) || new Set(v.items).size !== v.items.length) { errors.push(`${where}.items must be a list of distinct items (${SHELF_ITEMS.join(", ")})`); } else if (v.show !== false) { const deck = isObj(chrome?.deck) ? chrome.deck : {}; for (const id of SHELF_EXTRAS) { const pos = isObj(deck[id]) ? deck[id].position : undefined; if (v.items.includes(id) && pos !== undefined && pos !== "shelf") { errors.push(`${where}.items puts ${id} on the shelf, but render.chrome.deck.${id}.position is "${pos}" (position "shelf", or take it out of items)`); } if (!v.items.includes(id) && pos === "shelf") { errors.push(`render.chrome.deck.${id}.position is "shelf", but ${where}.items leaves it out (add it, or another position)`); } } } } return errors; } /** * An entry's `shelfNote` as written: `{ value, error }` -- the words trimmed * (null for none: the key removed), `error` a sentence when they are not one * line of at most SHELF_NOTE_MAX characters. * * @returns {{ value: string | null, error: string | null }} */ export function parseShelfNote(v) { if (v === null || v === undefined || v === "" || v === false) return { value: null, error: null }; if (typeof v !== "string") return { value: null, error: `shelfNote is words, not ${JSON.stringify(v)}` }; const s = v.trim(); if (!s) return { value: null, error: null }; if (/[\r\n]/.test(s)) return { value: null, error: "shelfNote must be one line" }; if ([...s].length > SHELF_NOTE_MAX) return { value: null, error: `shelfNote is ${[...s].length} characters (at most ${SHELF_NOTE_MAX})` }; return { value: s, error: null }; }