oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/toolbar-roving.mjs — the formatting bar's keyboard
2// navigation, split out of toolbar.mjs to keep both under spec §12's
3// 300-line-per-module cap.
4//
5// It is a genuinely separate concern: toolbar.mjs decides what each
6// control DOES to the document, and this decides which control the
7// keyboard is on. Nothing here knows about ProseMirror.
8
9
10// setupRoving makes the whole bar ONE tab stop, with the arrow keys
11// moving between the controls inside it — the WAI-ARIA toolbar
12// pattern, which `role="toolbar"` in views/show.html is a promise to
13// implement.
14//
15// Without it the bar is fourteen tab stops between the shell bar and
16// the first word of the document, on every page load, for someone who
17// only wanted to type. It is also what a word processor does, so it is
18// what people's hands already expect. (Raised by the caret-placement
19// session, who was right that the fix is this and not reordering the
20// DOM: the bar sits above the paper visually, so DOM order already
21// matches reading order, and pulling focus order away from visual
22// order to save a few tab presses would trade a small annoyance for a
23// WCAG 2.4.3 Focus Order violation.)
24//
25// Left/Right rather than all four arrows, and preventDefault on the
26// ones handled: a closed native <select> changes its own value on
27// Left/Right, so the toolbar has to consume those; Up/Down are left
28// alone, which is how the three selects stay operable from the
29// keyboard without opening.
30//
31// Nothing here touches focus *visibility*. The controls keep
32// rastrillo's own focus ring — a roving tabindex decides which control
33// Tab reaches, not whether a person can see which one they are on, and
34// show.html's ring removal is scoped to [docs-editor] alone precisely
35// so this stays true.
36export function setupRoving(root, controls) {
37 if (!root || controls.length === 0) return { ensureReachable() {}, focusStop() { return false } }
38
39 // Reachable is enabled AND drawn. From 64rem the bar is the
40 // inspector, which the person can close, and a closed inspector's
41 // controls are display: none but still in this list: a tab stop
42 // parked on one, or Escape focusing one, sends focus nowhere (spec
43 // I2, revision 2.1's C2). checkVisibility where the engine has it;
44 // otherwise a control with no boxes is not drawn.
45 const drawn = (c) => {
46 if (typeof c.checkVisibility === 'function') return c.checkVisibility()
47 if (typeof c.getClientRects === 'function') return c.getClientRects().length > 0
48 return true // no layout to ask (the node tests' DOM)
49 }
50 const reachable = () => controls.filter((c) => !c.disabled && drawn(c))
51
52 function setStop(target) {
53 for (const c of controls) c.tabIndex = c === target ? 0 : -1
54 }
55
56 // ensureReachable is called after every sync, because sync is what
57 // enables and disables buttons: undo and redo start disabled, and a
58 // disabled button cannot take focus at all — so a tab stop parked on
59 // one makes the entire bar unreachable by keyboard rather than
60 // merely awkward. That failure is invisible to every other check
61 // here, which is why fixtures/browser-drive.mjs tabs in from outside
62 // and asserts it can get through.
63 function ensureReachable() {
64 const list = reachable()
65 if (list.length === 0) return
66 if (list.some((c) => c.tabIndex === 0)) return
67 setStop(list[0])
68 }
69
70 root.addEventListener('keydown', (event) => {
71 const key = event.key
72 if (key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End') return
73 const list = reachable()
74 const at = list.indexOf(document.activeElement)
75 if (at < 0 || list.length === 0) return
76
77 let next
78 if (key === 'Home') next = list[0]
79 else if (key === 'End') next = list[list.length - 1]
80 else if (key === 'ArrowRight') next = list[(at + 1) % list.length]
81 else next = list[(at - 1 + list.length) % list.length]
82
83 event.preventDefault()
84 setStop(next)
85 next.focus()
86 })
87
88 // A control reached by mouse becomes the tab stop, so Tab leaves
89 // from where the person last was rather than from wherever the
90 // keyboard happened to leave it.
91 root.addEventListener('focusin', (event) => {
92 if (controls.includes(event.target)) setStop(event.target)
93 })
94
95 ensureReachable()
96 // Showing or hiding the inspector, or crossing its breakpoint, changes
97 // what is drawn without enabling or disabling anything.
98 if (typeof ResizeObserver === 'function') new ResizeObserver(() => ensureReachable()).observe(root)
99 // focusStop moves focus onto the bar's current tab stop, and reports
100 // whether it managed to.
101 //
102 // It exists for Escape inside the editable surface. Tab is
103 // repurposed in there — it indents rather than moving focus — and
104 // although Shift+Tab still escapes backwards, there is no forward
105 // exit below indent 8. WCAG 2.1.2 permits a modified key provided
106 // the person is told, but "you can only leave backwards" is a poor
107 // thing to have to tell them, so Escape leaves forwards onto the
108 // nearest chrome instead.
109 // With nothing drawn (the inspector closed) it reports false, and
110 // Escape in the editor stays where it is (editor-entry.js).
111 function focusStop() {
112 const list = reachable()
113 if (list.length === 0) return false
114 const target = list.find((c) => c.tabIndex === 0) || list[0]
115 setStop(target)
116 target.focus()
117 return true
118 }
119
120 return { ensureReachable, focusStop }
121}
122