| 1 | // stage-focus.mjs: every box a tab stop (spec §4.4), against the DOM |
| 2 | // stub. A box with no text is given one after each install; focus on it |
| 3 | // selects it; on a canvas slide the overlay takes over, and Tab on the |
| 4 | // overlay walks the boxes in the slide's order. |
| 5 | import test from 'node:test' |
| 6 | import assert from 'node:assert/strict' |
| 7 | import { el, withDocument } from './dom-stub.mjs' |
| 8 | import { mountStageFocus, tabStop, lastStop, boxesOf } from './stage-focus.mjs' |
| 9 | |
| 10 | const TEXT = ['heading', 'text', 'list', 'quote', 'code', 'table'] |
| 11 | const labels = { types: { heading: 'Heading', image: 'Image', shape: 'Shape', table: 'Table', spacer: 'Spacer', columns: 'Columns', text: 'Text' } } |
| 12 | |
| 13 | // A slide of a heading (its text editor is the box), an image, a |
| 14 | // columns box holding a text box, a shape, a table whose cells are its |
| 15 | // editors, and a spacer. |
| 16 | function stage (layout = 'flow') { |
| 17 | const box = (type, id, attrs = {}, children = []) => el('div', { 'slides-block': '', 'data-block': type, 'data-id': id, ...attrs }, children) |
| 18 | const heading = box('heading', 'h1', { contenteditable: 'true' }) |
| 19 | const image = box('image', 'i1') |
| 20 | const inner = box('text', 'x1', { contenteditable: 'true' }) |
| 21 | const columns = box('columns', 'c1', {}, [inner]) |
| 22 | const shape = box('shape', 's1') |
| 23 | const spacer = box('spacer', 'p1', { 'aria-hidden': 'true' }) |
| 24 | const cell = () => el('td', { contenteditable: 'true' }) |
| 25 | const cells = [cell(), cell()] |
| 26 | const table = box('table', 't1', {}, [el('table', {}, cells)]) |
| 27 | const section = el('section', { 'slides-slide': '', 'data-layout': layout }, [heading, image, columns, shape, table, spacer]) |
| 28 | const overlay = el('div', { 'slides-canvas-overlay': '' }) |
| 29 | const stageEl = el('div', { 'slides-stage': '' }, [section, overlay]) |
| 30 | return { stageEl, section, overlay, heading, image, columns, inner, shape, spacer, table, cells } |
| 31 | } |
| 32 | |
| 33 | function mount (s, { canvas = null } = {}) { |
| 34 | const selected = [] |
| 35 | const sf = mountStageFocus({ |
| 36 | stageEl: s.stageEl, |
| 37 | select: id => { |
| 38 | selected.push(id) |
| 39 | for (const b of boxesOf(s.stageEl)) { |
| 40 | if (b.getAttribute('data-id') === id) b.setAttribute('data-selected', 'true') |
| 41 | else b.removeAttribute('data-selected') |
| 42 | } |
| 43 | }, |
| 44 | canvas, |
| 45 | isText: t => TEXT.includes(t), |
| 46 | labels |
| 47 | }) |
| 48 | return { sf, selected } |
| 49 | } |
| 50 | |
| 51 | test('refresh makes each box with no text a named group in the tab order, and leaves text to its editor', () => { |
| 52 | const ctx = withDocument() |
| 53 | try { |
| 54 | const s = stage() |
| 55 | const { sf } = mount(s) |
| 56 | sf.refresh() |
| 57 | sf.refresh() // again, after the next install: the same |
| 58 | for (const [box, name] of [[s.image, 'Image'], [s.columns, 'Columns'], [s.shape, 'Shape'], [s.spacer, 'Spacer']]) { |
| 59 | assert.equal(box.getAttribute('tabindex'), '0', name) |
| 60 | assert.equal(box.getAttribute('role'), 'group', name) |
| 61 | assert.equal(box.getAttribute('aria-label'), name) |
| 62 | } |
| 63 | assert.equal(s.spacer.getAttribute('aria-hidden'), null, 'what can take focus is not hidden') |
| 64 | for (const box of [s.heading, s.inner, s.table]) { |
| 65 | assert.equal(box.getAttribute('tabindex'), null, box.getAttribute('data-block')) |
| 66 | assert.equal(box.getAttribute('role'), null) |
| 67 | } |
| 68 | // A box's stop: its own editor, never one of a box inside it. |
| 69 | assert.equal(tabStop(s.heading), s.heading) |
| 70 | assert.equal(tabStop(s.table), s.cells[0]) |
| 71 | assert.equal(lastStop(s.table), s.cells[1], 'reached backwards, a table\'s last cell') |
| 72 | assert.equal(tabStop(s.columns), s.columns) |
| 73 | assert.equal(tabStop(s.image), s.image) |
| 74 | } finally { |
| 75 | ctx.restore() |
| 76 | } |
| 77 | }) |
| 78 | |
| 79 | test('focus on a box with no text selects it; focus in text is the text editor\'s to say', () => { |
| 80 | const ctx = withDocument() |
| 81 | try { |
| 82 | const s = stage() |
| 83 | const { sf, selected } = mount(s) |
| 84 | sf.refresh() |
| 85 | s.shape.focus() |
| 86 | assert.deepEqual(selected, ['s1']) |
| 87 | s.heading.focus() |
| 88 | s.cells[1].focus() |
| 89 | assert.deepEqual(selected, ['s1'], 'a text editor taking focus selects through the format bar, not here') |
| 90 | } finally { |
| 91 | ctx.restore() |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | test('on a canvas slide focus on a box goes on to the overlay', () => { |
| 96 | const ctx = withDocument() |
| 97 | try { |
| 98 | const s = stage('canvas') |
| 99 | let overlayFocus = 0 |
| 100 | const { sf, selected } = mount(s, { canvas: { focus: () => { overlayFocus++; s.overlay.focus() } } }) |
| 101 | sf.refresh() |
| 102 | s.image.focus() |
| 103 | assert.deepEqual(selected, ['i1']) |
| 104 | assert.equal(overlayFocus, 1) |
| 105 | assert.equal(ctx.document.activeElement, s.overlay) |
| 106 | } finally { |
| 107 | ctx.restore() |
| 108 | } |
| 109 | }) |
| 110 | |
| 111 | test('Tab and Shift+Tab on the overlay walk the boxes in the slide\'s order', () => { |
| 112 | const ctx = withDocument() |
| 113 | try { |
| 114 | const s = stage('canvas') |
| 115 | const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) |
| 116 | sf.refresh() |
| 117 | const tab = (shiftKey = false) => s.overlay.dispatchEvent({ type: 'keydown', key: 'Tab', shiftKey }) |
| 118 | |
| 119 | s.image.focus() |
| 120 | assert.equal(tab(), false, 'Tab is taken') |
| 121 | assert.equal(ctx.document.activeElement, s.overlay, 'the columns box, then its overlay') |
| 122 | assert.equal(selected.at(-1), 'c1') |
| 123 | // The columns box: next is the text inside it, its own editor. |
| 124 | assert.equal(tab(), false) |
| 125 | assert.equal(ctx.document.activeElement, s.inner) |
| 126 | |
| 127 | // Shift+Tab from the shape's overlay: the text in the columns box. |
| 128 | s.shape.focus() |
| 129 | assert.equal(selected.at(-1), 's1') |
| 130 | assert.equal(tab(true), false) |
| 131 | assert.equal(ctx.document.activeElement, s.inner) |
| 132 | |
| 133 | // Shift+Tab from the spacer's overlay: the table's last cell, so |
| 134 | // Shift+Tab walks its cells back. |
| 135 | s.spacer.focus() |
| 136 | assert.equal(selected.at(-1), 'p1') |
| 137 | assert.equal(tab(true), false) |
| 138 | assert.equal(ctx.document.activeElement, s.cells[1]) |
| 139 | |
| 140 | // The last box: Tab is the browser's, on past the slide. |
| 141 | s.spacer.setAttribute('data-selected', 'true') |
| 142 | assert.equal(tab(), true, 'Tab past the last box is not taken') |
| 143 | |
| 144 | // Before the first box: the browser's step, from the section. |
| 145 | s.spacer.removeAttribute('data-selected') |
| 146 | s.heading.setAttribute('data-selected', 'true') |
| 147 | ctx.document.activeElement = s.overlay |
| 148 | assert.equal(tab(true), true, 'Shift+Tab before the first box is not taken') |
| 149 | assert.equal(ctx.document.activeElement, s.section) |
| 150 | assert.equal(s.section.getAttribute('tabindex'), '-1') |
| 151 | s.section.dispatchEvent({ type: 'blur' }) |
| 152 | assert.equal(s.section.getAttribute('tabindex'), null, 'and only for that step') |
| 153 | |
| 154 | // Other keys are the overlay's own (arrows nudge, canvas.mjs). |
| 155 | assert.equal(s.overlay.dispatchEvent({ type: 'keydown', key: 'ArrowRight' }), true) |
| 156 | } finally { |
| 157 | ctx.restore() |
| 158 | } |
| 159 | }) |
| 160 | |
| 161 | // A box hidden on a phone is not drawn in Phone view, and what is not |
| 162 | // drawn takes no focus: Tab goes past it to the next box drawn, and the |
| 163 | // Outline is how it is reached. |
| 164 | test('Tab and Shift+Tab on the overlay pass a box that is not drawn', () => { |
| 165 | const ctx = withDocument() |
| 166 | try { |
| 167 | const s = stage('canvas') |
| 168 | const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) |
| 169 | sf.refresh() |
| 170 | s.columns.checkVisibility = () => false |
| 171 | s.inner.checkVisibility = () => false |
| 172 | s.shape.checkVisibility = () => true |
| 173 | const tab = (shiftKey = false) => s.overlay.dispatchEvent({ type: 'keydown', key: 'Tab', shiftKey }) |
| 174 | s.image.focus() |
| 175 | assert.equal(tab(), false) |
| 176 | assert.equal(ctx.document.activeElement, s.overlay) |
| 177 | assert.equal(selected.at(-1), 's1', 'past the columns box and its text, to the shape') |
| 178 | assert.equal(tab(true), false) |
| 179 | assert.equal(selected.at(-1), 'i1', 'and back to the image') |
| 180 | // Selected from the Outline while not drawn: Tab still goes on from it. |
| 181 | s.image.removeAttribute('data-selected') |
| 182 | s.columns.setAttribute('data-selected', 'true') |
| 183 | ctx.document.activeElement = s.overlay |
| 184 | assert.equal(tab(), false) |
| 185 | assert.equal(selected.at(-1), 's1') |
| 186 | } finally { |
| 187 | ctx.restore() |
| 188 | } |
| 189 | }) |
| 190 | |
| 191 | test('focus(id) puts focus on the box: its text, the box itself, or on a canvas slide the overlay', () => { |
| 192 | const ctx = withDocument() |
| 193 | try { |
| 194 | const s = stage() |
| 195 | const { sf, selected } = mount(s) |
| 196 | sf.refresh() |
| 197 | assert.equal(sf.focus('h1'), true) |
| 198 | assert.equal(ctx.document.activeElement, s.heading) |
| 199 | sf.focus('i1') |
| 200 | assert.equal(ctx.document.activeElement, s.image) |
| 201 | assert.deepEqual(selected, ['i1']) |
| 202 | assert.equal(sf.focus('gone'), false) |
| 203 | |
| 204 | const c = stage('canvas') |
| 205 | const m = mount(c, { canvas: { focus: () => c.overlay.focus() } }) |
| 206 | m.sf.refresh() |
| 207 | m.sf.focus('h1') |
| 208 | assert.equal(ctx.document.activeElement, c.overlay, 'a text box on a canvas slide too: its overlay moves it') |
| 209 | assert.deepEqual(m.selected, ['h1']) |
| 210 | } finally { |
| 211 | ctx.restore() |
| 212 | } |
| 213 | }) |
| 214 | |
| 215 | test('destroy takes every listener off the stage', () => { |
| 216 | const s = stage() |
| 217 | const { sf } = mount(s) |
| 218 | assert.equal(s.stageEl.countListeners('focusin'), 1) |
| 219 | assert.equal(s.stageEl.countListeners('keydown'), 1) |
| 220 | sf.destroy() |
| 221 | assert.equal(s.stageEl.countListeners('focusin'), 0) |
| 222 | assert.equal(s.stageEl.countListeners('keydown'), 0) |
| 223 | }) |
| 224 | |
| 225 | // A load replaces the stage's DOM, and focus on a box with it: what was |
| 226 | // on a box itself (its stop, or the canvas overlay) before is put back |
| 227 | // on the same box after, so a key on the box (send back, Delete's |
| 228 | // neighbours) leaves the keyboard where it was. Text is not held, and |
| 229 | // nothing is taken from somewhere focus has gone since. |
| 230 | test('held and restore keep focus on a box across a load', () => { |
| 231 | const ctx = withDocument() |
| 232 | try { |
| 233 | const s = stage() |
| 234 | const { sf } = mount(s) |
| 235 | sf.refresh() |
| 236 | s.shape.focus() |
| 237 | assert.equal(sf.held(), 's1') |
| 238 | // The install: a new section, nothing focused. |
| 239 | const next = stage() |
| 240 | s.stageEl.children.splice(0, s.stageEl.children.length) |
| 241 | s.stageEl.append(...next.stageEl.children) |
| 242 | ctx.document.activeElement = null |
| 243 | sf.refresh() |
| 244 | assert.equal(sf.restore('s1'), true) |
| 245 | assert.equal(ctx.document.activeElement, next.shape) |
| 246 | |
| 247 | // Text is not a box's own focus; and focus that went elsewhere stays. |
| 248 | next.heading.focus() |
| 249 | assert.equal(sf.held(), '') |
| 250 | const card = el('input') |
| 251 | ctx.document.append(card) |
| 252 | card.focus() |
| 253 | assert.equal(sf.restore('s1'), false) |
| 254 | assert.equal(ctx.document.activeElement, card) |
| 255 | } finally { |
| 256 | ctx.restore() |
| 257 | } |
| 258 | }) |
| 259 | |
| 260 | test('held on a canvas slide is the selected box, from its overlay', () => { |
| 261 | const ctx = withDocument() |
| 262 | try { |
| 263 | const s = stage('canvas') |
| 264 | const canvas = { focus: () => s.overlay.focus() } |
| 265 | const { sf } = mount(s, { canvas }) |
| 266 | sf.refresh() |
| 267 | s.shape.setAttribute('data-selected', 'true') |
| 268 | s.overlay.focus() |
| 269 | assert.equal(sf.held(), 's1') |
| 270 | } finally { |
| 271 | ctx.restore() |
| 272 | } |
| 273 | }) |
| 274 | |
| 275 | // After a command from the box's menu or its keys (the review's A7): |
| 276 | // Delete puts focus on the next box drawn, else the previous, else the |
| 277 | // slide; a move keeps focus on the box, its text included, across the |
| 278 | // load that follows (held, then restore). |
| 279 | test('after Delete, focus goes to the next box, else the previous, else the slide', () => { |
| 280 | const ctx = withDocument() |
| 281 | try { |
| 282 | const s = stage('canvas') |
| 283 | const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) |
| 284 | sf.refresh() |
| 285 | // The columns box: next is the shape (not the text inside the box going). |
| 286 | sf.after('block-delete', 'c1') |
| 287 | assert.equal(selected.at(-1), 's1') |
| 288 | assert.equal(ctx.document.activeElement, s.overlay) |
| 289 | assert.equal(sf.held(), 's1', 'held for the load that takes the box away') |
| 290 | // The last box: the one before it. |
| 291 | sf.after('block-delete', 'p1') |
| 292 | assert.equal(selected.at(-1), 't1') |
| 293 | // Not drawn in this view: passed over. |
| 294 | s.table.checkVisibility = () => false |
| 295 | sf.after('block-delete', 'p1') |
| 296 | assert.equal(selected.at(-1), 's1') |
| 297 | } finally { |
| 298 | ctx.restore() |
| 299 | } |
| 300 | const ctx2 = withDocument() |
| 301 | try { |
| 302 | const box = el('div', { 'slides-block': '', 'data-block': 'shape', 'data-id': 'only' }) |
| 303 | const section = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [box]) |
| 304 | const stageEl = el('div', { 'slides-stage': '' }, [section]) |
| 305 | const sf = mountStageFocus({ stageEl, select: () => {}, labels }) |
| 306 | sf.refresh() |
| 307 | sf.after('block-delete', 'only') |
| 308 | assert.equal(ctx2.document.activeElement, section, 'nothing else on the slide: the slide itself') |
| 309 | const slide = sf.held() |
| 310 | assert.ok(slide, 'held across the load') |
| 311 | // The load: a new section, focus gone with the old one. |
| 312 | const next = el('section', { 'slides-slide': '', 'data-layout': 'flow' }) |
| 313 | stageEl.children.splice(0, stageEl.children.length) |
| 314 | stageEl.append(next) |
| 315 | ctx2.document.activeElement = null |
| 316 | assert.equal(sf.restore(slide, ''), true) |
| 317 | assert.equal(ctx2.document.activeElement, next) |
| 318 | } finally { |
| 319 | ctx2.restore() |
| 320 | } |
| 321 | }) |
| 322 | |
| 323 | test('after a move, focus in the box\'s text is held across the load too', () => { |
| 324 | const ctx = withDocument() |
| 325 | try { |
| 326 | const s = stage() |
| 327 | const { sf } = mount(s) |
| 328 | sf.refresh() |
| 329 | s.heading.focus() |
| 330 | assert.equal(sf.held(), '', 'text is the text editor\'s, ordinarily') |
| 331 | sf.after('block-move-down', 'h1') |
| 332 | assert.equal(sf.held(), 'h1') |
| 333 | assert.equal(sf.held(), '', 'for the one load that follows') |
| 334 | // Focus elsewhere by the time the load comes: not taken back. |
| 335 | sf.after('block-move-down', 'h1') |
| 336 | s.image.focus() |
| 337 | assert.equal(sf.held(), 'i1', 'the box focus is on now') |
| 338 | // A box no longer the selection is not given focus. |
| 339 | ctx.document.activeElement = null |
| 340 | assert.equal(sf.restore('h1', 'i1'), false) |
| 341 | assert.equal(sf.restore('h1', 'h1'), true) |
| 342 | assert.equal(ctx.document.activeElement, s.heading) |
| 343 | // Commands that put focus somewhere else keep nothing. |
| 344 | sf.after('block-format', 'h1') |
| 345 | s.heading.focus() |
| 346 | assert.equal(sf.held(), '') |
| 347 | } finally { |
| 348 | ctx.restore() |
| 349 | } |
| 350 | }) |
| 351 | |