oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// scene-pages.mjs — scenes on the pages whose slide the server draws
2// and a script swaps in place (scenes and clips spec §3): the viewer
3// and its scriptless presenting mode (viewer-nav.mjs swaps the root),
4// and the follow view and the projector of a session that does not hold
5// the talk (static/present.js swaps the stage, and steps it in place).
6// Mounted by fonts-gate.mjs, which every slide surface loads; the
7// presenting windows that hold the talk and the editor mount their own
8// layer.
9//
10// The scenes a slide runs come with its page, in a
11// <script type="application/json" slides-scenes> inside what is swapped
12// (scene_pages.go), so they change with the slide. The layer sits in
13// what is never swapped: the viewer root's parent, or the surface. Every
14// change that lands in the DOM is shown again, so a frame always sits
15// over the block now on the page (the layer says nothing to a scene
16// whose slide and state are unchanged). One press apart moves the scene
17// (pageMove); anything else is a jump. The page's clips play there too
18// (clip.mjs).
19import { mountSceneLayer, scenePositions } from './scene-layer.mjs'
20import { mountClips } from './clip.mjs'
21
22/**
23 * Whether a page's move from was to now ({id, index, step, presses}) is
24 * one press: a step on the same slide, onto the next slide from the
25 * last step, or back onto the last step of the slide before. A page
26 * that does not step (the viewer, step null) shows finished slides, and
27 * its next or previous slide is one move.
28 */
29export function pageMove (was, now) {
30 if (!was || !now) return false
31 if (was.id === now.id) return was.step !== null && now.step !== null && Math.abs(now.step - was.step) === 1
32 const d = now.index - was.index
33 if (Math.abs(d) !== 1) return false
34 if (was.step === null || now.step === null) return true
35 return d === 1 ? now.step === 0 && was.step >= was.presses : was.step === 0 && now.step >= now.presses
36}
37
38// The presses a drawn slide has: the latest any of its boxes arrives on.
39function pressesOf (section) {
40 let n = 0
41 for (const el of section.querySelectorAll('[data-press]')) n = Math.max(n, Number(el.getAttribute('data-press')) || 0)
42 return n
43}
44
45export function mountScenePages ({ doc = document, win = window } = {}) {
46 if (typeof win.MutationObserver !== 'function') return null
47 const viewer = () => doc.querySelector('[slides-viewer], [slides-present]')
48 const surface = doc.querySelector('[slides-surface]:not([data-talk])')
49 let host
50 let rootOf
51 if (viewer()) {
52 host = viewer().parentElement
53 rootOf = viewer
54 } else if (surface && surface.querySelector('[slides-stage]')) {
55 host = surface
56 rootOf = () => surface
57 }
58 if (!host) return null
59 // A slide's clips play over their posters there too (clip.mjs).
60 mountClips({ root: host, doc, win })
61
62 const scenesOf = root => {
63 const s = root && root.querySelector('script[slides-scenes]')
64 try {
65 return { text: s ? s.textContent : '', scenes: s ? JSON.parse(s.textContent) || {} : {} }
66 } catch {
67 return { text: '', scenes: {} }
68 }
69 }
70 let current = { text: '', scenes: {} }
71 const layer = mountSceneLayer({ root: host, deck: () => current, doc, win })
72
73 let was = null
74 function sync () {
75 const root = rootOf()
76 const section = root && root.querySelector('[slides-slide]')
77 const hidden = !section || (root.querySelector('[slides-stage]') || section).hidden
78 current = scenesOf(root)
79 const positions = hidden ? [] : scenePositions(root)
80 const step = root && root.hasAttribute('data-step') ? Number(root.getAttribute('data-step')) || 0 : null
81 const now = section && !hidden
82 ? { id: section.getAttribute('data-id'), index: Number(section.getAttribute('data-index')), step, presses: pressesOf(section) }
83 : null
84 const moved = !was || !now || was.id !== now.id || was.step !== now.step
85 layer.show(now ? now.id : '', positions, { jump: moved ? !pageMove(was, now) : true })
86 if (moved) was = now
87 }
88 new win.MutationObserver(records => {
89 if (records.every(r => layer.element.contains(r.target))) return
90 sync()
91 }).observe(host, { childList: true, subtree: true, attributes: true, attributeFilter: ['data-waiting', 'hidden'] })
92 sync()
93 return layer
94}
95