oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// The asset half of the inspector (spec §2.7): one finished upload as
2// the two ops it is, the Unused images list derived from the document,
3// and the words a refusal is said in.
4//
5// A file of its own rather than more of inspector.test.mjs: that one is
6// about controls and their ops, and this is about a thing that arrives
7// from outside the document and then joins it.
8import test from 'node:test'
9import assert from 'node:assert/strict'
10import * as Y from 'yjs'
11import { applyOps, view } from './ops.mjs'
12import { registerAssetOps, removeAssetOp, unusedAssets } from './inspector-ops.mjs'
13import { fillUnused, uploadMessage } from './inspector-image.mjs'
14import { el as stub, withDocument } from './dom-stub.mjs'
15
16function deck () {
17 const doc = new Y.Doc()
18 applyOps(doc, [
19 { op: 'set_meta', title: 'T', theme: 'paper' },
20 { op: 'add_slide', id: 'slideaaaaaaa', layout: 'flow', template: 'title-body' },
21 { op: 'add_slide', id: 'slidebbbbbbb', layout: 'canvas' },
22 { op: 'add_block', slide: 'slidebbbbbbb', id: 'canvasimage1', type: 'image', box: { x: 0.2, y: 0.6, w: 0.3, h: 0.2 }, z: 0 },
23 { op: 'add_slide', id: 'slideccccccc', layout: 'flow', template: 'blank' }
24 ], 'test')
25 return doc
26}
27const accepts = (doc, ops) => assert.doesNotThrow(() => applyOps(doc, Array.isArray(ops) ? ops : [ops], 'test'))
28
29test('one finished upload is register_asset plus the block that uses it, in one batch', () => {
30 const doc = deck()
31 const record = { id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' }
32 const ops = registerAssetOps(record, 'slidebbbbbbb', 'canvasimage1')
33 assert.deepEqual(ops, [
34 { op: 'register_asset', id: 'assetaaaaaaa', ref: 'aa01bb02', name: 'chart.png', width: 1600, height: 900, alt: 'A bar chart' },
35 { op: 'set_block', slide: 'slidebbbbbbb', id: 'canvasimage1', props: { asset: 'assetaaaaaaa' } }
36 ])
37 accepts(doc, ops)
38 const v = view(doc)
39 assert.equal(v.assets.assetaaaaaaa.ref, 'aa01bb02')
40 assert.equal(v.slides[1].blocks[0].props.asset, 'assetaaaaaaa')
41
42 // With no block selected it is the registration alone — still legal,
43 // and it lands in the Unused images list where somebody can drop it.
44 assert.equal(registerAssetOps({ ...record, id: 'assetbbbbbbb' }, '', '').length, 1)
45})
46
47test('a record missing its numbers still makes an op the core accepts', () => {
48 const doc = deck()
49 accepts(doc, registerAssetOps({ id: 'assetcccccccc', ref: 'r9' }, '', ''))
50 const a = view(doc).assets.assetcccccccc
51 assert.equal(a.width, 0)
52 assert.equal(a.height, 0)
53 assert.equal(a.alt, '')
54})
55
56test('unusedAssets is what no slide refers to, and remove_asset drops one', () => {
57 const doc = deck()
58 applyOps(doc, [
59 ...registerAssetOps({ id: 'usedbyblock1', ref: 'r1', name: 'used.png', width: 1, height: 1, alt: 'a' }, 'slidebbbbbbb', 'canvasimage1'),
60 ...registerAssetOps({ id: 'usedasbackgr', ref: 'r2', name: 'bg.png', width: 1, height: 1, alt: 'b' }, '', ''),
61 ...registerAssetOps({ id: 'usedbynobody', ref: 'r3', name: 'orphan.png', width: 1, height: 1, alt: 'c' }, '', ''),
62 { op: 'set_slide', id: 'slideccccccc', background: { kind: 'image', value: 'usedasbackgr' } }
63 ], 'test')
64
65 const v = view(doc)
66 assert.deepEqual(unusedAssets(v).map(a => a.id), ['usedbynobody'])
67 // The entry carries the asset's own fields, so the list can name it.
68 assert.equal(unusedAssets(v)[0].name, 'orphan.png')
69
70 accepts(doc, removeAssetOp('usedbynobody'))
71 assert.deepEqual(unusedAssets(view(doc)), [])
72
73 // Deleting the block that used one leaves the registration behind
74 // (§2.7) — which is the whole reason this list exists.
75 accepts(doc, { op: 'remove_block', slide: 'slidebbbbbbb', id: 'canvasimage1' })
76 assert.deepEqual(unusedAssets(view(doc)).map(a => a.id), ['usedbyblock1'])
77})
78
79test('the Unused images list reconciles by id and names each Remove button', () => {
80 const ctx = withDocument()
81 try {
82 const list = stub('ul', { 'slides-unused-list': '' })
83 const none = stub('p', { 'slides-unused-none': '' })
84 const assets = [{ id: 'a1', name: 'chart.png' }, { id: 'a2', name: '', ref: 'ff00aa11bb22' }]
85
86 fillUnused({ list, none, assets, removeLabel: 'Remove' })
87 assert.equal(none.hidden, true)
88 assert.deepEqual(list.children.map(li => li.dataset.asset), ['a1', 'a2'])
89 const [first, second] = list.children
90 assert.equal(first.querySelector('[slides-asset-name]').textContent, 'chart.png')
91 // No filename: the first eight characters of the address, which is
92 // the only other thing there is to call it.
93 assert.equal(second.querySelector('[slides-asset-name]').textContent, 'ff00aa11')
94 assert.equal(first.querySelector('[data-remove-asset]').getAttribute('aria-label'), 'Remove: chart.png')
95
96 fillUnused({ list, none, assets, removeLabel: 'Remove' })
97 assert.equal(list.children[0], first, 'the same element, not a replacement')
98 assert.equal(list.children[1], second)
99
100 fillUnused({ list, none, assets: [], removeLabel: 'Remove' })
101 assert.deepEqual(list.children, [])
102 assert.equal(none.hidden, false)
103 } finally {
104 ctx.restore()
105 }
106})
107
108test('an upload refusal is said in words the person can act on', () => {
109 const labels = { type: 'Use a PNG…', toolarge: 'Bigger than 20 MB.', altfirst: 'Alt text first.', failed: 'Try again.' }
110 const say = (code, message) => uploadMessage({ code, serverMessage: message || '' }, labels)
111 assert.equal(say('type'), labels.type)
112 assert.equal(say('toolarge'), labels.toolarge)
113 assert.equal(say('too_large'), labels.toolarge)
114 assert.equal(say('altfirst'), labels.altfirst)
115 assert.equal(say('nohash'), labels.failed)
116 // The server knows better than the catalog does why IT said no.
117 assert.equal(say('unsafe_svg', 'That SVG carries a script.'), 'That SVG carries a script.')
118 assert.equal(uploadMessage(null, labels), '')
119})
120