// fixtures/outline.mjs โ€” View โ†’ Outline (spec ยง4.4): a popover listing // the slide's boxes by kind and first words. Choosing one selects that // box, closes the Outline and puts focus on the box. It is the list the // card held (the Boxes group) until the boxes became tab stops // (stage-focus.mjs), moved out of the card and opened by name. // // The popover is the template's (#slides-outline, in the top layer); // every word in it is the catalog's. Escape closes it and gives focus // back to where it was opened from (the View menu's title), and goes no // further: the selection.mjs Escape that gives a box up is not this // one. A click outside closes it too (the browser's light dismiss). // // mountOutline({popover, getBlocks, getSelected, select, labels}) โ†’ // {fill(), open({returnTo}), close(), isOpen(), destroy()}. getBlocks() is the // slide's blocks in order ([] with no slide), select(id) selects a box // and focuses it, labels.types names each kind. import { blockLabel } from './inspector-ops.mjs' /** * The list, reconciled by id rather than rebuilt: fill() runs on EVERY * document change while the Outline is open, a colleague's keystrokes * included, and a rebuild would take the focused row out of the * document under somebody's finger. Same shape as strip.plan: keep * what is wanted, drop what is not, and walk the order once. * * list is the rows' container, none the "no boxes yet" line, blocks * the slide's blocks in order, selected the selected block's id, * label(b) the words on a row, and make() a new row: a button whose * [estilo-menu-label], or itself, takes the words. */ export function fillBlocks ({ list, none, blocks, selected, label, make = plainRow }) { if (none) none.hidden = blocks.length > 0 const have = new Map() for (const el of list.querySelectorAll('[data-select-block]')) have.set(el.dataset.selectBlock, el) const want = [] for (const b of blocks) { let btn = have.get(b.id) if (btn) have.delete(b.id) else { btn = make() btn.dataset.selectBlock = b.id } // Written only when it differs: assigning the same text replaces // the text node, which is enough to lose a selection inside it. const words = label(b) const text = btn.querySelector('[estilo-menu-label]') || btn if (text.textContent !== words) text.textContent = words const pressed = b.id === selected ? 'true' : 'false' if (btn.getAttribute('aria-pressed') !== pressed) btn.setAttribute('aria-pressed', pressed) want.push(btn) } for (const el of have.values()) el.remove() // insertBefore only where the order actually differs. Re-appending a // node that is already in place is still a remove and an insert, and // a focused button would be blurred by it. let at = list.firstElementChild for (const btn of want) { if (at === btn) { at = btn.nextElementSibling continue } list.insertBefore(btn, at) } } // How far the panel keeps from the window's edges, in CSS px. const EDGE = 8 function plainRow () { const btn = document.createElement('button') btn.type = 'button' return btn } export function mountOutline ({ popover, getBlocks, getSelected = () => '', select, labels = { types: {} } }) { const list = popover.querySelector('[slides-outline-list]') const none = popover.querySelector('[slides-outline-none]') // A row as the template draws it (estilo's menu row, with its check). const tmpl = popover.querySelector('[slides-outline-row-template]') const make = tmpl && tmpl.content ? () => tmpl.content.firstElementChild.cloneNode(true) : plainRow const abort = new AbortController() const { signal } = abort let shown = false let returnTo = null function fill () { if (!shown) return fillBlocks({ list, none, blocks: getBlocks(), selected: getSelected(), label: b => blockLabel(b, labels.types[b.type] || b.type), make }) } // Under what opened it (the View menu's title), and focus on the // selected box's row, or the first; on the popover with no rows. function open ({ returnTo: back = null } = {}) { returnTo = back shown = true fill() const r = back && typeof back.getBoundingClientRect === 'function' ? back.getBoundingClientRect() : null if (r && popover.style) { popover.style.left = `${Math.round(r.left)}px` popover.style.top = `${Math.round(r.bottom)}px` } popover.showPopover() if (r && popover.style) keepInWindow(r) const rows = [...list.querySelectorAll('[data-select-block]')] const row = rows.find(b => b.getAttribute('aria-pressed') === 'true') || rows[0] || popover row.focus() } // The panel inside the window on every side, EDGE short of it: as // tall as the room under its title allows (it scrolls inside itself // past that), and moved back from the end edge when its title is // nearer that edge than the panel is wide (a narrow window). function keepInWindow (r) { const w = globalThis.innerWidth const h = globalThis.innerHeight if (typeof w !== 'number' || typeof popover.getBoundingClientRect !== 'function') return const top = Math.min(Math.round(r.bottom), Math.max(EDGE, h - EDGE - 64)) popover.style.top = `${top}px` popover.style.maxBlockSize = `${Math.max(0, h - top - EDGE)}px` const width = popover.getBoundingClientRect().width popover.style.left = `${Math.round(Math.max(EDGE, Math.min(r.left, w - width - EDGE)))}px` } // restore: focus back to what opened it (Escape); not when a row was // chosen, which puts it on the box. function close ({ restore = true } = {}) { if (shown) { shown = false popover.hidePopover() } const back = returnTo returnTo = null if (restore && back && back.isConnected !== false && typeof back.focus === 'function') back.focus() } // A click outside, or the browser closing it any other way. popover.addEventListener('toggle', e => { if (e.newState === 'closed') shown = false }, { signal }) popover.addEventListener('click', e => { const row = e.target.closest('[data-select-block]') if (!row) return const id = row.dataset.selectBlock close({ restore: false }) select(id) }, { signal }) popover.addEventListener('keydown', e => { if (e.key !== 'Escape' || e.isComposing) return e.preventDefault() e.stopPropagation() close() }, { signal }) return { fill, open, close, isOpen: () => shown, destroy () { abort.abort() } } }