// fixtures/layout-pick.mjs — the card at rest (spec §3.1): the slide's // layout as eight pictures, the slide's seven templates and then Free // placement, exactly one lit; and the two quiet rows under them, // Background and Transition, each opening a popover that holds the // field the card used to show. // // The markup is the server's (templates/editor.html), drawn with the // painted slide's picture already lit, so the first paint is final; // here the pictures are lit for whichever slide is current, and a // choice is turned into the ops the Template and Layout fields wrote. // // A template chosen on a canvas slide drops every box's position, so it // asks first, in the card ([slides-confirm-layout], the way Delete // slide asks). While it asks, the lit (checked) picture is still the // slide's true one, Free placement; the picture asked about is marked // apart from it (data-asked, a dashed outline, and aria-describedby // naming the question), never as chosen. Use the layout commits both // ops in one commit, one undo step; Keep free placement commits // nothing. The question is about one slide as it was when asked: if // that slide stops being canvas (a colleague, an undo) or the card // moves to another slide, the question withdraws and nothing is // committed (Review Focus 2). // // Escape inside a row's popover closes it and puts focus back on its // row, and goes no further, as the Outline's does (outline.mjs): the // popover is in the top layer, but in the DOM it is inside the slide // form, so the form's change listener (inspector.mjs) still hears its // fields. import { slideOp } from './inspector-ops.mjs' export const FREE = 'free' /** The pictures' values, in the card's order: the templates, then Free placement. */ export const PICTURES = ['title', 'title-body', 'two-column', 'section', 'image-full', 'quote', 'blank', FREE] /** The picture lit for slide: Free placement on a canvas slide, else its template. */ export function litFor (slide) { if (!slide) return '' return slide.layout === 'canvas' ? FREE : slide.template } /** * What choosing value on slide writes, and whether it asks first: a * template on a flow slide is the template; Free placement is the * canvas layout; a template on a canvas slide is the flow layout and * then the template, asked first; the lit picture again is nothing. */ export function pickOps (slide, value) { if (value === litFor(slide)) return { ops: [], ask: false } if (value === FREE) return { ops: [slideOp(slide, 'layout', 'canvas')], ask: false } if (slide.layout === 'canvas') { return { ops: [slideOp(slide, 'layout', 'flow'), slideOp(slide, 'template', value)], ask: true } } return { ops: [slideOp(slide, 'template', value)], ask: false } } /** * Mounts the pictures, the question and the rows' popovers on form, * the card's slide form. getSlide() is the current slide's JSON (null * with none); commit(ops) applies one batch. Returns {fill, withdraw, * destroy}: fill() after every document change, withdraw() to drop an * open question. */ export function mountLayoutPick ({ form, getSlide, commit }) { const radios = () => [...form.querySelectorAll('input[name="slides-layout-pick"]')] const confirm = form.querySelector('[slides-confirm-layout]') const action = confirm && confirm.querySelector('[data-command="layout-leave-free"]') const question = confirm && confirm.querySelector('[slides-confirm-layout-text]') // The question being asked: which slide, which picture, what it writes. let asked = null // The picture asked about, marked as asked about and described by the // question; '' takes the mark off every picture. function markAsked (value) { for (const r of radios()) { const item = r.closest('[slides-layout-pick-item]') if (r.value === value) { if (item) item.setAttribute('data-asked', '') if (question) r.setAttribute('aria-describedby', question.getAttribute('id')) } else { if (item && item.hasAttribute('data-asked')) item.removeAttribute('data-asked') if (r.hasAttribute('aria-describedby')) r.removeAttribute('aria-describedby') } } } function light (value) { for (const r of radios()) { const on = r.value === value if (r.checked !== on) r.checked = on } } const radioFor = value => radios().find(r => r.value === value) || null function withdraw () { asked = null if (confirm) confirm.hidden = true markAsked('') light(litFor(getSlide())) } function fill () { const slide = getSlide() if (asked && (!slide || slide.id !== asked.id || slide.layout !== 'canvas')) withdraw() else light(litFor(slide)) } function choose (value) { const slide = getSlide() if (!slide) return const { ops, ask } = pickOps(slide, value) if (ask) { asked = { id: slide.id, value, ops } light(litFor(slide)) markAsked(value) confirm.hidden = false if (action) action.focus() return } if (asked) withdraw() if (ops.length) commit(ops) } function leave () { const slide = getSlide() const q = asked if (!q || !slide || slide.id !== q.id || slide.layout !== 'canvas') { withdraw() return } asked = null confirm.hidden = true markAsked('') light(q.value) const r = radioFor(q.value) if (r) r.focus() commit(q.ops) } function cancel () { withdraw() const r = radioFor(litFor(getSlide())) if (r) r.focus() } const abort = new AbortController() const { signal } = abort form.addEventListener('change', e => { const t = e.target if (t && t.name === 'slides-layout-pick' && t.checked !== false) choose(t.value) }, { signal }) form.addEventListener('click', e => { const btn = e.target && e.target.closest ? e.target.closest('[data-command]') : null if (!btn) return const name = btn.dataset.command if (name === 'layout-leave-free') leave() else if (name === 'layout-leave-free-cancel') cancel() }, { signal }) form.addEventListener('keydown', e => { if (e.key !== 'Escape' || e.isComposing) return const pop = e.target && e.target.closest ? e.target.closest('[popover]') : null if (!pop || !form.contains(pop)) return e.preventDefault() e.stopPropagation() if (pop.matches(':popover-open')) pop.hidePopover() const row = form.querySelector(`[popovertarget="${pop.getAttribute('id')}"]`) if (row) row.focus() }, { signal }) return { fill, withdraw, destroy () { abort.abort() } } }