// presenter-window.mjs — the presenter window (templates/presenter.html, // spec §5), moving at once. // // The server draws the window, and with scripts off its forms post and // redirect. This takes over: the window holds the talk // (talk-driver.mjs), a press redraws Now, Next, the notes and the header // from it in one task, and the server is told afterwards. Every word // comes from the page (data-text-* on the root, rendered from the // catalog); nothing here is English. // // Every press is answered in the header: the step count flashes, and // the position pulses, even when the press changes nothing (next at the // end of the talk). import { talkOrder } from './moves.mjs' import { createDriver, bindForms } from './talk-driver.mjs' import { createKeys, takeEarlyKeys } from './presenter-keys.mjs' import { mountPicker } from './presenter-picker.mjs' import { mountNotes, readPrefs, DEFAULT_SIZE, DEFAULT_PANELS } from './presenter-notes.mjs' import { mountArrange } from './presenter-arrange.mjs' import { install, reveal, fill, talkPlace, indexOf, restart, createPreview, mountClips } from './talk-draw.mjs' import { mountClock } from './presenter-clock.mjs' import { mountSceneLayer, scenePositions, talkMove } from './scene-layer.mjs' function el (tag, attr, text) { const e = document.createElement(tag) if (attr) e.setAttribute(attr, '') if (text !== undefined) e.textContent = text return e } export function mountPresenterWindow (root) { const T = root.dataset const $ = sel => root.querySelector(sel) const head = $('[presenter-head]') const pos = $('[presenter-pos]') const status = $('[presenter-status]') const elapsed = $('[presenter-elapsed]') window.name = 'slides-presenter' // The header's parts that come and go with the slide, made once and // hidden when there is nothing to say. function part (attr, after) { let e = $('[' + attr + ']') if (!e) { e = el('span', attr) e.hidden = true after.after(e) } return e } const stepcount = part('presenter-stepcount', pos) if (!stepcount.firstChild) { stepcount.setAttribute('role', 'status') const track = el('span', 'presenter-track') track.setAttribute('aria-hidden', 'true') stepcount.append(track, el('span', 'presenter-steptext')) } const mode = part('presenter-mode', stepcount) const black = part('presenter-black', mode) // Now runs the slide's scenes and clips; Next shows stills and posters (plan call R5). let held = null const scenes = mountSceneLayer({ root: $('[presenter-panel="now"] [presenter-frame]'), deck: () => held }) mountClips({ root: $('[presenter-panel="now"] [presenter-scale]') }) function drawHead (s, to, store) { const view = store.view const count = pos.querySelector('[presenter-count]') if (count) count.textContent = fill(T.textProgress, talkPlace(view, to.mode, indexOf(view, to.slide))) const n = Number(s.presses) || 0 stepcount.hidden = n === 0 if (n) { const track = stepcount.querySelector('[presenter-track]') while (track.children.length < n) track.append(el('i')) while (track.children.length > n) track.lastChild.remove() ;[...track.children].forEach((i, k) => i.toggleAttribute('data-done', k < to.step)) stepcount.querySelector('[presenter-steptext]').textContent = fill(T.textStep, { n: to.step, total: n }) } const both = store.slides.some(x => x.full) mode.hidden = !both if (both) mode.textContent = to.mode === 'full' ? T.textModeFull : T.textModeCore black.hidden = !to.black black.textContent = T.textBlack const menuMode = root.querySelector('[presenter-menu] form input[name="op"][value="mode"]') const label = menuMode && menuMode.form.querySelector('button') if (label) label.textContent = to.mode === 'full' ? T.textMenuCore : T.textMenuFull } // frame is a fresh presenter-frame holding html at step, or the end. function frame (html, step) { const f = el('div', 'presenter-frame') if (html === null) { f.setAttribute('data-end', '') return f } const scale = el('div', 'presenter-scale') install(scale, html, step) f.append(scale) return f } // Next shows what the next press brings: the slide's next step, the // next slide of the talk, or the end, once that slide is ready // (talk-draw.mjs's createPreview). nextDrawn is what it shows, so a // press that leaves it the same redraws nothing. let nextDrawn = '' const preview = createPreview(({ html, step, words, seq }) => { $('[presenter-next-label] span').textContent = words const drawn = seq + ':' + step + ':' + html if (drawn === nextDrawn) return nextDrawn = drawn $('[presenter-panel="next"] [presenter-fit]').replaceChildren(frame(html, step)) }) function drawNext (s, to, store) { const view = store.view const index = indexOf(view, to.slide) const n = Number(s.presses) || 0 const after = talkOrder(view, to.mode).find(i => i > index) if (to.step < n) preview(store, s.id, to.step + 1, T.textNextStep) else if (after !== undefined) preview(store, view.slides[after].id, 0, T.textNextSlide) else preview(store, null, 0, T.textNextEnd) } // The notes: the cookie's choice, or what the server drew when there // is none (its data-notes-mode and the size its style gave). const notesBox = $('[presenter-notes]') const saved = readPrefs() || {} const notes = mountNotes({ root, prefs: { notes: saved.notes || notesBox.getAttribute('data-notes-mode'), size: saved.size || Math.round(parseFloat(getComputedStyle(notesBox).fontSize) / 2) * 2 || DEFAULT_SIZE, panels: saved.panels || DEFAULT_PANELS } }) // The empty talk (every slide parked or gone): the empty state the // server draws for it, and nothing left of the slide that was up. const panels = $('[presenter-panels]') const emptyBox = $('[presenter-empty]') function drawEmpty (to, store) { root.setAttribute('data-empty', '') if (emptyBox) emptyBox.hidden = false if (panels) panels.hidden = true $('[presenter-panel="now"] [presenter-scale]').replaceChildren() scenes.show('', []) $('[presenter-panel="next"] [presenter-fit]').replaceChildren() nextDrawn = '' notes.clear() const count = pos.querySelector('[presenter-count]') if (count) count.textContent = '' pos.hidden = true stepcount.hidden = true mode.hidden = true black.hidden = !to.black root.setAttribute('data-slide', '') root.setAttribute('data-step', '0') root.setAttribute('data-mode', to.mode) root.setAttribute('data-blackout', to.black ? 'true' : 'false') root.setAttribute('data-deck-seq', String(store.deckSeq)) } function draw (to, from, { fresh, store }) { const s = to.slide ? store.slide(to.slide) : null if (!s) { if (!to.slide) drawEmpty(to, store) return } const wasEmpty = root.hasAttribute('data-empty') if (wasEmpty) { root.removeAttribute('data-empty') if (emptyBox) emptyBox.hidden = true if (panels) panels.hidden = false pos.hidden = false } const now = $('[presenter-panel="now"] [presenter-frame]') const scale = now.querySelector('[presenter-scale]') const same = !wasEmpty && !fresh && from && from.slide === to.slide if (!same) install(scale, s.html, to.step) else if (from.step !== to.step) reveal(scale, to.step) held = store scenes.show(to.slide, scenePositions(scale), { jump: !talkMove(store, from, to) }) now.toggleAttribute('data-black', !!to.black) drawHead(s, to, store) drawNext(s, to, store) if (!same || from.step !== to.step) notes.show(Array.isArray(s.notes) ? s.notes : [], to.step) root.setAttribute('data-slide', String(s.number)) root.setAttribute('data-step', String(to.step)) root.setAttribute('data-mode', to.mode) root.setAttribute('data-blackout', to.black ? 'true' : 'false') root.setAttribute('data-deck-seq', String(store.deckSeq)) // The address says where the window was drawn when it loaded; a // reload should draw where the talk is. const u = new URL(location.href) if (u.searchParams.has('slide') || u.searchParams.has('step')) { u.searchParams.delete('slide') u.searchParams.delete('step') history.replaceState(history.state, '', u.href) } } // What the header says beside the talk: how far the loading has got, // or that this window is on its own. let progress = '' let offline = false function say () { if (status) status.textContent = offline ? T.textOffline : progress } // The timer starts at the talk's first press, here at once, and from // the server's word whichever window pressed (presenter-clock.mjs). const clock = mountClock(elapsed, $('[data-clock]')) function pressed (m) { restart(stepcount, 'data-flash') restart(pos, 'data-pulse') clock.press(m) } const driver = createDriver({ root, talkHref: T.talk, streamHref: T.stream, action: T.command, token: root.getAttribute('data-remote-token') || '', draw, onLoad: (store, at) => { held = store; scenes.show(at.slide, scenePositions($('[presenter-panel="now"] [presenter-scale]')), { jump: true }) }, onPressed: pressed, onTimer: ms => clock.heard(ms), onStatus: s => { offline = s === 'offline'; say() }, onProgress: p => { root.setAttribute('data-loaded', p.finished && !p.failed ? 'all' : String(p.done)) progress = !p.finished ? fill(T.textLoading, { n: p.done, total: p.total }) : p.failed ? fill(T.textLoadingFailed, { n: p.failed }) : '' say() } }) function resetTimer () { driver.timerReset() clock.reset() } const menu = $('[presenter-more-menu]') // Pairing opens in a window of its own (target slides-pair), so this // one keeps its talk; the menu closes behind it. root.addEventListener('submit', e => { if (menu && e.target.getAttribute('target') === 'slides-pair') menu.open = false }) bindForms(root, T.command, { move: m => driver.move(m), timerReset: resetTimer, get store () { return driver.store } }, () => { if (menu) menu.open = false }) // Jumping to any slide: the position opens the picker, and a choice // goes through the driver like any jump. const picker = mountPicker({ root, talk: { get slides () { return driver.store ? driver.store.slides : [] }, get seq () { return driver.store ? driver.store.deckSeq : 0 } }, position: () => ({ number: Number(root.getAttribute('data-slide')) }), go: s => driver.move({ op: 'goto', slide: s.id }) }) const onKey = createKeys({ act (a) { if (a.move) driver.move(a.move) else if (a.jump) driver.jump(a.jump) else if (a.timer) resetTimer() else if (a.escape) { if (picker.isOpen()) { // The focus goes back to the position, and stays. picker.close() return } if (menu && menu.open) menu.open = false const f = document.activeElement if (f && f !== document.body && f.blur) f.blur() } // P opens the presenter window, which this is. } }) // Arranging the panels (presenter-arrange.mjs), from the menu. While // it is on, the clicker's keys still press; every other key is its. const arrange = mountArrange({ root, panels: saved.panels || DEFAULT_PANELS, save: notes.setPanels }) $('[presenter-arrange]')?.addEventListener('click', () => { if (menu) menu.open = false arrange.start() }) function keydown (e) { if (arrange.isOn() && e.key !== 'PageUp' && e.key !== 'PageDown') arrange.key(e) else onKey(e) } document.addEventListener('keydown', keydown) // And the keys pressed before this script was here (static/presskeys.js). takeEarlyKeys(keydown) }