| 1 | // fixtures/stage-focus.mjs — every box on the stage a tab stop, in the |
| 2 | // slide's order (spec §4.4). |
| 3 | // |
| 4 | // The stage is the server's own HTML (stage.mjs) and the renderer is |
| 5 | // the viewer's too, so nothing here is drawn: after each install the |
| 6 | // editor adds what makes a box reachable. A box with text already has |
| 7 | // its tab stop, its text editor (a table's are its cells, which Tab |
| 8 | // walks before it leaves); focus there selects the box (format-bar.mjs's |
| 9 | // onCaret). A box with none (an image, a shape, a divider, a spacer, a |
| 10 | // columns box) is given one: tabindex="0", role="group" and its kind |
| 11 | // as its name. Focus on it selects it, and the card shows its options. |
| 12 | // |
| 13 | // The slide's order is the stage's document order: the renderer emits |
| 14 | // blocks in reading order (render.go's renderer, codex 10), and that is |
| 15 | // the order Tab already walks the text in. |
| 16 | // |
| 17 | // On a canvas slide the selected box has the overlay (canvas.mjs), |
| 18 | // which nudges and resizes it from the arrow keys, so focus on a box |
| 19 | // moves on to the overlay. The overlay is not itself in the tab order; |
| 20 | // Tab and Shift+Tab on it go to the next and the previous box, and past |
| 21 | // the last box (or before the first) on to what the page has there. |
| 22 | // |
| 23 | // A load replaces the stage's DOM (stage.mjs), and focus with it. Focus |
| 24 | // on a box itself (its stop, or the canvas overlay over it) is held |
| 25 | // before and given back after (held(), restore(id, selected)), so a key |
| 26 | // or a menu row that changes the box (box-menu.mjs) leaves the keyboard |
| 27 | // on it. Focus in text is the text editor's, and is not held, but for |
| 28 | // the load after the box's menu or keys moved that box (after(name, |
| 29 | // id)): then it goes back to the box's text. After Delete, focus goes |
| 30 | // on to the next box drawn, else the previous, else the slide itself, |
| 31 | // and is held there across the load that takes the box away. |
| 32 | // |
| 33 | // mountStageFocus({stageEl, select, canvas, isText, labels}) → |
| 34 | // {refresh(), focus(id), held(), restore(id, selected), after(name, |
| 35 | // id), destroy()}. select(id) selects a box; canvas |
| 36 | // is the overlay's mount ({focus()}); isText(type) says whether a kind |
| 37 | // holds text; labels.types names each kind. |
| 38 | |
| 39 | const BOX = '[slides-block][data-id]' |
| 40 | const EDITOR = '[contenteditable="true"]' |
| 41 | // The attribute this module marks the boxes it made tab stops with, so |
| 42 | // it never mistakes a text editor's own focus for one of them. |
| 43 | const STOP = 'slides-box-stop' |
| 44 | // What held() answers for focus on the slide itself. |
| 45 | const SLIDE = '#slide' |
| 46 | // The box's commands that leave it where it was in the slide's boxes. |
| 47 | const MOVES = ['block-move-up', 'block-move-down', 'block-forward', 'block-back', 'region'] |
| 48 | |
| 49 | /** The boxes on the stage, in the slide's order. */ |
| 50 | export function boxesOf (stageEl) { |
| 51 | return [...stageEl.querySelectorAll(BOX)] |
| 52 | } |
| 53 | |
| 54 | /** Whether a box is drawn in the view the stage is in (where there is no way to ask, it is). */ |
| 55 | export function drawn (box) { |
| 56 | return typeof box.checkVisibility !== 'function' || box.checkVisibility() |
| 57 | } |
| 58 | |
| 59 | /** A box's own text editors (not those of a box inside it): a table's cells, or the one. */ |
| 60 | function editorsOf (box) { |
| 61 | if (box.matches(EDITOR)) return [box] |
| 62 | return [...box.querySelectorAll(EDITOR)].filter(e => e.closest(BOX) === box) |
| 63 | } |
| 64 | |
| 65 | /** Where focus goes for a box: its text (a table's first cell), or the box itself. */ |
| 66 | export function tabStop (box) { |
| 67 | return editorsOf(box)[0] || box |
| 68 | } |
| 69 | |
| 70 | /** Where focus goes for a box reached backwards: its last cell, its text, or the box itself. */ |
| 71 | export function lastStop (box) { |
| 72 | return editorsOf(box).at(-1) || box |
| 73 | } |
| 74 | |
| 75 | export function mountStageFocus ({ stageEl, select, canvas = null, isText = () => false, labels = { types: {} } }) { |
| 76 | const abort = new AbortController() |
| 77 | const { signal } = abort |
| 78 | const onCanvas = () => !!stageEl.querySelector('[slides-slide][data-layout="canvas"]') |
| 79 | const byId = id => boxesOf(stageEl).find(b => b.getAttribute('data-id') === id) || null |
| 80 | |
| 81 | function refresh () { |
| 82 | for (const box of boxesOf(stageEl)) { |
| 83 | const type = box.getAttribute('data-block') || '' |
| 84 | if (isText(type) && editorsOf(box).length) continue |
| 85 | box.setAttribute(STOP, '') |
| 86 | box.setAttribute('tabindex', '0') |
| 87 | box.setAttribute('role', 'group') |
| 88 | box.setAttribute('aria-label', labels.types[type] || type) |
| 89 | // A spacer, and a shape with no label, are hidden from a screen |
| 90 | // reader on the viewer; here each is a box a person can reach, |
| 91 | // and what has focus is never hidden. |
| 92 | if (box.getAttribute('aria-hidden') === 'true') box.removeAttribute('aria-hidden') |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // The box itself takes focus: on a canvas slide, the overlay that |
| 97 | // moves it; elsewhere, its tab stop. |
| 98 | function focus (id) { |
| 99 | const box = byId(id) |
| 100 | if (!box) return false |
| 101 | if (canvas && onCanvas()) { |
| 102 | select(id) |
| 103 | canvas.focus() |
| 104 | } else { |
| 105 | tabStop(box).focus() |
| 106 | } |
| 107 | return true |
| 108 | } |
| 109 | |
| 110 | // The slide itself, for a moment: focus there until it moves on. |
| 111 | function focusSlide () { |
| 112 | const section = stageEl.querySelector('[slides-slide]') |
| 113 | if (!section) return false |
| 114 | section.setAttribute('tabindex', '-1') |
| 115 | section.addEventListener('blur', () => section.removeAttribute('tabindex'), { once: true }) |
| 116 | section.focus({ preventScroll: true }) |
| 117 | return true |
| 118 | } |
| 119 | |
| 120 | // What the next load gives focus back to, past what held() holds of |
| 121 | // its own (after()); taken by that load. |
| 122 | let kept = '' |
| 123 | function after (name, id) { |
| 124 | kept = '' |
| 125 | if (name === 'block-delete') { |
| 126 | const gone = byId(id) |
| 127 | const boxes = boxesOf(stageEl).filter(b => b === gone || (drawn(b) && !(gone && gone.contains(b)))) |
| 128 | const at = boxes.indexOf(gone) |
| 129 | const next = at < 0 ? null : boxes[at + 1] || boxes[at - 1] |
| 130 | if (next) focus(next.getAttribute('data-id')) |
| 131 | else focusSlide() |
| 132 | kept = next ? next.getAttribute('data-id') : SLIDE |
| 133 | } else if (MOVES.includes(name)) kept = id |
| 134 | } |
| 135 | |
| 136 | // The box focus is on itself, by id, SLIDE, or ''. |
| 137 | function held () { |
| 138 | const k = kept |
| 139 | kept = '' |
| 140 | const a = globalThis.document ? globalThis.document.activeElement : null |
| 141 | if (!a || typeof a.hasAttribute !== 'function' || !stageEl.contains(a)) return '' |
| 142 | if (k === SLIDE && a.matches('[slides-slide]')) return SLIDE |
| 143 | if (a.hasAttribute(STOP)) return a.getAttribute('data-id') || '' |
| 144 | const box = a.matches('[slides-canvas-overlay]') ? boxesOf(stageEl).find(b => b.getAttribute('data-selected') === 'true') : null |
| 145 | if (box) return box.getAttribute('data-id') |
| 146 | const moved = k && k !== SLIDE ? byId(k) : null |
| 147 | return moved && moved.contains(a) ? k : '' |
| 148 | } |
| 149 | |
| 150 | // Focus back on box id while it is the selection (or on the slide), |
| 151 | // unless focus has gone somewhere since. |
| 152 | function restore (id, selected = id) { |
| 153 | const doc = globalThis.document |
| 154 | const a = doc ? doc.activeElement : null |
| 155 | if (!id || (a && a !== doc.body && a !== doc.documentElement)) return false |
| 156 | if (id === SLIDE) return focusSlide() |
| 157 | return id === selected && focus(id) |
| 158 | } |
| 159 | |
| 160 | stageEl.addEventListener('focusin', e => { |
| 161 | const box = e.target |
| 162 | if (!box || typeof box.hasAttribute !== 'function' || !box.hasAttribute(STOP)) return |
| 163 | select(box.getAttribute('data-id')) |
| 164 | if (canvas && onCanvas()) canvas.focus() |
| 165 | }, { signal }) |
| 166 | |
| 167 | stageEl.addEventListener('keydown', e => { |
| 168 | if (e.key !== 'Tab' || e.altKey || e.ctrlKey || e.metaKey || e.defaultPrevented) return |
| 169 | const overlay = e.target |
| 170 | if (!overlay || typeof overlay.matches !== 'function' || !overlay.matches('[slides-canvas-overlay]')) return |
| 171 | // The stops are the boxes drawn in this view (a box hidden on a |
| 172 | // phone is not, and takes no focus: the Outline reaches it), and the |
| 173 | // selected one, wherever it is, to go on from. |
| 174 | const boxes = boxesOf(stageEl).filter(b => b.getAttribute('data-selected') === 'true' || drawn(b)) |
| 175 | const at = boxes.findIndex(b => b.getAttribute('data-selected') === 'true') |
| 176 | if (at < 0) return |
| 177 | const next = boxes[at + (e.shiftKey ? -1 : 1)] |
| 178 | if (next) { |
| 179 | e.preventDefault() |
| 180 | ;(e.shiftKey ? lastStop(next) : tabStop(next)).focus() |
| 181 | return |
| 182 | } |
| 183 | // Past the last box the browser's own step from the overlay, which |
| 184 | // comes after the slide, is right. Before the first, it would land on |
| 185 | // the last box: the step is taken from the slide's section instead, |
| 186 | // focused for that moment only. |
| 187 | if (e.shiftKey) focusSlide() |
| 188 | }, { signal }) |
| 189 | |
| 190 | return { refresh, focus, held, restore, after, destroy () { abort.abort() } } |
| 191 | } |
| 192 | |