| 1 | // talk-draw.mjs — what both presenting windows draw a slide with, from |
| 2 | // the talk they hold (talk-store.mjs): a slide at a step, a step |
| 3 | // applied in place, the counter's place in the talk, and the catalog's |
| 4 | // sentences filled in. |
| 5 | // |
| 6 | // A slide comes from the server rendered at press 0, every box that |
| 7 | // arrives on a press marked data-press (render.go). At step k a box |
| 8 | // whose press is after k is data-waiting, and keeps its space |
| 9 | // (slides.css). Moving to another step of the slide on screen is done in |
| 10 | // place: what has just arrived is marked data-arriving until its effect |
| 11 | // ends (slides.css draws it, and leaves it out under reduced motion); |
| 12 | // back marks nothing. The same as static/present.js does for the follow |
| 13 | // link and the remote. |
| 14 | import { talkOrder } from './moves.mjs' |
| 15 | // A slide's clips play over their posters in either window's Now (clip.mjs). |
| 16 | export { mountClips } from './clip.mjs' |
| 17 | |
| 18 | /** Marks what is still to come at step, without animating anything. */ |
| 19 | export function waitAt (container, step) { |
| 20 | for (const el of container.querySelectorAll('[data-press]')) { |
| 21 | el.removeAttribute('data-arriving') |
| 22 | if (Number(el.getAttribute('data-press')) > step) el.setAttribute('data-waiting', '') |
| 23 | else el.removeAttribute('data-waiting') |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** Draws a slide's html in container at step, in one go. */ |
| 28 | export function install (container, html, step) { |
| 29 | const t = document.createElement('template') |
| 30 | t.innerHTML = html |
| 31 | waitAt(t.content, step) |
| 32 | container.replaceChildren(t.content) |
| 33 | } |
| 34 | |
| 35 | function arrived (e) { |
| 36 | if (e.target !== e.currentTarget) return |
| 37 | e.currentTarget.removeAttribute('data-arriving') |
| 38 | e.currentTarget.removeEventListener('animationend', arrived) |
| 39 | } |
| 40 | |
| 41 | /** Moves the slide in container to step in place; only what arrives is marked arriving. */ |
| 42 | export function reveal (container, step) { |
| 43 | for (const el of container.querySelectorAll('[data-arriving]')) el.removeAttribute('data-arriving') |
| 44 | for (const el of container.querySelectorAll('[data-press]')) { |
| 45 | if (Number(el.getAttribute('data-press')) > step) { |
| 46 | el.setAttribute('data-waiting', '') |
| 47 | } else if (el.hasAttribute('data-waiting')) { |
| 48 | el.removeAttribute('data-waiting') |
| 49 | el.setAttribute('data-arriving', '') |
| 50 | el.addEventListener('animationend', arrived) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** A catalog sentence with its {name}s filled in. */ |
| 56 | export function fill (pattern, values) { |
| 57 | return String(pattern || '').replace(/\{(\w+)\}/g, (m, k) => (k in values ? String(values[k]) : m)) |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * The counter's "n / total" for the slide at index of view: its place |
| 62 | * in the talk being given, in mode. A slide outside that talk (a |
| 63 | * full-only slide shown in the short version) takes the place of the |
| 64 | * slide before it (presenting_position.go's talkPlace). |
| 65 | */ |
| 66 | export function talkPlace (view, mode, index) { |
| 67 | const order = talkOrder(view, mode) |
| 68 | if (!order.length) return { n: index + 1, total: view.slides.length } |
| 69 | let n = 1 |
| 70 | order.forEach((i, k) => { if (i <= index) n = k + 1 }) |
| 71 | return { n, total: order.length } |
| 72 | } |
| 73 | |
| 74 | /** The index of the slide with id in view, or -1. */ |
| 75 | export function indexOf (view, id) { |
| 76 | return view.slides.findIndex(s => s.id === id) |
| 77 | } |
| 78 | |
| 79 | /** Restarts a CSS animation by flipping the attribute it hangs on between two values. */ |
| 80 | export function restart (el, name) { |
| 81 | if (!el) return |
| 82 | el.setAttribute(name, el.getAttribute(name) === 'a' ? 'b' : 'a') |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Next's preview: preview(store, id, step, words) asks for slide id at |
| 87 | * step (null for the end of the talk), and put({html, step, words, seq}) |
| 88 | * draws it once the slide is ready, so Next never shows a slide |
| 89 | * unfinished; until then the preview there is stays. The newest ask |
| 90 | * wins, and an ask made of one version of the deck puts up nothing once |
| 91 | * the window holds another (the next ask, made when the window draws |
| 92 | * from that version, does): the slide must be ready in the version it |
| 93 | * is drawn from, at the moment it is drawn. |
| 94 | */ |
| 95 | export function createPreview (put) { |
| 96 | let asked = 0 |
| 97 | return function preview (store, id, step, words) { |
| 98 | const mine = ++asked |
| 99 | const seq = store.deckSeq |
| 100 | const go = () => { |
| 101 | if (mine !== asked || store.deckSeq !== seq) return |
| 102 | if (id !== null && !store.has(id)) return |
| 103 | const rec = id === null ? null : store.slide(id) |
| 104 | if (id !== null && !rec) return |
| 105 | put({ html: rec ? rec.html : null, step, words, seq }) |
| 106 | } |
| 107 | if (id === null || store.has(id)) go() |
| 108 | else store.ready(id).then(ok => { if (ok) go() }) |
| 109 | } |
| 110 | } |
| 111 | |