// talk-draw.mjs — what both presenting windows draw a slide with, from // the talk they hold (talk-store.mjs): a slide at a step, a step // applied in place, the counter's place in the talk, and the catalog's // sentences filled in. // // A slide comes from the server rendered at press 0, every box that // arrives on a press marked data-press (render.go). At step k a box // whose press is after k is data-waiting, and keeps its space // (slides.css). Moving to another step of the slide on screen is done in // place: what has just arrived is marked data-arriving until its effect // ends (slides.css draws it, and leaves it out under reduced motion); // back marks nothing. The same as static/present.js does for the follow // link and the remote. import { talkOrder } from './moves.mjs' // A slide's clips play over their posters in either window's Now (clip.mjs). export { mountClips } from './clip.mjs' /** Marks what is still to come at step, without animating anything. */ export function waitAt (container, step) { for (const el of container.querySelectorAll('[data-press]')) { el.removeAttribute('data-arriving') if (Number(el.getAttribute('data-press')) > step) el.setAttribute('data-waiting', '') else el.removeAttribute('data-waiting') } } /** Draws a slide's html in container at step, in one go. */ export function install (container, html, step) { const t = document.createElement('template') t.innerHTML = html waitAt(t.content, step) container.replaceChildren(t.content) } function arrived (e) { if (e.target !== e.currentTarget) return e.currentTarget.removeAttribute('data-arriving') e.currentTarget.removeEventListener('animationend', arrived) } /** Moves the slide in container to step in place; only what arrives is marked arriving. */ export function reveal (container, step) { for (const el of container.querySelectorAll('[data-arriving]')) el.removeAttribute('data-arriving') for (const el of container.querySelectorAll('[data-press]')) { if (Number(el.getAttribute('data-press')) > step) { el.setAttribute('data-waiting', '') } else if (el.hasAttribute('data-waiting')) { el.removeAttribute('data-waiting') el.setAttribute('data-arriving', '') el.addEventListener('animationend', arrived) } } } /** A catalog sentence with its {name}s filled in. */ export function fill (pattern, values) { return String(pattern || '').replace(/\{(\w+)\}/g, (m, k) => (k in values ? String(values[k]) : m)) } /** * The counter's "n / total" for the slide at index of view: its place * in the talk being given, in mode. A slide outside that talk (a * full-only slide shown in the short version) takes the place of the * slide before it (presenting_position.go's talkPlace). */ export function talkPlace (view, mode, index) { const order = talkOrder(view, mode) if (!order.length) return { n: index + 1, total: view.slides.length } let n = 1 order.forEach((i, k) => { if (i <= index) n = k + 1 }) return { n, total: order.length } } /** The index of the slide with id in view, or -1. */ export function indexOf (view, id) { return view.slides.findIndex(s => s.id === id) } /** Restarts a CSS animation by flipping the attribute it hangs on between two values. */ export function restart (el, name) { if (!el) return el.setAttribute(name, el.getAttribute(name) === 'a' ? 'b' : 'a') } /** * Next's preview: preview(store, id, step, words) asks for slide id at * step (null for the end of the talk), and put({html, step, words, seq}) * draws it once the slide is ready, so Next never shows a slide * unfinished; until then the preview there is stays. The newest ask * wins, and an ask made of one version of the deck puts up nothing once * the window holds another (the next ask, made when the window draws * from that version, does): the slide must be ready in the version it * is drawn from, at the moment it is drawn. */ export function createPreview (put) { let asked = 0 return function preview (store, id, step, words) { const mine = ++asked const seq = store.deckSeq const go = () => { if (mine !== asked || store.deckSeq !== seq) return if (id !== null && !store.has(id)) return const rec = id === null ? null : store.slide(id) if (id !== null && !rec) return put({ html: rec ? rec.html : null, step, words, seq }) } if (id === null || store.has(id)) go() else store.ready(id).then(ok => { if (ok) go() }) } }