// fixtures/box-menu.mjs — the menu on a selected box (the quieter // editor, spec §4): arranging a box moved out of the card and onto the // box. The round button ([slides-box-more]) sits on the selected box's // outline, at its top end corner where there is room, never over a // resize handle and never outside the stage; it opens the menu // ([slides-box-menu]), estilo's panel and rows as a manual popover. So // do the context-menu key and Shift+F10 with a box selected, and a // right-click on a box, unless the pointer is in text being edited: // there the browser's own menu opens, because spelling and paste live // in it. // // The menu's markup is the server's, every row drawn; open() hides the // rows that do not apply (rowsFor: layers and Position and size on a // canvas slide, the region rows on a flow one). A row is the command // its card button was, run through run(name) (commands.mjs); a region // row is setRegion(id, region). The arrows move through the rows, // Enter and Space press one, and Esc (or Tab) closes the menu and puts // focus back on the box, the selection kept. // // The box's keys are here too (boxKey): bring forward and send back, // move up and down, Delete, and Shift+F10 for the menu. They are heard // only while a box is selected and focus is on the stage: never in the // bar, the notes, the strip (whose Delete asks about a slide, head.mjs) // or a dialog. Delete and Mod+Shift+arrows are the box's only when the // box itself has focus (its canvas overlay, or its own tab stop): in // text they delete and select text. // // mountBoxMenu({root, stageEl, getSelected, getSlide, run, setRegion, // focusBox, select, after}) → {open('button' | {x, y}), close({restore}), // place(), sync(), isOpen(), destroy()}. getSlide() is the slide's JSON; // focusBox(id) puts focus on a box and select(id) selects one // (stage-focus.mjs); after(name, id) hears each row or key run on box id // ('region' for a region row), for where focus goes next. place() runs // wherever the canvas overlay is placed; sync() after every change to // the document, closing the open menu when its box or the slide's // layout changed under it. Where the button and the menu go is // box-spot.mjs; with no room on the stage the button docks beside the // view switch (data-docked). import { defaultRegion } from './layout.mjs' import { buttonSpot, menuSpot, clip, overlaps } from './box-spot.mjs' export { buttonSpot, menuSpot } const ROWS = { canvas: ['block-format', 'block-forward', 'block-back', 'block-move-up', 'block-move-down', 'block-place', 'block-delete'], flow: ['block-format', 'block-move-up', 'block-move-down', 'region', 'block-delete'] } const EDITABLE = '[contenteditable="true"]' /** The menu's rows for a box, in order: what decides is the slide's layout. */ export function rowsFor ({ layout }) { return [...(layout === 'canvas' ? ROWS.canvas : ROWS.flow)] } /** Whether a right-click on target is the box menu's: not in text being edited. */ export function canOwnContextMenu (target) { if (!target || typeof target.closest !== 'function') return false const editable = target.closest(EDITABLE) if (!editable) return true const active = globalThis.document ? globalThis.document.activeElement : null return !(active && editable.contains(active)) } /** * The box's command for a keydown, 'menu', or null. onBox: the box * itself has focus, no text is being edited. */ export function boxKey (e, { onBox = false } = {}) { const mod = !!(e.ctrlKey || e.metaKey) if (e.key === 'F10' && e.shiftKey && !mod && !e.altKey) return 'menu' if (mod && !e.shiftKey && !e.altKey) { if (e.key === ']' || e.code === 'BracketRight') return 'block-forward' if (e.key === '[' || e.code === 'BracketLeft') return 'block-back' } if (!onBox) return null if (mod && e.shiftKey && !e.altKey && e.key === 'ArrowUp') return 'block-move-up' if (mod && e.shiftKey && !e.altKey && e.key === 'ArrowDown') return 'block-move-down' if ((e.key === 'Delete' || e.key === 'Backspace') && !mod && !e.altKey && !e.shiftKey) return 'block-delete' return null } const rectOf = el => (el && typeof el.getBoundingClientRect === 'function' ? el.getBoundingClientRect() : null) export function mountBoxMenu ({ root, stageEl, getSelected, getSlide, run, setRegion, focusBox = () => {}, select = () => {}, after = () => {} }) { const button = root.querySelector('[slides-box-more]') const menu = root.querySelector('[slides-box-menu]') const doc = globalThis.document // In Phone view the stage's frame is the phone's screen, and it is // what scrolls; elsewhere it is nothing (display: contents, no box). const frame = typeof stageEl.closest === 'function' ? stageEl.closest('[slides-stage-frame]') : null const abort = new AbortController() const { signal } = abort let shown = false let boxId = '' // The slide's layout when the menu opened: its rows are that layout's. let openLayout = '' const boxEl = id => (id ? [...stageEl.querySelectorAll('[slides-block]')].find(b => b.getAttribute('data-id') === id) || null : null) 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 } // The rows drawn: the row itself, or its group, not hidden. const drawn = () => [...menu.querySelectorAll('[estilo-menu-item]')].filter(i => !i.hidden && !(i.closest('[data-row]') || i).hidden) // The round button over the selected box, in the stage pane's // pixels; hidden with nothing selected, and the menu closed then. function place () { const id = getSelected() const el = boxEl(id) if (shown && (!el || id !== boxId)) close() sync() if (!button) return button.hidden = !el if (!el || !button.style) return const box = rectOf(el) const pane = rectOf(button.offsetParent || button.parentNode) if (!box || !pane) return // What can be seen of the stage: in Phone view, what the screen shows. const bound = clip(rectOf(stageEl), rectOf(frame)) if (bound && !overlaps(box, bound)) return void (button.hidden = true) const handles = [...stageEl.querySelectorAll('[slides-handle]')].map(rectOf).filter(h => h && h.width > 0) const avoid = [...stageEl.querySelectorAll('[slides-block]')].filter(b => !b.contains(el) && !el.contains(b)).map(rectOf).filter(r => r && r.width > 0) const rtl = typeof getComputedStyle === 'function' && getComputedStyle(stageEl).direction === 'rtl' const size = button.offsetWidth || 28 let spot = buttonSpot({ box, handles, avoid, bound, size, rtl }) // A stage too small for it anywhere clear of the handles: docked in // the stage bar beside the view switch, off the slide. const docked = !spot const sw = docked ? rectOf(root.querySelector('[slides-switcher]')) : null if (docked && !sw) return if (docked) spot = { left: rtl ? sw.left - 8 - size : sw.right + 8, top: sw.top + (sw.height - size) / 2 } if (docked) button.setAttribute('data-docked', '') else button.removeAttribute('data-docked') button.style.left = `${spot.left - pane.left}px` button.style.top = `${spot.top - pane.top}px` } function position (at) { if (!menu.style || typeof menu.getBoundingClientRect !== 'function') return const anchor = at && typeof at === 'object' ? { left: at.x, right: at.x, top: at.y, bottom: at.y } : (button && !button.hidden && rectOf(button)) || rectOf(boxEl(boxId)) if (!anchor) return menu.style.maxBlockSize = '' const { width, height } = menu.getBoundingClientRect() const spot = menuSpot({ anchor, width, height, vw: globalThis.innerWidth, vh: globalThis.innerHeight }) menu.style.left = `${spot.left}px` menu.style.top = `${spot.top}px` menu.style.maxBlockSize = `${spot.maxHeight}px` } // The selected box's menu, its rows those that apply, the box's // region checked; at the button, or at the pointer ({x, y}). function open (at = 'button') { const sel = selected() if (!sel || !menu) return false const rows = rowsFor({ layout: sel.slide.layout, type: sel.block.type }) for (const r of menu.querySelectorAll('[data-row]')) r.hidden = !rows.includes(r.dataset.row) checkRegion(sel.block) boxId = sel.block.id openLayout = sel.slide.layout if (!shown && typeof menu.showPopover === 'function') menu.showPopover() shown = true if (button) button.setAttribute('aria-expanded', 'true') position(at) const first = drawn()[0] if (first) first.focus() return true } function checkRegion (block) { const region = block.region || defaultRegion(block.type) for (const b of menu.querySelectorAll('[data-region]')) b.setAttribute('aria-checked', String(b.dataset.region === region)) } // The open menu against the document as it is now (a colleague's // change, an undo): closed when its box is gone or no longer the // selection, or the slide's layout is not the one its rows were drawn // for; otherwise its region checks follow the box. function sync () { if (!shown) return const sel = selected() if (sel && sel.block.id === boxId && sel.slide.layout === openLayout) return checkRegion(sel.block) close({ restore: !!sel && sel.block.id === boxId && menu.contains(doc.activeElement) }) } // restore: focus back on the box (Esc, a row that leaves it there). function close ({ restore = false } = {}) { const id = boxId if (shown) { shown = false try { if (typeof menu.hidePopover === 'function') menu.hidePopover() } catch { /* closed already */ } } if (button) button.setAttribute('aria-expanded', 'false') if (restore && id) focusBox(id) } menu.addEventListener('click', e => { const row = e.target && typeof e.target.closest === 'function' ? e.target.closest('[estilo-menu-item]') : null if (!row || !shown) return const id = boxId // A row runs only while it still applies to the box as it is now. const sel = selected() if (!sel || sel.block.id !== id || !rowsFor({ layout: sel.slide.layout }).includes((row.closest('[data-row]') || row).dataset.row)) { close({ restore: !!sel && sel.block.id === id }) return } if (row.dataset.region) { close({ restore: true }) setRegion(id, row.dataset.region) after('region', id) return } const name = row.dataset.command if (!name) return // The box is still there afterwards, but for Delete. close({ restore: name !== 'block-delete' }) run(name) after(name, id) }, { signal }) menu.addEventListener('keydown', e => { if (e.isComposing) return if (e.key === 'Escape' || e.key === 'Tab') { e.preventDefault() e.stopPropagation() close({ restore: true }) return } const rows = drawn() if (!rows.length) return const at = rows.indexOf(doc.activeElement) const next = { ArrowDown: rows[(at + 1) % rows.length], ArrowUp: rows[(at - 1 + rows.length) % rows.length], Home: rows[0], End: rows[rows.length - 1] }[e.key] if (!next) return e.preventDefault() next.focus() }, { signal }) if (button) button.addEventListener('click', () => (shown ? close({ restore: true }) : open('button')), { signal }) // A press anywhere else closes it (a manual popover has no light dismiss). doc.addEventListener('pointerdown', e => { if (!shown || !e.target) return if (menu.contains(e.target) || (button && button.contains(e.target))) return close() }, { signal, capture: true }) // A right-click on a box (or on the overlay over the selected one), // outside text being edited; from the keyboard (no pointer), at the // round button. A box not yet selected is selected first. stageEl.addEventListener('contextmenu', e => { const t = e.target if (!canOwnContextMenu(t)) return const box = t.closest('[slides-canvas-overlay]') ? boxEl(getSelected()) : t.closest('[slides-block]') const id = box ? box.getAttribute('data-id') : '' if (!id) return e.preventDefault() if (id !== getSelected()) select(id) open(e.clientX || e.clientY ? { x: e.clientX, y: e.clientY } : 'button') }, { signal }) for (const el of [stageEl, frame]) if (el) el.addEventListener('scroll', () => place(), { signal, passive: true }) doc.addEventListener('keydown', e => { if (e.defaultPrevented || e.isComposing || shown) return const a = doc.activeElement if (!a || !stageEl.contains(a) || !getSelected()) return const inText = typeof a.closest === 'function' && !!a.closest(EDITABLE) const onBox = !inText && typeof a.matches === 'function' && (a.matches('[slides-canvas-overlay]') || a.matches('[slides-block]')) const name = boxKey(e, { onBox }) if (!name) return // Held from the browser (Mod+[ and Mod+] go back and forward in // history in some) even where the box has no such row. e.preventDefault() if (name === 'menu') { open('button') return } const sel = selected() if (!sel || !rowsFor({ layout: sel.slide.layout, type: sel.block.type }).includes(name)) return run(name) after(name, sel.block.id) }, { signal }) return { open, close, place, sync, isOpen: () => shown, destroy () { close() abort.abort() } } }