oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/editor-inert.mjs — the editor while a change waits for its
2// font (editor-fonts.mjs): the stage, the strip, the inspector and the
3// menus go inert, like a short modal. Kept apart so editor-fonts.mjs
4// stays the gate itself.
5//
6// While the editor is inert, focus is on the page itself, and there a
7// key that would have edited is the browser's: Backspace goes back in
8// history in WebKit (Docs' review 8, which lost the waiting paste),
9// Space and the arrows scroll. Those keys do nothing during the wait.
10// A real control outside the inert parts keeps them, and so does Tab.
11// Leaving the page while a change waits asks first.
12
13const HELD_KEYS = new Set(['Backspace', ' ', 'Spacebar', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageUp', 'PageDown', 'Home', 'End'])
14const holdKeys = e => {
15 if (!HELD_KEYS.has(e.key)) return
16 const t = e.target
17 if (!t || t === document.body || t === document.documentElement || (t.closest && t.closest('[inert]'))) e.preventDefault()
18}
19const holdUnload = e => {
20 e.preventDefault()
21 e.returnValue = ''
22}
23
24/** setInert(on) for the editor under root; onInert() runs as it goes inert. */
25export function createInert (root, onInert) {
26 const inertParts = () => [
27 root.querySelector('[slides-stage-pane]'),
28 root.querySelector('[slides-strip-nav]'),
29 root.querySelector('[slides-inspector]'),
30 root.querySelector('[slides-deck-dialog]'),
31 root.querySelector('[slides-menubar]'),
32 root.querySelector('[slides-history]'),
33 root.querySelector('[slides-head-title]')
34 ].filter(Boolean)
35 let focusBack = null
36 let inertOn = false
37 return function setInert (on) {
38 if (on === inertOn) return
39 inertOn = on
40 const listen = on ? 'addEventListener' : 'removeEventListener'
41 globalThis[listen]('keydown', holdKeys)
42 globalThis[listen]('beforeunload', holdUnload)
43 const parts = inertParts()
44 if (on) {
45 const active = document.activeElement
46 focusBack = active && parts.some(el => el.contains(active)) ? active : null
47 }
48 for (const el of parts) el.inert = on
49 const pane = root.querySelector('[slides-stage-pane]')
50 if (pane) pane.setAttribute('aria-busy', on ? 'true' : 'false')
51 if (on) onInert()
52 else if (focusBack && focusBack.isConnected && (!document.activeElement || document.activeElement === document.body)) focusBack.focus()
53 }
54}
55