// stage-focus.mjs: every box a tab stop (spec ยง4.4), against the DOM // stub. A box with no text is given one after each install; focus on it // selects it; on a canvas slide the overlay takes over, and Tab on the // overlay walks the boxes in the slide's order. import test from 'node:test' import assert from 'node:assert/strict' import { el, withDocument } from './dom-stub.mjs' import { mountStageFocus, tabStop, lastStop, boxesOf } from './stage-focus.mjs' const TEXT = ['heading', 'text', 'list', 'quote', 'code', 'table'] const labels = { types: { heading: 'Heading', image: 'Image', shape: 'Shape', table: 'Table', spacer: 'Spacer', columns: 'Columns', text: 'Text' } } // A slide of a heading (its text editor is the box), an image, a // columns box holding a text box, a shape, a table whose cells are its // editors, and a spacer. function stage (layout = 'flow') { const box = (type, id, attrs = {}, children = []) => el('div', { 'slides-block': '', 'data-block': type, 'data-id': id, ...attrs }, children) const heading = box('heading', 'h1', { contenteditable: 'true' }) const image = box('image', 'i1') const inner = box('text', 'x1', { contenteditable: 'true' }) const columns = box('columns', 'c1', {}, [inner]) const shape = box('shape', 's1') const spacer = box('spacer', 'p1', { 'aria-hidden': 'true' }) const cell = () => el('td', { contenteditable: 'true' }) const cells = [cell(), cell()] const table = box('table', 't1', {}, [el('table', {}, cells)]) const section = el('section', { 'slides-slide': '', 'data-layout': layout }, [heading, image, columns, shape, table, spacer]) const overlay = el('div', { 'slides-canvas-overlay': '' }) const stageEl = el('div', { 'slides-stage': '' }, [section, overlay]) return { stageEl, section, overlay, heading, image, columns, inner, shape, spacer, table, cells } } function mount (s, { canvas = null } = {}) { const selected = [] const sf = mountStageFocus({ stageEl: s.stageEl, select: id => { selected.push(id) for (const b of boxesOf(s.stageEl)) { if (b.getAttribute('data-id') === id) b.setAttribute('data-selected', 'true') else b.removeAttribute('data-selected') } }, canvas, isText: t => TEXT.includes(t), labels }) return { sf, selected } } test('refresh makes each box with no text a named group in the tab order, and leaves text to its editor', () => { const ctx = withDocument() try { const s = stage() const { sf } = mount(s) sf.refresh() sf.refresh() // again, after the next install: the same for (const [box, name] of [[s.image, 'Image'], [s.columns, 'Columns'], [s.shape, 'Shape'], [s.spacer, 'Spacer']]) { assert.equal(box.getAttribute('tabindex'), '0', name) assert.equal(box.getAttribute('role'), 'group', name) assert.equal(box.getAttribute('aria-label'), name) } assert.equal(s.spacer.getAttribute('aria-hidden'), null, 'what can take focus is not hidden') for (const box of [s.heading, s.inner, s.table]) { assert.equal(box.getAttribute('tabindex'), null, box.getAttribute('data-block')) assert.equal(box.getAttribute('role'), null) } // A box's stop: its own editor, never one of a box inside it. assert.equal(tabStop(s.heading), s.heading) assert.equal(tabStop(s.table), s.cells[0]) assert.equal(lastStop(s.table), s.cells[1], 'reached backwards, a table\'s last cell') assert.equal(tabStop(s.columns), s.columns) assert.equal(tabStop(s.image), s.image) } finally { ctx.restore() } }) test('focus on a box with no text selects it; focus in text is the text editor\'s to say', () => { const ctx = withDocument() try { const s = stage() const { sf, selected } = mount(s) sf.refresh() s.shape.focus() assert.deepEqual(selected, ['s1']) s.heading.focus() s.cells[1].focus() assert.deepEqual(selected, ['s1'], 'a text editor taking focus selects through the format bar, not here') } finally { ctx.restore() } }) test('on a canvas slide focus on a box goes on to the overlay', () => { const ctx = withDocument() try { const s = stage('canvas') let overlayFocus = 0 const { sf, selected } = mount(s, { canvas: { focus: () => { overlayFocus++; s.overlay.focus() } } }) sf.refresh() s.image.focus() assert.deepEqual(selected, ['i1']) assert.equal(overlayFocus, 1) assert.equal(ctx.document.activeElement, s.overlay) } finally { ctx.restore() } }) test('Tab and Shift+Tab on the overlay walk the boxes in the slide\'s order', () => { const ctx = withDocument() try { const s = stage('canvas') const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) sf.refresh() const tab = (shiftKey = false) => s.overlay.dispatchEvent({ type: 'keydown', key: 'Tab', shiftKey }) s.image.focus() assert.equal(tab(), false, 'Tab is taken') assert.equal(ctx.document.activeElement, s.overlay, 'the columns box, then its overlay') assert.equal(selected.at(-1), 'c1') // The columns box: next is the text inside it, its own editor. assert.equal(tab(), false) assert.equal(ctx.document.activeElement, s.inner) // Shift+Tab from the shape's overlay: the text in the columns box. s.shape.focus() assert.equal(selected.at(-1), 's1') assert.equal(tab(true), false) assert.equal(ctx.document.activeElement, s.inner) // Shift+Tab from the spacer's overlay: the table's last cell, so // Shift+Tab walks its cells back. s.spacer.focus() assert.equal(selected.at(-1), 'p1') assert.equal(tab(true), false) assert.equal(ctx.document.activeElement, s.cells[1]) // The last box: Tab is the browser's, on past the slide. s.spacer.setAttribute('data-selected', 'true') assert.equal(tab(), true, 'Tab past the last box is not taken') // Before the first box: the browser's step, from the section. s.spacer.removeAttribute('data-selected') s.heading.setAttribute('data-selected', 'true') ctx.document.activeElement = s.overlay assert.equal(tab(true), true, 'Shift+Tab before the first box is not taken') assert.equal(ctx.document.activeElement, s.section) assert.equal(s.section.getAttribute('tabindex'), '-1') s.section.dispatchEvent({ type: 'blur' }) assert.equal(s.section.getAttribute('tabindex'), null, 'and only for that step') // Other keys are the overlay's own (arrows nudge, canvas.mjs). assert.equal(s.overlay.dispatchEvent({ type: 'keydown', key: 'ArrowRight' }), true) } finally { ctx.restore() } }) // A box hidden on a phone is not drawn in Phone view, and what is not // drawn takes no focus: Tab goes past it to the next box drawn, and the // Outline is how it is reached. test('Tab and Shift+Tab on the overlay pass a box that is not drawn', () => { const ctx = withDocument() try { const s = stage('canvas') const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) sf.refresh() s.columns.checkVisibility = () => false s.inner.checkVisibility = () => false s.shape.checkVisibility = () => true const tab = (shiftKey = false) => s.overlay.dispatchEvent({ type: 'keydown', key: 'Tab', shiftKey }) s.image.focus() assert.equal(tab(), false) assert.equal(ctx.document.activeElement, s.overlay) assert.equal(selected.at(-1), 's1', 'past the columns box and its text, to the shape') assert.equal(tab(true), false) assert.equal(selected.at(-1), 'i1', 'and back to the image') // Selected from the Outline while not drawn: Tab still goes on from it. s.image.removeAttribute('data-selected') s.columns.setAttribute('data-selected', 'true') ctx.document.activeElement = s.overlay assert.equal(tab(), false) assert.equal(selected.at(-1), 's1') } finally { ctx.restore() } }) test('focus(id) puts focus on the box: its text, the box itself, or on a canvas slide the overlay', () => { const ctx = withDocument() try { const s = stage() const { sf, selected } = mount(s) sf.refresh() assert.equal(sf.focus('h1'), true) assert.equal(ctx.document.activeElement, s.heading) sf.focus('i1') assert.equal(ctx.document.activeElement, s.image) assert.deepEqual(selected, ['i1']) assert.equal(sf.focus('gone'), false) const c = stage('canvas') const m = mount(c, { canvas: { focus: () => c.overlay.focus() } }) m.sf.refresh() m.sf.focus('h1') assert.equal(ctx.document.activeElement, c.overlay, 'a text box on a canvas slide too: its overlay moves it') assert.deepEqual(m.selected, ['h1']) } finally { ctx.restore() } }) test('destroy takes every listener off the stage', () => { const s = stage() const { sf } = mount(s) assert.equal(s.stageEl.countListeners('focusin'), 1) assert.equal(s.stageEl.countListeners('keydown'), 1) sf.destroy() assert.equal(s.stageEl.countListeners('focusin'), 0) assert.equal(s.stageEl.countListeners('keydown'), 0) }) // A load replaces the stage's DOM, and focus on a box with it: what was // on a box itself (its stop, or the canvas overlay) before is put back // on the same box after, so a key on the box (send back, Delete's // neighbours) leaves the keyboard where it was. Text is not held, and // nothing is taken from somewhere focus has gone since. test('held and restore keep focus on a box across a load', () => { const ctx = withDocument() try { const s = stage() const { sf } = mount(s) sf.refresh() s.shape.focus() assert.equal(sf.held(), 's1') // The install: a new section, nothing focused. const next = stage() s.stageEl.children.splice(0, s.stageEl.children.length) s.stageEl.append(...next.stageEl.children) ctx.document.activeElement = null sf.refresh() assert.equal(sf.restore('s1'), true) assert.equal(ctx.document.activeElement, next.shape) // Text is not a box's own focus; and focus that went elsewhere stays. next.heading.focus() assert.equal(sf.held(), '') const card = el('input') ctx.document.append(card) card.focus() assert.equal(sf.restore('s1'), false) assert.equal(ctx.document.activeElement, card) } finally { ctx.restore() } }) test('held on a canvas slide is the selected box, from its overlay', () => { const ctx = withDocument() try { const s = stage('canvas') const canvas = { focus: () => s.overlay.focus() } const { sf } = mount(s, { canvas }) sf.refresh() s.shape.setAttribute('data-selected', 'true') s.overlay.focus() assert.equal(sf.held(), 's1') } finally { ctx.restore() } }) // After a command from the box's menu or its keys (the review's A7): // Delete puts focus on the next box drawn, else the previous, else the // slide; a move keeps focus on the box, its text included, across the // load that follows (held, then restore). test('after Delete, focus goes to the next box, else the previous, else the slide', () => { const ctx = withDocument() try { const s = stage('canvas') const { sf, selected } = mount(s, { canvas: { focus: () => s.overlay.focus() } }) sf.refresh() // The columns box: next is the shape (not the text inside the box going). sf.after('block-delete', 'c1') assert.equal(selected.at(-1), 's1') assert.equal(ctx.document.activeElement, s.overlay) assert.equal(sf.held(), 's1', 'held for the load that takes the box away') // The last box: the one before it. sf.after('block-delete', 'p1') assert.equal(selected.at(-1), 't1') // Not drawn in this view: passed over. s.table.checkVisibility = () => false sf.after('block-delete', 'p1') assert.equal(selected.at(-1), 's1') } finally { ctx.restore() } const ctx2 = withDocument() try { const box = el('div', { 'slides-block': '', 'data-block': 'shape', 'data-id': 'only' }) const section = el('section', { 'slides-slide': '', 'data-layout': 'flow' }, [box]) const stageEl = el('div', { 'slides-stage': '' }, [section]) const sf = mountStageFocus({ stageEl, select: () => {}, labels }) sf.refresh() sf.after('block-delete', 'only') assert.equal(ctx2.document.activeElement, section, 'nothing else on the slide: the slide itself') const slide = sf.held() assert.ok(slide, 'held across the load') // The load: a new section, focus gone with the old one. const next = el('section', { 'slides-slide': '', 'data-layout': 'flow' }) stageEl.children.splice(0, stageEl.children.length) stageEl.append(next) ctx2.document.activeElement = null assert.equal(sf.restore(slide, ''), true) assert.equal(ctx2.document.activeElement, next) } finally { ctx2.restore() } }) test('after a move, focus in the box\'s text is held across the load too', () => { const ctx = withDocument() try { const s = stage() const { sf } = mount(s) sf.refresh() s.heading.focus() assert.equal(sf.held(), '', 'text is the text editor\'s, ordinarily') sf.after('block-move-down', 'h1') assert.equal(sf.held(), 'h1') assert.equal(sf.held(), '', 'for the one load that follows') // Focus elsewhere by the time the load comes: not taken back. sf.after('block-move-down', 'h1') s.image.focus() assert.equal(sf.held(), 'i1', 'the box focus is on now') // A box no longer the selection is not given focus. ctx.document.activeElement = null assert.equal(sf.restore('h1', 'i1'), false) assert.equal(sf.restore('h1', 'h1'), true) assert.equal(ctx.document.activeElement, s.heading) // Commands that put focus somewhere else keep nothing. sf.after('block-format', 'h1') s.heading.focus() assert.equal(sf.held(), '') } finally { ctx.restore() } })