oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/assist-panel.mjs — the assistant's panel (editor.html's
2// [slides-assist], present only with SLIDES_AI=on): the header toggle,
3// the ask form, the busy state, what came back, and Undo.
4//
5// The panel is a manual popover: the toggle and Close open and shut it
6// natively (popovertarget); this keeps the toggle lit while it is open
7// (aria-pressed, like the Inspector toggle), shuts it on Escape, and
8// puts focus somewhere sensible both ways. Every word is the panel's own
9// data-text-* (the catalog's); the summary is the model's sentence and
10// is data, set as text.
11//
12// mountAssist({root, assist, getSlideId, go, hold}) → {destroy()} or
13// null when the page has no panel. assist is assist.mjs's createAssist;
14// go(slideId) shows a slide; hold is the busy rule's least spinner time.
15
16// The busy rule's hold (static/busy.js): a spinner shows for at least
17// this long, so a quick answer reads as a beat rather than a flicker.
18export const HOLD_MS = 650
19
20const camel = s => s.replace(/-([a-z])/g, (m, c) => c.toUpperCase())
21
22export function mountAssist ({ root, assist, getSlideId = () => '', go = () => {}, hold = HOLD_MS }) {
23 const panel = root.querySelector('[slides-assist]')
24 const toggle = root.querySelector('[slides-assist-toggle]')
25 if (!panel || !toggle) return null
26 const form = panel.querySelector('[slides-assist-form]')
27 const askBtn = panel.querySelector('[slides-assist-ask]')
28 const outcome = panel.querySelector('[slides-assist-outcome]')
29 const summaryEl = panel.querySelector('[slides-assist-summary]')
30 const noteEl = panel.querySelector('[slides-assist-note]')
31 const undoBtn = panel.querySelector('[slides-assist-undo]')
32 const field = name => form.elements.namedItem(name)
33 const prompt = field('slides-assist-prompt')
34 const scope = field('slides-assist-scope')
35 const model = field('slides-assist-model')
36 const text = key => panel.dataset[camel('text-' + key)] || ''
37 const abort = new AbortController()
38 const on = (el, type, fn) => el && el.addEventListener(type, fn, { signal: abort.signal })
39 let busy = false
40
41 const max = Number(panel.dataset.maxPrompt) || 0
42 if (max && prompt) prompt.maxLength = max
43
44 function say ({ summary = '', note = '', error = false }) {
45 summaryEl.textContent = summary
46 noteEl.textContent = note
47 if (error) outcome.setAttribute('data-tone', 'error')
48 else outcome.removeAttribute('data-tone')
49 }
50
51 // Busy is aria-disabled, not disabled: a disabled button drops the
52 // focus the person pressed it with onto the page, and the panel's
53 // Escape (and the next Tab) with it. The busy flag is the guard.
54 function setBusy (on) {
55 busy = on
56 if (on) {
57 askBtn.setAttribute('aria-busy', 'true')
58 askBtn.setAttribute('aria-disabled', 'true')
59 const spin = document.createElement('span')
60 spin.setAttribute('rst-spin', '')
61 spin.setAttribute('aria-hidden', 'true')
62 askBtn.append(spin)
63 } else {
64 askBtn.removeAttribute('aria-busy')
65 askBtn.removeAttribute('aria-disabled')
66 const spin = askBtn.querySelector('[rst-spin]')
67 if (spin) spin.remove()
68 }
69 }
70
71 // The panel opening and closing, however it was done.
72 on(panel, 'toggle', e => {
73 const open = e.newState === 'open'
74 toggle.setAttribute('aria-pressed', String(open))
75 if (open) {
76 // "This slide" means nothing on a deck with none.
77 if (!getSlideId() && scope) scope.value = 'deck'
78 if (prompt) prompt.focus()
79 } else if (panel.contains(document.activeElement)) {
80 toggle.focus()
81 }
82 })
83 on(panel, 'keydown', e => {
84 if (e.key !== 'Escape') return
85 e.preventDefault()
86 e.stopPropagation()
87 panel.hidePopover()
88 })
89
90 on(form, 'submit', async e => {
91 e.preventDefault()
92 if (busy) return
93 undoBtn.hidden = true
94 setBusy(true)
95 say({ note: text('working') })
96 const shown = Date.now()
97 let result
98 try {
99 result = await assist.ask({
100 prompt: prompt ? prompt.value : '',
101 scope: scope ? scope.value : 'slide',
102 slide: getSlideId(),
103 model: model ? model.value : ''
104 })
105 } finally {
106 const left = hold - (Date.now() - shown)
107 if (left > 0) await new Promise(resolve => setTimeout(resolve, left))
108 setBusy(false)
109 }
110 if (result.state === 'error') {
111 say({ note: text('error-' + result.word) || text('error-generic'), error: true })
112 if (result.word === 'prompt' && prompt) prompt.focus()
113 return
114 }
115 if (result.state === 'nothing') {
116 say({ summary: result.summary, note: text('nothing') })
117 return
118 }
119 say({ summary: result.summary, note: text('applied') })
120 undoBtn.hidden = !assist.canUndo
121 if (result.focus && result.focus !== getSlideId()) go(result.focus)
122 })
123
124 on(undoBtn, 'click', async () => {
125 undoBtn.hidden = true
126 const undone = await assist.undo()
127 say({ summary: summaryEl.textContent, note: undone ? text('undone') : text('undo-later') })
128 if (prompt) prompt.focus()
129 })
130
131 return { destroy () { abort.abort() } }
132}
133