| 1 | // suggest.mjs — [[ under the caret (spec §6.6). It offers team titles |
| 2 | // and aliases ("Old title of Halden Robotics") and the viewer's own |
| 3 | // pending link texts, from /links/suggest unchanged; a typed date, as the |
| 4 | // day's note; and last, "Link 'Hal'", a page not written yet. ↵ makes |
| 5 | // the chip; "|" closes the list, so shown text can follow. Backspace |
| 6 | // into a chip turns it back into the [[text it was typed as. |
| 7 | |
| 8 | import { Plugin, PluginKey } from 'prosemirror-state' |
| 9 | import { Combobox } from './combobox.mjs' |
| 10 | import { isDate } from './markup.mjs' |
| 11 | import { dayLabel, linkKey } from './resolutions.mjs' |
| 12 | |
| 13 | export const suggestKey = new PluginKey('mapa-suggest') |
| 14 | const trigger = /\[\[([^[\]\n|#]{0,80})$/u |
| 15 | |
| 16 | // typing is the [[ query before the caret, or null. |
| 17 | export function typing (state) { |
| 18 | const { $from, empty } = state.selection |
| 19 | if (!empty || $from.parent.type.spec.code || !$from.parent.inlineContent) return null |
| 20 | const before = $from.parent.textBetween(0, $from.parentOffset, null, '') |
| 21 | const match = trigger.exec(before) |
| 22 | return match ? { text: match[1], from: $from.pos - match[0].length, to: $from.pos } : null |
| 23 | } |
| 24 | |
| 25 | function chip (schema, attrs) { |
| 26 | return isDate(attrs.target) |
| 27 | ? schema.nodes.datelink.create({ date: attrs.target }) |
| 28 | : schema.nodes.wikilink.create(attrs) |
| 29 | } |
| 30 | |
| 31 | function insert (found, target) { |
| 32 | return view => { |
| 33 | const query = typing(view.state) |
| 34 | if (!query) return |
| 35 | const node = chip(view.state.schema, { target }) |
| 36 | view.dispatch(view.state.tr.replaceWith(query.from, query.to, node).scrollIntoView()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | const fold = text => text.normalize('NFKD').replace(/\p{Mn}/gu, '').toLowerCase().trim().replace(/\s+/gu, ' ') |
| 41 | |
| 42 | // rows are the list for a query and the server's answers. make(target) |
| 43 | // is what choosing a row runs: for [[, the chip in place of the typed |
| 44 | // query; the bubble's Page link passes its own (pagelink.mjs). |
| 45 | export function rows (query, suggestions, states, make = target => insert(query, target)) { |
| 46 | const text = query.text.trim() |
| 47 | const out = [] |
| 48 | if (isDate(text)) out.push({ label: dayLabel(text), detail: 'Your note', run: make(text) }) |
| 49 | for (const { text: title, pending } of suggestions) { |
| 50 | const state = states[linkKey(title, '')] |
| 51 | let detail = 'Team page' |
| 52 | if (pending) detail = 'Not written yet' |
| 53 | else if (state?.alias) detail = `Old title of ${state.title}` |
| 54 | out.push({ label: title, detail, run: make(title) }) |
| 55 | } |
| 56 | if (text !== '' && !isDate(text) && !suggestions.some(row => fold(row.text) === fold(text))) { |
| 57 | out.push({ label: `Link “${text}”`, detail: 'a page not written yet', run: make(text) }) |
| 58 | } |
| 59 | return out |
| 60 | } |
| 61 | |
| 62 | // unchip is Backspace into a chip: its [[text again, so the list opens. |
| 63 | export function unchip (state, dispatch) { |
| 64 | const { $from, empty } = state.selection |
| 65 | const before = $from.nodeBefore |
| 66 | if (!empty || !before || !['wikilink', 'datelink'].includes(before.type.name)) return false |
| 67 | if (dispatch) { |
| 68 | const attrs = before.attrs |
| 69 | const target = attrs.target ?? attrs.date |
| 70 | const text = `[[${target}${attrs.anchor ? `#^${attrs.anchor}` : ''}${attrs.shown ? `|${attrs.shown}` : ''}` |
| 71 | dispatch(state.tr.replaceWith($from.pos - before.nodeSize, $from.pos, state.schema.text(text))) |
| 72 | } |
| 73 | return true |
| 74 | } |
| 75 | |
| 76 | export function suggestPlugin ({ ask, resolutions }) { |
| 77 | let box = null |
| 78 | let asked = 0 |
| 79 | const count = items => items.length === 1 ? '1 page' : `${items.length} pages` |
| 80 | async function refresh (view, query) { |
| 81 | const mine = ++asked |
| 82 | let suggestions = [] |
| 83 | try { suggestions = await ask(query.text) } catch {} |
| 84 | const states = await resolutions.lookup(suggestions.map(row => row.text)) |
| 85 | if (mine !== asked) return |
| 86 | const now = typing(view.state) |
| 87 | if (!now || now.from !== query.from) return |
| 88 | box.show(rows(now, suggestions, states), now.from) |
| 89 | } |
| 90 | return new Plugin({ |
| 91 | key: suggestKey, |
| 92 | view (view) { |
| 93 | box = new Combobox(view, { label: 'Pages to link', count }) |
| 94 | let timer = null |
| 95 | return { |
| 96 | update (view, previous) { |
| 97 | if (view.state.doc.eq(previous.doc) && view.state.selection.eq(previous.selection)) return |
| 98 | const query = typing(view.state) |
| 99 | clearTimeout(timer) |
| 100 | if (!query) { |
| 101 | asked++ |
| 102 | box.close() |
| 103 | return |
| 104 | } |
| 105 | timer = setTimeout(() => refresh(view, query), 30) |
| 106 | }, |
| 107 | destroy () { box.destroy() } |
| 108 | } |
| 109 | }, |
| 110 | props: { |
| 111 | handleKeyDown (view, event) { return box?.keys(event) ?? false }, |
| 112 | handleDOMEvents: { blur () { box?.close(); return false } } |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | |