oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// box-menu.mjs: the menu on a selected box (the quieter editor, spec
2// §4). Which rows a box has, whose a right-click is, which keys are the
3// box's, where the round button and the menu go, and the menu itself
4// against the DOM stub: it draws only the rows that apply, a row runs
5// its command and closes, a region row sets the region, Esc closes and
6// gives focus back to the box, and an empty selection closes it.
7import test from 'node:test'
8import assert from 'node:assert/strict'
9import { el as stub, withDocument } from './dom-stub.mjs'
10import { rowsFor, canOwnContextMenu, boxKey, mountBoxMenu } from './box-menu.mjs'
11
12test('rowsFor: a canvas box is arranged in layers and placed; a flow box by region', () => {
13 assert.deepEqual(rowsFor({ layout: 'canvas', type: 'shape' }),
14 ['block-format', 'block-forward', 'block-back', 'block-move-up', 'block-move-down', 'block-place', 'block-delete'])
15 assert.deepEqual(rowsFor({ layout: 'flow', type: 'heading' }),
16 ['block-format', 'block-move-up', 'block-move-down', 'region', 'block-delete'])
17 // Every kind has the same rows: what decides is the slide.
18 assert.deepEqual(rowsFor({ layout: 'flow', type: 'image' }), rowsFor({ layout: 'flow', type: 'heading' }))
19 // A copy each time: a caller cannot change the next answer.
20 rowsFor({ layout: 'flow', type: 'text' }).pop()
21 assert.equal(rowsFor({ layout: 'flow', type: 'text' }).length, 5)
22})
23
24test('canOwnContextMenu: never in text being edited, anywhere else on a box', () => {
25 const ctx = withDocument()
26 try {
27 const text = stub('span')
28 const editable = stub('h1', { contenteditable: 'true' }, [text])
29 const shape = stub('div', { 'slides-block': '' })
30 ctx.document.append(editable, shape)
31 ctx.document.activeElement = editable
32 assert.equal(canOwnContextMenu(text), false, 'the caret is in this text: the browser\'s menu, spelling and paste')
33 assert.equal(canOwnContextMenu(editable), false)
34 assert.equal(canOwnContextMenu(shape), true)
35 ctx.document.activeElement = null
36 assert.equal(canOwnContextMenu(text), true, 'text nobody is editing: the box\'s menu')
37 assert.equal(canOwnContextMenu(null), false)
38 } finally {
39 ctx.restore()
40 }
41})
42
43test('boxKey: the box\'s keys, and Delete and the arrows only from the box itself', () => {
44 const k = (key, mods = {}, code = '') => ({ key, code, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, ...mods })
45 for (const onBox of [true, false]) {
46 assert.equal(boxKey(k(']', { ctrlKey: true }), { onBox }), 'block-forward')
47 assert.equal(boxKey(k('[', { metaKey: true }), { onBox }), 'block-back')
48 assert.equal(boxKey(k('F10', { shiftKey: true }), { onBox }), 'menu')
49 }
50 // By the key's place when the layout gives it another character.
51 assert.equal(boxKey(k('ü', { ctrlKey: true }, 'BracketLeft'), { onBox: true }), 'block-back')
52 // Mod+Shift+[ is the browser's (the tab before), and Alt is not ours.
53 assert.equal(boxKey(k('{', { metaKey: true, shiftKey: true }, 'BracketLeft'), { onBox: true }), null)
54 assert.equal(boxKey(k(']', { ctrlKey: true, altKey: true }), { onBox: true }), null)
55
56 assert.equal(boxKey(k('ArrowUp', { ctrlKey: true, shiftKey: true }), { onBox: true }), 'block-move-up')
57 assert.equal(boxKey(k('ArrowDown', { metaKey: true, shiftKey: true }), { onBox: true }), 'block-move-down')
58 assert.equal(boxKey(k('Delete'), { onBox: true }), 'block-delete')
59 assert.equal(boxKey(k('Backspace'), { onBox: true }), 'block-delete')
60 // In text these select and delete text.
61 assert.equal(boxKey(k('ArrowUp', { ctrlKey: true, shiftKey: true }), { onBox: false }), null)
62 assert.equal(boxKey(k('Delete'), { onBox: false }), null)
63 assert.equal(boxKey(k('Backspace'), { onBox: false }), null)
64 // The overlay's own: the arrows nudge, Shift further, Alt resizes.
65 for (const mods of [{}, { shiftKey: true }, { altKey: true }]) assert.equal(boxKey(k('ArrowUp', mods), { onBox: true }), null)
66 assert.equal(boxKey(k('Delete', { ctrlKey: true }), { onBox: true }), null)
67 assert.equal(boxKey(k('F10'), { onBox: true }), null)
68})
69
70// The menu as editor.html draws it: every row, a group of region rows.
71function menuMarkup () {
72 const row = (id, extra = {}) => stub('button', { 'estilo-menu-item': '', 'data-row': id, 'data-command': id, ...extra })
73 const regions = ['heading', 'body', 'aside', 'media'].map(r => stub('button', { 'estilo-menu-item': '', 'data-region': r, 'aria-checked': 'false' }))
74 const menu = stub('div', { 'slides-box-menu': '' }, [
75 row('block-format'), stub('hr'), row('block-forward'), row('block-back'), row('block-move-up'), row('block-move-down'), stub('hr'),
76 row('block-place'), stub('div', { 'data-row': 'region' }, regions), stub('hr'), row('block-delete')
77 ])
78 menu.showPopover = () => { menu.popoverOpen = true }
79 menu.hidePopover = () => { menu.popoverOpen = false }
80 return menu
81}
82
83function rig ({ layout = 'canvas', type = 'shape', region } = {}) {
84 const ctx = withDocument()
85 const box = stub('div', { 'slides-block': '', 'data-id': 'b1' })
86 const stageEl = stub('div', { 'slides-stage': '' }, [box])
87 // The stage's frame: nothing outside Phone view, the phone's screen in it.
88 const frame = stub('div', { 'slides-stage-frame': '' }, [stageEl])
89 const button = stub('button', { 'slides-box-more': '' })
90 const menu = menuMarkup()
91 const root = stub('div', { 'slides-editor': '' }, [frame, button, menu])
92 ctx.document.append(root)
93 const calls = []
94 const state = { selected: 'b1', layout }
95 const block = { id: 'b1', type, region }
96 const m = mountBoxMenu({
97 root,
98 stageEl,
99 getSelected: () => state.selected,
100 getSlide: () => ({ id: 's1', layout: state.layout, blocks: [block] }),
101 run: name => calls.push(['run', name]),
102 setRegion: (id, r) => calls.push(['region', id, r]),
103 focusBox: id => calls.push(['focus', id]),
104 select: id => { state.selected = id },
105 after: (name, id) => calls.push(['after', name, id])
106 })
107 const shown = () => menu.querySelectorAll('[estilo-menu-item]').filter(i => !i.hidden && !i.closest('[data-row]').hidden)
108 return { ctx, m, root, menu, button, box, stageEl, frame, calls, state, shown, block }
109}
110
111test('open draws only the rows that apply, and focuses the first', () => {
112 const canvas = rig()
113 try {
114 assert.equal(canvas.m.open('button'), true)
115 assert.equal(canvas.m.isOpen(), true)
116 assert.equal(canvas.menu.popoverOpen, true)
117 assert.deepEqual(canvas.shown().map(i => i.dataset.row), rowsFor({ layout: 'canvas' }))
118 assert.equal(canvas.ctx.document.activeElement, canvas.shown()[0])
119 assert.equal(canvas.button.getAttribute('aria-expanded'), 'true')
120 } finally {
121 canvas.m.destroy()
122 canvas.ctx.restore()
123 }
124 const flow = rig({ layout: 'flow', type: 'image' })
125 try {
126 flow.m.open('button')
127 const rows = flow.shown().map(i => i.dataset.row || 'region:' + i.dataset.region)
128 assert.deepEqual(rows, ['block-format', 'block-move-up', 'block-move-down', 'region:heading', 'region:body', 'region:aside', 'region:media', 'block-delete'])
129 // The box's region is checked: an image's, where nobody said, is media.
130 assert.deepEqual(flow.menu.querySelectorAll('[data-region]').map(b => b.getAttribute('aria-checked')), ['false', 'false', 'false', 'true'])
131 } finally {
132 flow.m.destroy()
133 flow.ctx.restore()
134 }
135})
136
137test('a row runs its command and closes; a region row sets the region', () => {
138 const r = rig({ layout: 'flow', type: 'text', region: 'aside' })
139 try {
140 r.m.open('button')
141 const row = r.menu.querySelector('[data-row="block-move-down"]')
142 row.dispatchEvent({ type: 'click', target: row })
143 assert.equal(r.m.isOpen(), false)
144 assert.equal(r.menu.popoverOpen, false)
145 assert.equal(r.button.getAttribute('aria-expanded'), 'false')
146 assert.deepEqual(r.calls, [['focus', 'b1'], ['run', 'block-move-down'], ['after', 'block-move-down', 'b1']])
147
148 r.calls.length = 0
149 r.m.open('button')
150 const body = r.menu.querySelector('[data-region="body"]')
151 body.dispatchEvent({ type: 'click', target: body })
152 assert.equal(r.m.isOpen(), false)
153 assert.deepEqual(r.calls, [['focus', 'b1'], ['region', 'b1', 'body'], ['after', 'region', 'b1']])
154
155 // Delete: no focus back to the box, which is going; where focus goes
156 // next is after()'s (stage-focus.mjs).
157 r.calls.length = 0
158 r.m.open('button')
159 const del = r.menu.querySelector('[data-row="block-delete"]')
160 del.dispatchEvent({ type: 'click', target: del })
161 assert.deepEqual(r.calls, [['run', 'block-delete'], ['after', 'block-delete', 'b1']])
162 } finally {
163 r.m.destroy()
164 r.ctx.restore()
165 }
166})
167
168test('the arrows move through the rows drawn; Esc closes and focuses the box, and goes no further', () => {
169 const r = rig({ layout: 'flow', type: 'heading' })
170 try {
171 r.m.open('button')
172 const rows = r.shown()
173 const key = k => { const ev = { type: 'keydown', key: k, target: r.ctx.document.activeElement }; r.ctx.document.activeElement.dispatchEvent(ev); return ev }
174 key('ArrowDown')
175 assert.equal(r.ctx.document.activeElement, rows[1])
176 key('ArrowUp'); key('ArrowUp')
177 assert.equal(r.ctx.document.activeElement, rows[rows.length - 1], 'up from the first is the last')
178 key('Home')
179 assert.equal(r.ctx.document.activeElement, rows[0])
180 const esc = key('Escape')
181 assert.equal(esc.defaultPrevented, true)
182 assert.equal(esc.stopped, true, 'the selection\'s own Escape never hears it')
183 assert.equal(r.m.isOpen(), false)
184 assert.deepEqual(r.calls, [['focus', 'b1']])
185 } finally {
186 r.m.destroy()
187 r.ctx.restore()
188 }
189})
190
191test('place: the round button follows the selection, and an empty selection closes the menu', () => {
192 const r = rig()
193 try {
194 r.m.place()
195 assert.equal(r.button.hidden, false)
196 r.m.open('button')
197 r.state.selected = ''
198 r.m.place()
199 assert.equal(r.m.isOpen(), false, 'the box it was for is not selected now')
200 assert.equal(r.button.hidden, true)
201 // A box that has gone from the stage is the same.
202 r.state.selected = 'b1'
203 r.m.place()
204 r.m.open('button')
205 r.box.remove()
206 r.m.place()
207 assert.equal(r.m.isOpen(), false)
208 assert.equal(r.button.hidden, true)
209 assert.equal(r.m.open('button'), true, 'the slide still has the box: its menu still opens')
210 r.state.selected = ''
211 assert.equal(r.m.close() ?? true, true)
212 assert.equal(r.m.open('button'), false, 'nothing selected: no menu')
213 } finally {
214 r.m.destroy()
215 r.ctx.restore()
216 }
217})
218
219test('a right-click on a box opens the menu; in text being edited it is the browser\'s', () => {
220 const r = rig({ layout: 'flow', type: 'heading' })
221 try {
222 const word = stub('span')
223 const editable = stub('h1', { 'slides-block': '', 'data-id': 'b1', contenteditable: 'true' }, [word])
224 r.box.remove()
225 r.stageEl.append(editable)
226 r.ctx.document.activeElement = editable
227 const inText = { type: 'contextmenu', target: word, clientX: 40, clientY: 40 }
228 word.dispatchEvent(inText)
229 assert.equal(inText.defaultPrevented, undefined)
230 assert.equal(r.m.isOpen(), false)
231
232 r.ctx.document.activeElement = null
233 const onBox = { type: 'contextmenu', target: word, clientX: 40, clientY: 40 }
234 word.dispatchEvent(onBox)
235 assert.equal(onBox.defaultPrevented, true)
236 assert.equal(r.m.isOpen(), true)
237 } finally {
238 r.m.destroy()
239 r.ctx.restore()
240 }
241})
242
243test('the keys: from the box, from its text, and never from outside the stage', () => {
244 const r = rig({ layout: 'flow', type: 'shape' })
245 try {
246 const press = (target, key, mods = {}) => {
247 r.ctx.document.activeElement = target
248 const ev = { type: 'keydown', key, target, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, ...mods }
249 target.dispatchEvent(ev)
250 return ev
251 }
252 // The box itself has focus: Delete deletes it.
253 assert.equal(press(r.box, 'Delete').defaultPrevented, true)
254 assert.deepEqual(r.calls, [['run', 'block-delete'], ['after', 'block-delete', 'b1']])
255 // Bring forward on a flow slide: held from the browser, nothing done.
256 r.calls.length = 0
257 assert.equal(press(r.box, ']', { ctrlKey: true }).defaultPrevented, true)
258 assert.deepEqual(r.calls, [])
259 // Outside the stage nothing is the box's: the strip's Delete, the bar's.
260 const strip = stub('a')
261 r.ctx.document.append(strip)
262 assert.equal(press(strip, 'Delete').defaultPrevented, undefined)
263 assert.equal(press(strip, '[', { metaKey: true }).defaultPrevented, undefined)
264 // Nothing selected: nothing.
265 r.state.selected = ''
266 assert.equal(press(r.box, 'Delete').defaultPrevented, undefined)
267 assert.deepEqual(r.calls, [])
268 // Shift+F10 opens the menu.
269 r.state.selected = 'b1'
270 press(r.box, 'F10', { shiftKey: true })
271 assert.equal(r.m.isOpen(), true)
272 } finally {
273 r.m.destroy()
274 r.ctx.restore()
275 }
276})
277
278// A colleague changes the slide under an open menu (the review's A3):
279// its rows were for the slide as it was.
280test('sync: the menu closes when the slide\'s layout changes, and its region checks follow the box', () => {
281 const r = rig({ layout: 'canvas', type: 'shape' })
282 try {
283 r.m.open('button')
284 r.calls.length = 0
285 r.m.sync()
286 assert.equal(r.m.isOpen(), true, 'nothing changed: it stays open')
287 r.state.layout = 'flow'
288 r.m.sync()
289 assert.equal(r.m.isOpen(), false, 'the slide is flow now: the canvas rows were the old slide\'s')
290 assert.deepEqual(r.calls, [['focus', 'b1']], 'focus back on the box, which is still there')
291 } finally {
292 r.m.destroy()
293 r.ctx.restore()
294 }
295 const f = rig({ layout: 'flow', type: 'text', region: 'body' })
296 try {
297 f.m.open('button')
298 f.block.region = 'aside'
299 f.m.sync()
300 assert.equal(f.m.isOpen(), true)
301 assert.deepEqual(f.menu.querySelectorAll('[data-region]').map(b => b.getAttribute('aria-checked')), ['false', 'false', 'true', 'false'])
302 } finally {
303 f.m.destroy()
304 f.ctx.restore()
305 }
306})
307
308test('a row that no longer applies to the box does nothing, and closes the menu', () => {
309 const r = rig({ layout: 'canvas', type: 'shape' })
310 try {
311 r.m.open('button')
312 r.state.layout = 'flow'
313 r.calls.length = 0
314 const place = r.menu.querySelector('[data-row="block-place"]')
315 place.dispatchEvent({ type: 'click', target: place })
316 assert.equal(r.m.isOpen(), false)
317 assert.deepEqual(r.calls.filter(c => c[0] === 'run'), [], 'Position and size is not a flow slide\'s')
318 // The box gone from the slide: nothing either.
319 r.state.layout = 'canvas'
320 r.m.open('button')
321 r.block.id = 'gone'
322 r.calls.length = 0
323 const del = r.menu.querySelector('[data-row="block-delete"]')
324 del.dispatchEvent({ type: 'click', target: del })
325 assert.deepEqual(r.calls.filter(c => c[0] === 'run'), [])
326 } finally {
327 r.m.destroy()
328 r.ctx.restore()
329 }
330})
331
332test('destroy takes every listener off the page\'s elements', () => {
333 const r = rig()
334 r.m.destroy()
335 for (const [el, type] of [[r.menu, 'click'], [r.menu, 'keydown'], [r.button, 'click'], [r.stageEl, 'contextmenu'], [r.stageEl, 'scroll'], [r.frame, 'scroll'], [r.ctx.document, 'keydown'], [r.ctx.document, 'pointerdown']]) {
336 assert.equal(el.countListeners(type), 0, type)
337 }
338 r.ctx.restore()
339})
340
341// Fix round 2: where no place on the stage is clear of the handles (a
342// stage smaller than the button), the round button docks in the stage
343// bar beside the view switch, off the slide; on a stage with room it
344// is placed on the box again.
345test('place: a stage with no room docks the round button beside the view switch', () => {
346 const r = rig()
347 try {
348 const rectOf = (left, top, width, height) => () => ({ left, top, width, height, right: left + width, bottom: top + height })
349 const switcher = stub('fieldset', { 'slides-switcher': '' })
350 switcher.getBoundingClientRect = rectOf(500, 10, 100, 28)
351 r.root.append(switcher)
352 r.root.getBoundingClientRect = rectOf(0, 0, 1000, 800)
353 r.button.style = {}
354 r.button.offsetWidth = 28
355 r.stageEl.getBoundingClientRect = rectOf(100, 100, 20, 20)
356 r.box.getBoundingClientRect = rectOf(100, 100, 20, 20)
357 r.m.place()
358 assert.equal(r.button.hidden, false)
359 assert.equal(r.button.hasAttribute('data-docked'), true)
360 assert.deepEqual([r.button.style.left, r.button.style.top], ['608px', '10px'])
361
362 r.stageEl.getBoundingClientRect = rectOf(100, 100, 600, 340)
363 r.box.getBoundingClientRect = rectOf(200, 200, 200, 100)
364 r.m.place()
365 assert.equal(r.button.hasAttribute('data-docked'), false)
366 assert.deepEqual([r.button.style.left, r.button.style.top], ['372px', '156px'], 'above the top end corner')
367 } finally {
368 r.m.destroy()
369 r.ctx.restore()
370 }
371})
372
373// Phone view scrolls the phone's screen (the stage's frame), not the
374// stage (the review's A4): the round button follows that scroll, kept
375// inside what the screen shows, and is not drawn while its box is
376// scrolled out of it.
377test('place: the round button follows the phone screen\'s scroll, and goes while its box is out of sight', () => {
378 const r = rig()
379 try {
380 const rectOf = (left, top, width, height) => () => ({ left, top, width, height, right: left + width, bottom: top + height })
381 r.root.getBoundingClientRect = rectOf(0, 0, 1000, 800)
382 r.button.style = {}
383 r.button.offsetWidth = 28
384 r.frame.getBoundingClientRect = rectOf(100, 100, 390, 400)
385 const at = top => {
386 r.stageEl.getBoundingClientRect = rectOf(100, top - 100, 390, 1200)
387 r.box.getBoundingClientRect = rectOf(150, top, 200, 100)
388 }
389 at(200)
390 r.m.place()
391 assert.equal(r.button.hidden, false)
392 assert.deepEqual([r.button.style.left, r.button.style.top], ['322px', '156px'], 'above the top end corner')
393 // Scrolled until the box is above the screen: no button.
394 at(-150)
395 r.frame.dispatchEvent({ type: 'scroll', target: r.frame })
396 assert.equal(r.button.hidden, true)
397 // Back until the box's top is near the screen's: the button is in
398 // the screen, beside the box rather than above the screen.
399 at(150)
400 r.frame.dispatchEvent({ type: 'scroll', target: r.frame })
401 assert.equal(r.button.hidden, false)
402 assert.ok(parseFloat(r.button.style.top) >= 100, `the button's top ${r.button.style.top} is inside the screen`)
403 // Outside Phone view the frame is nothing (display: contents, no box).
404 r.frame.getBoundingClientRect = rectOf(0, 0, 0, 0)
405 at(200)
406 r.stageEl.dispatchEvent({ type: 'scroll', target: r.stageEl })
407 assert.deepEqual([r.button.style.left, r.button.style.top], ['322px', '156px'])
408 } finally {
409 r.m.destroy()
410 r.ctx.restore()
411 }
412})
413