oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// play-through.mjs: what waits at each press, the control's words, and
2// the stage driven through a slide's presses and back to the finished
3// slide (on a stub DOM: which elements carry data-waiting, and when).
4import test from 'node:test'
5import assert from 'node:assert/strict'
6import { waitingAt, pressesWords, mountPlayThrough } from './play-through.mjs'
7import { el, withDocument } from './dom-stub.mjs'
8
9const para = t => [{ t }]
10// Three presses: a text on 1, a list item by item on 2 and 3 (two
11// items), and a heading with the slide.
12const slide = {
13 id: 's1',
14 blocks: [
15 { id: 'h', type: 'heading', text: [para('Title')] },
16 { id: 'a', type: 'text', step: 1, text: [para('First')] },
17 { id: 'l', type: 'list', step: 2, items: 'each', text: [para('one'), para('two')] }
18 ]
19}
20const plain = { id: 's2', blocks: [{ id: 'h2', type: 'heading', text: [para('Plain')] }] }
21
22const texts = { one: '1 press', many: '{n} presses', exit: 'Press {n} of {total}. Esc shows the finished slide.' }
23
24test('waitingAt is everything whose press comes after the one given', () => {
25 const key = ms => ms.map(m => `${m.block}:${m.item}`).join(' ')
26 assert.equal(key(waitingAt(slide, 0)), 'a:-1 l:0 l:1')
27 assert.equal(key(waitingAt(slide, 1)), 'l:0 l:1')
28 assert.equal(key(waitingAt(slide, 2)), 'l:1')
29 assert.equal(key(waitingAt(slide, 3)), '')
30 assert.equal(key(waitingAt(plain, 0)), '')
31 assert.equal(key(waitingAt(null, 1)), '')
32})
33
34test('pressesWords: the count at rest, the press while playing', () => {
35 assert.equal(pressesWords(texts, 1, 0), '1 press')
36 assert.equal(pressesWords(texts, 3, 0), '3 presses')
37 assert.equal(pressesWords(texts, 3, 2), 'Press 2 of 3. Esc shows the finished slide.')
38 assert.equal(pressesWords(texts, 0, 0), '')
39})
40
41// A stage holding slide's boxes, and the pane's control beside it.
42function stageFor () {
43 const li = [el('li'), el('li')]
44 const boxes = {
45 h: el('h1', { 'slides-block': '', 'data-id': 'h' }),
46 a: el('div', { 'slides-block': '', 'data-id': 'a' }),
47 l: el('ul', { 'slides-block': '', 'data-id': 'l' }, li)
48 }
49 const section = el('section', { 'slides-slide': '' }, Object.values(boxes))
50 const stageEl = el('div', { 'slides-stage': '' }, [section])
51 const button = el('button', { 'slides-presses': '' })
52 button.hidden = true
53 const pane = el('section', { 'slides-stage-pane': '' }, [stageEl, button])
54 return { boxes, li, stageEl, button, pane }
55}
56
57const waiting = s => [...Object.entries(s.boxes).filter(([, b]) => b.hasAttribute('data-waiting')).map(([id]) => id),
58 ...s.li.map((l, i) => l.hasAttribute('data-waiting') ? 'l' + i : null).filter(Boolean)].join(' ')
59
60test('a click plays press 1, each click the next, and one after the last shows the finished slide', () => {
61 const { restore } = withDocument()
62 try {
63 const s = stageFor()
64 let current = slide
65 let exits = 0
66 const play = mountPlayThrough({ stageEl: s.stageEl, getSlide: () => current, onExit: () => exits++, button: s.button, texts })
67 play.refresh()
68 assert.equal(s.button.hidden, false)
69 assert.equal(s.button.textContent, '3 presses')
70 assert.equal(waiting(s), '')
71
72 s.button.dispatchEvent({ type: 'click' })
73 assert.equal(s.button.textContent, 'Press 1 of 3. Esc shows the finished slide.')
74 assert.equal(waiting(s), 'l0 l1')
75 s.button.dispatchEvent({ type: 'keydown', key: 'ArrowRight' })
76 assert.equal(waiting(s), 'l1')
77 s.button.dispatchEvent({ type: 'click' })
78 assert.equal(waiting(s), '')
79 assert.equal(s.button.textContent, 'Press 3 of 3. Esc shows the finished slide.')
80 s.button.dispatchEvent({ type: 'click' })
81 assert.equal(waiting(s), '')
82 assert.equal(s.button.textContent, '3 presses')
83 assert.equal(exits, 1)
84
85 // Esc, and refresh() (a change of slide, any edit), end it too.
86 s.button.dispatchEvent({ type: 'click' })
87 assert.equal(waiting(s), 'l0 l1')
88 document.dispatchEvent({ type: 'keydown', key: 'Escape' })
89 assert.equal(waiting(s), '')
90 assert.equal(s.button.textContent, '3 presses')
91 s.button.dispatchEvent({ type: 'click' })
92 assert.notEqual(waiting(s), '')
93 play.refresh()
94 assert.equal(waiting(s), '')
95 assert.equal(s.button.textContent, '3 presses')
96
97 // A slide without presses: no control.
98 current = plain
99 play.refresh()
100 assert.equal(s.button.hidden, true)
101 play.stop()
102 play.destroy()
103 } finally {
104 restore()
105 }
106})
107
108test('Esc while not playing is left alone', () => {
109 const { restore } = withDocument()
110 try {
111 const s = stageFor()
112 const play = mountPlayThrough({ stageEl: s.stageEl, getSlide: () => slide, button: s.button, texts })
113 play.refresh()
114 const ev = { type: 'keydown', key: 'Escape' }
115 document.dispatchEvent(ev)
116 assert.equal(ev.defaultPrevented, undefined)
117 play.destroy()
118 } finally {
119 restore()
120 }
121})
122
123test('a looping or once clip with a step waits for it; a clip on a press keeps its poster', () => {
124 const { restore } = withDocument()
125 try {
126 const clips = {
127 id: 's3',
128 blocks: [
129 { id: 'p', type: 'clip', props: { asset: 'v', play: 'press' } },
130 { id: 'o', type: 'clip', step: 2, props: { asset: 'v', play: 'loop' } },
131 { id: 'n', type: 'clip', step: 3, props: { asset: 'v', play: 'once' } }
132 ]
133 }
134 const boxes = {
135 p: el('figure', { 'slides-block': '', 'data-id': 'p', 'data-block': 'clip', 'data-clip': 'press' }),
136 o: el('figure', { 'slides-block': '', 'data-id': 'o', 'data-block': 'clip', 'data-clip': 'loop' }),
137 n: el('figure', { 'slides-block': '', 'data-id': 'n', 'data-block': 'clip', 'data-clip': 'once' })
138 }
139 const stageEl = el('div', { 'slides-stage': '' }, [el('section', { 'slides-slide': '' }, Object.values(boxes))])
140 const button = el('button', { 'slides-presses': '' })
141 el('section', { 'slides-stage-pane': '' }, [stageEl, button])
142 const play = mountPlayThrough({ stageEl, getSlide: () => clips, button, texts })
143 play.refresh()
144 assert.equal(button.textContent, '3 presses')
145 const waits = () => Object.entries(boxes).filter(([, b]) => b.hasAttribute('data-waiting')).map(([id]) => id).join(' ')
146 button.dispatchEvent({ type: 'click' })
147 assert.equal(waits(), 'o n', 'press 1: the press clip shows its poster; the others wait')
148 button.dispatchEvent({ type: 'click' })
149 assert.equal(waits(), 'n')
150 button.dispatchEvent({ type: 'click' })
151 assert.equal(waits(), '')
152 play.destroy()
153 } finally {
154 restore()
155 }
156})
157