| 1 | // bubble.mjs — the selection bubble (spec §6.6, §6.11): bold, italic, |
| 2 | // code, Page link and Web link over a selection, then Make team page on |
| 3 | // a private note. It sits above the selection, or below it on a coarse |
| 4 | // pointer, clear of iOS's own callout, and above the keyboard. |
| 5 | // |
| 6 | // Page link (⌘L, build note 81) opens a field holding the selected |
| 7 | // words, with the same pages [[ offers; choosing one makes the words a |
| 8 | // chip, [[Target|words]]. Web link (⌘⇧K) opens the address field. Two |
| 9 | // fields, not one guessing: "example.com" is as good a page title as an |
| 10 | // address. A URL pasted over a selection makes a web link (linkPaste). |
| 11 | |
| 12 | import { Plugin, PluginKey, TextSelection } from 'prosemirror-state' |
| 13 | import { toggleMark } from 'prosemirror-commands' |
| 14 | import { safeURL } from './markup.mjs' |
| 15 | import { Combobox } from './combobox.mjs' |
| 16 | import { rows } from './suggest.mjs' |
| 17 | import { linkable, linkCheck, pageLinkSource } from './pagelink.mjs' |
| 18 | |
| 19 | export const bubbleKey = new PluginKey('mapa-bubble') |
| 20 | |
| 21 | // openers is each view's Page link, for ⌘L, the Insert menu and the |
| 22 | // phone's row (pagelink.mjs's linkPage). |
| 23 | const openers = new WeakMap() |
| 24 | |
| 25 | // openPageLink opens the bubble's Page link over found (linkable's). |
| 26 | export function openPageLink (view, found) { |
| 27 | openers.get(view)?.(found) |
| 28 | } |
| 29 | |
| 30 | function element (tag, attrs = {}, text = '') { |
| 31 | const node = document.createElement(tag) |
| 32 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 33 | if (text) node.textContent = text |
| 34 | return node |
| 35 | } |
| 36 | |
| 37 | function markActive (state, type) { |
| 38 | const { from, to, empty, $from } = state.selection |
| 39 | return empty ? Boolean(type.isInSet(state.storedMarks || $from.marks())) : state.doc.rangeHasMark(from, to, type) |
| 40 | } |
| 41 | |
| 42 | // setLink links the selection to href, or unlinks it with none. |
| 43 | export function setLink (href) { |
| 44 | return (state, dispatch) => { |
| 45 | const { from, to, empty } = state.selection |
| 46 | const type = state.schema.marks.link |
| 47 | if (empty) return false |
| 48 | if (dispatch) { |
| 49 | const tr = state.tr.removeMark(from, to, type) |
| 50 | if (href) tr.addMark(from, to, type.create({ href })) |
| 51 | dispatch(tr) |
| 52 | } |
| 53 | return true |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function showing (state) { |
| 58 | const { selection } = state |
| 59 | return selection instanceof TextSelection && !selection.empty && |
| 60 | !selection.$from.parent.type.spec.code |
| 61 | } |
| 62 | |
| 63 | const pageCount = items => items.length === 1 ? '1 page' : `${items.length} pages` |
| 64 | |
| 65 | // pageLink is the bubble's Page link: its field, its list, and the |
| 66 | // line that says why a selection cannot be linked. |
| 67 | function pageLink (view, bar, { ask, resolutions, back }) { |
| 68 | const field = element('input', { |
| 69 | type: 'text', role: 'combobox', 'aria-autocomplete': 'list', 'aria-expanded': 'false', |
| 70 | 'aria-label': 'Page to link to', placeholder: 'Find a page', autocomplete: 'off', |
| 71 | 'mapa-bubble-page': '' |
| 72 | }) |
| 73 | field.hidden = true |
| 74 | const note = element('p', { role: 'status', 'mapa-bubble-note': '' }) |
| 75 | bar.append(field, note) |
| 76 | const box = new Combobox(view, { label: 'Pages to link to', count: pageCount, input: field }) |
| 77 | let words = '' |
| 78 | let asked = 0 |
| 79 | let timer = null |
| 80 | const close = () => { |
| 81 | asked++ |
| 82 | clearTimeout(timer) |
| 83 | box.close() |
| 84 | field.hidden = true |
| 85 | } |
| 86 | // choose links the words, or says why not (review low 2): the page |
| 87 | // changed under the field, or the link would not read back. |
| 88 | const choose = target => view => { |
| 89 | const { tr, refuse } = linkCheck(view.state, target, words) |
| 90 | close() |
| 91 | if (tr) view.dispatch(tr) |
| 92 | else note.textContent = refuse |
| 93 | back() |
| 94 | } |
| 95 | // make is a row's action, or null for a target the words cannot be |
| 96 | // linked to as typed (a # or | in a page not written yet). |
| 97 | const make = target => pageLinkSource(target, target === words ? '' : words) ? choose(target) : null |
| 98 | async function refresh () { |
| 99 | const mine = ++asked |
| 100 | const text = field.value |
| 101 | let suggestions = [] |
| 102 | try { suggestions = await ask(text) } catch {} |
| 103 | const states = resolutions ? await resolutions.lookup(suggestions.map(row => row.text)) : {} |
| 104 | if (mine !== asked || field.hidden) return |
| 105 | box.show(rows({ text }, suggestions, states, make).filter(item => item.run), 0) |
| 106 | } |
| 107 | field.addEventListener('input', () => { |
| 108 | clearTimeout(timer) |
| 109 | timer = setTimeout(refresh, 30) |
| 110 | }) |
| 111 | field.addEventListener('keydown', event => { |
| 112 | if (event.isComposing) return |
| 113 | if (event.key === 'Escape') { |
| 114 | event.preventDefault() |
| 115 | close() |
| 116 | back() |
| 117 | return |
| 118 | } |
| 119 | if (box.keys(event)) { |
| 120 | event.preventDefault() |
| 121 | return |
| 122 | } |
| 123 | if (event.key === 'Enter') event.preventDefault() |
| 124 | }) |
| 125 | field.addEventListener('blur', () => box.close()) |
| 126 | return { |
| 127 | open (found) { |
| 128 | note.textContent = '' |
| 129 | if (found.refuse) { |
| 130 | close() |
| 131 | note.textContent = found.refuse |
| 132 | return false |
| 133 | } |
| 134 | words = found.words |
| 135 | field.hidden = false |
| 136 | field.value = words |
| 137 | field.focus() |
| 138 | field.select() |
| 139 | refresh() |
| 140 | return true |
| 141 | }, |
| 142 | close, |
| 143 | // quiet takes the note away when the selection moves on. |
| 144 | quiet () { note.textContent = '' }, |
| 145 | // saying is whether the note is up: the bubble stays to show it, |
| 146 | // even over a selection it has no buttons for (⌘A, review low 1). |
| 147 | get saying () { return note.textContent !== '' }, |
| 148 | destroy () { box.destroy() } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | export function bubblePlugin ({ makeTeamPage = null, ask = async () => [], resolutions = null } = {}) { |
| 153 | let bar = null |
| 154 | let field = null |
| 155 | return new Plugin({ |
| 156 | key: bubbleKey, |
| 157 | view (view) { |
| 158 | const schema = view.state.schema |
| 159 | bar = element('div', { 'mapa-bubble': '', role: 'toolbar', 'aria-label': 'Format' }) |
| 160 | bar.hidden = true |
| 161 | const fields = () => [...bar.querySelectorAll('input')] |
| 162 | bar.addEventListener('mousedown', event => { |
| 163 | if (!fields().includes(event.target)) event.preventDefault() |
| 164 | }) |
| 165 | // The toolbar's own keys: arrows move between its buttons, Esc goes |
| 166 | // back to the text. |
| 167 | bar.addEventListener('keydown', event => { |
| 168 | if (fields().includes(event.target)) return |
| 169 | const all = [...bar.querySelectorAll('button')] |
| 170 | const at = all.indexOf(document.activeElement) |
| 171 | if (event.key === 'Escape') { |
| 172 | event.preventDefault() |
| 173 | view.focus() |
| 174 | } else if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') { |
| 175 | event.preventDefault() |
| 176 | all[(at + (event.key === 'ArrowRight' ? 1 : -1) + all.length) % all.length]?.focus() |
| 177 | } |
| 178 | }) |
| 179 | const buttons = [] |
| 180 | const add = (label, text, run, { mark = null, keeps = false, title = '' } = {}) => { |
| 181 | const button = element('button', { type: 'button', 'aria-label': label, 'mapa-bubble-button': '' }, text) |
| 182 | if (title) button.title = title |
| 183 | button.addEventListener('click', () => { |
| 184 | run(view) |
| 185 | if (!keeps) view.focus() |
| 186 | }) |
| 187 | bar.append(button) |
| 188 | buttons.push({ button, mark }) |
| 189 | return button |
| 190 | } |
| 191 | add('Bold', 'B', view => toggleMark(schema.marks.strong)(view.state, view.dispatch), { mark: schema.marks.strong }) |
| 192 | add('Italic', 'I', view => toggleMark(schema.marks.em)(view.state, view.dispatch), { mark: schema.marks.em }) |
| 193 | add('Code', '<>', view => toggleMark(schema.marks.code)(view.state, view.dispatch), { mark: schema.marks.code }) |
| 194 | add('Page link', 'Page link', view => openPage(linkable(view.state)), { keeps: true, title: 'Link to a page (⌘L)' }) |
| 195 | add('Web link', 'Web link', () => openLink(), { mark: schema.marks.link, keeps: true, title: 'Link to a web address (⇧⌘K)' }) |
| 196 | if (makeTeamPage) add('Make team page', 'Make team page', view => makeTeamPage(view), { keeps: true }) |
| 197 | field = element('input', { type: 'url', 'aria-label': 'Link address', placeholder: 'Paste a link', 'mapa-bubble-field': '' }) |
| 198 | field.hidden = true |
| 199 | bar.append(field) |
| 200 | document.body.append(bar) |
| 201 | const page = pageLink(view, bar, { ask, resolutions, back: () => view.focus() }) |
| 202 | field.addEventListener('keydown', event => { |
| 203 | if (event.key === 'Enter') { |
| 204 | event.preventDefault() |
| 205 | const href = field.value.trim() |
| 206 | if (href === '' || safeURL(href)) setLink(href)(view.state, view.dispatch) |
| 207 | closeLink() |
| 208 | } else if (event.key === 'Escape') { |
| 209 | event.preventDefault() |
| 210 | closeLink() |
| 211 | } |
| 212 | }) |
| 213 | function openLink () { |
| 214 | page.close() |
| 215 | page.quiet() |
| 216 | field.hidden = false |
| 217 | field.value = '' |
| 218 | field.focus() |
| 219 | } |
| 220 | function closeLink () { |
| 221 | field.hidden = true |
| 222 | view.focus() |
| 223 | } |
| 224 | function openPage (found) { |
| 225 | if (!found) return |
| 226 | field.hidden = true |
| 227 | bar.hidden = false |
| 228 | place() |
| 229 | if (!page.open(found)) view.focus() |
| 230 | place() |
| 231 | } |
| 232 | bar.openLink = openLink |
| 233 | openers.set(view, openPage) |
| 234 | const place = () => { |
| 235 | const shown = showing(view.state) || (page.saying && !view.state.selection.empty) |
| 236 | if (!shown || (!view.hasFocus() && !bar.contains(document.activeElement))) { |
| 237 | bar.hidden = true |
| 238 | field.hidden = true |
| 239 | page.close() |
| 240 | return |
| 241 | } |
| 242 | for (const { button, mark } of buttons) { |
| 243 | if (mark) button.setAttribute('aria-pressed', markActive(view.state, mark) ? 'true' : 'false') |
| 244 | } |
| 245 | bar.hidden = false |
| 246 | const { from, to } = view.state.selection |
| 247 | const start = view.coordsAtPos(from) |
| 248 | const end = view.coordsAtPos(to) |
| 249 | const coarse = window.matchMedia('(pointer: coarse)').matches |
| 250 | const top = coarse ? end.bottom + 12 : start.top - bar.offsetHeight - 8 |
| 251 | const left = Math.max(4, Math.min((start.left + end.left) / 2 - bar.offsetWidth / 2, |
| 252 | window.innerWidth - bar.offsetWidth - 4)) |
| 253 | bar.style.setProperty('top', `${Math.max(top, 4)}px`) |
| 254 | bar.style.setProperty('left', `${left}px`) |
| 255 | } |
| 256 | const blurred = () => setTimeout(place, 0) |
| 257 | view.dom.addEventListener('blur', blurred) |
| 258 | bar.addEventListener('focusout', blurred) |
| 259 | return { |
| 260 | update (view, previous) { |
| 261 | if (!view.state.selection.eq(previous.selection)) page.quiet() |
| 262 | place() |
| 263 | }, |
| 264 | destroy () { |
| 265 | view.dom.removeEventListener('blur', blurred) |
| 266 | openers.delete(view) |
| 267 | page.destroy() |
| 268 | bar.remove() |
| 269 | } |
| 270 | } |
| 271 | }, |
| 272 | props: { |
| 273 | handleKeyDown (view, event) { |
| 274 | // ⌥F10 moves to the toolbar, as in most editors; Esc comes back. |
| 275 | if (event.key === 'F10' && event.altKey && bar && !bar.hidden) { |
| 276 | event.preventDefault() |
| 277 | bar.querySelector('button')?.focus() |
| 278 | return true |
| 279 | } |
| 280 | if (event.key.toLowerCase() === 'k' && event.shiftKey && (event.metaKey || event.ctrlKey) && showing(view.state)) { |
| 281 | event.preventDefault() |
| 282 | bar?.openLink() |
| 283 | return true |
| 284 | } |
| 285 | return false |
| 286 | } |
| 287 | } |
| 288 | }) |
| 289 | } |
| 290 | |
| 291 | // linkPaste: a URL pasted over selected text links the text. |
| 292 | export function linkPaste () { |
| 293 | return new Plugin({ |
| 294 | props: { |
| 295 | handlePaste (view, event) { |
| 296 | const text = event.clipboardData?.getData('text/plain')?.trim() ?? '' |
| 297 | if (view.state.selection.empty || !/^https?:\/\/\S+$/u.test(text) || !safeURL(text)) return false |
| 298 | return setLink(text)(view.state, view.dispatch) |
| 299 | } |
| 300 | } |
| 301 | }) |
| 302 | } |
| 303 | |