// presenter-notes.mjs — the presenter window's notes (spec §5.2, §10). // // The notes are one HTML string per part of the slide: the part for the // step the talk is at is bright, the others dimmed. Fit starts from the // size the presenter chose and steps down a pixel at a time until the // notes fit their panel, to a floor of 16px; past the floor the panel // scrolls inside itself (never the document), keeps the bright part in // view, and fades at the edge that has more. Scroll uses the chosen // size. Smaller and larger move the size 2px between 16 and 48 and // leave the mode alone. Every change is saved (the cookie // slides-presenter, read by the server, so the first paint matches), // and so is the window's arrangement, through setPanels. // // The words come from the page (data-text-nonotes); nothing here is // English. Tested in presenter-notes.test.mjs. export const MIN_SIZE = 16 export const MAX_SIZE = 48 export const SIZE_STEP = 2 export const DEFAULT_SIZE = 24 export const PREFS_COOKIE = 'slides-presenter' export const DEFAULT_PANELS = '0,0,7,8;0,8,7,4;7,0,5,12' /** The part to show bright: the step's part, or the last there is. */ export function brightPart (parts, step) { const n = Array.isArray(parts) ? parts.length : Number(parts) || 0 return Math.max(0, Math.min(Math.floor(Number(step)) || 0, n - 1)) } /** The size one step smaller (dir < 0) or larger, kept in 16 to 48. */ export function stepSize (size, dir) { const s = Math.round(Number(size) / SIZE_STEP) * SIZE_STEP return Math.max(MIN_SIZE, Math.min(MAX_SIZE, (Number.isFinite(s) ? s : DEFAULT_SIZE) + (dir < 0 ? -SIZE_STEP : SIZE_STEP))) } /** The largest size from the chosen one down to the floor at which * overflows(px) is false; the floor if none is. */ export function fitSize (size, overflows) { let px = Math.max(MIN_SIZE, Math.min(MAX_SIZE, Math.round(size))) while (px > MIN_SIZE && overflows(px)) px-- return px } /** "fit.24.panels" read: { notes, size, panels }, or null when it is * not the cookie's shape (the server's own reading is stricter). */ export function parsePrefs (raw) { const f = String(raw ?? '').split('.') if (f.length !== 3 || (f[0] !== 'fit' && f[0] !== 'scroll')) return null const size = Number(f[1]) if (!Number.isInteger(size) || size < MIN_SIZE || size > MAX_SIZE || size % SIZE_STEP) return null return { notes: f[0], size, panels: f[2] } } /** The cookie's value for prefs, percent-encoded: a ";" cannot be kept * raw in a cookie. */ export function formatPrefs (p) { return encodeURIComponent(`${p.notes}.${p.size}.${p.panels || DEFAULT_PANELS}`) } /** The preferences in the cookie, or null. */ export function readPrefs (doc = globalThis.document) { try { for (const part of String(doc?.cookie || '').split(';')) { const at = part.indexOf('=') if (at > 0 && part.slice(0, at).trim() === PREFS_COOKIE) return parsePrefs(decodeURIComponent(part.slice(at + 1).trim())) } } catch { /* unreadable: as if absent */ } return null } /** Writes the cookie: path /p, a year, SameSite=Lax. */ export function writePrefs (p, doc = globalThis.document, loc = globalThis.location) { const secure = loc?.protocol === 'https:' ? '; Secure' : '' try { if (doc) doc.cookie = `${PREFS_COOKIE}=${formatPrefs(p)}; Path=/p; Max-Age=31536000; SameSite=Lax${secure}` } catch { /* cookies blocked: this window only */ } } /** The edges with more to read: 'top', 'bottom', 'both' or ''. */ export function fadeEdges (scrollTop, clientHeight, scrollHeight) { const top = scrollTop > 1 const bottom = scrollTop + clientHeight < scrollHeight - 1 return top && bottom ? 'both' : top ? 'top' : bottom ? 'bottom' : '' } /** Where to scroll so the part [top, bottom) shows: nowhere (null) if * it already does, else its top. Positions are in the scrolled * content. */ export function scrollFor (top, bottom, viewTop, viewHeight) { if (top >= viewTop && bottom <= viewTop + viewHeight) return null return Math.max(0, top) } export function mountNotes ({ root, prefs, savePrefs = writePrefs }) { const notes = root.querySelector('[presenter-notes]') const tools = name => root.querySelector('[presenter-notes-' + name + ']') const buttons = { fit: tools('fit'), scroll: tools('scroll'), smaller: tools('smaller'), larger: tools('larger') } let shown = null // what the panel holds, to tell new notes from a new step const state = { notes: prefs.notes === 'scroll' ? 'scroll' : 'fit', size: prefs.size || DEFAULT_SIZE, panels: prefs.panels || DEFAULT_PANELS } function fade () { notes.setAttribute('data-fade', fadeEdges(notes.scrollTop, notes.clientHeight, notes.scrollHeight)) } function keepInView () { const s = notes.querySelector('[data-part="now"]') if (!s) return const origin = notes.getBoundingClientRect().top - notes.scrollTop const top = s.getBoundingClientRect().top - origin const to = scrollFor(top - 12, top + s.offsetHeight, notes.scrollTop, notes.clientHeight) if (to !== null) notes.scrollTop = to } // layout sets the size (shrunk to fit in Fit), then keeps the bright // part in view wherever the panel scrolls. function layout () { notes.setAttribute('data-notes-mode', state.notes) let px = state.size if (state.notes === 'fit') { px = fitSize(state.size, p => { notes.style.fontSize = p + 'px' return notes.scrollHeight > notes.clientHeight }) } notes.style.fontSize = px + 'px' const scrolls = notes.scrollHeight > notes.clientHeight notes.setAttribute('data-notes-scrolls', scrolls ? 'true' : 'false') if (scrolls) keepInView() else notes.scrollTop = 0 fade() buttons.fit?.setAttribute('aria-pressed', state.notes === 'fit' ? 'true' : 'false') buttons.scroll?.setAttribute('aria-pressed', state.notes === 'scroll' ? 'true' : 'false') } function change (patch) { Object.assign(state, patch) savePrefs({ notes: state.notes, size: state.size, panels: state.panels }) layout() } buttons.fit?.addEventListener('click', () => change({ notes: 'fit' })) buttons.scroll?.addEventListener('click', () => change({ notes: 'scroll' })) buttons.smaller?.addEventListener('click', () => change({ size: stepSize(state.size, -1) })) buttons.larger?.addEventListener('click', () => change({ size: stepSize(state.size, 1) })) notes.addEventListener('scroll', fade) if (typeof ResizeObserver === 'function') { let last = '' new ResizeObserver(() => { const box = notes.clientWidth + 'x' + notes.clientHeight if (box === last) return last = box layout() }).observe(notes) } function show (parts, step) { const list = Array.isArray(parts) ? parts : [] const key = JSON.stringify(list) if (key !== shown) notes.scrollTop = 0 // new notes begin at their beginning shown = key if (!list.length) { const p = document.createElement('p') p.setAttribute('presenter-nonotes', '') p.textContent = root.dataset.textNonotes || '' notes.replaceChildren(p) } else { const bright = brightPart(list, step) notes.replaceChildren(...list.map((html, i) => { const section = document.createElement('section') section.setAttribute('presenter-part', '') if (list.length > 1) section.setAttribute('data-part', i < bright ? 'past' : i > bright ? 'later' : 'now') section.innerHTML = html return section })) } layout() } // The empty talk has no notes, not even "no notes": nothing is up. function clear () { shown = null notes.scrollTop = 0 notes.replaceChildren() } // The server drew the first paint; this sizes and places it. layout() // Arranging saves the panels through here (presenter-arrange.mjs), so // the cookie has one writer and keeps the notes' part. function setPanels (panels) { change({ panels }) } return { show, clear, setPanels } }