// suggest.mjs — [[ under the caret (spec §6.6). It offers team titles // and aliases ("Old title of Halden Robotics") and the viewer's own // pending link texts, from /links/suggest unchanged; a typed date, as the // day's note; and last, "Link 'Hal'", a page not written yet. ↵ makes // the chip; "|" closes the list, so shown text can follow. Backspace // into a chip turns it back into the [[text it was typed as. import { Plugin, PluginKey } from 'prosemirror-state' import { Combobox } from './combobox.mjs' import { isDate } from './markup.mjs' import { dayLabel, linkKey } from './resolutions.mjs' export const suggestKey = new PluginKey('mapa-suggest') const trigger = /\[\[([^[\]\n|#]{0,80})$/u // typing is the [[ query before the caret, or null. export function typing (state) { const { $from, empty } = state.selection if (!empty || $from.parent.type.spec.code || !$from.parent.inlineContent) return null const before = $from.parent.textBetween(0, $from.parentOffset, null, '') const match = trigger.exec(before) return match ? { text: match[1], from: $from.pos - match[0].length, to: $from.pos } : null } function chip (schema, attrs) { return isDate(attrs.target) ? schema.nodes.datelink.create({ date: attrs.target }) : schema.nodes.wikilink.create(attrs) } function insert (found, target) { return view => { const query = typing(view.state) if (!query) return const node = chip(view.state.schema, { target }) view.dispatch(view.state.tr.replaceWith(query.from, query.to, node).scrollIntoView()) } } const fold = text => text.normalize('NFKD').replace(/\p{Mn}/gu, '').toLowerCase().trim().replace(/\s+/gu, ' ') // rows are the list for a query and the server's answers. make(target) // is what choosing a row runs: for [[, the chip in place of the typed // query; the bubble's Page link passes its own (pagelink.mjs). export function rows (query, suggestions, states, make = target => insert(query, target)) { const text = query.text.trim() const out = [] if (isDate(text)) out.push({ label: dayLabel(text), detail: 'Your note', run: make(text) }) for (const { text: title, pending } of suggestions) { const state = states[linkKey(title, '')] let detail = 'Team page' if (pending) detail = 'Not written yet' else if (state?.alias) detail = `Old title of ${state.title}` out.push({ label: title, detail, run: make(title) }) } if (text !== '' && !isDate(text) && !suggestions.some(row => fold(row.text) === fold(text))) { out.push({ label: `Link “${text}”`, detail: 'a page not written yet', run: make(text) }) } return out } // unchip is Backspace into a chip: its [[text again, so the list opens. export function unchip (state, dispatch) { const { $from, empty } = state.selection const before = $from.nodeBefore if (!empty || !before || !['wikilink', 'datelink'].includes(before.type.name)) return false if (dispatch) { const attrs = before.attrs const target = attrs.target ?? attrs.date const text = `[[${target}${attrs.anchor ? `#^${attrs.anchor}` : ''}${attrs.shown ? `|${attrs.shown}` : ''}` dispatch(state.tr.replaceWith($from.pos - before.nodeSize, $from.pos, state.schema.text(text))) } return true } export function suggestPlugin ({ ask, resolutions }) { let box = null let asked = 0 const count = items => items.length === 1 ? '1 page' : `${items.length} pages` async function refresh (view, query) { const mine = ++asked let suggestions = [] try { suggestions = await ask(query.text) } catch {} const states = await resolutions.lookup(suggestions.map(row => row.text)) if (mine !== asked) return const now = typing(view.state) if (!now || now.from !== query.from) return box.show(rows(now, suggestions, states), now.from) } return new Plugin({ key: suggestKey, view (view) { box = new Combobox(view, { label: 'Pages to link', count }) let timer = null return { update (view, previous) { if (view.state.doc.eq(previous.doc) && view.state.selection.eq(previous.selection)) return const query = typing(view.state) clearTimeout(timer) if (!query) { asked++ box.close() return } timer = setTimeout(() => refresh(view, query), 30) }, destroy () { box.destroy() } } }, props: { handleKeyDown (view, event) { return box?.keys(event) ?? false }, handleDOMEvents: { blur () { box?.close(); return false } } } }) }