// fixtures/play-through.mjs โ€” seeing a slide's steps in the editor // (steps and presenter spec ยง4, Seeing it): on a slide with presses, a // small "3 presses" control at the slide's bottom end corner plays the // slide through on the stage, one press per click (or โ†’ or Space while // it has focus). While it plays its words say which press this is and // that Esc shows the finished slide; Esc, a change of slide or any edit // (refresh()) does, and so does a click after the last press. // // The stage is not a second renderer: playing marks what has not yet // arrived data-waiting, the attribute the server's own drawing at a // press uses (render/reveal.go), on the boxes and list items already on // the stage, and slides.css hides them while keeping their space. The // server never writes it on the editor's stage, so at rest nothing is // marked. The control is the page's, a real button in the stage pane // outside the slide (templates/editor.html), drawn only on a slide with // presses, and placed here just under the slide's corner, its end edge // on the slide's: never over the slide, whose every pixel is the // author's (and stays as it was when the editor mounts late, // TestFontALateEditorMovesNothing). Its words are the catalog's, on // [slides-editor] (data-text-presses-*). // // A scene block's states are its items (presses.mjs, plan call R2): // each still waits as a list item does, and onDraw(moved) tells the // editor to move the scene's frame (scene-layer.mjs), moved true for a // press and false for the finished slide coming back. // // mountPlayThrough({stageEl, getSlide, getScenes?, onExit, onDraw?, // button?, texts?}) โ†’ {refresh(), stop(), place(), playing(), // destroy()}. refresh() after the stage loads a slide and after any // change to the document; place() whenever the stage moves; onExit() // hears the finished slide come back. import { presses } from './presses.mjs' /** What waits at press `at` (0 is the slide as it arrives): [{block, item}]. scenes is the deck's. */ export function waitingAt (slide, at, scenes) { if (!slide) return [] return presses(slide, scenes).slice(Math.max(0, at)).flat() } /** The control's words: the count at rest, which press while playing; '' with none. */ export function pressesWords (texts, total, at) { if (!total) return '' if (at > 0) return texts.exit.replace('{n}', String(at)).replace('{total}', String(total)) return total === 1 ? texts.one : texts.many.replace('{n}', String(total)) } // The room between the slide's bottom edge and the control. const GAP = 4 export function mountPlayThrough ({ stageEl, getSlide, getScenes = () => null, onExit = () => {}, onDraw = () => {}, button, texts }) { const pane = stageEl.closest('[slides-stage-pane]') button = button || (pane && pane.querySelector('[slides-presses]')) if (!button) return { refresh () {}, stop () {}, place () {}, playing: () => false, destroy () {} } if (!texts) { const d = (button.closest('[slides-editor]') || button).dataset texts = { one: d.textPressesOne || '', many: d.textPressesMany || '', exit: d.textPressesExit || '' } } const abort = new AbortController() const signal = abort.signal let at = 0 let total = 0 let marked = [] const boxEl = id => stageEl.querySelector(`[slides-block][data-id="${id.replace(/["\\]/g, '\\$&')}"]`) function unmark () { for (const el of marked) el.removeAttribute('data-waiting') marked = [] } // A state of a scene block on the stage: its still, by the state's id. function stateEl (box, scenes, item) { const scene = scenes && scenes[box.getAttribute('data-scene')] const st = scene && Array.isArray(scene.states) ? scene.states[item] : null return st ? Array.from(box.children).find(c => c.getAttribute('data-scene-state') === st.id) : null } function draw (moved = false) { unmark() if (at > 0) { const scenes = getScenes() for (const m of waitingAt(getSlide(), at, scenes)) { const box = boxEl(m.block) // A clip played on a press starts its video on it; its poster is // there all along. A loop or once arrives on its step as any box. if (!box || (box.getAttribute('data-block') === 'clip' && box.getAttribute('data-clip') === 'press')) continue const el = m.item < 0 ? box : box.getAttribute('data-block') === 'scene' ? stateEl(box, scenes, m.item) : Array.from(box.children).filter(c => c.tagName === 'LI')[m.item] if (!el) continue el.setAttribute('data-waiting', '') marked.push(el) } } const words = pressesWords(texts, total, at) if (button.textContent !== words) { button.textContent = words // Longer or shorter words: its end edge back on the slide's. place() } onDraw(moved) } function stop () { if (!at) return at = 0 draw() onExit() } function advance () { if (!total) return if (at >= total) return stop() at++ draw(true) } function refresh () { if (at) stop() const slide = getSlide() total = slide ? presses(slide, getScenes()).length : 0 button.hidden = !total draw() place() } // Under the slide's bottom end corner, in the pane's own pixels: in // the stage's padding and the pane's gap below it, which together are // the control's height and the gap. Where the slide runs past what // can be seen (a tall slide in Phone view's screen, a short window) // it stays at the foot of what can. Never wider than the slide: the // playing words wrap inside it (editor.css keeps the room for that // under a narrow slide), measured after the width is set. function place () { if (button.hidden || typeof button.getBoundingClientRect !== 'function') return const slide = stageEl.querySelector('[slides-slide]') const base = button.offsetParent || pane if (!slide || !base) return const s = slide.getBoundingClientRect() button.style.maxInlineSize = `${Math.max(s.width, 24)}px` // An absolute box shrinks to the room past its old left: measured // from the start, so it is as wide as its words (up to the slide). button.style.left = '0px' const p = base.getBoundingClientRect() const h = button.offsetHeight const frame = stageEl.closest('[slides-stage-frame]') const fr = frame && frame.getBoundingClientRect() const phone = fr && fr.height > 0 const room = phone ? fr.bottom - GAP : stageEl.getBoundingClientRect().bottom + (parseFloat(getComputedStyle(pane).rowGap) || 0) const top = Math.min(s.bottom + GAP, room - h) const rtl = getComputedStyle(stageEl).direction === 'rtl' const x = rtl ? s.left : s.right - button.offsetWidth button.style.left = `${x - p.left}px` button.style.top = `${top - p.top}px` button.setAttribute('data-placed', '') } button.addEventListener('click', advance, { signal }) button.addEventListener('keydown', e => { if (e.key !== 'ArrowRight' || e.altKey || e.ctrlKey || e.metaKey) return e.preventDefault() advance() }, { signal }) // Esc from anywhere ends a play-through; from the control it is the // control's alone. document.addEventListener('keydown', e => { if (e.key !== 'Escape' || !at) return stop() if (e.target === button) { e.preventDefault() e.stopPropagation() } }, { signal, capture: true }) const frame = stageEl.closest('[slides-stage-frame]') for (const el of [stageEl, frame]) if (el) el.addEventListener('scroll', () => place(), { signal, passive: true }) const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(() => place()) : null if (ro) ro.observe(stageEl) return { refresh, stop, place, playing: () => at > 0, destroy () { if (ro) ro.disconnect() unmark() abort.abort() } } }