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 panels on their 12×12 grid, as a person
2// moves and resizes them, the keys that do it, and the stylesheet that
3// places them (the server's own, presenter_window.go's css).
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { clampPanels, parsePanels, formatPanels, keyChange, toolChange, canChange, whereWords, mirror, arrangeCSS, DEFAULT_PANELS } from './presenter-grid.mjs'
7
8const defaults = parsePanels(DEFAULT_PANELS)
9// Room to move: Now 4×4 at the top left, Next 4×4 below it, the notes
10// 4×12 on the right, and the middle of the grid empty.
11const roomy = parsePanels('0,0,4,4;0,4,4,4;8,0,4,12')
12
13function valid (panels) {
14 for (const p of panels) {
15 assert.ok(p.w >= 2 && p.h >= 2, 'at least 2×2: ' + JSON.stringify(p))
16 assert.ok(p.x >= 0 && p.y >= 0 && p.x + p.w <= 12 && p.y + p.h <= 12, 'inside the grid: ' + JSON.stringify(p))
17 }
18 for (let i = 0; i < panels.length; i++) {
19 for (let j = i + 1; j < panels.length; j++) {
20 const a = panels[i]
21 const b = panels[j]
22 assert.ok(!(a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h),
23 'no overlap: ' + JSON.stringify([a, b]))
24 }
25 }
26}
27
28test('the cookie\'s panels read and write back the same', () => {
29 assert.deepEqual(defaults, [{ x: 0, y: 0, w: 7, h: 8 }, { x: 0, y: 8, w: 7, h: 4 }, { x: 7, y: 0, w: 5, h: 12 }])
30 assert.equal(formatPanels(defaults), DEFAULT_PANELS)
31 assert.equal(DEFAULT_PANELS, '0,0,7,8;0,8,7,4;7,0,5,12')
32 for (const bad of ['', 'x', '0,0,7,8;0,8,7,4', '0,0,7,8;0,8,7,4;7,0,5,13', '0,0,1,8;0,8,7,4;7,0,5,12',
33 '0,0,7,8;0,7,7,4;7,0,5,12', '0,0,7,8;0,8,7,4;7,0,5,12;1,1,2,2', '-1,0,7,8;0,8,7,4;7,0,5,12', '0,0,7.5,8;0,8,7,4;7,0,5,12']) {
34 assert.equal(parsePanels(bad), null, bad)
35 }
36})
37
38test('a panel moves by whole cells into free room', () => {
39 const out = clampPanels(roomy, 0, { dx: 4, dy: 1 })
40 assert.deepEqual(out[0], { x: 4, y: 1, w: 4, h: 4 })
41 assert.deepEqual(out[1], roomy[1])
42 assert.deepEqual(out[2], roomy[2])
43 valid(out)
44 assert.deepEqual(roomy[0], { x: 0, y: 0, w: 4, h: 4 }, 'the panels given are not changed')
45})
46
47test('a move stops at the edge of the grid', () => {
48 const out = clampPanels(roomy, 0, { dx: -5, dy: -3 })
49 assert.deepEqual(out[0], { x: 0, y: 0, w: 4, h: 4 })
50 const down = clampPanels(parsePanels('0,0,4,4;4,8,4,4;8,0,4,4'), 0, { dy: 40 })
51 assert.deepEqual(down[0], { x: 0, y: 8, w: 4, h: 4 })
52 valid(down)
53})
54
55test('a move goes round a panel in its way when it can', () => {
56 // Down then across is blocked by Next; across then down is not.
57 assert.deepEqual(clampPanels(roomy, 0, { dx: 4, dy: 2 })[0], { x: 4, y: 2, w: 4, h: 4 })
58 // Across then down is blocked by the notes; down then across is not.
59 const tall = parsePanels('0,0,4,4;4,0,4,4;8,0,4,4')
60 assert.deepEqual(clampPanels(tall, 1, { dx: 4, dy: 4 })[1], { x: 8, y: 4, w: 4, h: 4 })
61})
62
63test('a move stops at the panel in its way, never over it', () => {
64 // Now moving right runs into the notes at column 8.
65 const out = clampPanels(roomy, 0, { dx: 7 })
66 assert.deepEqual(out[0], { x: 4, y: 0, w: 4, h: 4 })
67 valid(out)
68 // Next moving up runs into Now.
69 const up = clampPanels(roomy, 1, { dy: -3 })
70 assert.deepEqual(up[1], roomy[1])
71 // The default window is full: nothing moves until something shrinks.
72 for (let i = 0; i < 3; i++) {
73 for (const change of [{ dx: 1 }, { dx: -1 }, { dy: 1 }, { dy: -1 }]) {
74 const moved = clampPanels(defaults, i, change)
75 assert.deepEqual(moved, defaults, `panel ${i} ${JSON.stringify(change)}`)
76 }
77 }
78})
79
80test('a resize keeps at least 2×2, inside the grid, clear of the others', () => {
81 assert.deepEqual(clampPanels(roomy, 0, { dw: -9, dh: -9 })[0], { x: 0, y: 0, w: 2, h: 2 })
82 assert.deepEqual(clampPanels(roomy, 2, { dw: 5 })[2], { x: 8, y: 0, w: 4, h: 12 })
83 // Now grows right to the notes and down to Next, no further.
84 const grown = clampPanels(roomy, 0, { dw: 9, dh: 9 })
85 assert.deepEqual(grown[0], { x: 0, y: 0, w: 8, h: 4 })
86 valid(grown)
87 const shrunk = clampPanels(defaults, 0, { dw: -2, dh: -3 })
88 assert.deepEqual(shrunk[0], { x: 0, y: 0, w: 5, h: 5 })
89 valid(shrunk)
90})
91
92test('every change from every arrangement stays a valid window', () => {
93 const starts = [defaults, roomy, parsePanels('5,5,2,2;0,0,2,2;10,10,2,2')]
94 const changes = []
95 for (const d of [-13, -3, -1, 0, 1, 2, 13]) {
96 changes.push({ dx: d }, { dy: d }, { dw: d }, { dh: d }, { dx: d, dy: -d }, { dw: d, dh: d })
97 }
98 for (const start of starts) {
99 for (let i = 0; i < 3; i++) {
100 for (const change of changes) valid(clampPanels(start, i, change))
101 }
102 }
103})
104
105test('arrow keys move a panel a cell; with Shift they resize it', () => {
106 assert.deepEqual(keyChange('ArrowLeft', false), { dx: -1 })
107 assert.deepEqual(keyChange('ArrowRight', false), { dx: 1 })
108 assert.deepEqual(keyChange('ArrowUp', false), { dy: -1 })
109 assert.deepEqual(keyChange('ArrowDown', false), { dy: 1 })
110 assert.deepEqual(keyChange('ArrowLeft', true), { dw: -1 })
111 assert.deepEqual(keyChange('ArrowRight', true), { dw: 1 })
112 assert.deepEqual(keyChange('ArrowUp', true), { dh: -1 })
113 assert.deepEqual(keyChange('ArrowDown', true), { dh: 1 })
114 assert.equal(keyChange('PageDown', false), null)
115 assert.equal(keyChange('b', false), null)
116})
117
118test('the stylesheet places the panels as the server does', () => {
119 const css = arrangeCSS(roomy)
120 assert.match(css, /^@media \(min-width: 720px\) and \(min-height: 560px\) \{/)
121 assert.ok(css.includes('[presenter-panel="now"] { grid-area: 1 / 1 / span 4 / span 4; }'))
122 assert.ok(css.includes('[presenter-panel="next"] { grid-area: 5 / 1 / span 4 / span 4; }'))
123 assert.ok(css.includes('[presenter-panel="notes"] { grid-area: 1 / 9 / span 12 / span 4; }'))
124 assert.ok(css.includes('[presenter-panel] { --p-now: min(4 / 12 * 100cqw - 32px, (4 / 12 * 100cqh - 16px) * 16 / 9); }'))
125 // Arranged, Next fills its own panel.
126 assert.ok(css.includes('[presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9); }'))
127
128 // The default leaves Next room for half of Now, and Next is half.
129 const def = arrangeCSS(defaults)
130 assert.ok(def.includes('[presenter-panel] { --p-now: min(7 / 12 * 100cqw - 32px, (8 / 12 * 100cqh - 16px) * 16 / 9, (4 / 12 * 100cqh - 48px) * 2 * 16 / 9); }'))
131 assert.ok(def.includes('[presenter-panel="next"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9, var(--p-now) / 2); }'))
132})
133
134test('each of the eight buttons is one cell of one change', () => {
135 assert.deepEqual(toolChange('left'), { dx: -1 })
136 assert.deepEqual(toolChange('right'), { dx: 1 })
137 assert.deepEqual(toolChange('up'), { dy: -1 })
138 assert.deepEqual(toolChange('down'), { dy: 1 })
139 assert.deepEqual(toolChange('narrower'), { dw: -1 })
140 assert.deepEqual(toolChange('wider'), { dw: 1 })
141 assert.deepEqual(toolChange('shorter'), { dh: -1 })
142 assert.deepEqual(toolChange('taller'), { dh: 1 })
143 assert.equal(toolChange('sideways'), null)
144})
145
146test('a button is offered only where its change can be made', () => {
147 assert.equal(canChange(defaults, 0, toolChange('narrower')), true)
148 assert.equal(canChange(defaults, 0, toolChange('shorter')), true)
149 for (const op of ['left', 'right', 'up', 'down', 'wider', 'taller']) {
150 assert.equal(canChange(defaults, 0, toolChange(op)), false, op)
151 }
152 assert.equal(canChange(roomy, 0, toolChange('right')), true)
153 assert.equal(canChange(roomy, 0, null), false)
154})
155
156test('the live region says the panel\'s size and place, counted from one', () => {
157 const where = '{name}: {w} by {h}, column {x}, row {y}'
158 assert.equal(whereWords(where, 'Coming next', { x: 0, y: 5, w: 7, h: 4 }), 'Coming next: 7 by 4, column 1, row 6')
159 assert.equal(whereWords(where, 'Speaker notes', { x: 7, y: 0, w: 5, h: 12 }), 'Speaker notes: 5 by 12, column 8, row 1')
160})
161
162test('right to left, moves are mirrored and the buttons\' sizes keep their meaning', () => {
163 assert.deepEqual(mirror(toolChange('left'), true, true), { dx: 1 })
164 assert.deepEqual(mirror(toolChange('right'), true, true), { dx: -1 })
165 assert.deepEqual(mirror(toolChange('narrower'), true, true), { dw: -1 })
166 assert.deepEqual(mirror(toolChange('wider'), true, true), { dw: 1 })
167 assert.deepEqual(mirror(toolChange('up'), true, true), { dy: -1 })
168 // An arrow key is a direction: Shift+ArrowLeft pulls the far edge,
169 // which is on the left, further left.
170 assert.deepEqual(mirror(keyChange('ArrowLeft', true), true, false), { dw: 1 })
171 assert.deepEqual(mirror(keyChange('ArrowLeft', false), true, false), { dx: 1 })
172 // Left to right, nothing changes.
173 assert.deepEqual(mirror(toolChange('left'), false, true), { dx: -1 })
174 assert.deepEqual(mirror(keyChange('ArrowRight', true), false, false), { dw: 1 })
175 assert.equal(mirror(null, true, true), null)
176 const c = toolChange('left')
177 mirror(c, true, true)
178 assert.deepEqual(c, { dx: -1 }, 'the change given is not altered')
179})
180