// fixtures/head.mjs — the editor's header and its menus (editor.html's // [slides-head], I2 after Docs): the commands that are not already the // root's [data-command] buttons. // // Every row here drives a control that already exists and stays the // control of record, so there is one path for each change and nothing // to keep in step but the menus' own check marks: // // - a layout (data-layout-choice, the Insert menu and the strip's // picker) is commands.newSlide(layout); // - a block (data-insert-block) is commands.addBlock(kind), which // adds it after the selection and selects it: Insert is the one // place a box is added (spec §5); Scene (data-insert-scene) is a // scene block of one of the deck's scenes (scene-card.mjs), and // Clip (data-insert-clip) a clip whose video is picked at once // (clip-card.mjs); // - Desktop / Phone / Both (data-view-choice) check the switcher's // radio, Speaker notes opens or closes the notes card (notes-card.mjs, // the same as its × and its icon), and Inspector // (data-view-toggle="inspector") shows or hides the inspector card, // as the bar's Format button does, and Outline // (data-view-toggle="outline") opens the slide's boxes in a list // (outline.mjs) under the View menu's title; // - the header's title is the one place the deck's title is edited // (spec §8): a change is commitTitle(title), one set_meta; // - a menu's dialog (Help's shortcuts, File's Deck settings) opens // with showModal (data-open-dialog), and when it closes focus goes // back to that menu's title, as estilo's own dialog rows do; // - Delete or Backspace on a slide in the strip asks to delete it, // through the same confirm the menus use. // // mountHead({root, commands, notes, outline, storage, commitTitle}) → {setTitle(title), // slideShown(has), notesShown(open), showInspector(on, {focus}), // inspectorShown()}. notes is the notes card; its every open and close // is told to notesShown, which keeps the Speaker notes row's check. // // The inspector card (spec §4, Docs' Format): whether it shows is the // slides-inspector cookie, which the server reads and renders as the // card's data-state and the Format button's aria-expanded, so the first // paint is final. This module is the one owner of both after that // (estilo's chrome.js binds no data-estilo-toggle), and of the View // menu row's aria-pressed, and writes the cookie for the next page. // Where a cookie cannot be written the toggle still works, for this // page. The localStorage preference from before the cookie is applied // once, moved to the cookie and removed. import { readCookie, writeCookie } from './pref-cookie.mjs' export const INSPECTOR_KEY = 'slides.editor.inspector' export const INSPECTOR_COOKIE = 'slides-inspector' // A control the card can take focus into: the first one showing. const FOCUSABLE = 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), summary, [href], [tabindex]:not([tabindex="-1"])' /** * Whether c is a radio that does not stand for its group: unchecked, * with another of its name checked in root. Tab reaches a radio group * at its checked radio, and so does focus put into the card, so that * Space there chooses nothing new (the slide's layout is one). */ export function uncheckedOfGroup (c, root) { if (c.type !== 'radio' || c.checked || !c.name) return false return [...root.querySelectorAll('input[type="radio"]')].some(r => r.checked && r.name === c.name) } function writeInspectorCookie (open) { writeCookie(INSPECTOR_COOKIE, open ? 'open' : 'closed') } export function mountHead ({ root, commands, notes = null, outline = null, storage, commitTitle = () => {} }) { if (storage === undefined) { try { storage = globalThis.localStorage } catch { storage = null } } const titleInput = root.querySelector('[slides-head-title]') // The deck's title as the document has it: what Escape, or a title // left empty or unchanged, puts back in the field. let title = titleInput ? titleInput.value : '' // The deck's title has changed. The field's default value follows it, // because that is what estilo's bar (chrome.js) restores on Escape, // before this module hears the key. const rememberTitle = t => { title = t if (titleInput) titleInput.defaultValue = t } // Puts a title in the field. The input event is how estilo's bar // (chrome.js) refits the field to what it now holds. const showTitle = t => { titleInput.value = t titleInput.dispatchEvent?.(new Event('input', { bubbles: true })) } const switcher = root.querySelector('[slides-switcher]') const inspectorEl = root.querySelector('[slides-inspector]') const closeMenu = el => { const menu = el.closest('details') if (menu) menu.open = false const pop = el.closest('[popover]') if (pop && pop.matches(':popover-open')) pop.hidePopover() } // --- the inspector card: shown or hidden, remembered in a cookie --- function inspectorShown () { return !!inspectorEl && inspectorEl.getAttribute('data-state') !== 'closed' } function paintInspector (on) { if (inspectorEl) inspectorEl.setAttribute('data-state', on ? 'open' : 'closed') for (const b of root.querySelectorAll('[data-view-toggle="inspector"]')) { b.setAttribute(b.hasAttribute('aria-expanded') ? 'aria-expanded' : 'aria-pressed', String(on)) } } // Where focus was before a keyboard action took it into the card: // dismissing the card gives it back. An action from a menu passes the // menu's own button (returnTarget), since the row it was on is hidden // once the menu closes. let focusBack = null function firstControl () { if (!inspectorEl) return null for (const c of inspectorEl.querySelectorAll(FOCUSABLE)) { if (c.closest?.('[hidden]')) continue if (uncheckedOfGroup(c, inspectorEl)) continue if (typeof c.checkVisibility === 'function' && !c.checkVisibility()) continue return c } return null } // Something focus can actually go to: in the page, enabled, drawn. function usable (t) { if (!t || t.isConnected === false || t.disabled || typeof t.focus !== 'function') return false return typeof t.checkVisibility !== 'function' || t.checkVisibility() } // Focus back to t, or, where t cannot take it, to Format: never a // restore that silently leaves focus nowhere. function giveFocusBack (t) { const format = root.querySelector('[data-view-toggle="inspector"][aria-expanded]') for (const c of [t, format]) { if (!usable(c)) continue c.focus() if (globalThis.document?.activeElement === c) return } } function showInspector (on, { focus = false, returnTo = null } = {}) { on = !!on const active = globalThis.document?.activeElement || null const focusInCard = !!(active && inspectorEl && inspectorEl.contains(active)) paintInspector(on) writeInspectorCookie(on) if (on && focus) { if (!focusInCard) focusBack = returnTo || active firstControl()?.focus() } else if (!on) { const back = focusBack focusBack = null const lost = focusInCard || !usable(active) || active === globalThis.document?.body || active.matches?.('[data-view-toggle="inspector"]') if (back && lost) giveFocusBack(back) } } // The control to give focus back to for an action on el: the button // of the menu el is in (a
' summary), or el itself. function returnTarget (el) { const menu = el.closest('details') return (menu && menu.querySelector(':scope > summary')) || el } // The first paint is the server's; the View row and the button follow // the card. A preference from before the cookie, where there is no // cookie yet, is applied once and moved. paintInspector(inspectorShown()) let old = null try { old = storage?.getItem(INSPECTOR_KEY) ?? null } catch { /* unreadable */ } if (old !== null) { if (readCookie(INSPECTOR_COOKIE) === null && (old === 'on' || old === 'off')) showInspector(old === 'on') try { storage.removeItem(INSPECTOR_KEY) } catch { /* this visit only */ } } // Deleting a slide asks in the card, at the top of the slide's // options, so a hidden card comes back first, with focus (capture: // before the root's command handler moves focus to the question). // Cancel gives focus back to what asked — the menu's button, or the // slide in the strip — and leaves the card showing. let askedFrom = null root.addEventListener('click', e => { const ask = e.target.closest('[data-command="slide-delete-ask"], [data-slide-command="delete"]') if (ask) { askedFrom = returnTarget(ask) showInspector(true, { focus: true, returnTo: askedFrom }) return } const answer = e.target.closest('[data-command="slide-delete-cancel"], [data-command="slide-delete"]') if (!answer) return const back = askedFrom askedFrom = null if (answer.dataset.command === 'slide-delete-cancel' && usable(back)) back.focus() }, true) // --- the View menu's checks follow the controls they drive --- function syncView () { const v = switcher?.querySelector('input:checked')?.value || 'desktop' for (const b of root.querySelectorAll('[data-view-choice]')) b.setAttribute('aria-pressed', String(b.dataset.viewChoice === v)) } function notesShown (open) { for (const b of root.querySelectorAll('[data-view-toggle="notes"]')) b.setAttribute('aria-pressed', String(!!open)) } switcher?.addEventListener('change', syncView) syncView() if (notes) notesShown(notes.isOpen()) root.addEventListener('click', e => { 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]') if (!el || !root.contains(el) || el.disabled) return const back = returnTarget(el) closeMenu(el) if (el.dataset.layoutChoice) return commands.newSlide(el.dataset.layoutChoice) if (el.dataset.insertBlock) return commands.addBlock(el.dataset.insertBlock) if (el.hasAttribute('data-insert-scene')) return commands.addBlock('scene') if (el.hasAttribute('data-insert-clip')) return commands.addBlock('clip') if (el.dataset.viewChoice) { const radio = switcher?.querySelector(`input[value="${CSS.escape(el.dataset.viewChoice)}"]`) if (radio && !radio.checked) radio.click() return } if (el.dataset.viewToggle === 'notes') return notes?.toggle() if (el.dataset.viewToggle === 'outline') return outline?.open({ returnTo: back }) // From the keyboard (a click with no pointer behind it), focus goes // into the card it opens. if (el.dataset.viewToggle === 'inspector') return showInspector(!inspectorShown(), { focus: e.detail === 0, returnTo: back }) if (el.dataset.openDialog) { const dialog = document.getElementById(el.dataset.openDialog) if (!dialog || dialog.open) return // The row is hidden with its menu closed, so the browser's own // return of focus would leave it nowhere. dialog.addEventListener('close', () => { if (usable(back)) back.focus() }, { once: true }) dialog.showModal() } }) // --- the title: the header's field is where the deck's is edited --- if (titleInput) { const send = () => { const v = titleInput.value.trim() if (!v || v === title) { showTitle(title); return } rememberTitle(v) showTitle(v) commitTitle(v) } titleInput.addEventListener('change', send) titleInput.addEventListener('keydown', e => { if (e.key === 'Enter') { e.preventDefault(); titleInput.blur() } if (e.key === 'Escape') { e.preventDefault(); showTitle(title); titleInput.blur() } }) } // --- Delete in the strip --- root.querySelector('[slides-strip]')?.addEventListener('keydown', e => { if ((e.key !== 'Delete' && e.key !== 'Backspace') || e.altKey || e.ctrlKey || e.metaKey) return const item = e.target.closest('[slides-strip-link]')?.closest('[slides-strip-item]') if (!item?.dataset.slideId) return e.preventDefault() askedFrom = e.target showInspector(true, { focus: true }) commands.strip('delete', item.dataset.slideId) }) return { // A title from the document (this tab's change, or a colleague's): // shown unless somebody is typing a new one into the header. setTitle (t) { rememberTitle(t) if (titleInput && document.activeElement !== titleInput) showTitle(t) }, // Whether a slide is on stage: the rows that act on one follow it. slideShown (has) { for (const el of root.querySelectorAll('[data-needs-slide]')) el.disabled = !has }, // The notes card opened or closed: the View menu's row follows. notesShown, showInspector, inspectorShown } }