// scene-layer.mjs — scenes running over a slide, and carried on between // slides (scenes and clips spec §3, §8.7). // // A slide's scene block is drawn by the server as its stills // (render.go's sceneBlock): the states it passes through, the newest // that has arrived shown. Where scripts run, this puts the scene's own // frame (scene-frame.mjs) over the block, in a layer of its own outside // the slide — a sibling over the stage, so that swapping the slide // never moves a frame to another parent, which would reload it. One // frame per placement: a scene's blocks on a slide are its placements, // numbered in the slide's order (scene-places.mjs, their slots), and // each runs. The next slide's placements are paired with these by that // number: when the next slide's first block of a scene is in the same // box (the same box on a canvas, the same region in a flow) as this // slide's first, its frame is kept and told the new state, moving // unless it was a jump, and so on for the second; the rest are made // afresh at the state their slide is at. A block with // something drawn over it (scene-places.mjs) runs inside its own block // instead, so the slide's stacking holds; such a frame lives and dies // with its block. The still stays under the frame as what a frame that // is not ready shows, and is what assistive technology reads; the frame // is inert. A phone view, and a reader who asked for less motion, get // the stills and no frame (each still is its state's settled end); a // window crossing the phone's width is followed. // // mountSceneLayer({root, deck, base, reducedMotion}) → {show(slideId, // positions, {jump}), place(), preview(scene, submit), destroy()}. // root is the element the layer sits in, over the stage, never swapped; // deck the deck's view, or a function giving it (its scenes by id, each // with its code, base making the bundle's address, or its src); // positions scenePositions(the stage) or a part of them. preview remakes // a scene's frame, named, for submit(name) to post a draft into. import { createSceneFrame } from './scene-frame.mjs' import { talkOrder } from './moves.mjs' import { stillsOnly } from './scene-places.mjs' import { frameFonts } from './scene-fonts.mjs' export { scenePositions, placeKey, stillsOnly } from './scene-places.mjs' export { sceneFonts } from './scene-fonts.mjs' /** * Which frames stay: frames is slot → {key, sig}; wanted the positions * to run, each with its slot (scenePositions) and its sig (the bundle, * words and data it was made with). A frame is kept for the same slot * in the same box made from the same sig (inside a block: that very * block); the rest of the wanted are made, and every frame not kept is * dropped. */ export function planFrames (frames, wanted) { const keep = [] const make = [] const kept = new Set() for (const p of wanted) { const f = frames.get(p.slot) if (f && f.key === p.key && f.sig === p.sig && (!p.inside || f.block === p.el)) { keep.push(p) kept.add(p.slot) } else { make.push(p) } } const drop = [...frames.keys()].filter(s => !kept.has(s)) return { keep, make, drop } } /** * Whether a presenting window's move from one position to another is a * single press (which animates) rather than a jump: a step on the same * slide, or onto the next slide of the talk from the last step, or back * onto the last step of the slide before. store is the talk * (talk-store.mjs): its view and each slide's presses. */ export function talkMove (store, from, to) { if (!from || !to || !from.slide || !to.slide) return false if (from.slide === to.slide) return Math.abs((to.step || 0) - (from.step || 0)) === 1 const ids = talkOrder(store.view, to.mode).map(i => store.view.slides[i].id) const a = ids.indexOf(from.slide) const b = ids.indexOf(to.slide) if (a < 0 || b < 0) return false const presses = id => Number((store.slide(id) || {}).presses) || 0 if (b === a + 1) return (to.step || 0) === 0 && (from.step || 0) >= presses(from.slide) if (b === a - 1) return (from.step || 0) === 0 && (to.step || 0) === presses(to.slide) return false } export function mountSceneLayer ({ root, deck, base = '', reducedMotion, doc = document, win = window, makeFrame = createSceneFrame }) { if (!root) return { show () {}, place () {}, destroy () {} } const motion = reducedMotion === undefined && win.matchMedia ? win.matchMedia('(prefers-reduced-motion: reduce)') : null const reduce = () => (motion ? motion.matches : !!reducedMotion) if (win.getComputedStyle(root).position === 'static') root.style.position = 'relative' const layer = doc.createElement('div') layer.setAttribute('slides-scene-layer', '') layer.setAttribute('aria-hidden', 'true') // margin:0: on a page main is a stack, whose sibling rule would give it one layer.style.cssText = 'position:absolute;inset:0;margin:0;overflow:hidden;pointer-events:none' root.append(layer) const frames = new Map() // slot → {p, frame, key, sig, block, live, failed} const ro = typeof win.ResizeObserver === 'function' ? new win.ResizeObserver(() => soon()) : null if (ro) ro.observe(root) function live (rec, on) { rec.live = on if (rec.block) rec.block.toggleAttribute('data-scene-live', on) } function over (rec, block) { if (rec.block === block) return if (rec.block) { rec.block.removeAttribute('data-scene-live') if (ro) ro.unobserve(rec.block) } rec.block = block if (ro) ro.observe(block) if (rec.live) block.setAttribute('data-scene-live', '') if (rec.frame && !rec.failed) rec.frame.el.setAttribute('data-for', block.getAttribute('data-id') || '') } function drop (slot) { const rec = frames.get(slot) frames.delete(slot) if (!rec) return if (rec.frame) rec.frame.destroy() else rec.el.remove() if (rec.block) { rec.block.removeAttribute('data-scene-live') if (ro) ro.unobserve(rec.block) } } let drafts = 0 function make (p, submit) { const rec = { p, key: p.key, sig: p.sig, state: p.state, inside: !!p.inside, block: null, live: false, failed: false, frame: null } frames.set(p.slot, rec) const el = rec.el = doc.createElement('iframe') el.setAttribute('title', p.sc.name || '') el.setAttribute('sandbox', 'allow-scripts') el.setAttribute('tabindex', '-1') el.setAttribute('inert', '') // Over the slide it is placed by hand (placeOne); inside its block // slides.css gives it its stills' cell and size. el.style.cssText = (p.inside ? '' : 'position:absolute;') + 'border:0;margin:0;padding:0;background:transparent;pointer-events:none;opacity:0' if (p.inside) el.setAttribute('slides-scene-frame', '') const attach = () => (p.inside ? p.el.append(el) : layer.append(el)) const watch = () => { rec.frame = makeFrame({ el, win, scene: p.sc, state: p.state, reducedMotion: false, fonts: () => frameFonts(p.sc.data, doc, win), onLive: on => live(rec, on), onFail: () => { rec.failed = true } }) over(rec, p.el) placeOne(rec, layer.getBoundingClientRect()) if (submit) return submit(el.name) el.src = p.src attach() } if (!submit) return watch() // A draft's frame goes in empty and named, and is watched once that // empty document has loaded (at once in Chromium, a moment later // elsewhere): the form posted into it is its first load. el.name = `slides-scene-draft-${++drafts}` let blank = false el.addEventListener('load', () => { blank = true }, { once: true }) attach() if (blank) watch() else el.addEventListener('load', watch, { once: true }) } function placeOne (rec, l) { if (rec.failed || rec.inside || !rec.block || !rec.frame) return // Over the still, which is the scene's box: the block's, or in a // flow region the largest 16:9 box that fits it (slides.css). const still = rec.block.querySelector('[data-scene-state]') const box = still && still.getBoundingClientRect() const r = box && box.width > 0 ? box : rec.block.getBoundingClientRect() const s = rec.frame.el.style s.left = `${r.left - l.left}px` s.top = `${r.top - l.top}px` s.width = `${r.width}px` s.height = `${r.height}px` } function place () { if (!frames.size) return const l = layer.getBoundingClientRect() for (const rec of frames.values()) placeOne(rec, l) } // A move of the window or the page places the frames again; one that // crosses the phone's width, or a change of the reader's motion // setting, decides again which scenes run. let queued = false let last = { slideId: null, positions: [], phone: '' } const phoneOf = positions => positions.map(p => (stillsOnly(p.el) ? 1 : 0)).join('') function soon () { if (queued) return queued = true win.requestAnimationFrame(() => { queued = false if (last.positions.length && phoneOf(last.positions) !== last.phone) show(last.slideId, last.positions, { jump: true }) else place() }) } const again = () => show(last.slideId, last.positions, { jump: true }) win.addEventListener('resize', soon) doc.addEventListener('scroll', soon, { capture: true, passive: true }) if (motion && motion.addEventListener) motion.addEventListener('change', again) let shown = null function show (slideId, positions, { jump = false } = {}) { positions = positions || [] last = { slideId, positions, phone: phoneOf(positions) } const d = positions.length && !reduce() ? (typeof deck === 'function' ? deck() : deck) : null const scenes = (d && d.scenes) || {} const wanted = [] for (const p of positions) { if (!Object.hasOwn(scenes, p.scene)) continue const sc = scenes[p.scene] || {} const src = sc.src || (sc.code && base ? `${base}/scenes/${encodeURIComponent(p.scene)}/${encodeURIComponent(sc.code)}` : '') if (!src || !p.state || stillsOnly(p.el)) continue wanted.push({ ...p, sc, src, sig: src + '\n' + JSON.stringify([sc.name || '', sc.words || {}, sc.data === undefined ? null : sc.data]) }) } const plan = planFrames(frames, wanted) for (const slot of plan.drop) drop(slot) for (const p of plan.keep) { const rec = frames.get(p.slot) over(rec, p.el) // Another slide is always said, even at the same state; the same // slide drawn again at the same state is not. if (!rec.failed && rec.frame && (slideId !== shown || rec.state !== p.state)) rec.frame.state(p.state, !jump) rec.state = p.state } shown = slideId for (const p of plan.make) make(p) place() } return { show, place, // A scene's first placement running (the editor runs one, the // block chosen) is the one remade. preview (scene, submit) { const slot = [...frames.keys()].find(k => frames.get(k).p.scene === scene) if (slot === undefined) return false const rec = frames.get(slot) const p = { ...rec.p, el: rec.block || rec.p.el, state: rec.state } drop(slot) make(p, submit) return true }, element: layer, destroy () { for (const slot of [...frames.keys()]) drop(slot) if (ro) ro.disconnect() win.removeEventListener('resize', soon) if (motion && motion.removeEventListener) motion.removeEventListener('change', again) doc.removeEventListener('scroll', soon, { capture: true }) layer.remove() } } }