// fixtures/stage-focus.mjs — every box on the stage a tab stop, in the // slide's order (spec §4.4). // // The stage is the server's own HTML (stage.mjs) and the renderer is // the viewer's too, so nothing here is drawn: after each install the // editor adds what makes a box reachable. A box with text already has // its tab stop, its text editor (a table's are its cells, which Tab // walks before it leaves); focus there selects the box (format-bar.mjs's // onCaret). A box with none (an image, a shape, a divider, a spacer, a // columns box) is given one: tabindex="0", role="group" and its kind // as its name. Focus on it selects it, and the card shows its options. // // The slide's order is the stage's document order: the renderer emits // blocks in reading order (render.go's renderer, codex 10), and that is // the order Tab already walks the text in. // // On a canvas slide the selected box has the overlay (canvas.mjs), // which nudges and resizes it from the arrow keys, so focus on a box // moves on to the overlay. The overlay is not itself in the tab order; // Tab and Shift+Tab on it go to the next and the previous box, and past // the last box (or before the first) on to what the page has there. // // A load replaces the stage's DOM (stage.mjs), and focus with it. Focus // on a box itself (its stop, or the canvas overlay over it) is held // before and given back after (held(), restore(id, selected)), so a key // or a menu row that changes the box (box-menu.mjs) leaves the keyboard // on it. Focus in text is the text editor's, and is not held, but for // the load after the box's menu or keys moved that box (after(name, // id)): then it goes back to the box's text. After Delete, focus goes // on to the next box drawn, else the previous, else the slide itself, // and is held there across the load that takes the box away. // // mountStageFocus({stageEl, select, canvas, isText, labels}) → // {refresh(), focus(id), held(), restore(id, selected), after(name, // id), destroy()}. select(id) selects a box; canvas // is the overlay's mount ({focus()}); isText(type) says whether a kind // holds text; labels.types names each kind. const BOX = '[slides-block][data-id]' const EDITOR = '[contenteditable="true"]' // The attribute this module marks the boxes it made tab stops with, so // it never mistakes a text editor's own focus for one of them. const STOP = 'slides-box-stop' // What held() answers for focus on the slide itself. const SLIDE = '#slide' // The box's commands that leave it where it was in the slide's boxes. const MOVES = ['block-move-up', 'block-move-down', 'block-forward', 'block-back', 'region'] /** The boxes on the stage, in the slide's order. */ export function boxesOf (stageEl) { return [...stageEl.querySelectorAll(BOX)] } /** Whether a box is drawn in the view the stage is in (where there is no way to ask, it is). */ export function drawn (box) { return typeof box.checkVisibility !== 'function' || box.checkVisibility() } /** A box's own text editors (not those of a box inside it): a table's cells, or the one. */ function editorsOf (box) { if (box.matches(EDITOR)) return [box] return [...box.querySelectorAll(EDITOR)].filter(e => e.closest(BOX) === box) } /** Where focus goes for a box: its text (a table's first cell), or the box itself. */ export function tabStop (box) { return editorsOf(box)[0] || box } /** Where focus goes for a box reached backwards: its last cell, its text, or the box itself. */ export function lastStop (box) { return editorsOf(box).at(-1) || box } export function mountStageFocus ({ stageEl, select, canvas = null, isText = () => false, labels = { types: {} } }) { const abort = new AbortController() const { signal } = abort const onCanvas = () => !!stageEl.querySelector('[slides-slide][data-layout="canvas"]') const byId = id => boxesOf(stageEl).find(b => b.getAttribute('data-id') === id) || null function refresh () { for (const box of boxesOf(stageEl)) { const type = box.getAttribute('data-block') || '' if (isText(type) && editorsOf(box).length) continue box.setAttribute(STOP, '') box.setAttribute('tabindex', '0') box.setAttribute('role', 'group') box.setAttribute('aria-label', labels.types[type] || type) // A spacer, and a shape with no label, are hidden from a screen // reader on the viewer; here each is a box a person can reach, // and what has focus is never hidden. if (box.getAttribute('aria-hidden') === 'true') box.removeAttribute('aria-hidden') } } // The box itself takes focus: on a canvas slide, the overlay that // moves it; elsewhere, its tab stop. function focus (id) { const box = byId(id) if (!box) return false if (canvas && onCanvas()) { select(id) canvas.focus() } else { tabStop(box).focus() } return true } // The slide itself, for a moment: focus there until it moves on. function focusSlide () { const section = stageEl.querySelector('[slides-slide]') if (!section) return false section.setAttribute('tabindex', '-1') section.addEventListener('blur', () => section.removeAttribute('tabindex'), { once: true }) section.focus({ preventScroll: true }) return true } // What the next load gives focus back to, past what held() holds of // its own (after()); taken by that load. let kept = '' function after (name, id) { kept = '' if (name === 'block-delete') { const gone = byId(id) const boxes = boxesOf(stageEl).filter(b => b === gone || (drawn(b) && !(gone && gone.contains(b)))) const at = boxes.indexOf(gone) const next = at < 0 ? null : boxes[at + 1] || boxes[at - 1] if (next) focus(next.getAttribute('data-id')) else focusSlide() kept = next ? next.getAttribute('data-id') : SLIDE } else if (MOVES.includes(name)) kept = id } // The box focus is on itself, by id, SLIDE, or ''. function held () { const k = kept kept = '' const a = globalThis.document ? globalThis.document.activeElement : null if (!a || typeof a.hasAttribute !== 'function' || !stageEl.contains(a)) return '' if (k === SLIDE && a.matches('[slides-slide]')) return SLIDE if (a.hasAttribute(STOP)) return a.getAttribute('data-id') || '' const box = a.matches('[slides-canvas-overlay]') ? boxesOf(stageEl).find(b => b.getAttribute('data-selected') === 'true') : null if (box) return box.getAttribute('data-id') const moved = k && k !== SLIDE ? byId(k) : null return moved && moved.contains(a) ? k : '' } // Focus back on box id while it is the selection (or on the slide), // unless focus has gone somewhere since. function restore (id, selected = id) { const doc = globalThis.document const a = doc ? doc.activeElement : null if (!id || (a && a !== doc.body && a !== doc.documentElement)) return false if (id === SLIDE) return focusSlide() return id === selected && focus(id) } stageEl.addEventListener('focusin', e => { const box = e.target if (!box || typeof box.hasAttribute !== 'function' || !box.hasAttribute(STOP)) return select(box.getAttribute('data-id')) if (canvas && onCanvas()) canvas.focus() }, { signal }) stageEl.addEventListener('keydown', e => { if (e.key !== 'Tab' || e.altKey || e.ctrlKey || e.metaKey || e.defaultPrevented) return const overlay = e.target if (!overlay || typeof overlay.matches !== 'function' || !overlay.matches('[slides-canvas-overlay]')) return // The stops are the boxes drawn in this view (a box hidden on a // phone is not, and takes no focus: the Outline reaches it), and the // selected one, wherever it is, to go on from. const boxes = boxesOf(stageEl).filter(b => b.getAttribute('data-selected') === 'true' || drawn(b)) const at = boxes.findIndex(b => b.getAttribute('data-selected') === 'true') if (at < 0) return const next = boxes[at + (e.shiftKey ? -1 : 1)] if (next) { e.preventDefault() ;(e.shiftKey ? lastStop(next) : tabStop(next)).focus() return } // Past the last box the browser's own step from the overlay, which // comes after the slide, is right. Before the first, it would land on // the last box: the step is taken from the slide's section instead, // focused for that moment only. if (e.shiftKey) focusSlide() }, { signal }) return { refresh, focus, held, restore, after, destroy () { abort.abort() } } }