oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// notes-card.mjs: the notes as a card at the foot of the desk, closed
2// to a text tab that counts their words, its height adjustable and
3// remembered in cookies the server reads (slides-notes,
4// slides-notes-height).
5import test from 'node:test'
6import assert from 'node:assert/strict'
7import { mountNotesCard, clampHeight, countWords, NOTES_KEY, NOTES_HEIGHT_KEY, NOTES_COOKIE, NOTES_HEIGHT_COOKIE, HEAD_PX, DEFAULT_HEIGHT, STEP } from './notes-card.mjs'
8
9// A stand-in element: attributes, dataset, style properties, listeners
10// and focus, which is all the module touches.
11function el (attrs = {}) {
12 const e = {
13 attrs: new Map(Object.entries(attrs)),
14 dataset: {},
15 props: {},
16 listeners: {},
17 hidden: false,
18 clientHeight: 0,
19 style: { setProperty (k, v) { e.props[k] = v } },
20 getAttribute: k => e.attrs.has(k) ? e.attrs.get(k) : null,
21 setAttribute: (k, v) => e.attrs.set(k, String(v)),
22 hasAttribute: k => e.attrs.has(k),
23 removeAttribute: k => e.attrs.delete(k),
24 toggleAttribute (k, on) { if (on) e.attrs.set(k, ''); else e.attrs.delete(k); return on },
25 addEventListener: (type, fn) => { (e.listeners[type] ||= []).push(fn) },
26 focus () { globalThis.document.activeElement = e },
27 setPointerCapture (id) { e.captured = id },
28 releasePointerCapture () { e.captured = null },
29 hasPointerCapture: id => e.captured === id,
30 fire (type, init = {}) {
31 const ev = { type, defaultPrevented: false, preventDefault () { ev.defaultPrevented = true }, ...init }
32 for (const fn of e.listeners[type] || []) fn(ev)
33 return ev
34 }
35 }
36 return e
37}
38
39function fakePane ({ deskPx = 600, state = 'open' } = {}) {
40 const parts = {
41 pane: el(),
42 open: el({ 'data-label': 'Notes', 'data-label-words': 'Notes · {n} words' }),
43 close: el(),
44 resize: el(),
45 notes: el()
46 }
47 const sel = {
48 '[slides-notes-pane]': parts.pane,
49 '[slides-notes-open]': parts.open,
50 '[slides-notes-close]': parts.close,
51 '[slides-notes-resize]': parts.resize,
52 '[slides-notes]': parts.notes
53 }
54 const root = el()
55 root.clientHeight = deskPx
56 root.querySelector = q => sel[q] || null
57 root.closest = () => null
58 parts.root = root
59 // The server's first paint: the state it read from the cookie.
60 parts.pane.dataset.state = state
61 return parts
62}
63
64function fakeStorage (init = {}) {
65 const m = new Map(Object.entries(init))
66 return {
67 m,
68 getItem: k => m.has(k) ? m.get(k) : null,
69 setItem: (k, v) => m.set(k, String(v)),
70 removeItem: k => m.delete(k)
71 }
72}
73
74// document.cookie as a browser has it: each write sets one cookie (its
75// attributes are kept to be checked), each read is every name=value.
76function fakeJar (init = {}) {
77 const m = new Map(Object.entries(init))
78 const written = []
79 return {
80 m,
81 written,
82 get cookie () { return [...m].map(([k, v]) => k + '=' + v).join('; ') },
83 set cookie (line) {
84 written.push(line)
85 const [pair] = line.split(';')
86 const at = pair.indexOf('=')
87 m.set(pair.slice(0, at), pair.slice(at + 1))
88 }
89 }
90}
91const cookie = name => globalThis.document.m.get(name) ?? null
92
93test.beforeEach(() => {
94 globalThis.document = Object.assign(fakeJar(), { activeElement: null })
95 globalThis.innerHeight = 10000
96 globalThis.getComputedStyle = () => ({ lineHeight: '24px' })
97 globalThis.addEventListener = () => {}
98})
99
100test('the keys are the ones the switch used, and the new height key', () => {
101 assert.equal(NOTES_KEY, 'slides.editor.notes')
102 assert.equal(NOTES_HEIGHT_KEY, 'slides.editor.notes.height')
103 assert.equal(DEFAULT_HEIGHT, 144)
104 assert.equal(STEP, 16)
105})
106
107test('the cookies are the ones the server reads', () => {
108 assert.equal(NOTES_COOKIE, 'slides-notes')
109 assert.equal(NOTES_HEIGHT_COOKIE, 'slides-notes-height')
110})
111
112test('open, close and toggle set data-state and remember open/closed in the cookie', () => {
113 const p = fakePane()
114 const storage = fakeStorage()
115 const card = mountNotesCard({ root: p.root, storage, words: () => 0 })
116 assert.equal(p.pane.dataset.state, 'open')
117 assert.equal(card.isOpen(), true)
118 card.close()
119 assert.equal(p.pane.dataset.state, 'closed')
120 assert.equal(cookie(NOTES_COOKIE), 'closed')
121 assert.equal(card.isOpen(), false)
122 card.open()
123 assert.equal(p.pane.dataset.state, 'open')
124 assert.equal(cookie(NOTES_COOKIE), 'open')
125 card.toggle()
126 assert.equal(p.pane.dataset.state, 'closed')
127 assert.equal(cookie(NOTES_COOKIE), 'closed')
128 card.toggle()
129 assert.equal(cookie(NOTES_COOKIE), 'open')
130 assert.equal(storage.getItem(NOTES_KEY), null, 'nothing in localStorage any more')
131 assert.match(document.written.at(-1), /^slides-notes=open; Path=\/; Max-Age=31536000; SameSite=Lax$/)
132})
133
134test('mounting keeps the state the server painted and writes nothing', () => {
135 const p = fakePane({ state: 'closed' })
136 const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 })
137 assert.equal(p.pane.dataset.state, 'closed')
138 assert.equal(card.isOpen(), false)
139 const q = fakePane({ state: 'open' })
140 mountNotesCard({ root: q.root, storage: fakeStorage(), words: () => 0 })
141 assert.equal(q.pane.dataset.state, 'open')
142 assert.deepEqual(document.written, [])
143})
144
145test('a localStorage preference from before the cookies is honoured once, moved to the cookies and removed', () => {
146 const p = fakePane({ state: 'open' })
147 const storage = fakeStorage({ [NOTES_KEY]: 'off', [NOTES_HEIGHT_KEY]: '220' })
148 const card = mountNotesCard({ root: p.root, storage, words: () => 0 })
149 assert.equal(card.isOpen(), false)
150 assert.equal(p.pane.dataset.state, 'closed')
151 assert.equal(p.pane.props['--slides-notes-block-size'], '220px')
152 assert.equal(cookie(NOTES_COOKIE), 'closed')
153 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '220')
154 assert.equal(storage.getItem(NOTES_KEY), null)
155 assert.equal(storage.getItem(NOTES_HEIGHT_KEY), null)
156
157 // Where the cookies are already set, they win; the old keys still go.
158 globalThis.document = Object.assign(fakeJar({ [NOTES_COOKIE]: 'open', [NOTES_HEIGHT_COOKIE]: '180' }), { activeElement: null })
159 const q = fakePane({ state: 'open' })
160 const qs = fakeStorage({ [NOTES_KEY]: 'off', [NOTES_HEIGHT_KEY]: '220' })
161 const qc = mountNotesCard({ root: q.root, storage: qs, words: () => 0 })
162 assert.equal(qc.isOpen(), true)
163 assert.equal(q.pane.props['--slides-notes-block-size'], '180px')
164 assert.equal(qs.getItem(NOTES_KEY), null)
165 assert.equal(qs.getItem(NOTES_HEIGHT_KEY), null)
166 assert.deepEqual(document.written, [])
167})
168
169test('cookies and storage that throw leave the notes working for the visit', () => {
170 globalThis.document = {
171 activeElement: null,
172 get cookie () { throw new Error('blocked') },
173 set cookie (v) { throw new Error('blocked') }
174 }
175 const p = fakePane()
176 const bad = { getItem () { throw new Error('blocked') }, setItem () { throw new Error('blocked') }, removeItem () { throw new Error('blocked') } }
177 const card = mountNotesCard({ root: p.root, storage: bad, words: () => 0 })
178 assert.equal(card.isOpen(), true)
179 card.close()
180 assert.equal(card.isOpen(), false)
181 assert.equal(p.pane.dataset.state, 'closed')
182 p.resize.fire('keydown', { key: 'ArrowUp' })
183 assert.equal(p.resize.getAttribute('aria-valuenow'), '160')
184})
185
186test('the × closes and moves focus to the tab; the tab opens and moves focus into the notes', () => {
187 const p = fakePane()
188 const changes = []
189 mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0, onChange: open => changes.push(open) })
190 p.close.fire('click')
191 assert.equal(p.pane.dataset.state, 'closed')
192 assert.equal(document.activeElement, p.open)
193 p.open.fire('click')
194 assert.equal(p.pane.dataset.state, 'open')
195 assert.equal(document.activeElement, p.notes)
196 assert.deepEqual(changes, [false, true])
197})
198
199test('mounting does not take focus', () => {
200 const p = fakePane()
201 mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 })
202 assert.equal(document.activeElement, null)
203})
204
205test('HEAD_PX is the card less its body as editor.css draws it: a hairline (1 + 1) and 12px of padding above and below', () => {
206 assert.equal(HEAD_PX, 1 + 1 + 12 + 12)
207})
208
209test('clampHeight: at least two lines and the head, at most half the desk', () => {
210 const min = HEAD_PX + 2 * 24
211 assert.equal(clampHeight(10, 600, 24), 74)
212 assert.equal(clampHeight(10, 600, 24), min)
213 assert.equal(clampHeight(144, 600, 24), 144)
214 assert.equal(clampHeight(500, 600, 24), 300)
215 // A desk too short for both: the two lines win.
216 assert.equal(clampHeight(500, 100, 24), min)
217 // No desk measured: no upper bound.
218 assert.equal(clampHeight(900, 0, 24), 900)
219 assert.ok(clampHeight(Number.NaN, 600, 24) >= min)
220})
221
222test('the height is the remembered one, clamped, written as a CSS length and as the separator\'s value', () => {
223 const p = fakePane({ deskPx: 600 })
224 document.m.set(NOTES_HEIGHT_COOKIE, '220')
225 mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 })
226 assert.equal(p.pane.props['--slides-notes-block-size'], '220px')
227 assert.equal(p.resize.getAttribute('aria-valuenow'), '220')
228 assert.equal(p.resize.getAttribute('aria-valuemin'), String(HEAD_PX + 48))
229 assert.equal(p.resize.getAttribute('aria-valuemax'), '300')
230
231 const q = fakePane({ deskPx: 600 })
232 document.m.set(NOTES_HEIGHT_COOKIE, '9000')
233 mountNotesCard({ root: q.root, storage: fakeStorage(), words: () => 0 })
234 assert.equal(q.pane.props['--slides-notes-block-size'], '300px')
235
236 const r = fakePane({ deskPx: 600 })
237 document.m.delete(NOTES_HEIGHT_COOKIE)
238 mountNotesCard({ root: r.root, storage: fakeStorage(), words: () => 0 })
239 assert.equal(r.pane.props['--slides-notes-block-size'], DEFAULT_HEIGHT + 'px')
240})
241
242test('ArrowUp and ArrowDown move the height by 16px, Home and End go to the limits, each remembered', () => {
243 const p = fakePane({ deskPx: 600 })
244 const storage = fakeStorage()
245 mountNotesCard({ root: p.root, storage, words: () => 0 })
246 const height = () => Number(p.resize.getAttribute('aria-valuenow'))
247 assert.equal(height(), 144)
248
249 let ev = p.resize.fire('keydown', { key: 'ArrowUp' })
250 assert.ok(ev.defaultPrevented)
251 assert.equal(height(), 160)
252 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '160')
253
254 p.resize.fire('keydown', { key: 'ArrowDown' })
255 p.resize.fire('keydown', { key: 'ArrowDown' })
256 assert.equal(height(), 128)
257 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '128')
258
259 p.resize.fire('keydown', { key: 'Home' })
260 assert.equal(height(), HEAD_PX + 48)
261 assert.equal(cookie(NOTES_HEIGHT_COOKIE), String(HEAD_PX + 48))
262
263 p.resize.fire('keydown', { key: 'End' })
264 assert.equal(height(), 300)
265 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '300')
266
267 ev = p.resize.fire('keydown', { key: 'a' })
268 assert.equal(ev.defaultPrevented, false)
269})
270
271test('dragging the top edge up makes the notes taller, within the limits, and remembers on release', () => {
272 const p = fakePane({ deskPx: 600 })
273 const storage = fakeStorage()
274 mountNotesCard({ root: p.root, storage, words: () => 0 })
275 p.resize.fire('pointerdown', { pointerId: 7, clientY: 500, button: 0 })
276 assert.equal(p.resize.captured, 7)
277 p.resize.fire('pointermove', { pointerId: 7, clientY: 450 })
278 assert.equal(p.pane.props['--slides-notes-block-size'], '194px')
279 p.resize.fire('pointermove', { pointerId: 7, clientY: 0 })
280 assert.equal(p.pane.props['--slides-notes-block-size'], '300px')
281 assert.equal(cookie(NOTES_HEIGHT_COOKIE), null)
282 p.resize.fire('pointerup', { pointerId: 7, clientY: 0 })
283 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '300')
284 // A move after the release changes nothing.
285 p.resize.fire('pointermove', { pointerId: 7, clientY: 600 })
286 assert.equal(p.pane.props['--slides-notes-block-size'], '300px')
287})
288
289test('a stored height clamped on mount: ArrowDown moves from what is shown, and that is remembered', () => {
290 const p = fakePane({ deskPx: 600 })
291 document.m.set(NOTES_HEIGHT_COOKIE, '400')
292 const storage = fakeStorage()
293 mountNotesCard({ root: p.root, storage, words: () => 0 })
294 assert.equal(p.resize.getAttribute('aria-valuenow'), '300')
295 p.resize.fire('keydown', { key: 'ArrowDown' })
296 assert.equal(p.resize.getAttribute('aria-valuenow'), '284')
297 assert.equal(p.pane.props['--slides-notes-block-size'], '284px')
298 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '284')
299})
300
301test('a desk that shrinks after mount: the next key starts from the height now shown', () => {
302 const p = fakePane({ deskPx: 600 })
303 document.m.set(NOTES_HEIGHT_COOKIE, '280')
304 const storage = fakeStorage()
305 mountNotesCard({ root: p.root, storage, words: () => 0 })
306 assert.equal(p.resize.getAttribute('aria-valuenow'), '280')
307 // No resize event reaches the card: the limits change under it.
308 p.root.clientHeight = 400
309 p.resize.fire('keydown', { key: 'ArrowDown' })
310 assert.equal(p.resize.getAttribute('aria-valuenow'), '184')
311 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '184')
312
313 const q = fakePane({ deskPx: 600 })
314 document.m.set(NOTES_HEIGHT_COOKIE, '280')
315 const qs = fakeStorage()
316 mountNotesCard({ root: q.root, storage: qs, words: () => 0 })
317 q.root.clientHeight = 400
318 q.resize.fire('keydown', { key: 'ArrowUp' })
319 // Shown 200 (half of 400); up from there is past the maximum.
320 assert.equal(q.resize.getAttribute('aria-valuenow'), '200')
321 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '200')
322})
323
324test('a window resize narrows what is shown and keeps the preferred height for when the room comes back', () => {
325 const listeners = {}
326 globalThis.addEventListener = (type, fn) => { listeners[type] = fn }
327 const p = fakePane({ deskPx: 600 })
328 document.m.set(NOTES_HEIGHT_COOKIE, '280')
329 const storage = fakeStorage()
330 mountNotesCard({ root: p.root, storage, words: () => 0 })
331 p.root.clientHeight = 400
332 listeners.resize()
333 assert.equal(p.resize.getAttribute('aria-valuenow'), '200')
334 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '280')
335 p.root.clientHeight = 600
336 listeners.resize()
337 assert.equal(p.resize.getAttribute('aria-valuenow'), '280')
338})
339
340test('a drag after clamping moves from the shown height on its first pixel', () => {
341 const p = fakePane({ deskPx: 600 })
342 document.m.set(NOTES_HEIGHT_COOKIE, '400')
343 const storage = fakeStorage()
344 mountNotesCard({ root: p.root, storage, words: () => 0 })
345 p.resize.fire('pointerdown', { pointerId: 1, clientY: 300, button: 0 })
346 p.resize.fire('pointermove', { pointerId: 1, clientY: 301 })
347 assert.equal(p.pane.props['--slides-notes-block-size'], '299px')
348 p.resize.fire('pointerup', { pointerId: 1, clientY: 301 })
349 assert.equal(cookie(NOTES_HEIGHT_COOKIE), '299')
350})
351
352test('refresh() writes the tab\'s text: the word for notes, and the count of words when there are any', () => {
353 const p = fakePane()
354 let n = 0
355 const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => n })
356 card.refresh()
357 assert.equal(p.open.textContent, 'Notes')
358 n = 42
359 card.refresh()
360 assert.equal(p.open.textContent, 'Notes · 42 words')
361 n = 1
362 card.refresh()
363 assert.equal(p.open.textContent, 'Notes · 1 words')
364 n = 0
365 card.refresh()
366 assert.equal(p.open.textContent, 'Notes')
367 // No dot, no name of its own: the text is the name. The tab keeps
368 // the server's two strings and is given no attribute of its own.
369 assert.deepEqual([...p.open.attrs.keys()].sort(), ['data-label', 'data-label-words'])
370 assert.deepEqual(p.open.dataset, {})
371})
372
373test('refresh() writes nothing when the tab already says it (the server\'s first paint)', () => {
374 const p = fakePane()
375 let text = 'Notes · 7 words'
376 let writes = 0
377 Object.defineProperty(p.open, 'textContent', { get: () => text, set: v => { writes++; text = v } })
378 const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 7 })
379 card.refresh()
380 assert.equal(writes, 0)
381 assert.equal(text, 'Notes · 7 words')
382})
383
384test('countWords: what the server counts (strings.Fields of the plain text), through paragraphs and runs', () => {
385 // A run is a delta insert, marks or not; a paragraph is runs side by
386 // side; the notes are paragraphs, joined by a line break (deck.Text's
387 // Plain).
388 const runs = (...ins) => ({ toDelta: () => ins.map(i => typeof i === 'string' ? { insert: i } : i) })
389 const para = (...kids) => ({ toArray: () => kids })
390 const notes = (...paras) => ({ toArray: () => paras })
391 assert.equal(countWords(null), 0)
392 assert.equal(countWords(notes()), 0)
393 assert.equal(countWords(notes(para(runs('')))), 0)
394 assert.equal(countWords(notes(para(runs(' \n\t ')))), 0)
395 assert.equal(countWords(notes(para(runs('one')))), 1)
396 // Marks split a word into runs, and a word is still one word.
397 assert.equal(countWords(notes(para(runs({ insert: 'Bo', attributes: { b: true } }, { insert: 'ld', attributes: { i: true } }, ' and ', { insert: 'a link', attributes: { href: 'https://example.com' } })))), 4)
398 // Two runs side by side in a paragraph run together, as Plain joins them.
399 assert.equal(countWords(notes(para(runs('half'), runs('way')))), 1)
400 // Paragraphs never run together, even with no space between them.
401 assert.equal(countWords(notes(para(runs('end')), para(runs('start')))), 2)
402 // Trailing and doubled spaces, and the spaces Go's unicode.IsSpace knows.
403 assert.equal(countWords(notes(para(runs(' two words ')), para(runs('and three more ')))), 5)
404 // A byte-order mark is not a space to Go; NEL is.
405 assert.equal(countWords(notes(para(runs('ab')))), 1)
406 assert.equal(countWords(notes(para(runs('a\u0085b')))), 2)
407 // An embed counts nothing.
408 assert.equal(countWords(notes(para(runs({ insert: { image: 'x' } }, 'word')))), 1)
409})
410