| 1 | // clip.mjs — a clip block's video, played over its poster (scenes and |
| 2 | // clips spec §2; plan call R3; Review Focus 5). |
| 3 | // |
| 4 | // The server draws a clip block as its poster, an <img> with the clip's |
| 5 | // description (render.go's clipBlock): what scripts off, a thumbnail, |
| 6 | // the PDF, the presenter's Next and the remote show. Where this runs, a |
| 7 | // <video> goes into the block over the poster, always muted, inline and |
| 8 | // without controls, and plays as the block's data-clip says: |
| 9 | // |
| 10 | // press once its press has come. A slide drawn at a press marks that |
| 11 | // press on a child of the block, data-clip-press, which waits |
| 12 | // (data-waiting) until it comes, as a list item does; back |
| 13 | // marks it waiting again, and the video is paused and rewound. |
| 14 | // A slide drawn finished (the viewer) has had every press. |
| 15 | // loop from when the block arrives, round and round. |
| 16 | // once from when the block arrives, to its end, where it stays. |
| 17 | // |
| 18 | // A reader who asked for less motion is shown the poster of a loop or a |
| 19 | // once, and of a press nobody made; a press they make still plays. The |
| 20 | // poster stays until the video plays (data-clip-live on the block, which |
| 21 | // slides.css shows the video for), so nothing flashes, and if the video |
| 22 | // cannot be had or played it is taken away and the poster is all there |
| 23 | // is; its press still counts, being the slide's, not the video's. |
| 24 | // |
| 25 | // mountClips({root}) watches root, the part of the page slides are |
| 26 | // drawn into, and keeps every clip block there in step with what has |
| 27 | // arrived; sync() does it now. → {sync, destroy}. |
| 28 | |
| 29 | const CLIPS = '[slides-block][data-block="clip"][data-clip-src]' |
| 30 | |
| 31 | /** Whether a clip block should be playing: arrived, pressed when it plays on a press, and motion allowed for what nobody pressed. */ |
| 32 | export function clipWants (block, { reduce = false, within = null } = {}) { |
| 33 | for (let n = block; n && n !== within; n = n.parentNode) { |
| 34 | if (n.hasAttribute && (n.hasAttribute('data-waiting') || n.hidden)) return false |
| 35 | } |
| 36 | if (block.getAttribute('data-clip') === 'press') { |
| 37 | const mark = [...(block.children || [])].find(c => c.hasAttribute('data-clip-press')) |
| 38 | return mark ? !mark.hasAttribute('data-waiting') : !reduce |
| 39 | } |
| 40 | return !reduce |
| 41 | } |
| 42 | |
| 43 | export function mountClips ({ root, reducedMotion, doc = document, win = window }) { |
| 44 | if (!root) return { sync () {}, destroy () {} } |
| 45 | const motion = reducedMotion === undefined && win.matchMedia ? win.matchMedia('(prefers-reduced-motion: reduce)') : null |
| 46 | const reduce = () => (motion ? motion.matches : !!reducedMotion) |
| 47 | const clips = new WeakMap() // block → {video, wants, failed} |
| 48 | |
| 49 | function fail (block, rec) { |
| 50 | rec.failed = true |
| 51 | block.removeAttribute('data-clip-live') |
| 52 | if (rec.video) rec.video.remove() |
| 53 | rec.video = null |
| 54 | } |
| 55 | |
| 56 | function make (block) { |
| 57 | const v = doc.createElement('video') |
| 58 | v.setAttribute('slides-clip-video', '') |
| 59 | v.muted = true |
| 60 | v.defaultMuted = true |
| 61 | v.setAttribute('muted', '') |
| 62 | v.playsInline = true |
| 63 | v.setAttribute('playsinline', '') |
| 64 | v.setAttribute('preload', 'auto') |
| 65 | v.setAttribute('aria-hidden', 'true') |
| 66 | v.setAttribute('tabindex', '-1') |
| 67 | v.disablePictureInPicture = true |
| 68 | v.loop = block.getAttribute('data-clip') === 'loop' |
| 69 | const rec = { video: v, wants: false, failed: false } |
| 70 | v.addEventListener('playing', () => { if (rec.wants && !rec.failed) block.setAttribute('data-clip-live', '') }) |
| 71 | v.addEventListener('error', () => fail(block, rec)) |
| 72 | v.src = block.getAttribute('data-clip-src') |
| 73 | block.append(v) |
| 74 | clips.set(block, rec) |
| 75 | return rec |
| 76 | } |
| 77 | |
| 78 | function apply (block, rec, wants) { |
| 79 | if (rec.failed || wants === rec.wants) return |
| 80 | rec.wants = wants |
| 81 | const v = rec.video |
| 82 | if (wants) { |
| 83 | if (v.currentTime) v.currentTime = 0 |
| 84 | const p = v.play() |
| 85 | // A browser that cannot decode it says so here; anything else |
| 86 | // (a pause before it began) is not the video's fault. |
| 87 | if (p && p.catch) p.catch(err => { if (err && err.name === 'NotSupportedError') fail(block, rec) }) |
| 88 | } else { |
| 89 | v.pause() |
| 90 | v.currentTime = 0 |
| 91 | block.removeAttribute('data-clip-live') |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | function sync () { |
| 96 | for (const block of root.querySelectorAll(CLIPS)) { |
| 97 | const rec = clips.get(block) || make(block) |
| 98 | if (!rec.failed) apply(block, rec, clipWants(block, { reduce: reduce(), within: root.parentNode })) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | let observer = null |
| 103 | if (typeof win.MutationObserver === 'function') { |
| 104 | observer = new win.MutationObserver(sync) |
| 105 | observer.observe(root, { childList: true, subtree: true, attributes: true, attributeFilter: ['data-waiting', 'hidden'] }) |
| 106 | } |
| 107 | if (motion && motion.addEventListener) motion.addEventListener('change', sync) |
| 108 | sync() |
| 109 | |
| 110 | return { |
| 111 | sync, |
| 112 | destroy () { |
| 113 | if (observer) observer.disconnect() |
| 114 | if (motion && motion.removeEventListener) motion.removeEventListener('change', sync) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |