// fixtures/assist-panel.mjs — the assistant's panel (editor.html's // [slides-assist], present only with SLIDES_AI=on): the header toggle, // the ask form, the busy state, what came back, and Undo. // // The panel is a manual popover: the toggle and Close open and shut it // natively (popovertarget); this keeps the toggle lit while it is open // (aria-pressed, like the Inspector toggle), shuts it on Escape, and // puts focus somewhere sensible both ways. Every word is the panel's own // data-text-* (the catalog's); the summary is the model's sentence and // is data, set as text. // // mountAssist({root, assist, getSlideId, go, hold}) → {destroy()} or // null when the page has no panel. assist is assist.mjs's createAssist; // go(slideId) shows a slide; hold is the busy rule's least spinner time. // The busy rule's hold (static/busy.js): a spinner shows for at least // this long, so a quick answer reads as a beat rather than a flicker. export const HOLD_MS = 650 const camel = s => s.replace(/-([a-z])/g, (m, c) => c.toUpperCase()) export function mountAssist ({ root, assist, getSlideId = () => '', go = () => {}, hold = HOLD_MS }) { const panel = root.querySelector('[slides-assist]') const toggle = root.querySelector('[slides-assist-toggle]') if (!panel || !toggle) return null const form = panel.querySelector('[slides-assist-form]') const askBtn = panel.querySelector('[slides-assist-ask]') const outcome = panel.querySelector('[slides-assist-outcome]') const summaryEl = panel.querySelector('[slides-assist-summary]') const noteEl = panel.querySelector('[slides-assist-note]') const undoBtn = panel.querySelector('[slides-assist-undo]') const field = name => form.elements.namedItem(name) const prompt = field('slides-assist-prompt') const scope = field('slides-assist-scope') const model = field('slides-assist-model') const text = key => panel.dataset[camel('text-' + key)] || '' const abort = new AbortController() const on = (el, type, fn) => el && el.addEventListener(type, fn, { signal: abort.signal }) let busy = false const max = Number(panel.dataset.maxPrompt) || 0 if (max && prompt) prompt.maxLength = max function say ({ summary = '', note = '', error = false }) { summaryEl.textContent = summary noteEl.textContent = note if (error) outcome.setAttribute('data-tone', 'error') else outcome.removeAttribute('data-tone') } // Busy is aria-disabled, not disabled: a disabled button drops the // focus the person pressed it with onto the page, and the panel's // Escape (and the next Tab) with it. The busy flag is the guard. function setBusy (on) { busy = on if (on) { askBtn.setAttribute('aria-busy', 'true') askBtn.setAttribute('aria-disabled', 'true') const spin = document.createElement('span') spin.setAttribute('rst-spin', '') spin.setAttribute('aria-hidden', 'true') askBtn.append(spin) } else { askBtn.removeAttribute('aria-busy') askBtn.removeAttribute('aria-disabled') const spin = askBtn.querySelector('[rst-spin]') if (spin) spin.remove() } } // The panel opening and closing, however it was done. on(panel, 'toggle', e => { const open = e.newState === 'open' toggle.setAttribute('aria-pressed', String(open)) if (open) { // "This slide" means nothing on a deck with none. if (!getSlideId() && scope) scope.value = 'deck' if (prompt) prompt.focus() } else if (panel.contains(document.activeElement)) { toggle.focus() } }) on(panel, 'keydown', e => { if (e.key !== 'Escape') return e.preventDefault() e.stopPropagation() panel.hidePopover() }) on(form, 'submit', async e => { e.preventDefault() if (busy) return undoBtn.hidden = true setBusy(true) say({ note: text('working') }) const shown = Date.now() let result try { result = await assist.ask({ prompt: prompt ? prompt.value : '', scope: scope ? scope.value : 'slide', slide: getSlideId(), model: model ? model.value : '' }) } finally { const left = hold - (Date.now() - shown) if (left > 0) await new Promise(resolve => setTimeout(resolve, left)) setBusy(false) } if (result.state === 'error') { say({ note: text('error-' + result.word) || text('error-generic'), error: true }) if (result.word === 'prompt' && prompt) prompt.focus() return } if (result.state === 'nothing') { say({ summary: result.summary, note: text('nothing') }) return } say({ summary: result.summary, note: text('applied') }) undoBtn.hidden = !assist.canUndo if (result.focus && result.focus !== getSlideId()) go(result.focus) }) on(undoBtn, 'click', async () => { undoBtn.hidden = true const undone = await assist.undo() say({ summary: summaryEl.textContent, note: undone ? text('undone') : text('undo-later') }) if (prompt) prompt.focus() }) return { destroy () { abort.abort() } } }