oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// presenter-grid.mjs — the presenter window's panels on their 12×12
2// grid, as data (spec §5.4): reading and writing the cookie's
3// arrangement, every change a person can make to it taken as far as it
4// can go (clampPanels: on the grid, at least 2×2, never overlapping),
5// the keys and buttons that make them, and the stylesheet that places
6// the panels, the server's own (presenter_window.go's css). Pure: no
7// DOM, no words. presenter-arrange.mjs is the arranging that uses them.
8// Tested in presenter-grid.test.mjs.
9import { DEFAULT_PANELS } from './presenter-notes.mjs'
10
11export { DEFAULT_PANELS }
12export const GRID = 12
13export const MIN_CELLS = 2
14export const NAMES = ['now', 'next', 'notes']
15// Where the panels are arranged at all; below it they stack.
16export const WIDE = '(min-width: 720px) and (min-height: 560px)'
17
18function overlaps (a, b) {
19 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
20}
21
22function inGrid (p) {
23 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
24}
25
26/** "x,y,w,h;x,y,w,h;x,y,w,h" read as three panels, or null for anything
27 * the server would not believe either (off the grid, under 2×2,
28 * overlapping). */
29export function parsePanels (raw) {
30 const rects = String(raw ?? '').split(';')
31 if (rects.length !== NAMES.length) return null
32 const out = []
33 for (const rect of rects) {
34 const n = rect.split(',')
35 if (n.length !== 4 || !n.every(v => /^[0-9]{1,2}$/.test(v))) return null
36 const [x, y, w, h] = n.map(Number)
37 const p = { x, y, w, h }
38 if (!inGrid(p)) return null
39 out.push(p)
40 }
41 for (let i = 0; i < out.length; i++) {
42 for (let j = i + 1; j < out.length; j++) if (overlaps(out[i], out[j])) return null
43 }
44 return out
45}
46
47/** The panels as the cookie keeps them. */
48export function formatPanels (panels) {
49 return panels.map(p => [p.x, p.y, p.w, p.h].join(',')).join(';')
50}
51
52function cells (n) {
53 const v = Math.round(Number(n) || 0)
54 return Math.max(-GRID, Math.min(GRID, v))
55}
56
57// walk moves panels[i] a cell at a time along each [key, delta] in
58// turn, each stopping at the first cell that would leave the grid,
59// shrink it under 2×2 or overlap another panel.
60function walk (panels, i, steps) {
61 const out = panels.map(p => ({ ...p }))
62 for (const [key, delta] of steps) {
63 const dir = Math.sign(delta)
64 for (let k = 0; k < Math.abs(delta); k++) {
65 const next = { ...out[i], [key]: out[i][key] + dir }
66 if (!inGrid(next) || out.some((q, j) => j !== i && overlaps(next, q))) break
67 out[i] = next
68 }
69 }
70 return out
71}
72
73/**
74 * panels with panels[moved] changed by change, {dx, dy} to move it or
75 * {dw, dh} to resize it from its far corner, in grid cells. A change is
76 * taken as far as it can go and no further: the result is always on
77 * the grid, inside it, at least 2×2 and free of overlaps. Of the two
78 * ways to take a change (across then down, or down then across), the
79 * one that ends nearer what was asked for. Pure: panels is not changed.
80 */
81export function clampPanels (panels, moved, change = {}) {
82 if (!panels[moved]) return panels.map(p => ({ ...p }))
83 const [a, b] = [['x', cells(change.dx)], ['y', cells(change.dy)]]
84 const [c, d] = [['w', cells(change.dw)], ['h', cells(change.dh)]]
85 const want = { ...panels[moved] }
86 for (const [key, delta] of [a, b, c, d]) want[key] += delta
87 const miss = out => ['x', 'y', 'w', 'h'].reduce((sum, k) => sum + Math.abs(out[moved][k] - want[k]), 0)
88 const first = walk(panels, moved, [a, b, c, d])
89 const second = walk(panels, moved, [b, a, d, c])
90 return miss(second) < miss(first) ? second : first
91}
92
93/** What an arrow key does to the focused panel: moves it a cell, or
94 * with Shift resizes it a cell; null for any other key. */
95export function keyChange (key, shift) {
96 const along = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[key]
97 if (!along) return null
98 const [x, y] = along
99 if (shift) return x ? { dw: x } : { dh: y }
100 return x ? { dx: x } : { dy: y }
101}
102
103const TOOLS = {
104 left: { dx: -1 },
105 right: { dx: 1 },
106 up: { dy: -1 },
107 down: { dy: 1 },
108 narrower: { dw: -1 },
109 wider: { dw: 1 },
110 shorter: { dh: -1 },
111 taller: { dh: 1 }
112}
113
114/** What one of the selected panel's eight buttons does (its
115 * data-arrange-op): one cell of one change; null for anything else. */
116export function toolChange (op) {
117 return Object.hasOwn(TOOLS, op) ? { ...TOOLS[op] } : null
118}
119
120/** Whether change can move panels[i] at all: a button that cannot is
121 * disabled. */
122export function canChange (panels, i, change) {
123 if (!change || !panels[i]) return false
124 return formatPanels(clampPanels(panels, i, change)) !== formatPanels(panels)
125}
126
127/** change as it applies in a right-to-left window (rtl), where the
128 * grid's columns run from the right: a move across is mirrored, so
129 * Move left still goes left. A button's resize keeps its meaning
130 * (keepSize: Narrower narrows); an arrow key's is a direction, and is
131 * mirrored with the moves. A copy: change is not altered. */
132export function mirror (change, rtl, keepSize) {
133 if (!change) return null
134 const c = { ...change }
135 if (rtl && c.dx) c.dx = -c.dx
136 if (rtl && c.dw && !keepSize) c.dw = -c.dw
137 return c
138}
139
140/** The live region's sentence for a panel: the catalog's pattern
141 * (slides.present.arrange.where) with its name, its size in cells, and
142 * its column and row counted from one. */
143export function whereWords (pattern, name, p) {
144 const v = { name, w: p.w, h: p.h, x: p.x + 1, y: p.y + 1 }
145 return String(pattern || '').replace(/\{(\w+)\}/g, (all, k) => (k in v ? String(v[k]) : all))
146}
147
148/** The stylesheet that places panels: presenter_window.go's css, less
149 * the notes' size, which the notes keep themselves. The default window
150 * leaves Next room for half of Now and draws it at half; an arranged
151 * one lets Next fill its own panel. */
152export function arrangeCSS (panels) {
153 const lines = [`@media ${WIDE} {`]
154 panels.forEach((p, i) => {
155 lines.push(` [presenter-panel="${NAMES[i]}"] { grid-area: ${p.y + 1} / ${p.x + 1} / span ${p.h} / span ${p.w}; }`)
156 })
157 const [now, next] = panels
158 let room = `${now.w} / ${GRID} * 100cqw - 32px, (${now.h} / ${GRID} * 100cqh - 16px) * 16 / 9`
159 const plain = formatPanels(panels) === DEFAULT_PANELS
160 if (plain) room += `, (${next.h} / ${GRID} * 100cqh - 48px) * 2 * 16 / 9`
161 lines.push(` [presenter-panel] { --p-now: min(${room}); }`)
162 lines.push(` [presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9${plain ? ', var(--p-now) / 2' : ''}); }`)
163 lines.push('}')
164 return lines.join('\n') + '\n'
165}
166