// notes-card.mjs: the notes as a card at the foot of the desk, closed // to a text tab that counts their words, its height adjustable and // remembered in cookies the server reads (slides-notes, // slides-notes-height). import test from 'node:test' import assert from 'node:assert/strict' import { mountNotesCard, clampHeight, countWords, NOTES_KEY, NOTES_HEIGHT_KEY, NOTES_COOKIE, NOTES_HEIGHT_COOKIE, HEAD_PX, DEFAULT_HEIGHT, STEP } from './notes-card.mjs' // A stand-in element: attributes, dataset, style properties, listeners // and focus, which is all the module touches. function el (attrs = {}) { const e = { attrs: new Map(Object.entries(attrs)), dataset: {}, props: {}, listeners: {}, hidden: false, clientHeight: 0, style: { setProperty (k, v) { e.props[k] = v } }, getAttribute: k => e.attrs.has(k) ? e.attrs.get(k) : null, setAttribute: (k, v) => e.attrs.set(k, String(v)), hasAttribute: k => e.attrs.has(k), removeAttribute: k => e.attrs.delete(k), toggleAttribute (k, on) { if (on) e.attrs.set(k, ''); else e.attrs.delete(k); return on }, addEventListener: (type, fn) => { (e.listeners[type] ||= []).push(fn) }, focus () { globalThis.document.activeElement = e }, setPointerCapture (id) { e.captured = id }, releasePointerCapture () { e.captured = null }, hasPointerCapture: id => e.captured === id, fire (type, init = {}) { const ev = { type, defaultPrevented: false, preventDefault () { ev.defaultPrevented = true }, ...init } for (const fn of e.listeners[type] || []) fn(ev) return ev } } return e } function fakePane ({ deskPx = 600, state = 'open' } = {}) { const parts = { pane: el(), open: el({ 'data-label': 'Notes', 'data-label-words': 'Notes · {n} words' }), close: el(), resize: el(), notes: el() } const sel = { '[slides-notes-pane]': parts.pane, '[slides-notes-open]': parts.open, '[slides-notes-close]': parts.close, '[slides-notes-resize]': parts.resize, '[slides-notes]': parts.notes } const root = el() root.clientHeight = deskPx root.querySelector = q => sel[q] || null root.closest = () => null parts.root = root // The server's first paint: the state it read from the cookie. parts.pane.dataset.state = state return parts } function fakeStorage (init = {}) { const m = new Map(Object.entries(init)) return { m, getItem: k => m.has(k) ? m.get(k) : null, setItem: (k, v) => m.set(k, String(v)), removeItem: k => m.delete(k) } } // document.cookie as a browser has it: each write sets one cookie (its // attributes are kept to be checked), each read is every name=value. function fakeJar (init = {}) { const m = new Map(Object.entries(init)) const written = [] return { m, written, get cookie () { return [...m].map(([k, v]) => k + '=' + v).join('; ') }, set cookie (line) { written.push(line) const [pair] = line.split(';') const at = pair.indexOf('=') m.set(pair.slice(0, at), pair.slice(at + 1)) } } } const cookie = name => globalThis.document.m.get(name) ?? null test.beforeEach(() => { globalThis.document = Object.assign(fakeJar(), { activeElement: null }) globalThis.innerHeight = 10000 globalThis.getComputedStyle = () => ({ lineHeight: '24px' }) globalThis.addEventListener = () => {} }) test('the keys are the ones the switch used, and the new height key', () => { assert.equal(NOTES_KEY, 'slides.editor.notes') assert.equal(NOTES_HEIGHT_KEY, 'slides.editor.notes.height') assert.equal(DEFAULT_HEIGHT, 144) assert.equal(STEP, 16) }) test('the cookies are the ones the server reads', () => { assert.equal(NOTES_COOKIE, 'slides-notes') assert.equal(NOTES_HEIGHT_COOKIE, 'slides-notes-height') }) test('open, close and toggle set data-state and remember open/closed in the cookie', () => { const p = fakePane() const storage = fakeStorage() const card = mountNotesCard({ root: p.root, storage, words: () => 0 }) assert.equal(p.pane.dataset.state, 'open') assert.equal(card.isOpen(), true) card.close() assert.equal(p.pane.dataset.state, 'closed') assert.equal(cookie(NOTES_COOKIE), 'closed') assert.equal(card.isOpen(), false) card.open() assert.equal(p.pane.dataset.state, 'open') assert.equal(cookie(NOTES_COOKIE), 'open') card.toggle() assert.equal(p.pane.dataset.state, 'closed') assert.equal(cookie(NOTES_COOKIE), 'closed') card.toggle() assert.equal(cookie(NOTES_COOKIE), 'open') assert.equal(storage.getItem(NOTES_KEY), null, 'nothing in localStorage any more') assert.match(document.written.at(-1), /^slides-notes=open; Path=\/; Max-Age=31536000; SameSite=Lax$/) }) test('mounting keeps the state the server painted and writes nothing', () => { const p = fakePane({ state: 'closed' }) const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 }) assert.equal(p.pane.dataset.state, 'closed') assert.equal(card.isOpen(), false) const q = fakePane({ state: 'open' }) mountNotesCard({ root: q.root, storage: fakeStorage(), words: () => 0 }) assert.equal(q.pane.dataset.state, 'open') assert.deepEqual(document.written, []) }) test('a localStorage preference from before the cookies is honoured once, moved to the cookies and removed', () => { const p = fakePane({ state: 'open' }) const storage = fakeStorage({ [NOTES_KEY]: 'off', [NOTES_HEIGHT_KEY]: '220' }) const card = mountNotesCard({ root: p.root, storage, words: () => 0 }) assert.equal(card.isOpen(), false) assert.equal(p.pane.dataset.state, 'closed') assert.equal(p.pane.props['--slides-notes-block-size'], '220px') assert.equal(cookie(NOTES_COOKIE), 'closed') assert.equal(cookie(NOTES_HEIGHT_COOKIE), '220') assert.equal(storage.getItem(NOTES_KEY), null) assert.equal(storage.getItem(NOTES_HEIGHT_KEY), null) // Where the cookies are already set, they win; the old keys still go. globalThis.document = Object.assign(fakeJar({ [NOTES_COOKIE]: 'open', [NOTES_HEIGHT_COOKIE]: '180' }), { activeElement: null }) const q = fakePane({ state: 'open' }) const qs = fakeStorage({ [NOTES_KEY]: 'off', [NOTES_HEIGHT_KEY]: '220' }) const qc = mountNotesCard({ root: q.root, storage: qs, words: () => 0 }) assert.equal(qc.isOpen(), true) assert.equal(q.pane.props['--slides-notes-block-size'], '180px') assert.equal(qs.getItem(NOTES_KEY), null) assert.equal(qs.getItem(NOTES_HEIGHT_KEY), null) assert.deepEqual(document.written, []) }) test('cookies and storage that throw leave the notes working for the visit', () => { globalThis.document = { activeElement: null, get cookie () { throw new Error('blocked') }, set cookie (v) { throw new Error('blocked') } } const p = fakePane() const bad = { getItem () { throw new Error('blocked') }, setItem () { throw new Error('blocked') }, removeItem () { throw new Error('blocked') } } const card = mountNotesCard({ root: p.root, storage: bad, words: () => 0 }) assert.equal(card.isOpen(), true) card.close() assert.equal(card.isOpen(), false) assert.equal(p.pane.dataset.state, 'closed') p.resize.fire('keydown', { key: 'ArrowUp' }) assert.equal(p.resize.getAttribute('aria-valuenow'), '160') }) test('the × closes and moves focus to the tab; the tab opens and moves focus into the notes', () => { const p = fakePane() const changes = [] mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0, onChange: open => changes.push(open) }) p.close.fire('click') assert.equal(p.pane.dataset.state, 'closed') assert.equal(document.activeElement, p.open) p.open.fire('click') assert.equal(p.pane.dataset.state, 'open') assert.equal(document.activeElement, p.notes) assert.deepEqual(changes, [false, true]) }) test('mounting does not take focus', () => { const p = fakePane() mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 }) assert.equal(document.activeElement, null) }) test('HEAD_PX is the card less its body as editor.css draws it: a hairline (1 + 1) and 12px of padding above and below', () => { assert.equal(HEAD_PX, 1 + 1 + 12 + 12) }) test('clampHeight: at least two lines and the head, at most half the desk', () => { const min = HEAD_PX + 2 * 24 assert.equal(clampHeight(10, 600, 24), 74) assert.equal(clampHeight(10, 600, 24), min) assert.equal(clampHeight(144, 600, 24), 144) assert.equal(clampHeight(500, 600, 24), 300) // A desk too short for both: the two lines win. assert.equal(clampHeight(500, 100, 24), min) // No desk measured: no upper bound. assert.equal(clampHeight(900, 0, 24), 900) assert.ok(clampHeight(Number.NaN, 600, 24) >= min) }) test('the height is the remembered one, clamped, written as a CSS length and as the separator\'s value', () => { const p = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '220') mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 0 }) assert.equal(p.pane.props['--slides-notes-block-size'], '220px') assert.equal(p.resize.getAttribute('aria-valuenow'), '220') assert.equal(p.resize.getAttribute('aria-valuemin'), String(HEAD_PX + 48)) assert.equal(p.resize.getAttribute('aria-valuemax'), '300') const q = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '9000') mountNotesCard({ root: q.root, storage: fakeStorage(), words: () => 0 }) assert.equal(q.pane.props['--slides-notes-block-size'], '300px') const r = fakePane({ deskPx: 600 }) document.m.delete(NOTES_HEIGHT_COOKIE) mountNotesCard({ root: r.root, storage: fakeStorage(), words: () => 0 }) assert.equal(r.pane.props['--slides-notes-block-size'], DEFAULT_HEIGHT + 'px') }) test('ArrowUp and ArrowDown move the height by 16px, Home and End go to the limits, each remembered', () => { const p = fakePane({ deskPx: 600 }) const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) const height = () => Number(p.resize.getAttribute('aria-valuenow')) assert.equal(height(), 144) let ev = p.resize.fire('keydown', { key: 'ArrowUp' }) assert.ok(ev.defaultPrevented) assert.equal(height(), 160) assert.equal(cookie(NOTES_HEIGHT_COOKIE), '160') p.resize.fire('keydown', { key: 'ArrowDown' }) p.resize.fire('keydown', { key: 'ArrowDown' }) assert.equal(height(), 128) assert.equal(cookie(NOTES_HEIGHT_COOKIE), '128') p.resize.fire('keydown', { key: 'Home' }) assert.equal(height(), HEAD_PX + 48) assert.equal(cookie(NOTES_HEIGHT_COOKIE), String(HEAD_PX + 48)) p.resize.fire('keydown', { key: 'End' }) assert.equal(height(), 300) assert.equal(cookie(NOTES_HEIGHT_COOKIE), '300') ev = p.resize.fire('keydown', { key: 'a' }) assert.equal(ev.defaultPrevented, false) }) test('dragging the top edge up makes the notes taller, within the limits, and remembers on release', () => { const p = fakePane({ deskPx: 600 }) const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) p.resize.fire('pointerdown', { pointerId: 7, clientY: 500, button: 0 }) assert.equal(p.resize.captured, 7) p.resize.fire('pointermove', { pointerId: 7, clientY: 450 }) assert.equal(p.pane.props['--slides-notes-block-size'], '194px') p.resize.fire('pointermove', { pointerId: 7, clientY: 0 }) assert.equal(p.pane.props['--slides-notes-block-size'], '300px') assert.equal(cookie(NOTES_HEIGHT_COOKIE), null) p.resize.fire('pointerup', { pointerId: 7, clientY: 0 }) assert.equal(cookie(NOTES_HEIGHT_COOKIE), '300') // A move after the release changes nothing. p.resize.fire('pointermove', { pointerId: 7, clientY: 600 }) assert.equal(p.pane.props['--slides-notes-block-size'], '300px') }) test('a stored height clamped on mount: ArrowDown moves from what is shown, and that is remembered', () => { const p = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '400') const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) assert.equal(p.resize.getAttribute('aria-valuenow'), '300') p.resize.fire('keydown', { key: 'ArrowDown' }) assert.equal(p.resize.getAttribute('aria-valuenow'), '284') assert.equal(p.pane.props['--slides-notes-block-size'], '284px') assert.equal(cookie(NOTES_HEIGHT_COOKIE), '284') }) test('a desk that shrinks after mount: the next key starts from the height now shown', () => { const p = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '280') const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) assert.equal(p.resize.getAttribute('aria-valuenow'), '280') // No resize event reaches the card: the limits change under it. p.root.clientHeight = 400 p.resize.fire('keydown', { key: 'ArrowDown' }) assert.equal(p.resize.getAttribute('aria-valuenow'), '184') assert.equal(cookie(NOTES_HEIGHT_COOKIE), '184') const q = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '280') const qs = fakeStorage() mountNotesCard({ root: q.root, storage: qs, words: () => 0 }) q.root.clientHeight = 400 q.resize.fire('keydown', { key: 'ArrowUp' }) // Shown 200 (half of 400); up from there is past the maximum. assert.equal(q.resize.getAttribute('aria-valuenow'), '200') assert.equal(cookie(NOTES_HEIGHT_COOKIE), '200') }) test('a window resize narrows what is shown and keeps the preferred height for when the room comes back', () => { const listeners = {} globalThis.addEventListener = (type, fn) => { listeners[type] = fn } const p = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '280') const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) p.root.clientHeight = 400 listeners.resize() assert.equal(p.resize.getAttribute('aria-valuenow'), '200') assert.equal(cookie(NOTES_HEIGHT_COOKIE), '280') p.root.clientHeight = 600 listeners.resize() assert.equal(p.resize.getAttribute('aria-valuenow'), '280') }) test('a drag after clamping moves from the shown height on its first pixel', () => { const p = fakePane({ deskPx: 600 }) document.m.set(NOTES_HEIGHT_COOKIE, '400') const storage = fakeStorage() mountNotesCard({ root: p.root, storage, words: () => 0 }) p.resize.fire('pointerdown', { pointerId: 1, clientY: 300, button: 0 }) p.resize.fire('pointermove', { pointerId: 1, clientY: 301 }) assert.equal(p.pane.props['--slides-notes-block-size'], '299px') p.resize.fire('pointerup', { pointerId: 1, clientY: 301 }) assert.equal(cookie(NOTES_HEIGHT_COOKIE), '299') }) test('refresh() writes the tab\'s text: the word for notes, and the count of words when there are any', () => { const p = fakePane() let n = 0 const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => n }) card.refresh() assert.equal(p.open.textContent, 'Notes') n = 42 card.refresh() assert.equal(p.open.textContent, 'Notes · 42 words') n = 1 card.refresh() assert.equal(p.open.textContent, 'Notes · 1 words') n = 0 card.refresh() assert.equal(p.open.textContent, 'Notes') // No dot, no name of its own: the text is the name. The tab keeps // the server's two strings and is given no attribute of its own. assert.deepEqual([...p.open.attrs.keys()].sort(), ['data-label', 'data-label-words']) assert.deepEqual(p.open.dataset, {}) }) test('refresh() writes nothing when the tab already says it (the server\'s first paint)', () => { const p = fakePane() let text = 'Notes · 7 words' let writes = 0 Object.defineProperty(p.open, 'textContent', { get: () => text, set: v => { writes++; text = v } }) const card = mountNotesCard({ root: p.root, storage: fakeStorage(), words: () => 7 }) card.refresh() assert.equal(writes, 0) assert.equal(text, 'Notes · 7 words') }) test('countWords: what the server counts (strings.Fields of the plain text), through paragraphs and runs', () => { // A run is a delta insert, marks or not; a paragraph is runs side by // side; the notes are paragraphs, joined by a line break (deck.Text's // Plain). const runs = (...ins) => ({ toDelta: () => ins.map(i => typeof i === 'string' ? { insert: i } : i) }) const para = (...kids) => ({ toArray: () => kids }) const notes = (...paras) => ({ toArray: () => paras }) assert.equal(countWords(null), 0) assert.equal(countWords(notes()), 0) assert.equal(countWords(notes(para(runs('')))), 0) assert.equal(countWords(notes(para(runs(' \n\t ')))), 0) assert.equal(countWords(notes(para(runs('one')))), 1) // Marks split a word into runs, and a word is still one word. 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) // Two runs side by side in a paragraph run together, as Plain joins them. assert.equal(countWords(notes(para(runs('half'), runs('way')))), 1) // Paragraphs never run together, even with no space between them. assert.equal(countWords(notes(para(runs('end')), para(runs('start')))), 2) // Trailing and doubled spaces, and the spaces Go's unicode.IsSpace knows. assert.equal(countWords(notes(para(runs(' two words ')), para(runs('and three more ')))), 5) // A byte-order mark is not a space to Go; NEL is. assert.equal(countWords(notes(para(runs('ab')))), 1) assert.equal(countWords(notes(para(runs('a\u0085b')))), 2) // An embed counts nothing. assert.equal(countWords(notes(para(runs({ insert: { image: 'x' } }, 'word')))), 1) })