| 1 | // slide-window.mjs — the slide window (templates/present.html, GET |
| 2 | // /p/{session}) of a session with a position, for whoever may drive it: |
| 3 | // the projector, moving at once. |
| 4 | // |
| 5 | // The server draws the slide, and with scripts off the bar's forms post |
| 6 | // and redirect. This holds the talk (talk-driver.mjs), takes the same |
| 7 | // keys as the presenter window, and swaps the slide in one task from |
| 8 | // memory: a step in place, the slide whole, never a blank stage. When |
| 9 | // the network is gone and this window is not the one pressing, it holds |
| 10 | // still and says it is waiting. Every word comes from the page |
| 11 | // (data-text-* on the surface). |
| 12 | // |
| 13 | // The follow link, the remote and sessions begun before positions keep |
| 14 | // static/present.js. |
| 15 | import { createDriver, bindForms } from './talk-driver.mjs' |
| 16 | import { createKeys, takeEarlyKeys } from './presenter-keys.mjs' |
| 17 | import { quietBar } from './quiet-bar.mjs' |
| 18 | import { install, reveal, fill, talkPlace, indexOf, mountClips } from './talk-draw.mjs' |
| 19 | import { mountSceneLayer, scenePositions, talkMove } from './scene-layer.mjs' |
| 20 | |
| 21 | export function mountSlideWindow (root) { |
| 22 | const T = root.dataset |
| 23 | const stage = root.querySelector('[slides-stage]') |
| 24 | const note = root.querySelector('[slides-blackout]') |
| 25 | const waiting = root.querySelector('[slides-waiting]') |
| 26 | const region = root.querySelector('[slides-announce]') |
| 27 | if (!stage) return |
| 28 | quietBar(root) |
| 29 | // The scenes on the slide run over it, from the talk (scene-layer.mjs). |
| 30 | let held = null |
| 31 | const scenes = mountSceneLayer({ root, deck: () => held }) |
| 32 | // And its clips play over their posters (clip.mjs). |
| 33 | mountClips({ root: stage }) |
| 34 | |
| 35 | // counterText is the counter's own words: the line after the label |
| 36 | // only a screen reader hears. |
| 37 | function counterText (text) { |
| 38 | const counter = root.querySelector('[slides-counter]') |
| 39 | if (!counter) return |
| 40 | const last = counter.lastChild |
| 41 | if (last && last.nodeType === Node.TEXT_NODE) last.textContent = ' ' + text |
| 42 | else counter.append(' ' + text) |
| 43 | } |
| 44 | |
| 45 | // The empty talk (every slide parked or gone): the empty state the |
| 46 | // server rendered for it, hidden until now, and no slide left up. |
| 47 | const empty = root.querySelector('[slides-empty]') |
| 48 | const controls = root.querySelector('[slides-bar]') |
| 49 | function drawEmpty (to, store) { |
| 50 | root.setAttribute('data-empty', '') |
| 51 | stage.replaceChildren() |
| 52 | stage.hidden = true |
| 53 | if (controls) controls.hidden = true |
| 54 | if (empty) empty.hidden = false |
| 55 | counterText('') |
| 56 | if (region && empty && region.textContent !== empty.innerText) region.textContent = empty.innerText |
| 57 | root.setAttribute('data-slide', '') |
| 58 | root.setAttribute('data-step', '0') |
| 59 | root.setAttribute('data-mode', to.mode) |
| 60 | root.setAttribute('data-blackout', to.black ? 'true' : 'false') |
| 61 | root.setAttribute('data-deck-seq', String(store.deckSeq)) |
| 62 | if (note) note.hidden = !to.black |
| 63 | scenes.show('', []) |
| 64 | } |
| 65 | |
| 66 | function draw (to, from, { fresh, store }) { |
| 67 | const s = to.slide ? store.slide(to.slide) : null |
| 68 | if (!s) { |
| 69 | if (!to.slide) drawEmpty(to, store) |
| 70 | return |
| 71 | } |
| 72 | const wasEmpty = root.hasAttribute('data-empty') |
| 73 | if (wasEmpty) { |
| 74 | root.removeAttribute('data-empty') |
| 75 | stage.hidden = false |
| 76 | if (controls) controls.hidden = false |
| 77 | if (empty) empty.hidden = true |
| 78 | } |
| 79 | const view = store.view |
| 80 | const same = !wasEmpty && !fresh && from && from.slide === to.slide |
| 81 | if (!same) { |
| 82 | install(stage, s.html, to.step) |
| 83 | const said = fill(T.textAnnounce, { n: s.number, total: store.slides.length, title: s.title }) |
| 84 | root.setAttribute('data-announce', said) |
| 85 | if (region && region.textContent !== said) region.textContent = said |
| 86 | } else if (from.step !== to.step) { |
| 87 | reveal(stage, to.step) |
| 88 | } |
| 89 | held = store |
| 90 | scenes.show(to.slide, scenePositions(stage), { jump: !talkMove(store, from, to) }) |
| 91 | const place = talkPlace(view, to.mode, indexOf(view, to.slide)) |
| 92 | counterText(fill(T.textProgress, place)) |
| 93 | const bar = root.querySelector('[slides-progress]') |
| 94 | if (bar) { |
| 95 | bar.setAttribute('value', String(place.n)) |
| 96 | bar.setAttribute('max', String(place.total)) |
| 97 | } |
| 98 | root.setAttribute('data-slide', String(s.number)) |
| 99 | root.setAttribute('data-step', String(to.step)) |
| 100 | root.setAttribute('data-mode', to.mode) |
| 101 | root.setAttribute('data-blackout', to.black ? 'true' : 'false') |
| 102 | root.setAttribute('data-deck-seq', String(store.deckSeq)) |
| 103 | if (note) note.hidden = !to.black |
| 104 | const u = new URL(location.href) |
| 105 | if (u.searchParams.has('slide') || u.searchParams.has('step')) { |
| 106 | u.searchParams.delete('slide') |
| 107 | u.searchParams.delete('step') |
| 108 | history.replaceState(history.state, '', u.href) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | const driver = createDriver({ |
| 113 | root, |
| 114 | talkHref: T.talk, |
| 115 | streamHref: T.stream, |
| 116 | action: T.command, |
| 117 | token: root.getAttribute('data-remote-token') || '', |
| 118 | draw, |
| 119 | onLoad: (store, at) => { held = store; scenes.show(at.slide, scenePositions(stage), { jump: true }) }, |
| 120 | onProgress: p => root.setAttribute('data-loaded', p.finished && !p.failed ? 'all' : String(p.done)), |
| 121 | onStatus: s => { |
| 122 | if (!waiting) return |
| 123 | waiting.textContent = T.textWaiting |
| 124 | waiting.hidden = s !== 'offline' |
| 125 | } |
| 126 | }) |
| 127 | |
| 128 | bindForms(root, T.command, driver) |
| 129 | |
| 130 | const onKey = createKeys({ |
| 131 | act (a) { |
| 132 | if (a.move) driver.move(a.move) |
| 133 | else if (a.jump) driver.jump(a.jump) |
| 134 | else if (a.timer) driver.timerReset() |
| 135 | else if (a.open && T.presenter) window.open(T.presenter, 'slides-presenter') |
| 136 | else if (a.escape) { |
| 137 | if (document.fullscreenElement) document.exitFullscreen() |
| 138 | const f = document.activeElement |
| 139 | if (f && f !== document.body && f.blur) f.blur() |
| 140 | } |
| 141 | } |
| 142 | }) |
| 143 | document.addEventListener('keydown', onKey) |
| 144 | // And the keys pressed before this script was here (static/presskeys.js). |
| 145 | takeEarlyKeys(onKey) |
| 146 | |
| 147 | // A swipe on a touch screen is a press too. |
| 148 | let startX = null |
| 149 | root.addEventListener('touchstart', e => { startX = e.changedTouches[0].clientX }, { passive: true }) |
| 150 | root.addEventListener('touchend', e => { |
| 151 | if (startX === null) return |
| 152 | const dx = e.changedTouches[0].clientX - startX |
| 153 | startX = null |
| 154 | if (Math.abs(dx) >= 60) driver.move({ op: dx < 0 ? 'next' : 'back' }) |
| 155 | }, { passive: true }) |
| 156 | } |
| 157 | |