// fixtures/notes-card.mjs — the speaker notes as a card at the foot of // the desk (the editor tidied, spec §5; editor.html's // [slides-notes-pane]). // // Open, the card is the stage pane's last child, in the flow, so the // stages give up its height by layout: it has an × that closes it, a // top edge that resizes it ([slides-notes-resize], a separator: drag // it, or ArrowUp/ArrowDown 16px at a time, Home and End to the limits), // and the notes themselves — the same [slides-notes] field as before, // which stage.mjs binds; nothing here touches it but focus. No heading: // the field's placeholder names it (the quieter editor, spec §6.2). // Closed, the card is display: none and the tab [slides-notes-open] // shows in the pane's bottom start corner: its text is the word for // notes, with the count of the slide's words when it has any // (refresh, from the server's data-label and data-label-words). // // Open or closed, and the height in px, are remembered in cookies the // server reads (NOTES_COOKIE: open or closed; NOTES_HEIGHT_COOKIE: whole // px; pref-cookie.mjs), as the inspector card's state is: the server // renders the card's data-state and its --slides-notes-block-size, so // the first paint is final and nothing here changes it on mount (ruling // R21: a card that closed or changed height under a slow bundle was a // flash). A preference from before the cookies, in localStorage under // NOTES_KEY ('on', 'off') and NOTES_HEIGHT_KEY, is honoured once where // there is no cookie yet, written to the cookies and removed. Where // cookies are blocked the card still opens, closes and resizes, for the // page. A per-person convenience, never document state. // // mountNotesCard({root, storage, words, onChange}) → {open, close, // toggle, isOpen, refresh}. root holds the card and the tab (the stage // pane); storage is where the old preference may be; words() is how // many words the slide on stage has in its notes (countWords); // onChange(open) hears every open or close (the View menu's check). import { readCookie, writeCookie } from './pref-cookie.mjs' export const NOTES_KEY = 'slides.editor.notes' export const NOTES_HEIGHT_KEY = 'slides.editor.notes.height' export const NOTES_COOKIE = 'slides-notes' export const NOTES_HEIGHT_COOKIE = 'slides-notes-height' /** The card's height before anybody has changed it, in px. */ export const DEFAULT_HEIGHT = 144 /** What one arrow key moves the top edge by, in px. */ export const STEP = 16 /** * The card less its body, in px: its hairline (1 + 1) and its padding * (12 + 12); there is no head row, the × sits in the padding's corner. * editor.css's "The notes card" draws exactly this, and editor.go's * notesMinHeight is this and two lines. */ export const HEAD_PX = 26 /** * The height the card may have: at least the head and two lines of * notes, at most half the desk (deskPx). Where the desk is too short * for both, the two lines win; with no desk measured (0) there is no * upper bound. Whole px. */ export function clampHeight (px, deskPx, lineHeightPx) { const min = Math.ceil(HEAD_PX + 2 * lineHeightPx) const max = deskPx > 0 ? Math.floor(deskPx / 2) : Infinity const want = Number.isFinite(px) ? Math.round(px) : DEFAULT_HEIGHT return Math.max(min, Math.min(want, max)) } // What Go's unicode.IsSpace says is a space, which is what the // server's strings.Fields splits on: not JavaScript's \s, which // counts U+FEFF and not U+0085. const SPACES = /[\t\n\v\f\r \u0085\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/ // A node's plain text: a run's inserts (its marks dropped, an embed // nothing), or its children's, side by side. function plain (node) { if (!node) return '' if (typeof node.toDelta === 'function') { return node.toDelta().map(op => typeof op.insert === 'string' ? op.insert : '').join('') } if (typeof node.toArray === 'function') return node.toArray().map(plain).join('') return '' } /** * How many words a notes fragment (Y.XmlFragment of paragraphs, or * anything with toArray() of elements and toDelta() of text) holds: * the count the tab says. The server counts the first slide's the same * way, len(strings.Fields(notes.Plain())): runs side by side in a * paragraph, paragraphs joined by a line break, split on whitespace. */ export function countWords (fragment) { if (!fragment || typeof fragment.toArray !== 'function') return 0 const text = fragment.toArray().map(plain).join('\n') return text.split(SPACES).filter(w => w !== '').length } function read (storage, key) { try { return storage ? storage.getItem(key) : null } catch { return null } } function forget (storage, key) { try { if (storage) storage.removeItem(key) } catch { /* nothing to move */ } } export function mountNotesCard ({ root, storage = globalThis.localStorage, words = () => 0, onChange = () => {} }) { const pane = root.querySelector('[slides-notes-pane]') const openBtn = root.querySelector('[slides-notes-open]') const closeBtn = root.querySelector('[slides-notes-close]') const edge = root.querySelector('[slides-notes-resize]') const notesEl = root.querySelector('[slides-notes]') // The server's first paint is the cookie's state. let open = !pane || pane.dataset.state !== 'closed' // Two heights: the preferred one, the person's own, which is what is // remembered; and the shown one, the preferred one clamped to the // desk as it is now (a smaller window narrows the card for the visit, // and the person's height comes back with the room). Every change — // a key, a drag — starts from what is shown, and its result becomes // the new preferred height. let preferred = Number(readCookie(NOTES_HEIGHT_COOKIE)) || DEFAULT_HEIGHT let shown = 0 // The desk the card may take half of: the pane, which from 1100px is // the desk's height, and never more than the window (narrower, the // page scrolls and the pane is as tall as what is in it). function deskPx () { const h = root.clientHeight || 0 const win = Number(globalThis.innerHeight) || 0 return win > 0 && h > 0 ? Math.min(h, win) : (h || win) } function lineHeightPx () { const lh = notesEl && typeof globalThis.getComputedStyle === 'function' ? parseFloat(globalThis.getComputedStyle(notesEl).lineHeight) : NaN return Number.isFinite(lh) && lh > 0 ? lh : 24 } function limits () { const desk = deskPx() const line = lineHeightPx() const min = clampHeight(0, desk, line) return { min, max: desk > 0 ? Math.max(min, Math.floor(desk / 2)) : Infinity, desk, line } } // Draws a height, clamped to the desk as it is now, and returns what // was drawn. Remembers nothing. function draw (px) { const { min, max, desk, line } = limits() const h = clampHeight(px, desk, line) if (pane) pane.style.setProperty('--slides-notes-block-size', h + 'px') if (edge) { edge.setAttribute('aria-valuemin', String(min)) edge.setAttribute('aria-valuemax', String(Number.isFinite(max) ? max : Math.max(h, min))) edge.setAttribute('aria-valuenow', String(h)) } return h } // The preferred height, drawn inside today's limits: what is shown. function redraw () { shown = draw(preferred) return shown } // A new height from a key or a drag: shown, and preferred from now on. function prefer (px) { preferred = shown = draw(px) writeCookie(NOTES_HEIGHT_COOKIE, String(preferred)) } function show (on, { focus = false } = {}) { open = on if (pane) pane.dataset.state = on ? 'open' : 'closed' writeCookie(NOTES_COOKIE, on ? 'open' : 'closed') if (on) redraw() if (focus) { const target = on ? notesEl : openBtn if (target) target.focus() } onChange(on) } // The tab's text, from the strings the server gave it: the word for // notes, or that and the count. Written only when it changes, so the // server's first paint is kept as it is. function refresh () { if (!openBtn) return const n = Math.max(0, Math.floor(Number(words()) || 0)) const label = n > 0 ? (openBtn.getAttribute('data-label-words') || '').replace('{n}', String(n)) : openBtn.getAttribute('data-label') if (label && openBtn.textContent !== label) openBtn.textContent = label } // The old localStorage preference, once: applied where no cookie says // otherwise yet, moved to the cookies, and removed either way. const oldOpen = read(storage, NOTES_KEY) if (oldOpen !== null) { if (readCookie(NOTES_COOKIE) === null && (oldOpen === 'on' || oldOpen === 'off')) { open = oldOpen === 'on' writeCookie(NOTES_COOKIE, open ? 'open' : 'closed') } forget(storage, NOTES_KEY) } const oldHeight = read(storage, NOTES_HEIGHT_KEY) if (oldHeight !== null) { const px = Number(oldHeight) if (readCookie(NOTES_HEIGHT_COOKIE) === null && Number.isFinite(px) && px > 0) { preferred = Math.round(px) writeCookie(NOTES_HEIGHT_COOKIE, String(preferred)) } forget(storage, NOTES_HEIGHT_KEY) } if (pane) pane.dataset.state = open ? 'open' : 'closed' redraw() closeBtn?.addEventListener('click', () => show(false, { focus: true })) openBtn?.addEventListener('click', () => show(true, { focus: true })) if (edge) { edge.addEventListener('keydown', e => { if (e.altKey || e.ctrlKey || e.metaKey) return if (!['ArrowUp', 'ArrowDown', 'Home', 'End'].includes(e.key)) return e.preventDefault() // From what is shown now: the desk may have changed since the // last draw. const from = redraw() const { min, max } = limits() if (e.key === 'ArrowUp') prefer(from + STEP) else if (e.key === 'ArrowDown') prefer(from - STEP) else if (e.key === 'Home') prefer(min) else prefer(Number.isFinite(max) ? max : from) }) // The top edge follows the pointer: up is taller. The pointer is // captured, so a drag that leaves the edge still drives it, and the // height is remembered once, on release. let drag = null edge.addEventListener('pointerdown', e => { if (e.button !== undefined && e.button !== 0) return e.preventDefault() drag = { id: e.pointerId, y: e.clientY, from: redraw() } edge.setPointerCapture(e.pointerId) }) edge.addEventListener('pointermove', e => { if (!drag || e.pointerId !== drag.id) return shown = draw(drag.from + (drag.y - e.clientY)) }) const end = e => { if (!drag || e.pointerId !== drag.id) return drag = null if (edge.hasPointerCapture(e.pointerId)) edge.releasePointerCapture(e.pointerId) prefer(shown) } edge.addEventListener('pointerup', end) edge.addEventListener('pointercancel', end) } // The window, and with it the desk, changed size: the height is drawn // again inside the new limits. if (typeof globalThis.addEventListener === 'function') { globalThis.addEventListener('resize', () => { if (open) redraw() }) } return { open: () => show(true, { focus: true }), close: () => show(false), toggle () { show(!open, { focus: !open }) }, isOpen: () => open, refresh } }