// fixtures/inspector-steps.mjs — how a box appears (steps and presenter // spec §4, In the editor): the Appears row at the head of the box's // card, With the slide, On a press and, for a list, Item by item; and // After…, a small popover listing the slide's presses, which places the // box after one of them. // // A box's step is a number compared with its neighbours' and nothing // else (presses.mjs), so nothing is ever renumbered: // // - On a press, on a box with no step, takes one more than the largest // step on the slide: the box arrives last. It clears items, so a list // arrives whole even if it was once item by item. // - Item by item is a list's: items "each", and a step as On a press // when the list has none. // - With the slide clears the step and the items. // - After press n writes the midpoint of the value press n was made // from and the next value on the slide, or one more after the last. // The box's own value is not counted: it is the one moving. Value 0 is // never offered: it is a scene block's with no step (its states are // the slide's first presses), and nothing is placed after the slide's // arrival but by On a press. // - scenes is the deck's (the view's scenes map), so a scene block's // states count as presses (presses.mjs). // // Each choice is one set_block, so one commit and one undo step. // fillAppears puts the box's choice in the row and the slide's presses // in After…; mountAppears hears After…'s rows. The press numbers on the // stage are step-marks.mjs's; the box menu's rows box-menu-appears.mjs's. import { presses, pressValues } from './presses.mjs' const stepOf = b => (b && typeof b.step === 'number' && b.step > 0 ? b.step : 0) /** 'slide', 'press' or 'each': how the box arrives. */ export function appearsOf (block) { if (!stepOf(block)) return 'slide' return block.type === 'list' && block.items === 'each' ? 'each' : 'press' } /** One more than the largest step on the slide (1 on a slide without steps). */ function nextStep (slide) { const blocks = Array.isArray(slide && slide.blocks) ? slide.blocks : [] return blocks.reduce((m, b) => Math.max(m, stepOf(b)), 0) + 1 } const setBlock = (slide, block, fields) => [{ op: 'set_block', slide: slide.id, id: block.id, ...fields }] /** The ops for choosing how block arrives: [] when it already does, or the choice is not the box's. */ export function appearsOps (slide, block, choice) { const now = appearsOf(block) if (choice === now) return [] if (choice === 'slide') return setBlock(slide, block, { step: null, items: null }) if (choice === 'press') { if (stepOf(block)) return setBlock(slide, block, { items: null }) // Items "each" left on a list with no step would make it item by // item the moment it has one: they go with the same op. return setBlock(slide, block, block.items ? { step: nextStep(slide), items: null } : { step: nextStep(slide) }) } if (choice === 'each' && block.type === 'list') { return setBlock(slide, block, stepOf(block) ? { items: 'each' } : { step: nextStep(slide), items: 'each' }) } return [] } /** The presses After… lists for block: every value another box makes, numbered by its last press; never value 0. */ export function afterChoices (slide, block, scenes) { const ps = presses(slide, scenes) const at = pressValues(slide, scenes) const out = [] ps.forEach((press, i) => { if (!(at[i] > 0) || !press.some(m => m.block !== block.id)) return const last = out.length ? out[out.length - 1] : null if (last && last.value === at[i]) last.n = i + 1 else out.push({ n: i + 1, value: at[i] }) }) return out } /** The op placing block after the press made from value: the midpoint to the next, or value + 1. */ export function afterOps (slide, block, value, scenes) { const later = afterChoices(slide, block, scenes).map(c => c.value).filter(v => v > value) const step = later.length ? (value + Math.min(...later)) / 2 : value + 1 if (step === stepOf(block)) return [] return setBlock(slide, block, { step }) } /** Puts block's choice in the card's Appears row, and the slide's presses in After…. */ export function fillAppears (form, slide, block, scenes) { const now = appearsOf(block) for (const r of form.querySelectorAll('input[name="rst-block-appears"]')) r.checked = r.value === now const button = form.querySelector('[slides-appears-after]') const pop = form.querySelector('#slides-pop-after') if (!button || !pop) return const choices = afterChoices(slide, block, scenes) button.hidden = choices.length === 0 if (!choices.length && typeof pop.hidePopover === 'function' && pop.matches(':popover-open')) pop.hidePopover() // The rows rebuilt only when they change, so a row with focus keeps it. const key = choices.map(c => `${c.n}:${c.value}`).join(' ') if (pop.dataset.drawn === key) return pop.dataset.drawn = key const label = pop.dataset.label || '{n}' pop.replaceChildren(...choices.map(c => { const row = document.createElement('button') row.type = 'button' row.setAttribute('slides-card-row', '') row.dataset.after = String(c.value) row.textContent = label.replace('{n}', String(c.n)) return row })) } /** * Hears After…'s rows in form, the card's block form: a row commits * afterOps for the selected box, closes the popover and puts focus back * on After…; Escape while it is open does the last two. getSlide() and * getBlock() are the card's current slide and box, getScenes() the * deck's scenes. Returns {destroy}. */ export function mountAppears ({ form, getSlide, getBlock, getScenes = () => null, commit }) { const button = form.querySelector('[slides-appears-after]') const pop = form.querySelector('#slides-pop-after') const abort = new AbortController() if (!button || !pop) return { destroy () { abort.abort() } } const back = () => { if (pop.matches(':popover-open')) pop.hidePopover() button.focus() } pop.addEventListener('click', e => { const row = e.target && e.target.closest ? e.target.closest('[data-after]') : null if (!row) return const slide = getSlide() const block = getBlock() back() if (!slide || !block) return const ops = afterOps(slide, block, Number(row.dataset.after), getScenes()) if (ops.length) commit(ops) }, { signal: abort.signal }) // Escape while it is open, from its rows or from After… itself, closes // it and goes no further: the card's own Escape (inspector.mjs) would // send focus to the box and, by taking the key, leave the popover open. form.addEventListener('keydown', e => { if (e.key !== 'Escape' || e.isComposing || !pop.matches(':popover-open')) return e.preventDefault() e.stopPropagation() back() }, { signal: abort.signal }) return { destroy () { abort.abort() } } }