oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// moves.mjs — the talk's order and where a press takes it, for the
2// browser: internal/slides/deck/talk.go's TalkOrder, Start, Advance and
3// Settle. vectors/moves.json holds the two to one answer, so a rule
4// changes on both sides or on neither.
5//
6// A position is {slide, step, mode, black}: the slide by id, the press
7// on it, 'core' or 'full', and whether the screen is black. A slide of
8// '' is nowhere, which is where an empty talk is. A move is {op, slide}.
9import { presses } from './presses.mjs'
10
11const slidesOf = view => (Array.isArray(view && view.slides) ? view.slides : [])
12const indexOf = (slides, id) => slides.findIndex(s => s.id === id)
13const count = (view, slide) => presses(slide, view && view.scenes).length
14const at = (slide, mode, step = 0) => ({ slide: slide.id, step, mode, black: false })
15const nowhere = mode => ({ slide: '', step: 0, mode, black: false })
16const clamp = (k, n) => Math.min(Math.max(k, 0), n)
17
18/** The deck indices of the slides in the talk: parked never, full-only only in full. */
19export function talkOrder (view, mode) {
20 const out = []
21 slidesOf(view).forEach((s, i) => {
22 if (s.talk === 'parked' || (s.talk === 'full' && mode !== 'full')) return
23 out.push(i)
24 })
25 return out
26}
27
28/** Where a talk begins: the first slide of the core talk. */
29export function start (view) {
30 const order = talkOrder(view, 'core')
31 return order.length ? at(slidesOf(view)[order[0]], 'core') : nowhere('core')
32}
33
34/**
35 * Where a move takes the talk from pos. next and back step through the
36 * slide's presses, then leave it for the neighbouring slide of the talk
37 * by its place in the deck (back lands fully built); at either end they
38 * stay. first, last and goto land at step 0. Each lifts black. goto a
39 * full-only slide in core switches to full; goto a parked or missing
40 * slide changes nothing. mode flips the mode, black toggles, and any
41 * other op changes nothing.
42 */
43export function advance (view, pos, move) {
44 const slides = slidesOf(view)
45 const order = talkOrder(view, pos.mode)
46 switch (move.op) {
47 case 'black':
48 return { ...pos, black: !pos.black }
49 case 'mode':
50 return { ...pos, mode: pos.mode === 'full' ? 'core' : 'full' }
51 case 'goto': {
52 const i = move.slide ? indexOf(slides, move.slide) : -1
53 if (i < 0 || slides[i].talk === 'parked') return pos
54 return at(slides[i], slides[i].talk === 'full' ? 'full' : pos.mode)
55 }
56 case 'first': case 'last':
57 if (!order.length) return nowhere(pos.mode)
58 return at(slides[move.op === 'first' ? order[0] : order[order.length - 1]], pos.mode)
59 case 'next': case 'back':
60 return step(view, pos, move.op === 'next', order)
61 }
62 return pos
63}
64
65function step (view, pos, forward, order) {
66 const slides = slidesOf(view)
67 if (!order.length) return nowhere(pos.mode)
68 const cur = indexOf(slides, pos.slide)
69 if (cur < 0) return { ...settle(view, pos), black: false }
70 const n = count(view, slides[cur])
71 const k = clamp(pos.step, n)
72 if (forward && k < n) return { ...pos, step: k + 1, black: false }
73 if (!forward && k > 0) return { ...pos, step: k - 1, black: false }
74 if (forward) {
75 const next = order.find(i => i > cur)
76 if (next !== undefined) return at(slides[next], pos.mode)
77 } else {
78 const prev = order.findLast(i => i < cur)
79 if (prev !== undefined) return at(slides[prev], pos.mode, count(view, slides[prev]))
80 }
81 return { ...pos, step: k, black: false }
82}
83
84/** settleNear with no place known for a missing slide. */
85export function settle (view, pos) {
86 return settleNear(view, pos, -1)
87}
88
89/**
90 * pos put back on its feet after the deck changed under it. near is the
91 * index pos's slide had in the old deck's full talk order, or -1 when
92 * that is unknown. A missing slide with near known becomes the slide
93 * that followed it: the entry at near in the new full order, or the
94 * first after that which is in pos's mode, at step 0. With near
95 * unknown, or nothing following, it becomes the order's last. A parked
96 * slide becomes the next slide of the order after its place in the deck
97 * at step 0, or the order's last. No slide at all becomes the first. A
98 * step outside the slide's presses clamps into them. An empty talk gives
99 * the mode alone.
100 */
101export function settleNear (view, pos, near) {
102 const slides = slidesOf(view)
103 const order = talkOrder(view, pos.mode)
104 if (!order.length) return nowhere(pos.mode)
105 const landing = i => ({ ...at(slides[i], pos.mode), black: pos.black })
106 const last = order[order.length - 1]
107 if (pos.slide === '') return landing(order[0])
108 const cur = indexOf(slides, pos.slide)
109 if (cur < 0 && near >= 0) {
110 const next = talkOrder(view, 'full').slice(near).find(i => order.includes(i))
111 return landing(next ?? last)
112 }
113 if (cur < 0) return landing(last)
114 if (slides[cur].talk === 'parked') return landing(order.find(i => i > cur) ?? last)
115 return { ...pos, step: clamp(pos.step, count(view, slides[cur])) }
116}
117