// fixtures/box-menu-appears.mjs — the Appears rows in the menu on a box // (steps and presenter spec §4): With the slide, On a press and, for a // list, Item by item, as menuitemradio rows, checked as the card's // Appears row is, each writing what that row's segment writes // (inspector-steps.mjs). // // box-menu.mjs draws and runs the menu's other rows and stays as it is: // the rows here carry no data-row, so its open() leaves them alone, and // their clicks are heard here first. When the menu opens (beforetoggle, // which showPopover() fires before the menu is placed and its first row // focused) the rows are shown for the selected box and its choice // checked; sync() keeps the check right while it is open. // // mountBoxMenuAppears({menu, getSelected, getSlide, close, commit}) → // {sync(), destroy()}. close() is the menu's close, focus back on the // box; commit(ops) applies one batch, one undo step. import { appearsOf, appearsOps } from './inspector-steps.mjs' /** The choices a box's menu offers: Item by item on a list alone. */ export function appearsShown (block) { return block && block.type === 'list' ? ['slide', 'press', 'each'] : ['slide', 'press'] } export function mountBoxMenuAppears ({ menu, getSelected, getSlide, close, commit }) { const abort = new AbortController() const { signal } = abort const rows = () => [...menu.querySelectorAll('[data-appears]')] function selected () { const id = getSelected() const slide = id ? getSlide() : null const block = slide && Array.isArray(slide.blocks) ? slide.blocks.find(b => b.id === id) : null return block ? { slide, block } : null } function sync () { const sel = selected() if (!sel) return const shown = appearsShown(sel.block) const now = appearsOf(sel.block) for (const r of rows()) { r.hidden = !shown.includes(r.dataset.appears) r.setAttribute('aria-checked', String(r.dataset.appears === now)) } } menu.addEventListener('beforetoggle', e => { if (e.newState === 'open') sync() }, { signal }) menu.addEventListener('click', e => { const row = e.target && e.target.closest ? e.target.closest('[data-appears]') : null if (!row) return e.stopImmediatePropagation() if (typeof menu.matches === 'function' && !menu.matches(':popover-open')) return const sel = selected() close() if (!sel) return const ops = appearsOps(sel.slide, sel.block, row.dataset.appears) if (ops.length) commit(ops) }, { signal, capture: true }) return { sync, destroy () { abort.abort() } } }