| 1 | // scene-layer.mjs — scenes running over a slide, and carried on between |
| 2 | // slides (scenes and clips spec §3, §8.7). |
| 3 | // |
| 4 | // A slide's scene block is drawn by the server as its stills |
| 5 | // (render.go's sceneBlock): the states it passes through, the newest |
| 6 | // that has arrived shown. Where scripts run, this puts the scene's own |
| 7 | // frame (scene-frame.mjs) over the block, in a layer of its own outside |
| 8 | // the slide — a sibling over the stage, so that swapping the slide |
| 9 | // never moves a frame to another parent, which would reload it. One |
| 10 | // frame per placement: a scene's blocks on a slide are its placements, |
| 11 | // numbered in the slide's order (scene-places.mjs, their slots), and |
| 12 | // each runs. The next slide's placements are paired with these by that |
| 13 | // number: when the next slide's first block of a scene is in the same |
| 14 | // box (the same box on a canvas, the same region in a flow) as this |
| 15 | // slide's first, its frame is kept and told the new state, moving |
| 16 | // unless it was a jump, and so on for the second; the rest are made |
| 17 | // afresh at the state their slide is at. A block with |
| 18 | // something drawn over it (scene-places.mjs) runs inside its own block |
| 19 | // instead, so the slide's stacking holds; such a frame lives and dies |
| 20 | // with its block. The still stays under the frame as what a frame that |
| 21 | // is not ready shows, and is what assistive technology reads; the frame |
| 22 | // is inert. A phone view, and a reader who asked for less motion, get |
| 23 | // the stills and no frame (each still is its state's settled end); a |
| 24 | // window crossing the phone's width is followed. |
| 25 | // |
| 26 | // mountSceneLayer({root, deck, base, reducedMotion}) → {show(slideId, |
| 27 | // positions, {jump}), place(), preview(scene, submit), destroy()}. |
| 28 | // root is the element the layer sits in, over the stage, never swapped; |
| 29 | // deck the deck's view, or a function giving it (its scenes by id, each |
| 30 | // with its code, base making the bundle's address, or its src); |
| 31 | // positions scenePositions(the stage) or a part of them. preview remakes |
| 32 | // a scene's frame, named, for submit(name) to post a draft into. |
| 33 | import { createSceneFrame } from './scene-frame.mjs' |
| 34 | import { talkOrder } from './moves.mjs' |
| 35 | import { stillsOnly } from './scene-places.mjs' |
| 36 | import { frameFonts } from './scene-fonts.mjs' |
| 37 | export { scenePositions, placeKey, stillsOnly } from './scene-places.mjs' |
| 38 | export { sceneFonts } from './scene-fonts.mjs' |
| 39 | |
| 40 | /** |
| 41 | * Which frames stay: frames is slot → {key, sig}; wanted the positions |
| 42 | * to run, each with its slot (scenePositions) and its sig (the bundle, |
| 43 | * words and data it was made with). A frame is kept for the same slot |
| 44 | * in the same box made from the same sig (inside a block: that very |
| 45 | * block); the rest of the wanted are made, and every frame not kept is |
| 46 | * dropped. |
| 47 | */ |
| 48 | export function planFrames (frames, wanted) { |
| 49 | const keep = [] |
| 50 | const make = [] |
| 51 | const kept = new Set() |
| 52 | for (const p of wanted) { |
| 53 | const f = frames.get(p.slot) |
| 54 | if (f && f.key === p.key && f.sig === p.sig && (!p.inside || f.block === p.el)) { |
| 55 | keep.push(p) |
| 56 | kept.add(p.slot) |
| 57 | } else { |
| 58 | make.push(p) |
| 59 | } |
| 60 | } |
| 61 | const drop = [...frames.keys()].filter(s => !kept.has(s)) |
| 62 | return { keep, make, drop } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Whether a presenting window's move from one position to another is a |
| 67 | * single press (which animates) rather than a jump: a step on the same |
| 68 | * slide, or onto the next slide of the talk from the last step, or back |
| 69 | * onto the last step of the slide before. store is the talk |
| 70 | * (talk-store.mjs): its view and each slide's presses. |
| 71 | */ |
| 72 | export function talkMove (store, from, to) { |
| 73 | if (!from || !to || !from.slide || !to.slide) return false |
| 74 | if (from.slide === to.slide) return Math.abs((to.step || 0) - (from.step || 0)) === 1 |
| 75 | const ids = talkOrder(store.view, to.mode).map(i => store.view.slides[i].id) |
| 76 | const a = ids.indexOf(from.slide) |
| 77 | const b = ids.indexOf(to.slide) |
| 78 | if (a < 0 || b < 0) return false |
| 79 | const presses = id => Number((store.slide(id) || {}).presses) || 0 |
| 80 | if (b === a + 1) return (to.step || 0) === 0 && (from.step || 0) >= presses(from.slide) |
| 81 | if (b === a - 1) return (from.step || 0) === 0 && (to.step || 0) === presses(to.slide) |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | export function mountSceneLayer ({ root, deck, base = '', reducedMotion, doc = document, win = window, makeFrame = createSceneFrame }) { |
| 86 | if (!root) return { show () {}, place () {}, destroy () {} } |
| 87 | const motion = reducedMotion === undefined && win.matchMedia ? win.matchMedia('(prefers-reduced-motion: reduce)') : null |
| 88 | const reduce = () => (motion ? motion.matches : !!reducedMotion) |
| 89 | if (win.getComputedStyle(root).position === 'static') root.style.position = 'relative' |
| 90 | const layer = doc.createElement('div') |
| 91 | layer.setAttribute('slides-scene-layer', '') |
| 92 | layer.setAttribute('aria-hidden', 'true') |
| 93 | // margin:0: on a page main is a stack, whose sibling rule would give it one |
| 94 | layer.style.cssText = 'position:absolute;inset:0;margin:0;overflow:hidden;pointer-events:none' |
| 95 | root.append(layer) |
| 96 | const frames = new Map() // slot → {p, frame, key, sig, block, live, failed} |
| 97 | const ro = typeof win.ResizeObserver === 'function' ? new win.ResizeObserver(() => soon()) : null |
| 98 | if (ro) ro.observe(root) |
| 99 | |
| 100 | function live (rec, on) { |
| 101 | rec.live = on |
| 102 | if (rec.block) rec.block.toggleAttribute('data-scene-live', on) |
| 103 | } |
| 104 | |
| 105 | function over (rec, block) { |
| 106 | if (rec.block === block) return |
| 107 | if (rec.block) { |
| 108 | rec.block.removeAttribute('data-scene-live') |
| 109 | if (ro) ro.unobserve(rec.block) |
| 110 | } |
| 111 | rec.block = block |
| 112 | if (ro) ro.observe(block) |
| 113 | if (rec.live) block.setAttribute('data-scene-live', '') |
| 114 | if (rec.frame && !rec.failed) rec.frame.el.setAttribute('data-for', block.getAttribute('data-id') || '') |
| 115 | } |
| 116 | |
| 117 | function drop (slot) { |
| 118 | const rec = frames.get(slot) |
| 119 | frames.delete(slot) |
| 120 | if (!rec) return |
| 121 | if (rec.frame) rec.frame.destroy() |
| 122 | else rec.el.remove() |
| 123 | if (rec.block) { |
| 124 | rec.block.removeAttribute('data-scene-live') |
| 125 | if (ro) ro.unobserve(rec.block) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | let drafts = 0 |
| 130 | function make (p, submit) { |
| 131 | const rec = { p, key: p.key, sig: p.sig, state: p.state, inside: !!p.inside, block: null, live: false, failed: false, frame: null } |
| 132 | frames.set(p.slot, rec) |
| 133 | const el = rec.el = doc.createElement('iframe') |
| 134 | el.setAttribute('title', p.sc.name || '') |
| 135 | el.setAttribute('sandbox', 'allow-scripts') |
| 136 | el.setAttribute('tabindex', '-1') |
| 137 | el.setAttribute('inert', '') |
| 138 | // Over the slide it is placed by hand (placeOne); inside its block |
| 139 | // slides.css gives it its stills' cell and size. |
| 140 | el.style.cssText = (p.inside ? '' : 'position:absolute;') + 'border:0;margin:0;padding:0;background:transparent;pointer-events:none;opacity:0' |
| 141 | if (p.inside) el.setAttribute('slides-scene-frame', '') |
| 142 | const attach = () => (p.inside ? p.el.append(el) : layer.append(el)) |
| 143 | const watch = () => { |
| 144 | rec.frame = makeFrame({ |
| 145 | el, |
| 146 | win, |
| 147 | scene: p.sc, |
| 148 | state: p.state, |
| 149 | reducedMotion: false, |
| 150 | fonts: () => frameFonts(p.sc.data, doc, win), |
| 151 | onLive: on => live(rec, on), |
| 152 | onFail: () => { rec.failed = true } |
| 153 | }) |
| 154 | over(rec, p.el) |
| 155 | placeOne(rec, layer.getBoundingClientRect()) |
| 156 | if (submit) return submit(el.name) |
| 157 | el.src = p.src |
| 158 | attach() |
| 159 | } |
| 160 | if (!submit) return watch() |
| 161 | // A draft's frame goes in empty and named, and is watched once that |
| 162 | // empty document has loaded (at once in Chromium, a moment later |
| 163 | // elsewhere): the form posted into it is its first load. |
| 164 | el.name = `slides-scene-draft-${++drafts}` |
| 165 | let blank = false |
| 166 | el.addEventListener('load', () => { blank = true }, { once: true }) |
| 167 | attach() |
| 168 | if (blank) watch() |
| 169 | else el.addEventListener('load', watch, { once: true }) |
| 170 | } |
| 171 | |
| 172 | function placeOne (rec, l) { |
| 173 | if (rec.failed || rec.inside || !rec.block || !rec.frame) return |
| 174 | // Over the still, which is the scene's box: the block's, or in a |
| 175 | // flow region the largest 16:9 box that fits it (slides.css). |
| 176 | const still = rec.block.querySelector('[data-scene-state]') |
| 177 | const box = still && still.getBoundingClientRect() |
| 178 | const r = box && box.width > 0 ? box : rec.block.getBoundingClientRect() |
| 179 | const s = rec.frame.el.style |
| 180 | s.left = `${r.left - l.left}px` |
| 181 | s.top = `${r.top - l.top}px` |
| 182 | s.width = `${r.width}px` |
| 183 | s.height = `${r.height}px` |
| 184 | } |
| 185 | |
| 186 | function place () { |
| 187 | if (!frames.size) return |
| 188 | const l = layer.getBoundingClientRect() |
| 189 | for (const rec of frames.values()) placeOne(rec, l) |
| 190 | } |
| 191 | |
| 192 | // A move of the window or the page places the frames again; one that |
| 193 | // crosses the phone's width, or a change of the reader's motion |
| 194 | // setting, decides again which scenes run. |
| 195 | let queued = false |
| 196 | let last = { slideId: null, positions: [], phone: '' } |
| 197 | const phoneOf = positions => positions.map(p => (stillsOnly(p.el) ? 1 : 0)).join('') |
| 198 | function soon () { |
| 199 | if (queued) return |
| 200 | queued = true |
| 201 | win.requestAnimationFrame(() => { |
| 202 | queued = false |
| 203 | if (last.positions.length && phoneOf(last.positions) !== last.phone) show(last.slideId, last.positions, { jump: true }) |
| 204 | else place() |
| 205 | }) |
| 206 | } |
| 207 | const again = () => show(last.slideId, last.positions, { jump: true }) |
| 208 | win.addEventListener('resize', soon) |
| 209 | doc.addEventListener('scroll', soon, { capture: true, passive: true }) |
| 210 | if (motion && motion.addEventListener) motion.addEventListener('change', again) |
| 211 | |
| 212 | let shown = null |
| 213 | function show (slideId, positions, { jump = false } = {}) { |
| 214 | positions = positions || [] |
| 215 | last = { slideId, positions, phone: phoneOf(positions) } |
| 216 | const d = positions.length && !reduce() ? (typeof deck === 'function' ? deck() : deck) : null |
| 217 | const scenes = (d && d.scenes) || {} |
| 218 | const wanted = [] |
| 219 | for (const p of positions) { |
| 220 | if (!Object.hasOwn(scenes, p.scene)) continue |
| 221 | const sc = scenes[p.scene] || {} |
| 222 | const src = sc.src || (sc.code && base ? `${base}/scenes/${encodeURIComponent(p.scene)}/${encodeURIComponent(sc.code)}` : '') |
| 223 | if (!src || !p.state || stillsOnly(p.el)) continue |
| 224 | wanted.push({ ...p, sc, src, sig: src + '\n' + JSON.stringify([sc.name || '', sc.words || {}, sc.data === undefined ? null : sc.data]) }) |
| 225 | } |
| 226 | const plan = planFrames(frames, wanted) |
| 227 | for (const slot of plan.drop) drop(slot) |
| 228 | for (const p of plan.keep) { |
| 229 | const rec = frames.get(p.slot) |
| 230 | over(rec, p.el) |
| 231 | // Another slide is always said, even at the same state; the same |
| 232 | // slide drawn again at the same state is not. |
| 233 | if (!rec.failed && rec.frame && (slideId !== shown || rec.state !== p.state)) rec.frame.state(p.state, !jump) |
| 234 | rec.state = p.state |
| 235 | } |
| 236 | shown = slideId |
| 237 | for (const p of plan.make) make(p) |
| 238 | place() |
| 239 | } |
| 240 | |
| 241 | return { |
| 242 | show, |
| 243 | place, |
| 244 | // A scene's first placement running (the editor runs one, the |
| 245 | // block chosen) is the one remade. |
| 246 | preview (scene, submit) { |
| 247 | const slot = [...frames.keys()].find(k => frames.get(k).p.scene === scene) |
| 248 | if (slot === undefined) return false |
| 249 | const rec = frames.get(slot) |
| 250 | const p = { ...rec.p, el: rec.block || rec.p.el, state: rec.state } |
| 251 | drop(slot) |
| 252 | make(p, submit) |
| 253 | return true |
| 254 | }, |
| 255 | element: layer, |
| 256 | destroy () { |
| 257 | for (const slot of [...frames.keys()]) drop(slot) |
| 258 | if (ro) ro.disconnect() |
| 259 | win.removeEventListener('resize', soon) |
| 260 | if (motion && motion.removeEventListener) motion.removeEventListener('change', again) |
| 261 | doc.removeEventListener('scroll', soon, { capture: true }) |
| 262 | layer.remove() |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |