| 1 | // imagebubble.mjs — a selected image's bubble (images spec §4.3, §4.6), |
| 2 | // and the live region. The bubble offers Alt text, Replace…, Open full |
| 3 | // size and Remove from Mapa. Alt text opens the description field laid |
| 4 | // over the image itself (altinset.mjs, build note I22). |
| 5 | |
| 6 | import { Plugin, NodeSelection } from 'prosemirror-state' |
| 7 | import { replaceImage, acceptList } from './upload.mjs' |
| 8 | import { describeAt } from './imageview.mjs' |
| 9 | import { ImagePrefix } from './markup.mjs' |
| 10 | |
| 11 | function element (tag, attrs = {}, text = '') { |
| 12 | const node = document.createElement(tag) |
| 13 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 14 | if (text) node.textContent = text |
| 15 | return node |
| 16 | } |
| 17 | |
| 18 | // live is the page's polite live region (§4.6). |
| 19 | export function live () { |
| 20 | let region = document.querySelector('[mapa-live]') |
| 21 | if (!region) { |
| 22 | region = element('div', { 'mapa-live': '', 'aria-live': 'polite', role: 'status', 'mapa-sr': '' }) |
| 23 | document.body.append(region) |
| 24 | } |
| 25 | return text => { |
| 26 | region.textContent = '' |
| 27 | setTimeout(() => { region.textContent = text }, 30) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | function selectedImage (state) { |
| 32 | const { selection } = state |
| 33 | return selection instanceof NodeSelection && selection.node.type.name === 'image' |
| 34 | ? { node: selection.node, pos: selection.from } |
| 35 | : null |
| 36 | } |
| 37 | |
| 38 | // replace swaps a selected image for a chosen file: the old one stays |
| 39 | // until the new one is in, and the alt is kept (upload.mjs, Fable M4). |
| 40 | function replace (view, hooks) { |
| 41 | const found = selectedImage(view.state) |
| 42 | if (!found) return |
| 43 | const input = element('input', { type: 'file', accept: acceptList, 'mapa-image-input': '' }) |
| 44 | input.hidden = true |
| 45 | input.addEventListener('change', () => { |
| 46 | const [file] = input.files |
| 47 | input.remove() |
| 48 | if (!file) return |
| 49 | // Where the chosen image is now: the picker may have been open while |
| 50 | // the text moved. |
| 51 | const id = found.node.attrs.id |
| 52 | let at = view.state.doc.nodeAt(found.pos)?.attrs.id === id ? found.pos : null |
| 53 | if (at === null) { |
| 54 | view.state.doc.descendants((node, pos) => { |
| 55 | if (at === null && node.type.name === 'image' && node.attrs.id === id) at = pos |
| 56 | }) |
| 57 | } |
| 58 | if (at !== null) replaceImage(view, at, file, hooks, found.node.attrs.id) |
| 59 | }) |
| 60 | document.body.append(input) |
| 61 | input.click() |
| 62 | } |
| 63 | |
| 64 | export function imageBubblePlugin (hooks) { |
| 65 | return new Plugin({ |
| 66 | view (view) { |
| 67 | // Styled as the text's bubble, but not [mapa-bubble]: that one is |
| 68 | // the selection's, and the page finds it by that name. |
| 69 | const bar = element('div', { 'mapa-image-bubble': '', role: 'toolbar', 'aria-label': 'Image' }) |
| 70 | bar.hidden = true |
| 71 | bar.addEventListener('mousedown', event => event.preventDefault()) |
| 72 | const add = (label, run) => { |
| 73 | const button = element('button', { type: 'button' }, label) |
| 74 | button.addEventListener('click', () => run()) |
| 75 | bar.append(button) |
| 76 | } |
| 77 | const id = () => selectedImage(view.state)?.node.attrs.id |
| 78 | add('Alt text', () => { |
| 79 | const found = selectedImage(view.state) |
| 80 | if (found) describeAt(view, found.pos, 'bubble') |
| 81 | }) |
| 82 | add('Replace…', () => replace(view, hooks)) |
| 83 | add('Open full size', () => { if (id()) window.open(ImagePrefix + id(), '_blank', 'noopener') }) |
| 84 | // The confirm page returns here: read when leaving, after the |
| 85 | // commit, which may have given a new page its address. |
| 86 | add('Remove from Mapa', () => { |
| 87 | const chosen = id() |
| 88 | if (chosen) hooks.leave(() => `${ImagePrefix}${chosen}/remove?back=${encodeURIComponent(location.pathname)}`) |
| 89 | }) |
| 90 | bar.addEventListener('keydown', event => { |
| 91 | const all = [...bar.querySelectorAll('button')] |
| 92 | const at = all.indexOf(document.activeElement) |
| 93 | if (event.key === 'Escape') { |
| 94 | event.preventDefault() |
| 95 | view.focus() |
| 96 | } else if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') { |
| 97 | event.preventDefault() |
| 98 | all[(at + (event.key === 'ArrowRight' ? 1 : -1) + all.length) % all.length]?.focus() |
| 99 | } |
| 100 | }) |
| 101 | document.body.append(bar) |
| 102 | const place = () => { |
| 103 | const found = selectedImage(view.state) |
| 104 | if (!found || (!view.hasFocus() && !bar.contains(document.activeElement))) { |
| 105 | bar.hidden = true |
| 106 | return |
| 107 | } |
| 108 | bar.hidden = false |
| 109 | const box = view.nodeDOM(found.pos)?.getBoundingClientRect?.() |
| 110 | if (!box) return |
| 111 | bar.style.setProperty('top', `${Math.max(4, box.top - bar.offsetHeight - 8)}px`) |
| 112 | bar.style.setProperty('left', `${Math.max(4, box.left)}px`) |
| 113 | } |
| 114 | const blurred = () => setTimeout(place, 0) |
| 115 | view.dom.addEventListener('blur', blurred) |
| 116 | bar.addEventListener('focusout', blurred) |
| 117 | return { |
| 118 | update: place, |
| 119 | destroy () { |
| 120 | view.dom.removeEventListener('blur', blurred) |
| 121 | bar.remove() |
| 122 | } |
| 123 | } |
| 124 | }, |
| 125 | props: { |
| 126 | handleKeyDown (view, event) { |
| 127 | // ⌥F10 moves to the image's bubble, as to the text's. |
| 128 | if (event.key === 'F10' && event.altKey && selectedImage(view.state)) { |
| 129 | const bar = document.querySelector('[mapa-image-bubble]') |
| 130 | if (bar && !bar.hidden) { |
| 131 | event.preventDefault() |
| 132 | bar.querySelector('button')?.focus() |
| 133 | return true |
| 134 | } |
| 135 | } |
| 136 | return false |
| 137 | } |
| 138 | } |
| 139 | }) |
| 140 | } |
| 141 | |