oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/head.mjs — the editor's header and its menus (editor.html's
2// [slides-head], I2 after Docs): the commands that are not already the
3// root's [data-command] buttons.
4//
5// Every row here drives a control that already exists and stays the
6// control of record, so there is one path for each change and nothing
7// to keep in step but the menus' own check marks:
8//
9// - a layout (data-layout-choice, the Insert menu and the strip's
10// picker) is commands.newSlide(layout);
11// - a block (data-insert-block) is commands.addBlock(kind), which
12// adds it after the selection and selects it: Insert is the one
13// place a box is added (spec §5); Scene (data-insert-scene) is a
14// scene block of one of the deck's scenes (scene-card.mjs), and
15// Clip (data-insert-clip) a clip whose video is picked at once
16// (clip-card.mjs);
17// - Desktop / Phone / Both (data-view-choice) check the switcher's
18// radio, Speaker notes opens or closes the notes card (notes-card.mjs,
19// the same as its × and its icon), and Inspector
20// (data-view-toggle="inspector") shows or hides the inspector card,
21// as the bar's Format button does, and Outline
22// (data-view-toggle="outline") opens the slide's boxes in a list
23// (outline.mjs) under the View menu's title;
24// - the header's title is the one place the deck's title is edited
25// (spec §8): a change is commitTitle(title), one set_meta;
26// - a menu's dialog (Help's shortcuts, File's Deck settings) opens
27// with showModal (data-open-dialog), and when it closes focus goes
28// back to that menu's title, as estilo's own dialog rows do;
29// - Delete or Backspace on a slide in the strip asks to delete it,
30// through the same confirm the menus use.
31//
32// mountHead({root, commands, notes, outline, storage, commitTitle}) → {setTitle(title),
33// slideShown(has), notesShown(open), showInspector(on, {focus}),
34// inspectorShown()}. notes is the notes card; its every open and close
35// is told to notesShown, which keeps the Speaker notes row's check.
36//
37// The inspector card (spec §4, Docs' Format): whether it shows is the
38// slides-inspector cookie, which the server reads and renders as the
39// card's data-state and the Format button's aria-expanded, so the first
40// paint is final. This module is the one owner of both after that
41// (estilo's chrome.js binds no data-estilo-toggle), and of the View
42// menu row's aria-pressed, and writes the cookie for the next page.
43// Where a cookie cannot be written the toggle still works, for this
44// page. The localStorage preference from before the cookie is applied
45// once, moved to the cookie and removed.
46
47import { readCookie, writeCookie } from './pref-cookie.mjs'
48
49export const INSPECTOR_KEY = 'slides.editor.inspector'
50export const INSPECTOR_COOKIE = 'slides-inspector'
51
52// A control the card can take focus into: the first one showing.
53const FOCUSABLE = 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), summary, [href], [tabindex]:not([tabindex="-1"])'
54
55/**
56 * Whether c is a radio that does not stand for its group: unchecked,
57 * with another of its name checked in root. Tab reaches a radio group
58 * at its checked radio, and so does focus put into the card, so that
59 * Space there chooses nothing new (the slide's layout is one).
60 */
61export function uncheckedOfGroup (c, root) {
62 if (c.type !== 'radio' || c.checked || !c.name) return false
63 return [...root.querySelectorAll('input[type="radio"]')].some(r => r.checked && r.name === c.name)
64}
65
66function writeInspectorCookie (open) {
67 writeCookie(INSPECTOR_COOKIE, open ? 'open' : 'closed')
68}
69
70export function mountHead ({ root, commands, notes = null, outline = null, storage, commitTitle = () => {} }) {
71 if (storage === undefined) {
72 try { storage = globalThis.localStorage } catch { storage = null }
73 }
74 const titleInput = root.querySelector('[slides-head-title]')
75 // The deck's title as the document has it: what Escape, or a title
76 // left empty or unchanged, puts back in the field.
77 let title = titleInput ? titleInput.value : ''
78 // The deck's title has changed. The field's default value follows it,
79 // because that is what estilo's bar (chrome.js) restores on Escape,
80 // before this module hears the key.
81 const rememberTitle = t => {
82 title = t
83 if (titleInput) titleInput.defaultValue = t
84 }
85 // Puts a title in the field. The input event is how estilo's bar
86 // (chrome.js) refits the field to what it now holds.
87 const showTitle = t => {
88 titleInput.value = t
89 titleInput.dispatchEvent?.(new Event('input', { bubbles: true }))
90 }
91 const switcher = root.querySelector('[slides-switcher]')
92 const inspectorEl = root.querySelector('[slides-inspector]')
93
94 const closeMenu = el => {
95 const menu = el.closest('details')
96 if (menu) menu.open = false
97 const pop = el.closest('[popover]')
98 if (pop && pop.matches(':popover-open')) pop.hidePopover()
99 }
100
101 // --- the inspector card: shown or hidden, remembered in a cookie ---
102 function inspectorShown () {
103 return !!inspectorEl && inspectorEl.getAttribute('data-state') !== 'closed'
104 }
105 function paintInspector (on) {
106 if (inspectorEl) inspectorEl.setAttribute('data-state', on ? 'open' : 'closed')
107 for (const b of root.querySelectorAll('[data-view-toggle="inspector"]')) {
108 b.setAttribute(b.hasAttribute('aria-expanded') ? 'aria-expanded' : 'aria-pressed', String(on))
109 }
110 }
111 // Where focus was before a keyboard action took it into the card:
112 // dismissing the card gives it back. An action from a menu passes the
113 // menu's own button (returnTarget), since the row it was on is hidden
114 // once the menu closes.
115 let focusBack = null
116 function firstControl () {
117 if (!inspectorEl) return null
118 for (const c of inspectorEl.querySelectorAll(FOCUSABLE)) {
119 if (c.closest?.('[hidden]')) continue
120 if (uncheckedOfGroup(c, inspectorEl)) continue
121 if (typeof c.checkVisibility === 'function' && !c.checkVisibility()) continue
122 return c
123 }
124 return null
125 }
126 // Something focus can actually go to: in the page, enabled, drawn.
127 function usable (t) {
128 if (!t || t.isConnected === false || t.disabled || typeof t.focus !== 'function') return false
129 return typeof t.checkVisibility !== 'function' || t.checkVisibility()
130 }
131 // Focus back to t, or, where t cannot take it, to Format: never a
132 // restore that silently leaves focus nowhere.
133 function giveFocusBack (t) {
134 const format = root.querySelector('[data-view-toggle="inspector"][aria-expanded]')
135 for (const c of [t, format]) {
136 if (!usable(c)) continue
137 c.focus()
138 if (globalThis.document?.activeElement === c) return
139 }
140 }
141 function showInspector (on, { focus = false, returnTo = null } = {}) {
142 on = !!on
143 const active = globalThis.document?.activeElement || null
144 const focusInCard = !!(active && inspectorEl && inspectorEl.contains(active))
145 paintInspector(on)
146 writeInspectorCookie(on)
147 if (on && focus) {
148 if (!focusInCard) focusBack = returnTo || active
149 firstControl()?.focus()
150 } else if (!on) {
151 const back = focusBack
152 focusBack = null
153 const lost = focusInCard || !usable(active) || active === globalThis.document?.body || active.matches?.('[data-view-toggle="inspector"]')
154 if (back && lost) giveFocusBack(back)
155 }
156 }
157 // The control to give focus back to for an action on el: the button
158 // of the menu el is in (a <details>' summary), or el itself.
159 function returnTarget (el) {
160 const menu = el.closest('details')
161 return (menu && menu.querySelector(':scope > summary')) || el
162 }
163 // The first paint is the server's; the View row and the button follow
164 // the card. A preference from before the cookie, where there is no
165 // cookie yet, is applied once and moved.
166 paintInspector(inspectorShown())
167 let old = null
168 try { old = storage?.getItem(INSPECTOR_KEY) ?? null } catch { /* unreadable */ }
169 if (old !== null) {
170 if (readCookie(INSPECTOR_COOKIE) === null && (old === 'on' || old === 'off')) showInspector(old === 'on')
171 try { storage.removeItem(INSPECTOR_KEY) } catch { /* this visit only */ }
172 }
173
174 // Deleting a slide asks in the card, at the top of the slide's
175 // options, so a hidden card comes back first, with focus (capture:
176 // before the root's command handler moves focus to the question).
177 // Cancel gives focus back to what asked — the menu's button, or the
178 // slide in the strip — and leaves the card showing.
179 let askedFrom = null
180 root.addEventListener('click', e => {
181 const ask = e.target.closest('[data-command="slide-delete-ask"], [data-slide-command="delete"]')
182 if (ask) {
183 askedFrom = returnTarget(ask)
184 showInspector(true, { focus: true, returnTo: askedFrom })
185 return
186 }
187 const answer = e.target.closest('[data-command="slide-delete-cancel"], [data-command="slide-delete"]')
188 if (!answer) return
189 const back = askedFrom
190 askedFrom = null
191 if (answer.dataset.command === 'slide-delete-cancel' && usable(back)) back.focus()
192 }, true)
193
194 // --- the View menu's checks follow the controls they drive ---
195 function syncView () {
196 const v = switcher?.querySelector('input:checked')?.value || 'desktop'
197 for (const b of root.querySelectorAll('[data-view-choice]')) b.setAttribute('aria-pressed', String(b.dataset.viewChoice === v))
198 }
199 function notesShown (open) {
200 for (const b of root.querySelectorAll('[data-view-toggle="notes"]')) b.setAttribute('aria-pressed', String(!!open))
201 }
202 switcher?.addEventListener('change', syncView)
203 syncView()
204 if (notes) notesShown(notes.isOpen())
205
206 root.addEventListener('click', e => {
207 const el = e.target.closest('[data-layout-choice], [data-insert-block], [data-insert-scene], [data-insert-clip], [data-view-choice], [data-view-toggle], [data-open-dialog]')
208 if (!el || !root.contains(el) || el.disabled) return
209 const back = returnTarget(el)
210 closeMenu(el)
211 if (el.dataset.layoutChoice) return commands.newSlide(el.dataset.layoutChoice)
212 if (el.dataset.insertBlock) return commands.addBlock(el.dataset.insertBlock)
213 if (el.hasAttribute('data-insert-scene')) return commands.addBlock('scene')
214 if (el.hasAttribute('data-insert-clip')) return commands.addBlock('clip')
215 if (el.dataset.viewChoice) {
216 const radio = switcher?.querySelector(`input[value="${CSS.escape(el.dataset.viewChoice)}"]`)
217 if (radio && !radio.checked) radio.click()
218 return
219 }
220 if (el.dataset.viewToggle === 'notes') return notes?.toggle()
221 if (el.dataset.viewToggle === 'outline') return outline?.open({ returnTo: back })
222 // From the keyboard (a click with no pointer behind it), focus goes
223 // into the card it opens.
224 if (el.dataset.viewToggle === 'inspector') return showInspector(!inspectorShown(), { focus: e.detail === 0, returnTo: back })
225 if (el.dataset.openDialog) {
226 const dialog = document.getElementById(el.dataset.openDialog)
227 if (!dialog || dialog.open) return
228 // The row is hidden with its menu closed, so the browser's own
229 // return of focus would leave it nowhere.
230 dialog.addEventListener('close', () => { if (usable(back)) back.focus() }, { once: true })
231 dialog.showModal()
232 }
233 })
234
235 // --- the title: the header's field is where the deck's is edited ---
236 if (titleInput) {
237 const send = () => {
238 const v = titleInput.value.trim()
239 if (!v || v === title) { showTitle(title); return }
240 rememberTitle(v)
241 showTitle(v)
242 commitTitle(v)
243 }
244 titleInput.addEventListener('change', send)
245 titleInput.addEventListener('keydown', e => {
246 if (e.key === 'Enter') { e.preventDefault(); titleInput.blur() }
247 if (e.key === 'Escape') { e.preventDefault(); showTitle(title); titleInput.blur() }
248 })
249 }
250
251 // --- Delete in the strip ---
252 root.querySelector('[slides-strip]')?.addEventListener('keydown', e => {
253 if ((e.key !== 'Delete' && e.key !== 'Backspace') || e.altKey || e.ctrlKey || e.metaKey) return
254 const item = e.target.closest('[slides-strip-link]')?.closest('[slides-strip-item]')
255 if (!item?.dataset.slideId) return
256 e.preventDefault()
257 askedFrom = e.target
258 showInspector(true, { focus: true })
259 commands.strip('delete', item.dataset.slideId)
260 })
261
262 return {
263 // A title from the document (this tab's change, or a colleague's):
264 // shown unless somebody is typing a new one into the header.
265 setTitle (t) {
266 rememberTitle(t)
267 if (titleInput && document.activeElement !== titleInput) showTitle(t)
268 },
269 // Whether a slide is on stage: the rows that act on one follow it.
270 slideShown (has) {
271 for (const el of root.querySelectorAll('[data-needs-slide]')) el.disabled = !has
272 },
273 // The notes card opened or closed: the View menu's row follows.
274 notesShown,
275 showInspector,
276 inspectorShown
277 }
278}
279