// presenter-grid.mjs — the presenter window's panels on their 12×12 // grid, as data (spec §5.4): reading and writing the cookie's // arrangement, every change a person can make to it taken as far as it // can go (clampPanels: on the grid, at least 2×2, never overlapping), // the keys and buttons that make them, and the stylesheet that places // the panels, the server's own (presenter_window.go's css). Pure: no // DOM, no words. presenter-arrange.mjs is the arranging that uses them. // Tested in presenter-grid.test.mjs. import { DEFAULT_PANELS } from './presenter-notes.mjs' export { DEFAULT_PANELS } export const GRID = 12 export const MIN_CELLS = 2 export const NAMES = ['now', 'next', 'notes'] // Where the panels are arranged at all; below it they stack. export const WIDE = '(min-width: 720px) and (min-height: 560px)' function overlaps (a, b) { return a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h } function inGrid (p) { return p.x >= 0 && p.y >= 0 && p.w >= MIN_CELLS && p.h >= MIN_CELLS && p.x + p.w <= GRID && p.y + p.h <= GRID } /** "x,y,w,h;x,y,w,h;x,y,w,h" read as three panels, or null for anything * the server would not believe either (off the grid, under 2×2, * overlapping). */ export function parsePanels (raw) { const rects = String(raw ?? '').split(';') if (rects.length !== NAMES.length) return null const out = [] for (const rect of rects) { const n = rect.split(',') if (n.length !== 4 || !n.every(v => /^[0-9]{1,2}$/.test(v))) return null const [x, y, w, h] = n.map(Number) const p = { x, y, w, h } if (!inGrid(p)) return null out.push(p) } for (let i = 0; i < out.length; i++) { for (let j = i + 1; j < out.length; j++) if (overlaps(out[i], out[j])) return null } return out } /** The panels as the cookie keeps them. */ export function formatPanels (panels) { return panels.map(p => [p.x, p.y, p.w, p.h].join(',')).join(';') } function cells (n) { const v = Math.round(Number(n) || 0) return Math.max(-GRID, Math.min(GRID, v)) } // walk moves panels[i] a cell at a time along each [key, delta] in // turn, each stopping at the first cell that would leave the grid, // shrink it under 2×2 or overlap another panel. function walk (panels, i, steps) { const out = panels.map(p => ({ ...p })) for (const [key, delta] of steps) { const dir = Math.sign(delta) for (let k = 0; k < Math.abs(delta); k++) { const next = { ...out[i], [key]: out[i][key] + dir } if (!inGrid(next) || out.some((q, j) => j !== i && overlaps(next, q))) break out[i] = next } } return out } /** * panels with panels[moved] changed by change, {dx, dy} to move it or * {dw, dh} to resize it from its far corner, in grid cells. A change is * taken as far as it can go and no further: the result is always on * the grid, inside it, at least 2×2 and free of overlaps. Of the two * ways to take a change (across then down, or down then across), the * one that ends nearer what was asked for. Pure: panels is not changed. */ export function clampPanels (panels, moved, change = {}) { if (!panels[moved]) return panels.map(p => ({ ...p })) const [a, b] = [['x', cells(change.dx)], ['y', cells(change.dy)]] const [c, d] = [['w', cells(change.dw)], ['h', cells(change.dh)]] const want = { ...panels[moved] } for (const [key, delta] of [a, b, c, d]) want[key] += delta const miss = out => ['x', 'y', 'w', 'h'].reduce((sum, k) => sum + Math.abs(out[moved][k] - want[k]), 0) const first = walk(panels, moved, [a, b, c, d]) const second = walk(panels, moved, [b, a, d, c]) return miss(second) < miss(first) ? second : first } /** What an arrow key does to the focused panel: moves it a cell, or * with Shift resizes it a cell; null for any other key. */ export function keyChange (key, shift) { const along = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[key] if (!along) return null const [x, y] = along if (shift) return x ? { dw: x } : { dh: y } return x ? { dx: x } : { dy: y } } const TOOLS = { left: { dx: -1 }, right: { dx: 1 }, up: { dy: -1 }, down: { dy: 1 }, narrower: { dw: -1 }, wider: { dw: 1 }, shorter: { dh: -1 }, taller: { dh: 1 } } /** What one of the selected panel's eight buttons does (its * data-arrange-op): one cell of one change; null for anything else. */ export function toolChange (op) { return Object.hasOwn(TOOLS, op) ? { ...TOOLS[op] } : null } /** Whether change can move panels[i] at all: a button that cannot is * disabled. */ export function canChange (panels, i, change) { if (!change || !panels[i]) return false return formatPanels(clampPanels(panels, i, change)) !== formatPanels(panels) } /** change as it applies in a right-to-left window (rtl), where the * grid's columns run from the right: a move across is mirrored, so * Move left still goes left. A button's resize keeps its meaning * (keepSize: Narrower narrows); an arrow key's is a direction, and is * mirrored with the moves. A copy: change is not altered. */ export function mirror (change, rtl, keepSize) { if (!change) return null const c = { ...change } if (rtl && c.dx) c.dx = -c.dx if (rtl && c.dw && !keepSize) c.dw = -c.dw return c } /** The live region's sentence for a panel: the catalog's pattern * (slides.present.arrange.where) with its name, its size in cells, and * its column and row counted from one. */ export function whereWords (pattern, name, p) { const v = { name, w: p.w, h: p.h, x: p.x + 1, y: p.y + 1 } return String(pattern || '').replace(/\{(\w+)\}/g, (all, k) => (k in v ? String(v[k]) : all)) } /** The stylesheet that places panels: presenter_window.go's css, less * the notes' size, which the notes keep themselves. The default window * leaves Next room for half of Now and draws it at half; an arranged * one lets Next fill its own panel. */ export function arrangeCSS (panels) { const lines = [`@media ${WIDE} {`] panels.forEach((p, i) => { lines.push(` [presenter-panel="${NAMES[i]}"] { grid-area: ${p.y + 1} / ${p.x + 1} / span ${p.h} / span ${p.w}; }`) }) const [now, next] = panels let room = `${now.w} / ${GRID} * 100cqw - 32px, (${now.h} / ${GRID} * 100cqh - 16px) * 16 / 9` const plain = formatPanels(panels) === DEFAULT_PANELS if (plain) room += `, (${next.h} / ${GRID} * 100cqh - 48px) * 2 * 16 / 9` lines.push(` [presenter-panel] { --p-now: min(${room}); }`) lines.push(` [presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9${plain ? ', var(--p-now) / 2' : ''}); }`) lines.push('}') return lines.join('\n') + '\n' }