// keys.mjs — the editor's keys (spec §6.6). ⌘B, ⌘I, ⌘E for marks; ⌘S // commits; ⌘Z and ⌘⇧Z undo and redo; ⌘K stays search (mapa.js); ⌘L // links the selected words to a page, or types [[ (pagelink.mjs). Tab // and ⇧Tab move list items, and only list items: anywhere else Tab // leaves the editor's text, and Esc then Tab always does (the // keyboard's way out, listed in Help). The browser's next stop may be a // button inside the page: a task box, or the ALT badge of an image with // no description (images build note I22); a described image's badge is // reached from its bubble instead. Enter never acts while an IME is // composing. import { keymap } from 'prosemirror-keymap' import { baseKeymap, chainCommands } from 'prosemirror-commands' import { splitListItem, sinkListItem, liftListItem } from 'prosemirror-schema-list' import { Plugin, TextSelection } from 'prosemirror-state' import { undoInputRule } from 'prosemirror-inputrules' import { markCommands, safeUndo, safeRedo } from './commands.mjs' import { unchip } from './suggest.mjs' import { emptyColumnBackspace, toColumn, deleteInColumns, layoutEdge } from './columns.mjs' // inItem is whether the caret is in a list item. function inItem (state) { const { $from } = state.selection for (let depth = $from.depth; depth > 0; depth--) { if ($from.node(depth).type.name === 'list_item') return $from.node(depth) } return null } // nextItem is the item Enter makes: the same bullet and indent, and an // open box after a task, so a checklist goes on as a checklist. function nextItem (schema) { return (state, dispatch, view) => { const item = inItem(state) if (!item) return false const attrs = { indent: item.attrs.indent, bullet: item.attrs.bullet, delim: item.attrs.delim, number: /^[0-9]{1,8}$/u.test(item.attrs.number ?? '') ? String(+item.attrs.number + 1) : null, task: item.attrs.task, checked: item.attrs.task ? ' ' : null } return splitListItem(schema.nodes.list_item, attrs)(state, dispatch, view) } } // leaveQuote is Enter in a quote: a Mapa quote is one block of lines, // so Enter ends it, and what follows the caret starts the paragraph // after it. ⇧Enter is a new line inside it. function leaveQuote (schema) { return (state, dispatch) => { const { $from, $to } = state.selection if ($from.depth < 2 || $from.node(-1).type !== schema.nodes.blockquote) return false if (dispatch) { const rest = $to.parent.content.cut($to.parentOffset) const after = $from.after(-1) const tr = state.tr.delete($from.pos, $from.end()) const at = tr.mapping.map(after) tr.insert(at, schema.nodes.paragraph.create(null, rest)) dispatch(tr.setSelection(TextSelection.create(tr.doc, at + 1)).scrollIntoView()) } return true } } function hardBreak (schema) { return (state, dispatch) => { const { $from } = state.selection if ($from.parent.type.spec.code || !$from.parent.inlineContent) return false if (dispatch) dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView()) return true } } export function keys (schema, { save, linkPage = null }) { const marks = markCommands(schema) let escaped = false const composing = command => (state, dispatch, view) => view?.composing ? false : command(state, dispatch, view) const bindings = keymap({ Backspace: chainCommands(undoInputRule, unchip, deleteInColumns, emptyColumnBackspace, layoutEdge(true)), Delete: chainCommands(deleteInColumns, layoutEdge(false)), // ⌥⌘→ and ⌥⌘←: the next or previous column (columns and icons §1.7). 'Mod-Alt-ArrowRight': toColumn(1), 'Mod-Alt-ArrowLeft': toColumn(-1), 'Mod-z': safeUndo, 'Shift-Mod-z': safeRedo, 'Mod-y': safeRedo, 'Mod-b': marks.strong, 'Mod-i': marks.em, 'Mod-e': marks.code, 'Mod-s': () => { save(); return true }, // ⌘L, taken while the page's text has focus: a command that answers // true has its keydown's default prevented, so the browser does not // also move to its address bar (build note 81). 'Mod-l': linkPage ?? (() => false), Enter: composing(chainCommands(nextItem(schema), leaveQuote(schema))), 'Shift-Enter': composing(hardBreak(schema)), 'Mod-[': liftListItem(schema.nodes.list_item), 'Mod-]': sinkListItem(schema.nodes.list_item) }) const base = keymap({ ...baseKeymap, Enter: composing(baseKeymap.Enter) }) // Esc arms the way out: the next Tab is the browser's. const escape = new Plugin({ props: { handleKeyDown (view, event) { if (event.key === 'Escape') { escaped = true return false } if (event.key !== 'Shift' && event.key !== 'Tab') escaped = false if (event.key === 'Tab' && !escaped && inItem(view.state)) { // Tab in a list never leaves, even when the item cannot move. const moved = (event.shiftKey ? liftListItem(schema.nodes.list_item) : sinkListItem(schema.nodes.list_item))(view.state, view.dispatch) event.preventDefault() return moved || true } if (event.key === 'Tab') escaped = false return false } } }) return [escape, bindings, base] }