| 1 | // fixtures/box-menu-appears.mjs — the Appears rows in the menu on a box |
| 2 | // (steps and presenter spec §4): With the slide, On a press and, for a |
| 3 | // list, Item by item, as menuitemradio rows, checked as the card's |
| 4 | // Appears row is, each writing what that row's segment writes |
| 5 | // (inspector-steps.mjs). |
| 6 | // |
| 7 | // box-menu.mjs draws and runs the menu's other rows and stays as it is: |
| 8 | // the rows here carry no data-row, so its open() leaves them alone, and |
| 9 | // their clicks are heard here first. When the menu opens (beforetoggle, |
| 10 | // which showPopover() fires before the menu is placed and its first row |
| 11 | // focused) the rows are shown for the selected box and its choice |
| 12 | // checked; sync() keeps the check right while it is open. |
| 13 | // |
| 14 | // mountBoxMenuAppears({menu, getSelected, getSlide, close, commit}) → |
| 15 | // {sync(), destroy()}. close() is the menu's close, focus back on the |
| 16 | // box; commit(ops) applies one batch, one undo step. |
| 17 | import { appearsOf, appearsOps } from './inspector-steps.mjs' |
| 18 | |
| 19 | /** The choices a box's menu offers: Item by item on a list alone. */ |
| 20 | export function appearsShown (block) { |
| 21 | return block && block.type === 'list' ? ['slide', 'press', 'each'] : ['slide', 'press'] |
| 22 | } |
| 23 | |
| 24 | export function mountBoxMenuAppears ({ menu, getSelected, getSlide, close, commit }) { |
| 25 | const abort = new AbortController() |
| 26 | const { signal } = abort |
| 27 | const rows = () => [...menu.querySelectorAll('[data-appears]')] |
| 28 | function selected () { |
| 29 | const id = getSelected() |
| 30 | const slide = id ? getSlide() : null |
| 31 | const block = slide && Array.isArray(slide.blocks) ? slide.blocks.find(b => b.id === id) : null |
| 32 | return block ? { slide, block } : null |
| 33 | } |
| 34 | |
| 35 | function sync () { |
| 36 | const sel = selected() |
| 37 | if (!sel) return |
| 38 | const shown = appearsShown(sel.block) |
| 39 | const now = appearsOf(sel.block) |
| 40 | for (const r of rows()) { |
| 41 | r.hidden = !shown.includes(r.dataset.appears) |
| 42 | r.setAttribute('aria-checked', String(r.dataset.appears === now)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | menu.addEventListener('beforetoggle', e => { if (e.newState === 'open') sync() }, { signal }) |
| 47 | menu.addEventListener('click', e => { |
| 48 | const row = e.target && e.target.closest ? e.target.closest('[data-appears]') : null |
| 49 | if (!row) return |
| 50 | e.stopImmediatePropagation() |
| 51 | if (typeof menu.matches === 'function' && !menu.matches(':popover-open')) return |
| 52 | const sel = selected() |
| 53 | close() |
| 54 | if (!sel) return |
| 55 | const ops = appearsOps(sel.slide, sel.block, row.dataset.appears) |
| 56 | if (ops.length) commit(ops) |
| 57 | }, { signal, capture: true }) |
| 58 | |
| 59 | return { sync, destroy () { abort.abort() } } |
| 60 | } |
| 61 | |