// imagebubble.mjs — a selected image's bubble (images spec §4.3, §4.6), // and the live region. The bubble offers Alt text, Replace…, Open full // size and Remove from Mapa. Alt text opens the description field laid // over the image itself (altinset.mjs, build note I22). import { Plugin, NodeSelection } from 'prosemirror-state' import { replaceImage, acceptList } from './upload.mjs' import { describeAt } from './imageview.mjs' import { ImagePrefix } from './markup.mjs' function element (tag, attrs = {}, text = '') { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) if (text) node.textContent = text return node } // live is the page's polite live region (§4.6). export function live () { let region = document.querySelector('[mapa-live]') if (!region) { region = element('div', { 'mapa-live': '', 'aria-live': 'polite', role: 'status', 'mapa-sr': '' }) document.body.append(region) } return text => { region.textContent = '' setTimeout(() => { region.textContent = text }, 30) } } function selectedImage (state) { const { selection } = state return selection instanceof NodeSelection && selection.node.type.name === 'image' ? { node: selection.node, pos: selection.from } : null } // replace swaps a selected image for a chosen file: the old one stays // until the new one is in, and the alt is kept (upload.mjs, Fable M4). function replace (view, hooks) { const found = selectedImage(view.state) if (!found) return const input = element('input', { type: 'file', accept: acceptList, 'mapa-image-input': '' }) input.hidden = true input.addEventListener('change', () => { const [file] = input.files input.remove() if (!file) return // Where the chosen image is now: the picker may have been open while // the text moved. const id = found.node.attrs.id let at = view.state.doc.nodeAt(found.pos)?.attrs.id === id ? found.pos : null if (at === null) { view.state.doc.descendants((node, pos) => { if (at === null && node.type.name === 'image' && node.attrs.id === id) at = pos }) } if (at !== null) replaceImage(view, at, file, hooks, found.node.attrs.id) }) document.body.append(input) input.click() } export function imageBubblePlugin (hooks) { return new Plugin({ view (view) { // Styled as the text's bubble, but not [mapa-bubble]: that one is // the selection's, and the page finds it by that name. const bar = element('div', { 'mapa-image-bubble': '', role: 'toolbar', 'aria-label': 'Image' }) bar.hidden = true bar.addEventListener('mousedown', event => event.preventDefault()) const add = (label, run) => { const button = element('button', { type: 'button' }, label) button.addEventListener('click', () => run()) bar.append(button) } const id = () => selectedImage(view.state)?.node.attrs.id add('Alt text', () => { const found = selectedImage(view.state) if (found) describeAt(view, found.pos, 'bubble') }) add('Replace…', () => replace(view, hooks)) add('Open full size', () => { if (id()) window.open(ImagePrefix + id(), '_blank', 'noopener') }) // The confirm page returns here: read when leaving, after the // commit, which may have given a new page its address. add('Remove from Mapa', () => { const chosen = id() if (chosen) hooks.leave(() => `${ImagePrefix}${chosen}/remove?back=${encodeURIComponent(location.pathname)}`) }) bar.addEventListener('keydown', event => { const all = [...bar.querySelectorAll('button')] const at = all.indexOf(document.activeElement) if (event.key === 'Escape') { event.preventDefault() view.focus() } else if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') { event.preventDefault() all[(at + (event.key === 'ArrowRight' ? 1 : -1) + all.length) % all.length]?.focus() } }) document.body.append(bar) const place = () => { const found = selectedImage(view.state) if (!found || (!view.hasFocus() && !bar.contains(document.activeElement))) { bar.hidden = true return } bar.hidden = false const box = view.nodeDOM(found.pos)?.getBoundingClientRect?.() if (!box) return bar.style.setProperty('top', `${Math.max(4, box.top - bar.offsetHeight - 8)}px`) bar.style.setProperty('left', `${Math.max(4, box.left)}px`) } const blurred = () => setTimeout(place, 0) view.dom.addEventListener('blur', blurred) bar.addEventListener('focusout', blurred) return { update: place, destroy () { view.dom.removeEventListener('blur', blurred) bar.remove() } } }, props: { handleKeyDown (view, event) { // ⌥F10 moves to the image's bubble, as to the text's. if (event.key === 'F10' && event.altKey && selectedImage(view.state)) { const bar = document.querySelector('[mapa-image-bubble]') if (bar && !bar.hidden) { event.preventDefault() bar.querySelector('button')?.focus() return true } } return false } } }) }