// moves.mjs — the talk's order and where a press takes it, for the // browser: internal/slides/deck/talk.go's TalkOrder, Start, Advance and // Settle. vectors/moves.json holds the two to one answer, so a rule // changes on both sides or on neither. // // A position is {slide, step, mode, black}: the slide by id, the press // on it, 'core' or 'full', and whether the screen is black. A slide of // '' is nowhere, which is where an empty talk is. A move is {op, slide}. import { presses } from './presses.mjs' const slidesOf = view => (Array.isArray(view && view.slides) ? view.slides : []) const indexOf = (slides, id) => slides.findIndex(s => s.id === id) const count = (view, slide) => presses(slide, view && view.scenes).length const at = (slide, mode, step = 0) => ({ slide: slide.id, step, mode, black: false }) const nowhere = mode => ({ slide: '', step: 0, mode, black: false }) const clamp = (k, n) => Math.min(Math.max(k, 0), n) /** The deck indices of the slides in the talk: parked never, full-only only in full. */ export function talkOrder (view, mode) { const out = [] slidesOf(view).forEach((s, i) => { if (s.talk === 'parked' || (s.talk === 'full' && mode !== 'full')) return out.push(i) }) return out } /** Where a talk begins: the first slide of the core talk. */ export function start (view) { const order = talkOrder(view, 'core') return order.length ? at(slidesOf(view)[order[0]], 'core') : nowhere('core') } /** * Where a move takes the talk from pos. next and back step through the * slide's presses, then leave it for the neighbouring slide of the talk * by its place in the deck (back lands fully built); at either end they * stay. first, last and goto land at step 0. Each lifts black. goto a * full-only slide in core switches to full; goto a parked or missing * slide changes nothing. mode flips the mode, black toggles, and any * other op changes nothing. */ export function advance (view, pos, move) { const slides = slidesOf(view) const order = talkOrder(view, pos.mode) switch (move.op) { case 'black': return { ...pos, black: !pos.black } case 'mode': return { ...pos, mode: pos.mode === 'full' ? 'core' : 'full' } case 'goto': { const i = move.slide ? indexOf(slides, move.slide) : -1 if (i < 0 || slides[i].talk === 'parked') return pos return at(slides[i], slides[i].talk === 'full' ? 'full' : pos.mode) } case 'first': case 'last': if (!order.length) return nowhere(pos.mode) return at(slides[move.op === 'first' ? order[0] : order[order.length - 1]], pos.mode) case 'next': case 'back': return step(view, pos, move.op === 'next', order) } return pos } function step (view, pos, forward, order) { const slides = slidesOf(view) if (!order.length) return nowhere(pos.mode) const cur = indexOf(slides, pos.slide) if (cur < 0) return { ...settle(view, pos), black: false } const n = count(view, slides[cur]) const k = clamp(pos.step, n) if (forward && k < n) return { ...pos, step: k + 1, black: false } if (!forward && k > 0) return { ...pos, step: k - 1, black: false } if (forward) { const next = order.find(i => i > cur) if (next !== undefined) return at(slides[next], pos.mode) } else { const prev = order.findLast(i => i < cur) if (prev !== undefined) return at(slides[prev], pos.mode, count(view, slides[prev])) } return { ...pos, step: k, black: false } } /** settleNear with no place known for a missing slide. */ export function settle (view, pos) { return settleNear(view, pos, -1) } /** * pos put back on its feet after the deck changed under it. near is the * index pos's slide had in the old deck's full talk order, or -1 when * that is unknown. A missing slide with near known becomes the slide * that followed it: the entry at near in the new full order, or the * first after that which is in pos's mode, at step 0. With near * unknown, or nothing following, it becomes the order's last. A parked * slide becomes the next slide of the order after its place in the deck * at step 0, or the order's last. No slide at all becomes the first. A * step outside the slide's presses clamps into them. An empty talk gives * the mode alone. */ export function settleNear (view, pos, near) { const slides = slidesOf(view) const order = talkOrder(view, pos.mode) if (!order.length) return nowhere(pos.mode) const landing = i => ({ ...at(slides[i], pos.mode), black: pos.black }) const last = order[order.length - 1] if (pos.slide === '') return landing(order[0]) const cur = indexOf(slides, pos.slide) if (cur < 0 && near >= 0) { const next = talkOrder(view, 'full').slice(near).find(i => order.includes(i)) return landing(next ?? last) } if (cur < 0) return landing(last) if (slides[cur].talk === 'parked') return landing(order.find(i => i > cur) ?? last) return { ...pos, step: clamp(pos.step, count(view, slides[cur])) } }