// presenter-arrange.mjs — arranging the presenter window (spec §5.4, // mockup #parrange). // // The three panels (now, next, notes) sit on a 12×12 grid. Arrange this // window, in the ⋯ menu, outlines them: a panel dragged moves, its // corner dragged resizes it, and a focused panel moves a cell with an // arrow key and resizes with Shift and an arrow, so none of it needs a // pointer (WCAG 2.5.7). Every change snaps to the grid, stays inside // it, keeps each panel at least 2×2 and never lets two overlap // (clampPanels). Done saves the arrangement in the window's cookie // (presenter-notes.mjs writes it, notes and all); Reset puts the // default back and saves that; Esc leaves without saving. A tap or a // click selects a panel and gives it eight buttons, a cell a press, so // arranging needs no drag (WCAG 2.5.7) and no keyboard; each panel is a // named group while arranging, and each change is said aloud. // // The panels are placed by a stylesheet that says what the server's // does (presenter_window.go's css), so the arrangement on screen while // arranging is the one the next first paint draws. Below 720×560 the // panels stack, the arrangement is ignored, and there is nothing to // arrange. // // No words here: the bar's are the page's. The grid itself, as data, is // presenter-grid.mjs's. import { DEFAULT_PANELS, GRID, NAMES, WIDE, parsePanels, formatPanels, clampPanels, keyChange, toolChange, canChange, mirror, whereWords, arrangeCSS } from './presenter-grid.mjs' /** * Arranging, in the window root. panels is the arrangement the page was * drawn with (the cookie's, or the default); save(panels) keeps one. * Returns start (the menu's Arrange), isOn, and key, the keydown * handler while arranging: the window gives it every key but the * clicker's. * * While arranging each panel is a focusable group named for what it * holds (data-arrange-name) and described by the keys * (#presenter-arrange-help). Focusing one, or a tap or click on it, * selects it: it gets the eight buttons (presenter-arrange-tools), one * cell per press, disabled where the change cannot be made, so neither * a drag nor a keyboard is needed. Every change, and every selection, * is said in the bar's polite live region (presenter-arrange-status). */ export function mountArrange ({ root, panels: initial, save }) { const doc = root.ownerDocument const win = doc.defaultView const bar = root.querySelector('[presenter-arrange-bar]') const box = root.querySelector('[presenter-panels]') const tools = root.querySelector('[presenter-arrange-tools]') const status = root.querySelector('[presenter-arrange-status]') const help = root.querySelector('#presenter-arrange-help') const els = NAMES.map(n => root.querySelector(`[presenter-panel="${n}"]`)) const wide = win.matchMedia(WIDE) let panels = parsePanels(initial) || parsePanels(DEFAULT_PANELS) let saved = panels // what Esc goes back to let on = false let style = null let drag = null let selected = 0 // A right-to-left window's grid runs the other way across. const rtl = () => win.getComputedStyle(root).direction === 'rtl' const across = () => (rtl() ? -1 : 1) const toolOf = b => mirror(toolChange(b.getAttribute('data-arrange-op')), rtl(), true) const nameOf = i => els[i]?.getAttribute('data-arrange-name') || '' function set (next) { panels = next if (!style) { style = doc.createElement('style') style.setAttribute('presenter-arranged', '') doc.head.append(style) } style.textContent = arrangeCSS(panels) } function say (i) { if (status) status.textContent = whereWords(root.dataset.textArrangeWhere, nameOf(i), panels[i]) } // The selected panel's buttons sit over its corner nearer the middle // of the window, across and down, so however small the panel and // wherever it is they open into the grid and stay in the window. Next // to it in the focus order, each disabled where its change cannot be // made. function tooled () { if (!tools || !on) return const p = panels[selected] tools.style.gridArea = `${p.y + 1} / ${p.x + 1} / span ${p.h} / span ${p.w}` tools.style.justifySelf = (p.x * 2 + p.w > GRID) ? 'end' : 'start' tools.style.alignSelf = (p.y * 2 + p.h > GRID) ? 'end' : 'start' for (const b of tools.querySelectorAll('[data-arrange-op]')) { b.setAttribute('aria-disabled', String(!canChange(panels, selected, toolOf(b)))) } } function select (i) { selected = i els.forEach((el, j) => el.toggleAttribute('data-selected', j === i)) if (tools) { tools.setAttribute('aria-label', nameOf(i)) els[i].after(tools) tools.hidden = false } tooled() say(i) } // change applied to panel i: placed, the buttons brought up to date, // and said when it moved anything. function change (i, next) { const moved = formatPanels(next) !== formatPanels(panels) set(next) tooled() if (moved) say(i) } function leave () { on = false drag = null root.removeAttribute('data-arranging') bar.hidden = true if (tools) tools.hidden = true if (status) status.textContent = '' for (const el of els) { for (const a of ['tabindex', 'role', 'aria-label', 'aria-describedby', 'data-selected']) el.removeAttribute(a) el.querySelector(':scope > [presenter-arrange-handle]')?.remove() } root.querySelector('[presenter-more]')?.focus() } function start () { if (on || !bar || !box || els.some(el => !el) || !wide.matches) return on = true saved = panels root.setAttribute('data-arranging', '') bar.hidden = false els.forEach((el, i) => { el.tabIndex = 0 el.setAttribute('role', 'group') el.setAttribute('aria-label', nameOf(i)) if (help) el.setAttribute('aria-describedby', help.id) const handle = doc.createElement('span') handle.setAttribute('presenter-arrange-handle', '') handle.setAttribute('aria-hidden', 'true') el.append(handle) }) els[0].focus() select(0) } function cancel () { set(saved) leave() } bar?.querySelector('[presenter-arrange-done]')?.addEventListener('click', () => { save(formatPanels(panels)) saved = panels leave() }) bar?.querySelector('[presenter-arrange-reset]')?.addEventListener('click', () => { change(selected, parsePanels(DEFAULT_PANELS)) save(DEFAULT_PANELS) saved = panels }) tools?.addEventListener('click', e => { const b = e.target.closest?.('[data-arrange-op]') if (!on || !b || b.getAttribute('aria-disabled') === 'true') return change(selected, clampPanels(panels, selected, toolOf(b))) }) // Narrowed below the grid while arranging: the panels stack, and // there is nothing left to arrange. wide.addEventListener?.('change', () => { if (on && !wide.matches) cancel() }) els.forEach((el, i) => { if (!el) return el.addEventListener('focus', () => { if (on && selected !== i) select(i) }) el.addEventListener('pointerdown', e => { if (!on || e.button !== 0) return e.preventDefault() el.focus({ preventScroll: true }) if (selected !== i) select(i) el.setPointerCapture?.(e.pointerId) const r = box.getBoundingClientRect() drag = { i, id: e.pointerId, resize: !!e.target.closest?.('[presenter-arrange-handle]'), x: e.clientX, y: e.clientY, w: r.width / GRID, h: r.height / GRID, from: panels } }) el.addEventListener('pointermove', e => { if (!drag || drag.i !== i || drag.id !== e.pointerId) return const dx = Math.round(across() * (e.clientX - drag.x) / drag.w) const dy = Math.round((e.clientY - drag.y) / drag.h) change(i, clampPanels(drag.from, i, drag.resize ? { dw: dx, dh: dy } : { dx, dy })) }) const end = e => { if (drag && drag.id === e.pointerId) drag = null } el.addEventListener('pointerup', end) el.addEventListener('pointercancel', end) }) function key (e) { if (e.metaKey || e.ctrlKey || e.altKey) return if (e.key === 'Escape') { e.preventDefault() cancel() return } const i = els.indexOf(doc.activeElement) const c = i >= 0 && keyChange(e.key, e.shiftKey) if (!c) return e.preventDefault() change(i, clampPanels(panels, i, mirror(c, rtl(), false))) } return { start, key, isOn: () => on } }