oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Each inspector control produces the one op it stands for
2// (inspector-ops.mjs), and every op it produces is one the ops module
3// accepts: each is applied to a real deck built through ops.mjs, so a
4// field that named a prop the core refuses would fail here.
5import test from 'node:test'
6import assert from 'node:assert/strict'
7import * as Y from 'yjs'
8import { applyOps, view } from './ops.mjs'
9import {
10 deckOp, slideOp, talkOp, moveSlideOp, propValue, propOp, regionOp, labelOp, boxFieldOp,
11 moveBlockOp, zOps, addBlockOp, blockLabel, DEFAULT_BOX
12} from './inspector-ops.mjs'
13import { blockChange, setValue, forceWrites } from './inspector-block.mjs'
14
15function deck () {
16 const doc = new Y.Doc()
17 applyOps(doc, [
18 { op: 'set_meta', title: 'T', theme: 'paper' },
19 { op: 'add_slide', id: 'slideaaaaaaa', layout: 'flow', template: 'title-body' },
20 { op: 'add_block', slide: 'slideaaaaaaa', id: 'blockheading', type: 'heading', region: 'heading', props: { level: 1 }, markdown: 'Hello world of slides' },
21 { op: 'add_block', slide: 'slideaaaaaaa', id: 'blocktexttxt', type: 'text', region: 'body', markdown: 'Body' },
22 { op: 'add_block', slide: 'slideaaaaaaa', id: 'blockshapeee', type: 'shape', region: 'body', props: { kind: 'rect' } },
23 { op: 'add_slide', id: 'slidebbbbbbb', layout: 'canvas' },
24 { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasblock1', type: 'text', box: { x: 0.1, y: 0.1, w: 0.3, h: 0.2 }, z: 0, markdown: 'One' },
25 { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasblock2', type: 'shape', box: { x: 0.5, y: 0.5, w: 0.3, h: 0.2 }, z: 1 },
26 { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasblock3', type: 'image', box: { x: 0.2, y: 0.6, w: 0.3, h: 0.2 }, z: 2 },
27 { op: 'add_slide', id: 'slideccccccc', layout: 'flow', template: 'blank' }
28 ], 'test')
29 return doc
30}
31const accepts = (doc, ops) => assert.doesNotThrow(() => applyOps(doc, Array.isArray(ops) ? ops : [ops], 'test'))
32
33test('deck controls are set_meta', () => {
34 const doc = deck()
35 const meta = view(doc).meta
36 assert.deepEqual(deckOp('title', 'New', meta), { op: 'set_meta', title: 'New' })
37 assert.deepEqual(deckOp('theme', 'ink', meta), { op: 'set_meta', theme: 'ink' })
38 assert.deepEqual(deckOp('accent', '#ff0000', meta), { op: 'set_meta', accent: '#ff0000' })
39 assert.deepEqual(deckOp('accent-reset', undefined, meta), { op: 'set_meta', accent: null })
40 // In the talk: each toggle writes itself, clearing the other, or takes itself away.
41 assert.deepEqual(talkOp({ id: 's', talk: 'parked' }, 'full'), { op: 'set_slide', id: 's', talk: 'full' })
42 assert.deepEqual(talkOp({ id: 's' }, 'parked'), { op: 'set_slide', id: 's', talk: 'parked' })
43 assert.deepEqual(talkOp({ id: 's', talk: 'full' }, 'full'), { op: 'set_slide', id: 's', talk: null })
44 // How things arrive: Appear is no setting at all.
45 assert.deepEqual(deckOp('arrive', 'fade', meta), { op: 'set_meta', arrive: 'fade' })
46 assert.deepEqual(deckOp('arrive', 'appear', meta), { op: 'set_meta', arrive: null })
47 assert.deepEqual(deckOp('font-heading', 'Georgia, serif', meta), { op: 'set_meta', fonts: { heading: 'Georgia, serif', body: '' } })
48 for (const f of ['title', 'theme', 'accent', 'accent-reset']) accepts(doc, deckOp(f, f === 'theme' ? 'ink' : '#123456', meta))
49 accepts(doc, deckOp('font-heading', 'Georgia, serif', meta))
50 const after = view(doc).meta
51 assert.deepEqual(deckOp('font-body', 'monospace', after), { op: 'set_meta', fonts: { heading: 'Georgia, serif', body: 'monospace' } })
52 // Both back to the theme's clears the map rather than storing two empties.
53 assert.deepEqual(deckOp('font-heading', '', { fonts: { heading: 'x', body: '' } }), { op: 'set_meta', fonts: null })
54 accepts(doc, deckOp('font-heading', '', after))
55 assert.equal(view(doc).meta.fonts, null)
56})
57
58test('slide controls are set_slide, and background keeps its colour', () => {
59 const doc = deck()
60 const s = view(doc).slides[0]
61 assert.deepEqual(slideOp(s, 'layout', 'canvas'), { op: 'set_slide', id: s.id, layout: 'canvas' })
62 assert.deepEqual(slideOp(s, 'template', 'quote'), { op: 'set_slide', id: s.id, template: 'quote' })
63 assert.deepEqual(slideOp(s, 'transition', 'fade'), { op: 'set_slide', id: s.id, transition: 'fade' })
64 assert.deepEqual(slideOp(s, 'phone', 'reflow'), { op: 'set_slide', id: s.id, phone: 'reflow' })
65 assert.deepEqual(slideOp(s, 'background-color', '#112233'), { op: 'set_slide', id: s.id, background: { kind: 'color', value: '#112233' } })
66 assert.deepEqual(slideOp(s, 'background', 'theme'), { op: 'set_slide', id: s.id, background: { kind: 'theme', value: '' } })
67 const coloured = { ...s, background: { kind: 'color', value: '#abcdef' } }
68 assert.deepEqual(slideOp(coloured, 'background', 'color').background, { kind: 'color', value: '#abcdef' })
69 assert.deepEqual(slideOp(s, 'background', 'color').background, { kind: 'color', value: '#ffffff' })
70 for (const [f, v] of [['template', 'quote'], ['transition', 'fade'], ['phone', 'reflow'], ['background-color', '#112233'], ['background', 'theme'], ['layout', 'canvas']]) accepts(doc, slideOp(s, f, v))
71 assert.equal(view(doc).slides[0].layout, 'canvas')
72 assert.throws(() => slideOp(s, 'nope', 1))
73})
74
75test('move up and down are move_slide with the right after', () => {
76 const ids = ['a', 'b', 'c']
77 assert.equal(moveSlideOp(ids, 'a', 'up'), null)
78 assert.deepEqual(moveSlideOp(ids, 'b', 'up'), { op: 'move_slide', id: 'b' })
79 assert.deepEqual(moveSlideOp(ids, 'c', 'up'), { op: 'move_slide', id: 'c', after: 'a' })
80 assert.deepEqual(moveSlideOp(ids, 'a', 'down'), { op: 'move_slide', id: 'a', after: 'b' })
81 assert.equal(moveSlideOp(ids, 'c', 'down'), null)
82 const doc = deck()
83 accepts(doc, moveSlideOp(view(doc).slides.map(s => s.id), 'slideccccccc', 'up'))
84 assert.deepEqual(view(doc).slides.map(s => s.id), ['slideaaaaaaa', 'slideccccccc', 'slidebbbbbbb'])
85})
86
87test('block props: value kinds, region, a shape label', () => {
88 const doc = deck()
89 const s = view(doc).slides[0]
90 const [heading, text, shape] = s.blocks
91 assert.equal(propValue('2', 'int'), 2)
92 assert.equal(propValue('', 'int'), null)
93 assert.equal(propValue('x', 'int'), null)
94 assert.equal(propValue(true, 'bool'), true)
95 assert.equal(propValue('on', 'bool'), true)
96 assert.equal(propValue('', ''), null)
97 assert.deepEqual(propOp(s.id, heading, 'level', 2), { op: 'set_block', slide: s.id, id: heading.id, props: { level: 2 } })
98 assert.deepEqual(regionOp(s.id, text, 'aside'), { op: 'set_block', slide: s.id, id: text.id, region: 'aside' })
99 assert.deepEqual(labelOp(s.id, shape, 'Go'), { op: 'set_text', slide: s.id, id: shape.id, markdown: 'Go' })
100 accepts(doc, [propOp(s.id, heading, 'level', 2), regionOp(s.id, text, 'aside'), labelOp(s.id, shape, 'Go'), propOp(s.id, text, 'align', null)])
101 const after = view(doc).slides[0]
102 assert.equal(after.blocks[0].props.level, 2)
103 assert.equal(after.blocks[1].region, 'aside')
104 assert.deepEqual(after.blocks[2].text, [[{ t: 'Go' }]])
105})
106
107test('box fields are set_block on the desktop and set_phone in the phone view', () => {
108 const doc = deck()
109 const s = view(doc).slides[1]
110 const b = s.blocks[0]
111 assert.deepEqual(boxFieldOp({ view: 'desktop', slideId: s.id, block: b, key: 'x', percent: '25' }),
112 { op: 'set_block', slide: s.id, id: b.id, box: { x: 0.25, y: 0.1, w: 0.3, h: 0.2 } })
113 assert.deepEqual(boxFieldOp({ view: 'phone', slideId: s.id, block: b, key: 'w', percent: '50' }),
114 { op: 'set_phone', slide: s.id, id: b.id, phone: { box: { x: 0.1, y: 0.1, w: 0.5, h: 0.2 } } })
115 assert.equal(boxFieldOp({ view: 'desktop', slideId: s.id, block: b, key: 'x', percent: 'abc' }), null)
116 accepts(doc, [boxFieldOp({ view: 'desktop', slideId: s.id, block: b, key: 'x', percent: '25' }), boxFieldOp({ view: 'phone', slideId: s.id, block: b, key: 'w', percent: '50' })])
117 const after = view(doc).slides[1].blocks[0]
118 assert.equal(after.box.x, 0.25)
119 assert.deepEqual(after.phone.box, { x: 0.1, y: 0.1, w: 0.5, h: 0.2 })
120 // Typed values are clamped to the frame.
121 assert.deepEqual(boxFieldOp({ view: 'desktop', slideId: s.id, block: b, key: 'x', percent: '95' }).box, { x: 0.7, y: 0.1, w: 0.3, h: 0.2 })
122})
123
124test('move block and the layer buttons', () => {
125 const doc = deck()
126 const s = view(doc).slides[1]
127 const ids = s.blocks.map(b => b.id)
128 assert.equal(moveBlockOp(s.id, ids, ids[0], 'up'), null)
129 assert.deepEqual(moveBlockOp(s.id, ids, ids[1], 'up'), { op: 'move_block', slide: s.id, id: ids[1] })
130 assert.deepEqual(moveBlockOp(s.id, ids, ids[2], 'up'), { op: 'move_block', slide: s.id, id: ids[2], after: ids[0] })
131 assert.deepEqual(moveBlockOp(s.id, ids, ids[0], 'down'), { op: 'move_block', slide: s.id, id: ids[0], after: ids[1] })
132 // z: 0,1,2 — bringing the first forward swaps it with the second.
133 assert.deepEqual(zOps(s.id, s.blocks, ids[0], 'forward'), [
134 { op: 'set_block', slide: s.id, id: ids[1], z: 0 },
135 { op: 'set_block', slide: s.id, id: ids[0], z: 1 }
136 ])
137 assert.deepEqual(zOps(s.id, s.blocks, ids[2], 'forward'), [])
138 assert.deepEqual(zOps(s.id, s.blocks, ids[0], 'back'), [])
139 // Duplicate zs are renumbered on the way through.
140 const dup = s.blocks.map(b => ({ ...b, z: 0 }))
141 assert.deepEqual(zOps(s.id, dup, ids[2], 'back').map(o => [o.id, o.z]).sort(), [[ids[1], 2], [ids[2], 1]].sort())
142 accepts(doc, zOps(s.id, s.blocks, ids[0], 'forward'))
143 assert.deepEqual(view(doc).slides[1].blocks.map(b => b.z), [1, 0, 2])
144 accepts(doc, moveBlockOp(s.id, ids, ids[2], 'up'))
145 assert.deepEqual(view(doc).slides[1].blocks.map(b => b.id), [ids[0], ids[2], ids[1]])
146})
147
148test('add block: region after the selection on flow, a default box on canvas, a heading level that fits', () => {
149 const doc = deck()
150 const [flow, canvas, blank] = view(doc).slides
151 const a = addBlockOp(flow, 'text', 'newblock0001', flow.blocks[0].id)
152 assert.deepEqual(a, { op: 'add_block', slide: flow.id, id: 'newblock0001', type: 'text', props: {}, after: flow.blocks[0].id, markdown: '', region: 'body' })
153 assert.equal(addBlockOp(flow, 'heading', 'newblock0002', '').props.level, 2)
154 assert.equal(addBlockOp(blank, 'heading', 'newblock0003', '').props.level, 1)
155 assert.equal(addBlockOp(flow, 'image', 'newblock0004', 'nosuchblock1').region, 'media')
156 assert.equal('after' in addBlockOp(flow, 'image', 'newblock0004', 'nosuchblock1'), false)
157 const c = addBlockOp(canvas, 'shape', 'newblock0005', '')
158 assert.deepEqual(c.box, DEFAULT_BOX)
159 assert.equal(c.z, 3)
160 assert.equal('markdown' in addBlockOp(canvas, 'divider', 'newblock0006', ''), false)
161 assert.deepEqual(addBlockOp(flow, 'table', 'newblock0007', '').props, { rows: 2, cols: 2 })
162 for (const kind of ['heading', 'text', 'list', 'quote', 'image', 'columns', 'code', 'shape', 'table', 'divider', 'spacer']) {
163 accepts(doc, addBlockOp(view(doc).slides[0], kind, 'added' + kind.padEnd(7, 'x'), ''))
164 accepts(doc, addBlockOp(view(doc).slides[1], kind, 'canvs' + kind.padEnd(7, 'x'), ''))
165 }
166 assert.equal(view(doc).slides[0].blocks.length, 3 + 11)
167})
168
169test('a block is named by its type and its first words', () => {
170 const doc = deck()
171 const s = view(doc).slides[0]
172 assert.equal(blockLabel(s.blocks[0], 'Heading'), 'Heading: Hello world of slides')
173 assert.equal(blockLabel(s.blocks[2], 'Shape'), 'Shape')
174 // A box whose words are its kind's name says it once, not "Shape: Shape".
175 assert.equal(blockLabel({ text: [[{ t: 'Shape' }]] }, 'Shape'), 'Shape')
176 assert.equal(blockLabel({ text: [[{ t: ' shape ' }]] }, 'Shape'), 'Shape')
177 assert.equal(blockLabel({ text: [[{ t: 'Shapes' }]] }, 'Shape'), 'Shape: Shapes')
178 assert.equal(blockLabel({ text: [[{ t: 'a'.repeat(60) }]] }, 'Text', 10), 'Text: ' + 'a'.repeat(9) + '…')
179})
180
181test('blockChange: a control element to its op', () => {
182 const doc = deck()
183 const flow = view(doc).slides[0]
184 const canvas = view(doc).slides[1]
185 const el = (dataset, value, extra = {}) => ({ dataset, value, type: 'text', ...extra })
186 assert.deepEqual(blockChange({ el: el({ prop: 'level', kind: 'int' }, '3'), slide: flow, block: flow.blocks[0], view: 'desktop' }),
187 [{ op: 'set_block', slide: flow.id, id: flow.blocks[0].id, props: { level: 3 } }])
188 assert.deepEqual(blockChange({ el: el({ prop: 'header', kind: 'bool' }, 'on', { type: 'checkbox', checked: true }), slide: flow, block: flow.blocks[1], view: 'desktop' }),
189 [{ op: 'set_block', slide: flow.id, id: flow.blocks[1].id, props: { header: true } }])
190 assert.deepEqual(blockChange({ el: el({ prop: 'size' }, 'l'), slide: flow, block: flow.blocks[1], view: 'desktop' }),
191 [{ op: 'set_block', slide: flow.id, id: flow.blocks[1].id, props: { size: 'l' } }])
192 assert.deepEqual(blockChange({ el: el({ prop: 'size' }, 'l'), slide: flow, block: flow.blocks[1], view: 'phone' }),
193 [{ op: 'set_phone', slide: flow.id, id: flow.blocks[1].id, phone: { size: 'l' } }])
194 assert.deepEqual(blockChange({ el: el({ text: 'label' }, ' Go '), slide: flow, block: flow.blocks[2], view: 'desktop' }),
195 [{ op: 'set_text', slide: flow.id, id: flow.blocks[2].id, markdown: 'Go' }])
196 assert.deepEqual(blockChange({ el: el({ place: 'region' }, 'aside'), slide: flow, block: flow.blocks[1], view: 'desktop' }),
197 [{ op: 'set_block', slide: flow.id, id: flow.blocks[1].id, region: 'aside' }])
198 assert.deepEqual(blockChange({ el: el({ box: 'y' }, '40'), slide: canvas, block: canvas.blocks[0], view: 'desktop' }),
199 [{ op: 'set_block', slide: canvas.id, id: canvas.blocks[0].id, box: { x: 0.1, y: 0.4, w: 0.3, h: 0.2 } }])
200 assert.deepEqual(blockChange({ el: el({ phone: 'hidden' }, 'on', { type: 'checkbox', checked: true }), slide: flow, block: flow.blocks[1], view: 'desktop' }),
201 [{ op: 'set_phone', slide: flow.id, id: flow.blocks[1].id, phone: { hidden: true } }])
202 assert.deepEqual(blockChange({ el: el({ phone: 'order' }, '-1'), slide: flow, block: flow.blocks[1], view: 'desktop' }),
203 [{ op: 'set_phone', slide: flow.id, id: flow.blocks[1].id, phone: { order: -1 } }])
204 assert.deepEqual(blockChange({ el: el({ phone: 'size' }, ''), slide: flow, block: { ...flow.blocks[1], phone: { size: 's', hidden: true } }, view: 'desktop' }),
205 [{ op: 'set_phone', slide: flow.id, id: flow.blocks[1].id, phone: { hidden: true } }])
206 assert.deepEqual(blockChange({ el: el({}, 'x'), slide: flow, block: flow.blocks[1], view: 'desktop' }), [])
207})
208
209test('a segmented choice: setValue checks the radio for the value, and its change is the prop', () => {
210 const radio = value => ({ type: 'radio', value, checked: false, dataset: { prop: 'align' } })
211 const radios = ['start', 'center', 'end'].map(radio)
212 for (const r of radios) setValue(r, 'center')
213 assert.deepEqual(radios.map(r => r.checked), [false, true, false])
214 for (const r of radios) setValue(r, undefined)
215 assert.deepEqual(radios.map(r => r.checked), [false, false, false])
216 const doc = deck()
217 const slide = view(doc).slides[0]
218 const block = slide.blocks.find(b => b.type === 'text')
219 const ops = blockChange({ el: { ...radios[2], checked: true }, slide, block, view: 'desktop' })
220 assert.deepEqual(ops, [propOp(slide.id, block, 'align', 'end')])
221 accepts(doc, ops)
222})
223
224test('corner radius shows for an image and a rectangle, not for a shape with no corners', async () => {
225 const { el: stub } = await import('./dom-stub.mjs')
226 const { fillBlock } = await import('./inspector-block.mjs')
227 const radius = stub('div', { 'rst-field': '', 'data-types': 'image shape', 'data-kinds': 'rect' })
228 const form = stub('form', {}, [radius, stub('p', { 'slides-phone-badge': '' })])
229 const slide = { id: 's', layout: 'canvas', blocks: [] }
230 const shown = block => { fillBlock({ form, slide, block, view: 'desktop' }); return !radius.hidden }
231 assert.equal(shown({ id: 'a', type: 'image', props: {} }), true)
232 assert.equal(shown({ id: 'b', type: 'shape', props: {} }), true, 'a shape is a rectangle unless it says')
233 assert.equal(shown({ id: 'c', type: 'shape', props: { kind: 'rect' } }), true)
234 for (const kind of ['ellipse', 'line', 'arrow']) assert.equal(shown({ id: 'd', type: 'shape', props: { kind } }), false, kind)
235 assert.equal(shown({ id: 'e', type: 'text', props: {} }), false)
236})
237
238// A shape that sets no fill is drawn with none, and one that sets no
239// stroke in the theme's ink (render.go): the swatches show the same, and
240// write nothing until a colour is picked (review V3).
241test('a shape\'s swatches show what the slide shows: no fill as none, no stroke in the ink', async () => {
242 const { el: stub, withDocument } = await import('./dom-stub.mjs')
243 const { fillBlock } = await import('./inspector-block.mjs')
244 const ctx = withDocument()
245 try {
246 const swatch = prop => { const s = stub('input', { 'slides-swatch': '', 'data-prop': prop }); s.type = 'color'; s.value = '#000000'; return s }
247 const fillEl = swatch('fill')
248 const strokeEl = swatch('stroke')
249 const form = stub('form', {}, [fillEl, strokeEl, stub('p', { 'slides-phone-badge': '' })])
250 const slide = { id: 's', layout: 'canvas', blocks: [] }
251 fillBlock({ form, slide, block: { id: 'a', type: 'shape', props: {} }, view: 'desktop', ink: '#1c1a17' })
252 assert.equal(fillEl.hasAttribute('data-none'), true, 'no fill: drawn as none')
253 assert.equal(strokeEl.hasAttribute('data-none'), false)
254 assert.equal(strokeEl.value, '#1c1a17', 'no stroke: the ink the slide draws it in')
255 fillBlock({ form, slide, block: { id: 'b', type: 'shape', props: { fill: '#cc3300', stroke: '#00aa00' } }, view: 'desktop', ink: '#1c1a17' })
256 assert.equal(fillEl.hasAttribute('data-none'), false)
257 assert.equal(fillEl.value, '#cc3300')
258 assert.equal(strokeEl.value, '#00aa00')
259 } finally {
260 ctx.restore()
261 }
262})
263
264// Position and size open or closed (spec §4.3): kept for the page, so an
265// editor rebuilt after a resync shows it as it was; a reload (a new
266// module) starts closed. Nothing is written to storage.
267test('Position and size keeps open or closed across a remount, in memory only', async () => {
268 const { el: stub } = await import('./dom-stub.mjs')
269 const { mountPlace } = await import('./inspector.mjs')
270 const first = stub('details', { 'slides-place': '' })
271 first.open = false
272 const a = mountPlace(first)
273 assert.equal(first.open, false, 'closed at first')
274 first.open = true
275 first.dispatchEvent({ type: 'toggle' })
276 a.destroy()
277 const second = stub('details', { 'slides-place': '' })
278 second.open = false
279 const b = mountPlace(second)
280 assert.equal(second.open, true, 'the rebuilt editor shows it open')
281 second.open = false
282 second.dispatchEvent({ type: 'toggle' })
283 b.destroy()
284 const third = stub('details', { 'slides-place': '' })
285 mountPlace(third).destroy()
286 assert.equal(!!third.open, false)
287})
288
289test('setValue leaves the focused select alone, except in a forced fill (undo, redo)', () => {
290 const sel = { tagName: 'SELECT', type: 'select-one', value: 'ink' }
291 const was = globalThis.document
292 globalThis.document = { activeElement: sel }
293 try {
294 setValue(sel, 'paper')
295 assert.equal(sel.value, 'ink', 'a peer edit does not change the control being used')
296 forceWrites(() => setValue(sel, 'paper'))
297 assert.equal(sel.value, 'paper', 'right after an undo the focused field shows what was taken back')
298 sel.value = 'ink'
299 setValue(sel, 'paper')
300 assert.equal(sel.value, 'ink', 'and the allowance ends with the forced fill')
301 } finally {
302 globalThis.document = was
303 }
304})
305