oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// outline.mjs: View → Outline (spec §4.4), against the DOM stub. The
2// list keeps its rows across fills; opening it fills it and puts focus
3// on the selected box's row; a row selects its box and closes; Escape
4// closes and gives focus back, and goes no further.
5import test from 'node:test'
6import assert from 'node:assert/strict'
7import { el as stub, withDocument } from './dom-stub.mjs'
8import { fillBlocks, mountOutline } from './outline.mjs'
9
10// The list is refilled on EVERY document transaction while it is open,
11// a colleague's keystroke included. So it is reconciled rather than
12// rebuilt: an unchanged list has to be the SAME button elements
13// afterwards, or the focus went out of the document with the old ones.
14test('the Outline\'s list reconciles by id and keeps its rows', () => {
15 const ctx = withDocument()
16 try {
17 const list = stub('div', { 'slides-outline-list': '' })
18 const none = stub('p', { 'slides-outline-none': '' })
19 const blocks = [{ id: 'b1', type: 'heading' }, { id: 'b2', type: 'text' }]
20 const label = b => b.type
21 const ids = () => list.children.map(b => b.dataset.selectBlock)
22
23 fillBlocks({ list, none, blocks, selected: 'b1', label })
24 assert.deepEqual(ids(), ['b1', 'b2'])
25 assert.equal(none.hidden, true)
26 const [first, second] = list.children
27 assert.equal(first.getAttribute('aria-pressed'), 'true')
28 assert.equal(first.type, 'button')
29
30 fillBlocks({ list, none, blocks, selected: 'b1', label })
31 assert.equal(list.children[0], first, 'the same element, not a replacement')
32 assert.equal(list.children[1], second)
33
34 // A selection change writes the attribute and moves nothing.
35 fillBlocks({ list, none, blocks, selected: 'b2', label })
36 assert.equal(list.children[0], first)
37 assert.equal(first.getAttribute('aria-pressed'), 'false')
38 assert.equal(second.getAttribute('aria-pressed'), 'true')
39
40 // Reordering moves the elements that are already there.
41 fillBlocks({ list, none, blocks: [blocks[1], blocks[0]], selected: '', label })
42 assert.deepEqual(ids(), ['b2', 'b1'])
43 assert.equal(list.children[0], second)
44 assert.equal(list.children[1], first)
45
46 // A removed block takes its row with it; an empty slide says so.
47 fillBlocks({ list, none, blocks: [blocks[0]], selected: '', label })
48 assert.deepEqual(ids(), ['b1'])
49 assert.equal(list.children[0], first)
50 fillBlocks({ list, none, blocks: [], selected: '', label })
51 assert.deepEqual(ids(), [])
52 assert.equal(none.hidden, false)
53 } finally {
54 ctx.restore()
55 }
56})
57
58function outline (blocks, selected = '') {
59 const list = stub('div', { 'slides-outline-list': '' })
60 const none = stub('p', { 'slides-outline-none': '' })
61 const popover = stub('div', { 'slides-outline': '', popover: '' }, [none, list])
62 const calls = { show: 0, hide: 0, selected: [] }
63 popover.showPopover = () => { calls.show++ }
64 popover.hidePopover = () => { calls.hide++ }
65 const back = stub('summary')
66 const o = mountOutline({
67 popover,
68 getBlocks: () => blocks,
69 getSelected: () => selected,
70 select: id => calls.selected.push(id),
71 labels: { types: { heading: 'Heading', shape: 'Shape' } }
72 })
73 return { o, popover, list, none, calls, back }
74}
75
76const blocks = [
77 { id: 'h1', type: 'heading', text: [[{ t: 'Welcome to ' }, { t: 'Slides' }]] },
78 { id: 's1', type: 'shape' }
79]
80
81test('open fills the list by kind and first words, and focuses the selected box\'s row', () => {
82 const ctx = withDocument()
83 try {
84 const { o, list, calls, back } = outline(blocks, 's1')
85 o.fill()
86 assert.equal(list.children.length, 0, 'closed, nothing is filled')
87 o.open({ returnTo: back })
88 assert.equal(calls.show, 1)
89 assert.equal(o.isOpen(), true)
90 assert.deepEqual(list.children.map(b => b.textContent), ['Heading: Welcome to Slides', 'Shape'])
91 assert.equal(ctx.document.activeElement, list.children[1])
92 } finally {
93 ctx.restore()
94 }
95})
96
97test('with no box on the slide it says so, and focus goes to the popover', () => {
98 const ctx = withDocument()
99 try {
100 const { o, popover, none } = outline([])
101 o.open()
102 assert.equal(none.hidden, false)
103 assert.equal(ctx.document.activeElement, popover)
104 } finally {
105 ctx.restore()
106 }
107})
108
109test('a row selects its box and closes, leaving focus to the box', () => {
110 const ctx = withDocument()
111 try {
112 const { o, list, calls, back } = outline(blocks)
113 o.open({ returnTo: back })
114 assert.equal(ctx.document.activeElement, list.children[0], 'nothing selected: the first row')
115 list.children[0].dispatchEvent({ type: 'click' })
116 assert.deepEqual(calls.selected, ['h1'])
117 assert.equal(calls.hide, 1)
118 assert.equal(o.isOpen(), false)
119 assert.notEqual(ctx.document.activeElement, back, 'focus is the box\'s, not the menu\'s')
120 } finally {
121 ctx.restore()
122 }
123})
124
125test('Escape closes it, gives focus back, and goes no further', () => {
126 const ctx = withDocument()
127 try {
128 const { o, list, calls, back } = outline(blocks)
129 o.open({ returnTo: back })
130 const ev = { type: 'keydown', key: 'Escape' }
131 list.children[0].dispatchEvent(ev)
132 assert.equal(ev.defaultPrevented, true)
133 assert.equal(ev.stopped, true, 'the selection\'s own Escape does not hear it')
134 assert.equal(calls.hide, 1)
135 assert.equal(ctx.document.activeElement, back)
136 assert.deepEqual(calls.selected, [])
137 } finally {
138 ctx.restore()
139 }
140})
141
142test('closed by the browser (a click outside), it fills no more; destroy takes its listeners off', () => {
143 const ctx = withDocument()
144 try {
145 const { o, popover, list } = outline(blocks)
146 o.open()
147 popover.dispatchEvent({ type: 'toggle', newState: 'closed' })
148 assert.equal(o.isOpen(), false)
149 blocks.push({ id: 'x1', type: 'shape' })
150 o.fill()
151 assert.equal(list.children.length, 2)
152 blocks.pop()
153 o.destroy()
154 for (const type of ['click', 'keydown', 'toggle']) assert.equal(popover.countListeners(type), 0, type)
155 } finally {
156 ctx.restore()
157 }
158})
159
160test('a row from the template takes its words in its label, and keeps its check', () => {
161 const ctx = withDocument()
162 try {
163 const list = stub('div', { 'slides-outline-list': '' })
164 const make = () => stub('button', { 'estilo-menu-item': '' }, [stub('span', { 'estilo-menu-check': '' }), stub('span', { 'estilo-menu-label': '' })])
165 fillBlocks({ list, none: null, blocks: [{ id: 'b1', type: 'shape' }], selected: 'b1', label: () => 'Shape', make })
166 const [row] = list.children
167 assert.equal(row.children.length, 2, 'the check is kept')
168 assert.equal(row.querySelector('[estilo-menu-label]').textContent, 'Shape')
169 assert.equal(row.getAttribute('aria-pressed'), 'true')
170 } finally {
171 ctx.restore()
172 }
173})
174
175test('open keeps the panel inside the window: moved back from the end edge, no taller than the room under its title', () => {
176 const ctx = withDocument()
177 const was = { w: globalThis.innerWidth, h: globalThis.innerHeight }
178 try {
179 globalThis.innerWidth = 320
180 globalThis.innerHeight = 700
181 const { o, popover, back } = outline(blocks)
182 popover.style = {}
183 popover.getBoundingClientRect = () => ({ width: 304 })
184 back.getBoundingClientRect = () => ({ left: 200, bottom: 60 })
185 o.open({ returnTo: back })
186 assert.equal(popover.style.left, '8px', 'its title is 200px in, the panel 304px wide: it starts 8px in')
187 assert.equal(popover.style.top, '60px')
188 assert.equal(popover.style.maxBlockSize, '632px')
189 o.close()
190 back.getBoundingClientRect = () => ({ left: 10, bottom: 60 })
191 globalThis.innerWidth = 1000
192 o.open({ returnTo: back })
193 assert.equal(popover.style.left, '10px', 'room enough: under its title')
194 } finally {
195 globalThis.innerWidth = was.w
196 globalThis.innerHeight = was.h
197 ctx.restore()
198 }
199})
200