oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// slash.mjs — the / menu (spec §6.6): at the start of an empty block, or
2// after a space, "/" offers the blocks Insert has, each with the
3// Markdown it writes, and filters them as you type. The Markdown
4// shortcuts work too; this is the dependable path on a phone, where
5// smart punctuation changes what is typed (§6.11). Date opens the date
6// picker (build note 83); Today's date puts today in at once. Image opens
7// the file picker (images §4.1).
8
9import { Plugin } from 'prosemirror-state'
10import { Combobox } from './combobox.mjs'
11import { blockCommands } from './commands.mjs'
12import { pickDate, insertToday } from './datepicker.mjs'
13
14const trigger = /(?:^|\s)\/([A-Za-z0-9 ]{0,24})$/u
15
16export const blocks = [
17 ['Heading 1', '#', 'insert-h1'], ['Heading 2', '##', 'insert-h2'],
18 ['Heading 3', '###', 'insert-h3'], ['List', '-', 'insert-list'],
19 ['Numbered', '1.', 'insert-numbered'], ['Checklist', '- [ ]', 'insert-check'],
20 ['Quote', '>', 'insert-quote'], ['Code', '```', 'insert-code'],
21 ['Divider', '---', 'insert-rule'], ['Columns', 'two side by side', 'insert-columns-2'],
22 ['Link to a page', '[[', 'insert-link'],
23 ['Date', 'choose a day', 'insert-date-pick'], ['Today’s date', 'today’s note', 'insert-date'],
24 ['Image', 'upload an image', 'insert-image']
25]
26
27function slashing (state) {
28 const { $from, empty } = state.selection
29 if (!empty || $from.parent.type.spec.code || !$from.parent.inlineContent) return null
30 const before = $from.parent.textBetween(0, $from.parentOffset, null, '')
31 const match = trigger.exec(before)
32 if (!match) return null
33 return { text: match[1].toLowerCase().trim(), from: $from.pos - match[1].length - 1, to: $from.pos }
34}
35
36// extra is commands the page brings: Image's file picker (images §4.1).
37export function slashPlugin (extra = {}) {
38 let box = null
39 return new Plugin({
40 view (view) {
41 const commands = { ...blockCommands(view.state.schema), 'insert-date-pick': pickDate, 'insert-date': insertToday, ...extra }
42 box = new Combobox(view, {
43 label: 'Blocks', count: items => items.length === 1 ? '1 block' : `${items.length} blocks`
44 })
45 return {
46 update (view, previous) {
47 if (view.state.doc.eq(previous.doc) && view.state.selection.eq(previous.selection)) return
48 const query = slashing(view.state)
49 if (!query) {
50 box.close()
51 return
52 }
53 const items = blocks
54 .filter(([label, , name]) => commands[name] && label.toLowerCase().includes(query.text))
55 .map(([label, detail, name]) => ({
56 label, detail,
57 run: view => {
58 const now = slashing(view.state)
59 if (!now) return
60 view.dispatch(view.state.tr.delete(now.from, now.to))
61 commands[name](view.state, view.dispatch, view)
62 }
63 }))
64 box.show(items, query.from)
65 },
66 destroy () { box.destroy() }
67 }
68 },
69 props: {
70 handleKeyDown (view, event) { return box?.keys(event) ?? false },
71 handleDOMEvents: { blur () { box?.close(); return false } }
72 }
73 })
74}
75