| 1 | // fixtures/play-through.mjs — seeing a slide's steps in the editor |
| 2 | // (steps and presenter spec §4, Seeing it): on a slide with presses, a |
| 3 | // small "3 presses" control at the slide's bottom end corner plays the |
| 4 | // slide through on the stage, one press per click (or → or Space while |
| 5 | // it has focus). While it plays its words say which press this is and |
| 6 | // that Esc shows the finished slide; Esc, a change of slide or any edit |
| 7 | // (refresh()) does, and so does a click after the last press. |
| 8 | // |
| 9 | // The stage is not a second renderer: playing marks what has not yet |
| 10 | // arrived data-waiting, the attribute the server's own drawing at a |
| 11 | // press uses (render/reveal.go), on the boxes and list items already on |
| 12 | // the stage, and slides.css hides them while keeping their space. The |
| 13 | // server never writes it on the editor's stage, so at rest nothing is |
| 14 | // marked. The control is the page's, a real button in the stage pane |
| 15 | // outside the slide (templates/editor.html), drawn only on a slide with |
| 16 | // presses, and placed here just under the slide's corner, its end edge |
| 17 | // on the slide's: never over the slide, whose every pixel is the |
| 18 | // author's (and stays as it was when the editor mounts late, |
| 19 | // TestFontALateEditorMovesNothing). Its words are the catalog's, on |
| 20 | // [slides-editor] (data-text-presses-*). |
| 21 | // |
| 22 | // A scene block's states are its items (presses.mjs, plan call R2): |
| 23 | // each still waits as a list item does, and onDraw(moved) tells the |
| 24 | // editor to move the scene's frame (scene-layer.mjs), moved true for a |
| 25 | // press and false for the finished slide coming back. |
| 26 | // |
| 27 | // mountPlayThrough({stageEl, getSlide, getScenes?, onExit, onDraw?, |
| 28 | // button?, texts?}) → {refresh(), stop(), place(), playing(), |
| 29 | // destroy()}. refresh() after the stage loads a slide and after any |
| 30 | // change to the document; place() whenever the stage moves; onExit() |
| 31 | // hears the finished slide come back. |
| 32 | import { presses } from './presses.mjs' |
| 33 | |
| 34 | /** What waits at press `at` (0 is the slide as it arrives): [{block, item}]. scenes is the deck's. */ |
| 35 | export function waitingAt (slide, at, scenes) { |
| 36 | if (!slide) return [] |
| 37 | return presses(slide, scenes).slice(Math.max(0, at)).flat() |
| 38 | } |
| 39 | |
| 40 | /** The control's words: the count at rest, which press while playing; '' with none. */ |
| 41 | export function pressesWords (texts, total, at) { |
| 42 | if (!total) return '' |
| 43 | if (at > 0) return texts.exit.replace('{n}', String(at)).replace('{total}', String(total)) |
| 44 | return total === 1 ? texts.one : texts.many.replace('{n}', String(total)) |
| 45 | } |
| 46 | |
| 47 | // The room between the slide's bottom edge and the control. |
| 48 | const GAP = 4 |
| 49 | |
| 50 | export function mountPlayThrough ({ stageEl, getSlide, getScenes = () => null, onExit = () => {}, onDraw = () => {}, button, texts }) { |
| 51 | const pane = stageEl.closest('[slides-stage-pane]') |
| 52 | button = button || (pane && pane.querySelector('[slides-presses]')) |
| 53 | if (!button) return { refresh () {}, stop () {}, place () {}, playing: () => false, destroy () {} } |
| 54 | if (!texts) { |
| 55 | const d = (button.closest('[slides-editor]') || button).dataset |
| 56 | texts = { one: d.textPressesOne || '', many: d.textPressesMany || '', exit: d.textPressesExit || '' } |
| 57 | } |
| 58 | const abort = new AbortController() |
| 59 | const signal = abort.signal |
| 60 | let at = 0 |
| 61 | let total = 0 |
| 62 | let marked = [] |
| 63 | |
| 64 | const boxEl = id => stageEl.querySelector(`[slides-block][data-id="${id.replace(/["\\]/g, '\\$&')}"]`) |
| 65 | |
| 66 | function unmark () { |
| 67 | for (const el of marked) el.removeAttribute('data-waiting') |
| 68 | marked = [] |
| 69 | } |
| 70 | |
| 71 | // A state of a scene block on the stage: its still, by the state's id. |
| 72 | function stateEl (box, scenes, item) { |
| 73 | const scene = scenes && scenes[box.getAttribute('data-scene')] |
| 74 | const st = scene && Array.isArray(scene.states) ? scene.states[item] : null |
| 75 | return st ? Array.from(box.children).find(c => c.getAttribute('data-scene-state') === st.id) : null |
| 76 | } |
| 77 | |
| 78 | function draw (moved = false) { |
| 79 | unmark() |
| 80 | if (at > 0) { |
| 81 | const scenes = getScenes() |
| 82 | for (const m of waitingAt(getSlide(), at, scenes)) { |
| 83 | const box = boxEl(m.block) |
| 84 | // A clip played on a press starts its video on it; its poster is |
| 85 | // there all along. A loop or once arrives on its step as any box. |
| 86 | if (!box || (box.getAttribute('data-block') === 'clip' && box.getAttribute('data-clip') === 'press')) continue |
| 87 | const el = m.item < 0 ? box |
| 88 | : box.getAttribute('data-block') === 'scene' ? stateEl(box, scenes, m.item) |
| 89 | : Array.from(box.children).filter(c => c.tagName === 'LI')[m.item] |
| 90 | if (!el) continue |
| 91 | el.setAttribute('data-waiting', '') |
| 92 | marked.push(el) |
| 93 | } |
| 94 | } |
| 95 | const words = pressesWords(texts, total, at) |
| 96 | if (button.textContent !== words) { |
| 97 | button.textContent = words |
| 98 | // Longer or shorter words: its end edge back on the slide's. |
| 99 | place() |
| 100 | } |
| 101 | onDraw(moved) |
| 102 | } |
| 103 | |
| 104 | function stop () { |
| 105 | if (!at) return |
| 106 | at = 0 |
| 107 | draw() |
| 108 | onExit() |
| 109 | } |
| 110 | |
| 111 | function advance () { |
| 112 | if (!total) return |
| 113 | if (at >= total) return stop() |
| 114 | at++ |
| 115 | draw(true) |
| 116 | } |
| 117 | |
| 118 | function refresh () { |
| 119 | if (at) stop() |
| 120 | const slide = getSlide() |
| 121 | total = slide ? presses(slide, getScenes()).length : 0 |
| 122 | button.hidden = !total |
| 123 | draw() |
| 124 | place() |
| 125 | } |
| 126 | |
| 127 | // Under the slide's bottom end corner, in the pane's own pixels: in |
| 128 | // the stage's padding and the pane's gap below it, which together are |
| 129 | // the control's height and the gap. Where the slide runs past what |
| 130 | // can be seen (a tall slide in Phone view's screen, a short window) |
| 131 | // it stays at the foot of what can. Never wider than the slide: the |
| 132 | // playing words wrap inside it (editor.css keeps the room for that |
| 133 | // under a narrow slide), measured after the width is set. |
| 134 | function place () { |
| 135 | if (button.hidden || typeof button.getBoundingClientRect !== 'function') return |
| 136 | const slide = stageEl.querySelector('[slides-slide]') |
| 137 | const base = button.offsetParent || pane |
| 138 | if (!slide || !base) return |
| 139 | const s = slide.getBoundingClientRect() |
| 140 | button.style.maxInlineSize = `${Math.max(s.width, 24)}px` |
| 141 | // An absolute box shrinks to the room past its old left: measured |
| 142 | // from the start, so it is as wide as its words (up to the slide). |
| 143 | button.style.left = '0px' |
| 144 | const p = base.getBoundingClientRect() |
| 145 | const h = button.offsetHeight |
| 146 | const frame = stageEl.closest('[slides-stage-frame]') |
| 147 | const fr = frame && frame.getBoundingClientRect() |
| 148 | const phone = fr && fr.height > 0 |
| 149 | const room = phone ? fr.bottom - GAP : stageEl.getBoundingClientRect().bottom + (parseFloat(getComputedStyle(pane).rowGap) || 0) |
| 150 | const top = Math.min(s.bottom + GAP, room - h) |
| 151 | const rtl = getComputedStyle(stageEl).direction === 'rtl' |
| 152 | const x = rtl ? s.left : s.right - button.offsetWidth |
| 153 | button.style.left = `${x - p.left}px` |
| 154 | button.style.top = `${top - p.top}px` |
| 155 | button.setAttribute('data-placed', '') |
| 156 | } |
| 157 | |
| 158 | button.addEventListener('click', advance, { signal }) |
| 159 | button.addEventListener('keydown', e => { |
| 160 | if (e.key !== 'ArrowRight' || e.altKey || e.ctrlKey || e.metaKey) return |
| 161 | e.preventDefault() |
| 162 | advance() |
| 163 | }, { signal }) |
| 164 | // Esc from anywhere ends a play-through; from the control it is the |
| 165 | // control's alone. |
| 166 | document.addEventListener('keydown', e => { |
| 167 | if (e.key !== 'Escape' || !at) return |
| 168 | stop() |
| 169 | if (e.target === button) { |
| 170 | e.preventDefault() |
| 171 | e.stopPropagation() |
| 172 | } |
| 173 | }, { signal, capture: true }) |
| 174 | const frame = stageEl.closest('[slides-stage-frame]') |
| 175 | for (const el of [stageEl, frame]) if (el) el.addEventListener('scroll', () => place(), { signal, passive: true }) |
| 176 | const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(() => place()) : null |
| 177 | if (ro) ro.observe(stageEl) |
| 178 | |
| 179 | return { |
| 180 | refresh, |
| 181 | stop, |
| 182 | place, |
| 183 | playing: () => at > 0, |
| 184 | destroy () { |
| 185 | if (ro) ro.disconnect() |
| 186 | unmark() |
| 187 | abort.abort() |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |