oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1// commands.mjs — the blocks the Insert menu, the / menu and the phone's
2// accessory row make (spec §3.4, §6.6), and the marks. Each is a
3// ProseMirror command; what it makes is a block Mapa's subset can say,
4// so the settle plugin finds nothing to change.
5
6import { undo, redo } from 'prosemirror-history'
7import { setBlockType, wrapIn, toggleMark, chainCommands, exitCode } from 'prosemirror-commands'
8import { wrapInList } from 'prosemirror-schema-list'
9import { TextSelection } from 'prosemirror-state'
10import { readsBack, refusals } from './pagelink.mjs'
11import { columnCommands } from './columns.mjs'
12
13// today is the date it is now, ISO, in the zone "today" uses for the
14// viewer: their own, else the team's (<body data-zone>, the zone offer's
15// too). It is read each time it is asked, so a page left open past
16// midnight moves on. Without a zone it is the browser's date.
17export function today (now = new Date(), zone = viewerZone()) {
18 if (zone) {
19 try {
20 const parts = {}
21 for (const { type, value } of new Intl.DateTimeFormat('en-CA', {
22 timeZone: zone, year: 'numeric', month: '2-digit', day: '2-digit'
23 }).formatToParts(now)) parts[type] = value
24 return `${parts.year.padStart(4, '0')}-${parts.month}-${parts.day}`
25 } catch {}
26 }
27 const pad = value => String(value).padStart(2, '0')
28 return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`
29}
30
31function viewerZone () {
32 return globalThis.document?.body?.getAttribute('data-zone') ?? ''
33}
34
35function heading (schema, level) {
36 return setBlockType(schema.nodes.heading, { level, source: null })
37}
38
39// taskList wraps the selection in a list whose items are open tasks.
40function taskList (schema) {
41 return (state, dispatch) => {
42 let tr = null
43 const wrapped = wrapInList(schema.nodes.bullet_list)(state, next => { tr = next })
44 if (!wrapped) return false
45 if (dispatch) {
46 const { from, to } = tr.selection
47 tr.doc.nodesBetween(from, to, (node, pos) => {
48 if (node.type === schema.nodes.list_item && !node.attrs.task) {
49 tr.setNodeMarkup(pos, null, { ...node.attrs, task: true, checked: ' ' })
50 }
51 })
52 dispatch(tr.scrollIntoView())
53 }
54 return true
55 }
56}
57
58// rule puts a divider after the block the caret is in.
59function rule (schema) {
60 return (state, dispatch) => {
61 const { $from } = state.selection
62 if ($from.depth < 1) return false
63 if (dispatch) {
64 const after = $from.after(1)
65 const tr = state.tr.insert(after, schema.nodes.horizontal_rule.create())
66 dispatch(tr.scrollIntoView())
67 }
68 return true
69 }
70}
71
72function code (schema) {
73 return chainCommands(setBlockType(schema.nodes.code_block, { fence: '```', info: '', closed: true, close: '```' }), exitCode)
74}
75
76// insertText puts text in at the caret, replacing the selection.
77export function insertText (text) {
78 return (state, dispatch) => {
79 if (dispatch) dispatch(state.tr.insertText(text).scrollIntoView())
80 return true
81 }
82}
83
84// dateInsertion is the transaction putting in the day's chip,
85// [[2026-11-20]], what [[ makes for a date, with the caret after it; or
86// a refusal; or null where no chip can go (code, or no text block).
87// It never replaces a selection: the chip goes at its end (review M3).
88// The block must read back with the chip as one date link and its text
89// unchanged (pagelink.mjs readsBack, as Link to a page checks): right
90// after a ! the chip would be an embed, so a space goes first; when
91// even that would not read back, it is refused (review M1, note 83).
92export function dateInsertion (state, date) {
93 const at = state.selection.to
94 const $at = state.doc.resolve(at)
95 if (!$at.parent.inlineContent || $at.parent.type.spec.code) return null
96 const chip = state.schema.nodes.datelink.create({ date })
97 for (const spaced of [false, true]) {
98 const tr = state.tr
99 const pos = spaced ? at + 1 : at
100 if (spaced) tr.insert(at, state.schema.text(' '))
101 tr.insert(pos, chip)
102 if (readsBack(tr.doc.resolve(pos).parent)) {
103 return { tr: tr.setSelection(TextSelection.create(tr.doc, pos + 1)).scrollIntoView(), spaced }
104 }
105 }
106 return { refuse: refusals.date }
107}
108
109// insertDate is dateInsertion as a command: Insert → Today's date with
110// today, the date picker with its day (build note 83). False when it
111// refuses; datepicker.mjs says why.
112export function insertDate (date) {
113 return (state, dispatch) => {
114 const made = dateInsertion(state, date)
115 if (!made?.tr) return false
116 if (dispatch) dispatch(made.tr)
117 return true
118 }
119}
120
121function date () {
122 return (state, dispatch) => insertDate(today())(state, dispatch)
123}
124
125// blockCommands are the Insert menu's, by its command names.
126export function blockCommands (schema) {
127 return {
128 'insert-h1': heading(schema, 1),
129 'insert-h2': heading(schema, 2),
130 'insert-h3': heading(schema, 3),
131 'insert-list': wrapInList(schema.nodes.bullet_list),
132 'insert-numbered': wrapInList(schema.nodes.ordered_list),
133 'insert-check': taskList(schema),
134 'insert-quote': wrapIn(schema.nodes.blockquote),
135 'insert-code': code(schema),
136 'insert-rule': rule(schema),
137 'insert-link': insertText('[['),
138 'insert-date': date(),
139 'insert-paragraph': setBlockType(schema.nodes.paragraph),
140 ...columnCommands()
141 }
142}
143
144export function markCommands (schema) {
145 return {
146 strong: toggleMark(schema.marks.strong),
147 em: toggleMark(schema.marks.em),
148 code: toggleMark(schema.marks.code)
149 }
150}
151
152// selectAll is Edit → Select all, inside the page.
153export function selectAll (state, dispatch) {
154 if (dispatch) dispatch(state.tr.setSelection(TextSelection.create(state.doc, 0, state.doc.content.size)))
155 return true
156}
157
158// guarded runs a history command so that it can never throw out of a key
159// or a menu: after many edits an undo step that sets a block's attribute
160// can be mapped onto a text node, and prosemirror-transform throws there
161// instead of failing the step (review L; 4 in 20,000 fuzzed bodies). The
162// step is then not taken, as if there were nothing to undo; the text and
163// what is saved are untouched.
164export function guarded (command) {
165 return (state, dispatch, view) => {
166 try {
167 return command(state, dispatch, view)
168 } catch {
169 return false
170 }
171 }
172}
173
174export const safeUndo = guarded(undo)
175export const safeRedo = guarded(redo)
176