| 1 | // strip.mjs's pure parts: the label a thumbnail gets, and the plan for |
| 2 | // bringing the strip's items into line with the deck's slide order. |
| 3 | import test from 'node:test' |
| 4 | import assert from 'node:assert/strict' |
| 5 | import * as Y from 'yjs' |
| 6 | import { labelFor, plan, mountStrip } from './strip.mjs' |
| 7 | import { el, withDocument } from './dom-stub.mjs' |
| 8 | |
| 9 | // deckWith builds a Y.Doc of slides, each optionally with a heading — |
| 10 | // the label's source now that a thumbnail is an <img> with no text in |
| 11 | // it for a script to read. |
| 12 | function deckWith (slides) { |
| 13 | const ydoc = new Y.Doc() |
| 14 | ydoc.transact(() => { |
| 15 | const arr = ydoc.getArray('slides') |
| 16 | arr.push(slides.map(({ id, heading }) => { |
| 17 | const m = new Y.Map() |
| 18 | m.set('id', id) |
| 19 | const blocks = new Y.Array() |
| 20 | if (heading !== undefined) { |
| 21 | const b = new Y.Map() |
| 22 | b.set('id', id + '-h') |
| 23 | b.set('type', 'heading') |
| 24 | const f = new Y.XmlFragment() |
| 25 | const p = new Y.XmlElement('paragraph') |
| 26 | p.insert(0, [new Y.XmlText(heading)]) |
| 27 | f.insert(0, [p]) |
| 28 | b.set('text', f) |
| 29 | blocks.push([b]) |
| 30 | } |
| 31 | m.set('blocks', blocks) |
| 32 | return m |
| 33 | })) |
| 34 | }) |
| 35 | return ydoc |
| 36 | } |
| 37 | |
| 38 | test('labelFor is the slide\'s first heading, else the numbered fallback', () => { |
| 39 | const ydoc = deckWith([ |
| 40 | { id: 'a', heading: ' Regions, not coordinates ' }, |
| 41 | { id: 'b', heading: ' ' }, |
| 42 | { id: 'c' } |
| 43 | ]) |
| 44 | assert.equal(labelFor(ydoc, 'a', 3, 'Slide {n}'), 'Regions, not coordinates') |
| 45 | assert.equal(labelFor(ydoc, 'b', 3, 'Slide {n}'), 'Slide 3') |
| 46 | assert.equal(labelFor(ydoc, 'c', 8, 'Slide {n}'), 'Slide 8') |
| 47 | assert.equal(labelFor(ydoc, 'gone', 2, 'Slide {n}'), 'Slide 2') |
| 48 | assert.equal(labelFor(null, 'a', 1, 'Slide {n}'), 'Slide 1') |
| 49 | }) |
| 50 | |
| 51 | // A thumbnail is re-pointed, not re-fetched: setting src IS the |
| 52 | // request, and the route's ETag makes the ones that did not change a |
| 53 | // 304. The label and the picture's alt are the same words, because a |
| 54 | // screen reader announces an image by its alt and never opens the file |
| 55 | // to read the <title> inside it. |
| 56 | test('flush re-points every dirty thumbnail and relabels it', () => { |
| 57 | const ctx = withDocument() |
| 58 | try { |
| 59 | const item = id => el('li', { 'slides-strip-item': '', 'data-slide-id': id }, [ |
| 60 | el('div', { 'slides-thumb': '' }, [el('img', { 'slides-thumb-img': '' })]), |
| 61 | el('a', { 'slides-strip-link': '' }, [el('span', { 'slides-strip-label': '' })]) |
| 62 | ]) |
| 63 | const root = el('ol', { 'slides-strip': '' }, [item('a'), item('b')]) |
| 64 | const ydoc = deckWith([{ id: 'a', heading: 'First' }, { id: 'b' }]) |
| 65 | let seq = 4 |
| 66 | const strip = mountStrip({ |
| 67 | root, ydoc, template: null, slideLabel: 'Slide {n}', |
| 68 | thumbURL: id => `/d/xyz/slides/${id}.svg?seq=${seq}` |
| 69 | }) |
| 70 | |
| 71 | strip.mark('a') |
| 72 | strip.flush() |
| 73 | const img = id => root.children.find(li => li.dataset.slideId === id).querySelector('[slides-thumb-img]') |
| 74 | assert.equal(img('a').src, '/d/xyz/slides/a.svg?seq=4') |
| 75 | assert.equal(img('b').src, undefined, 'a thumbnail nothing marked is left alone') |
| 76 | assert.equal(img('a').alt, 'First', 'the picture is announced by the slide\'s own label') |
| 77 | assert.equal(root.children[1].querySelector('[slides-strip-label]').textContent, 'Slide 2') |
| 78 | |
| 79 | // A second flush with nothing marked changes nothing; a later seq |
| 80 | // is what makes the browser ask again. |
| 81 | seq = 7 |
| 82 | strip.flush() |
| 83 | assert.equal(img('a').src, '/d/xyz/slides/a.svg?seq=4') |
| 84 | strip.mark('a') |
| 85 | strip.flush() |
| 86 | assert.equal(img('a').src, '/d/xyz/slides/a.svg?seq=7') |
| 87 | strip.destroy() |
| 88 | } finally { |
| 89 | ctx.restore() |
| 90 | } |
| 91 | }) |
| 92 | |
| 93 | // The <ol> is the page's and outlives every Y.Doc mounted over it: a |
| 94 | // strip that did not let go would run each item menu's command once |
| 95 | // per resync this session has had. |
| 96 | test('destroy takes the item-menu listener off; a second mount leaves one', () => { |
| 97 | const root = el('ol', { 'slides-strip': '' }) |
| 98 | const mount = () => mountStrip({ root, ydoc: null, thumbURL: () => '', template: null, slideLabel: 'Slide {n}' }) |
| 99 | const first = mount() |
| 100 | assert.equal(root.countListeners('click'), 1) |
| 101 | first.destroy() |
| 102 | assert.equal(root.countListeners('click'), 0) |
| 103 | const second = mount() |
| 104 | assert.equal(root.countListeners('click'), 1) |
| 105 | second.destroy() |
| 106 | }) |
| 107 | |
| 108 | test('plan says what to add, drop and where each item goes', () => { |
| 109 | assert.deepEqual(plan(['a', 'b', 'c'], ['a', 'b', 'c']), { add: [], drop: [], order: ['a', 'b', 'c'] }) |
| 110 | assert.deepEqual(plan(['a', 'b'], ['b', 'a', 'c']), { add: ['c'], drop: [], order: ['b', 'a', 'c'] }) |
| 111 | assert.deepEqual(plan(['a', 'b', 'c'], ['c']), { add: [], drop: ['a', 'b'], order: ['c'] }) |
| 112 | assert.deepEqual(plan([], []), { add: [], drop: [], order: [] }) |
| 113 | }) |
| 114 | |
| 115 | // A colleague reordering slides must not take focus off the strip: a |
| 116 | // browser blurs a node it moves, so sync moves only the items that are |
| 117 | // out of place and puts focus back where it was. |
| 118 | test('sync moves only the items out of place and keeps focus', () => { |
| 119 | const ctx = withDocument() |
| 120 | try { |
| 121 | const item = id => el('li', { 'slides-strip-item': '', 'data-slide-id': id }, [el('a', { 'slides-strip-link': '' })]) |
| 122 | const [a, b, c] = ['a', 'b', 'c'].map(item) |
| 123 | const root = el('ol', { 'slides-strip': '' }, [a, b, c]) |
| 124 | const ydoc = new Y.Doc() |
| 125 | const order = ids => ydoc.transact(() => { |
| 126 | const slides = ydoc.getArray('slides') |
| 127 | slides.delete(0, slides.length) |
| 128 | slides.push(ids.map(id => { const m = new Y.Map(); m.set('id', id); return m })) |
| 129 | }) |
| 130 | const moved = [] |
| 131 | const insertBefore = root.insertBefore.bind(root) |
| 132 | root.insertBefore = (node, ref) => { moved.push(node.dataset.slideId); return insertBefore(node, ref) } |
| 133 | const append = root.append.bind(root) |
| 134 | root.append = (...nodes) => { moved.push(...nodes.map(n => n.dataset.slideId)); return append(...nodes) } |
| 135 | const strip = mountStrip({ root, ydoc, thumbURL: id => '/x/' + id, template: null, slideLabel: 'Slide {n}' }) |
| 136 | |
| 137 | const link = c.querySelector('[slides-strip-link]') |
| 138 | link.focus() |
| 139 | order(['c', 'a', 'b']) |
| 140 | strip.sync() |
| 141 | assert.deepEqual(root.children.map(li => li.dataset.slideId), ['c', 'a', 'b']) |
| 142 | assert.deepEqual(moved, ['c'], 'only the item that changed position moves') |
| 143 | assert.equal(ctx.document.activeElement, link, 'focus is back on the moved item') |
| 144 | |
| 145 | moved.length = 0 |
| 146 | strip.sync() |
| 147 | assert.deepEqual(moved, [], 'nothing moves when nothing changed') |
| 148 | strip.destroy() |
| 149 | } finally { |
| 150 | ctx.restore() |
| 151 | } |
| 152 | }) |
| 153 | |
| 154 | // New slide is the <ol>'s last child (li[slides-strip-new], spec §6.1): |
| 155 | // a slide sync adds goes before it, a reorder never puts a slide after |
| 156 | // it, and it is never taken for a slide. |
| 157 | test('sync keeps the New slide row last', () => { |
| 158 | const ctx = withDocument() |
| 159 | try { |
| 160 | const item = id => el('li', { 'slides-strip-item': '', 'data-slide-id': id }, [el('a', { 'slides-strip-link': '' })]) |
| 161 | const row = el('li', { 'slides-strip-new': '' }) |
| 162 | const root = el('ol', { 'slides-strip': '' }, [item('a'), item('b'), row]) |
| 163 | const template = { content: { firstElementChild: { cloneNode: () => item('') } } } |
| 164 | const ydoc = new Y.Doc() |
| 165 | const order = ids => ydoc.transact(() => { |
| 166 | const slides = ydoc.getArray('slides') |
| 167 | slides.delete(0, slides.length) |
| 168 | slides.push(ids.map(id => { const m = new Y.Map(); m.set('id', id); return m })) |
| 169 | }) |
| 170 | const strip = mountStrip({ root, ydoc, thumbURL: id => '/x/' + id, template, slideLabel: 'Slide {n}' }) |
| 171 | const ids = () => root.children.map(li => li.dataset.slideId ?? 'new') |
| 172 | |
| 173 | order(['a', 'b', 'c']) |
| 174 | strip.sync() |
| 175 | assert.deepEqual(ids(), ['a', 'b', 'c', 'new'], 'an added slide goes before the row') |
| 176 | order(['c', 'b', 'a']) |
| 177 | strip.sync() |
| 178 | assert.deepEqual(ids(), ['c', 'b', 'a', 'new'], 'a reorder leaves the row last') |
| 179 | order([]) |
| 180 | strip.sync() |
| 181 | assert.deepEqual(ids(), ['new'], 'the row is not a slide to drop') |
| 182 | order(['d']) |
| 183 | strip.sync() |
| 184 | assert.deepEqual(ids(), ['d', 'new'], 'the first slide of an empty deck goes before the row') |
| 185 | strip.destroy() |
| 186 | } finally { |
| 187 | ctx.restore() |
| 188 | } |
| 189 | }) |
| 190 | |
| 191 | // The words under a thumbnail are gone from the screen; the link's |
| 192 | // title is them, as a tooltip, and follows the slide's heading. |
| 193 | test('relabel keeps the link\'s title the label', () => { |
| 194 | const ctx = withDocument() |
| 195 | try { |
| 196 | const li = el('li', { 'slides-strip-item': '', 'data-slide-id': 'a' }, [ |
| 197 | el('a', { 'slides-strip-link': '' }, [el('span', { 'slides-strip-label': '' })]) |
| 198 | ]) |
| 199 | const root = el('ol', { 'slides-strip': '' }, [li]) |
| 200 | const ydoc = deckWith([{ id: 'a', heading: 'First' }]) |
| 201 | const strip = mountStrip({ root, ydoc, thumbURL: () => '', template: null, slideLabel: 'Slide {n}' }) |
| 202 | strip.flush() |
| 203 | assert.equal(li.querySelector('[slides-strip-link]').title, 'First') |
| 204 | // The heading changes; the next flush brings the tooltip with it. |
| 205 | const heading = ydoc.getArray('slides').get(0).get('blocks').get(0).get('text') |
| 206 | ydoc.transact(() => { |
| 207 | heading.delete(0, heading.length) |
| 208 | const p = new Y.XmlElement('paragraph') |
| 209 | p.insert(0, [new Y.XmlText('Renamed')]) |
| 210 | heading.insert(0, [p]) |
| 211 | }) |
| 212 | strip.flush() |
| 213 | assert.equal(li.querySelector('[slides-strip-link]').title, 'Renamed') |
| 214 | assert.equal(li.querySelector('[slides-strip-label]').textContent, 'Renamed') |
| 215 | strip.destroy() |
| 216 | } finally { |
| 217 | ctx.restore() |
| 218 | } |
| 219 | }) |
| 220 | |
| 221 | // Right-click on a thumbnail opens its own menu (spec §6.1), with the |
| 222 | // trigger focused as a press on it would leave it; a right-click |
| 223 | // anywhere else in the strip is the browser's. |
| 224 | test('a contextmenu on an item opens its menu', () => { |
| 225 | const ctx = withDocument() |
| 226 | try { |
| 227 | const summary = el('summary') |
| 228 | const menu = el('details', { 'slides-strip-menu': '' }, [summary]) |
| 229 | const thumb = el('div', { 'slides-thumb': '' }) |
| 230 | const li = el('li', { 'slides-strip-item': '', 'data-slide-id': 'a' }, [thumb, el('a', { 'slides-strip-link': '' }), menu]) |
| 231 | const row = el('li', { 'slides-strip-new': '' }) |
| 232 | const root = el('ol', { 'slides-strip': '' }, [li, row]) |
| 233 | const strip = mountStrip({ root, ydoc: null, thumbURL: () => '', template: null, slideLabel: 'Slide {n}' }) |
| 234 | const fire = target => { |
| 235 | const e = { target, defaultPrevented: false, preventDefault () { this.defaultPrevented = true } } |
| 236 | for (const l of root.listeners.filter(l => l.type === 'contextmenu')) l.fn(e) |
| 237 | return e |
| 238 | } |
| 239 | |
| 240 | assert.equal(fire(row).defaultPrevented, false, 'the row keeps the browser\'s menu') |
| 241 | assert.notEqual(menu.open, true) |
| 242 | assert.equal(fire(thumb).defaultPrevented, true) |
| 243 | assert.equal(menu.open, true) |
| 244 | assert.equal(ctx.document.activeElement, summary) |
| 245 | strip.destroy() |
| 246 | assert.equal(root.countListeners('contextmenu'), 0) |
| 247 | } finally { |
| 248 | ctx.restore() |
| 249 | } |
| 250 | }) |
| 251 | |
| 252 | // The number is real text in two places: the gutter's, drawn and |
| 253 | // hidden from a screen reader, and its twin in the link's name. Both |
| 254 | // follow every change of order or count; the label stays the title |
| 255 | // alone. |
| 256 | test('sync renumbers both numbers after an add, a remove and a reorder', () => { |
| 257 | const ctx = withDocument() |
| 258 | try { |
| 259 | const item = id => el('li', { 'slides-strip-item': '', 'data-slide-id': id }, [ |
| 260 | el('a', { 'slides-strip-link': '' }, [ |
| 261 | el('span', { 'slides-strip-number': '', 'aria-hidden': 'true' }), |
| 262 | el('span', { 'slides-strip-number-said': '' }), |
| 263 | el('span', { 'slides-strip-label': '' }) |
| 264 | ]) |
| 265 | ]) |
| 266 | const root = el('ol', { 'slides-strip': '' }, [item('a'), item('b'), el('li', { 'slides-strip-new': '' })]) |
| 267 | const template = { content: { firstElementChild: { cloneNode: () => item('') } } } |
| 268 | const ydoc = new Y.Doc() |
| 269 | const order = ids => ydoc.transact(() => { |
| 270 | const slides = ydoc.getArray('slides') |
| 271 | slides.delete(0, slides.length) |
| 272 | slides.push(ids.map(id => { const m = new Y.Map(); m.set('id', id); return m })) |
| 273 | }) |
| 274 | const strip = mountStrip({ root, ydoc, thumbURL: () => '', template, slideLabel: 'Slide {n}' }) |
| 275 | const numbers = () => root.children.filter(li => li.dataset.slideId).map(li => [ |
| 276 | li.dataset.slideId, |
| 277 | li.querySelector('[slides-strip-number]').textContent, |
| 278 | li.querySelector('[slides-strip-number-said]').textContent, |
| 279 | li.querySelector('[slides-strip-label]').textContent |
| 280 | ]) |
| 281 | |
| 282 | order(['a', 'b', 'c']) |
| 283 | strip.sync() |
| 284 | assert.deepEqual(numbers(), [['a', '1', '1', 'Slide 1'], ['b', '2', '2', 'Slide 2'], ['c', '3', '3', 'Slide 3']], 'after an add') |
| 285 | order(['b', 'c']) |
| 286 | strip.sync() |
| 287 | assert.deepEqual(numbers(), [['b', '1', '1', 'Slide 1'], ['c', '2', '2', 'Slide 2']], 'after a remove') |
| 288 | order(['c', 'b']) |
| 289 | strip.sync() |
| 290 | assert.deepEqual(numbers(), [['c', '1', '1', 'Slide 1'], ['b', '2', '2', 'Slide 2']], 'after a reorder') |
| 291 | strip.destroy() |
| 292 | } finally { |
| 293 | ctx.restore() |
| 294 | } |
| 295 | }) |
| 296 | |