// commands.mjs — the blocks the Insert menu, the / menu and the phone's // accessory row make (spec §3.4, §6.6), and the marks. Each is a // ProseMirror command; what it makes is a block Mapa's subset can say, // so the settle plugin finds nothing to change. import { undo, redo } from 'prosemirror-history' import { setBlockType, wrapIn, toggleMark, chainCommands, exitCode } from 'prosemirror-commands' import { wrapInList } from 'prosemirror-schema-list' import { TextSelection } from 'prosemirror-state' import { readsBack, refusals } from './pagelink.mjs' import { columnCommands } from './columns.mjs' // today is the date it is now, ISO, in the zone "today" uses for the // viewer: their own, else the team's (, the zone offer's // too). It is read each time it is asked, so a page left open past // midnight moves on. Without a zone it is the browser's date. export function today (now = new Date(), zone = viewerZone()) { if (zone) { try { const parts = {} for (const { type, value } of new Intl.DateTimeFormat('en-CA', { timeZone: zone, year: 'numeric', month: '2-digit', day: '2-digit' }).formatToParts(now)) parts[type] = value return `${parts.year.padStart(4, '0')}-${parts.month}-${parts.day}` } catch {} } const pad = value => String(value).padStart(2, '0') return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}` } function viewerZone () { return globalThis.document?.body?.getAttribute('data-zone') ?? '' } function heading (schema, level) { return setBlockType(schema.nodes.heading, { level, source: null }) } // taskList wraps the selection in a list whose items are open tasks. function taskList (schema) { return (state, dispatch) => { let tr = null const wrapped = wrapInList(schema.nodes.bullet_list)(state, next => { tr = next }) if (!wrapped) return false if (dispatch) { const { from, to } = tr.selection tr.doc.nodesBetween(from, to, (node, pos) => { if (node.type === schema.nodes.list_item && !node.attrs.task) { tr.setNodeMarkup(pos, null, { ...node.attrs, task: true, checked: ' ' }) } }) dispatch(tr.scrollIntoView()) } return true } } // rule puts a divider after the block the caret is in. function rule (schema) { return (state, dispatch) => { const { $from } = state.selection if ($from.depth < 1) return false if (dispatch) { const after = $from.after(1) const tr = state.tr.insert(after, schema.nodes.horizontal_rule.create()) dispatch(tr.scrollIntoView()) } return true } } function code (schema) { return chainCommands(setBlockType(schema.nodes.code_block, { fence: '```', info: '', closed: true, close: '```' }), exitCode) } // insertText puts text in at the caret, replacing the selection. export function insertText (text) { return (state, dispatch) => { if (dispatch) dispatch(state.tr.insertText(text).scrollIntoView()) return true } } // dateInsertion is the transaction putting in the day's chip, // [[2026-11-20]], what [[ makes for a date, with the caret after it; or // a refusal; or null where no chip can go (code, or no text block). // It never replaces a selection: the chip goes at its end (review M3). // The block must read back with the chip as one date link and its text // unchanged (pagelink.mjs readsBack, as Link to a page checks): right // after a ! the chip would be an embed, so a space goes first; when // even that would not read back, it is refused (review M1, note 83). export function dateInsertion (state, date) { const at = state.selection.to const $at = state.doc.resolve(at) if (!$at.parent.inlineContent || $at.parent.type.spec.code) return null const chip = state.schema.nodes.datelink.create({ date }) for (const spaced of [false, true]) { const tr = state.tr const pos = spaced ? at + 1 : at if (spaced) tr.insert(at, state.schema.text(' ')) tr.insert(pos, chip) if (readsBack(tr.doc.resolve(pos).parent)) { return { tr: tr.setSelection(TextSelection.create(tr.doc, pos + 1)).scrollIntoView(), spaced } } } return { refuse: refusals.date } } // insertDate is dateInsertion as a command: Insert → Today's date with // today, the date picker with its day (build note 83). False when it // refuses; datepicker.mjs says why. export function insertDate (date) { return (state, dispatch) => { const made = dateInsertion(state, date) if (!made?.tr) return false if (dispatch) dispatch(made.tr) return true } } function date () { return (state, dispatch) => insertDate(today())(state, dispatch) } // blockCommands are the Insert menu's, by its command names. export function blockCommands (schema) { return { 'insert-h1': heading(schema, 1), 'insert-h2': heading(schema, 2), 'insert-h3': heading(schema, 3), 'insert-list': wrapInList(schema.nodes.bullet_list), 'insert-numbered': wrapInList(schema.nodes.ordered_list), 'insert-check': taskList(schema), 'insert-quote': wrapIn(schema.nodes.blockquote), 'insert-code': code(schema), 'insert-rule': rule(schema), 'insert-link': insertText('[['), 'insert-date': date(), 'insert-paragraph': setBlockType(schema.nodes.paragraph), ...columnCommands() } } export function markCommands (schema) { return { strong: toggleMark(schema.marks.strong), em: toggleMark(schema.marks.em), code: toggleMark(schema.marks.code) } } // selectAll is Edit → Select all, inside the page. export function selectAll (state, dispatch) { if (dispatch) dispatch(state.tr.setSelection(TextSelection.create(state.doc, 0, state.doc.content.size))) return true } // guarded runs a history command so that it can never throw out of a key // or a menu: after many edits an undo step that sets a block's attribute // can be mapped onto a text node, and prosemirror-transform throws there // instead of failing the step (review L; 4 in 20,000 fuzzed bodies). The // step is then not taken, as if there were nothing to undo; the text and // what is saved are untouched. export function guarded (command) { return (state, dispatch, view) => { try { return command(state, dispatch, view) } catch { return false } } } export const safeUndo = guarded(undo) export const safeRedo = guarded(redo)