| 1 | // keys.mjs — the editor's keys (spec §6.6). ⌘B, ⌘I, ⌘E for marks; ⌘S |
| 2 | // commits; ⌘Z and ⌘⇧Z undo and redo; ⌘K stays search (mapa.js); ⌘L |
| 3 | // links the selected words to a page, or types [[ (pagelink.mjs). Tab |
| 4 | // and ⇧Tab move list items, and only list items: anywhere else Tab |
| 5 | // leaves the editor's text, and Esc then Tab always does (the |
| 6 | // keyboard's way out, listed in Help). The browser's next stop may be a |
| 7 | // button inside the page: a task box, or the ALT badge of an image with |
| 8 | // no description (images build note I22); a described image's badge is |
| 9 | // reached from its bubble instead. Enter never acts while an IME is |
| 10 | // composing. |
| 11 | |
| 12 | import { keymap } from 'prosemirror-keymap' |
| 13 | import { baseKeymap, chainCommands } from 'prosemirror-commands' |
| 14 | import { splitListItem, sinkListItem, liftListItem } from 'prosemirror-schema-list' |
| 15 | import { Plugin, TextSelection } from 'prosemirror-state' |
| 16 | import { undoInputRule } from 'prosemirror-inputrules' |
| 17 | import { markCommands, safeUndo, safeRedo } from './commands.mjs' |
| 18 | import { unchip } from './suggest.mjs' |
| 19 | import { emptyColumnBackspace, toColumn, deleteInColumns, layoutEdge } from './columns.mjs' |
| 20 | |
| 21 | // inItem is whether the caret is in a list item. |
| 22 | function inItem (state) { |
| 23 | const { $from } = state.selection |
| 24 | for (let depth = $from.depth; depth > 0; depth--) { |
| 25 | if ($from.node(depth).type.name === 'list_item') return $from.node(depth) |
| 26 | } |
| 27 | return null |
| 28 | } |
| 29 | |
| 30 | // nextItem is the item Enter makes: the same bullet and indent, and an |
| 31 | // open box after a task, so a checklist goes on as a checklist. |
| 32 | function nextItem (schema) { |
| 33 | return (state, dispatch, view) => { |
| 34 | const item = inItem(state) |
| 35 | if (!item) return false |
| 36 | const attrs = { |
| 37 | indent: item.attrs.indent, bullet: item.attrs.bullet, delim: item.attrs.delim, |
| 38 | number: /^[0-9]{1,8}$/u.test(item.attrs.number ?? '') ? String(+item.attrs.number + 1) : null, |
| 39 | task: item.attrs.task, checked: item.attrs.task ? ' ' : null |
| 40 | } |
| 41 | return splitListItem(schema.nodes.list_item, attrs)(state, dispatch, view) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // leaveQuote is Enter in a quote: a Mapa quote is one block of lines, |
| 46 | // so Enter ends it, and what follows the caret starts the paragraph |
| 47 | // after it. ⇧Enter is a new line inside it. |
| 48 | function leaveQuote (schema) { |
| 49 | return (state, dispatch) => { |
| 50 | const { $from, $to } = state.selection |
| 51 | if ($from.depth < 2 || $from.node(-1).type !== schema.nodes.blockquote) return false |
| 52 | if (dispatch) { |
| 53 | const rest = $to.parent.content.cut($to.parentOffset) |
| 54 | const after = $from.after(-1) |
| 55 | const tr = state.tr.delete($from.pos, $from.end()) |
| 56 | const at = tr.mapping.map(after) |
| 57 | tr.insert(at, schema.nodes.paragraph.create(null, rest)) |
| 58 | dispatch(tr.setSelection(TextSelection.create(tr.doc, at + 1)).scrollIntoView()) |
| 59 | } |
| 60 | return true |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function hardBreak (schema) { |
| 65 | return (state, dispatch) => { |
| 66 | const { $from } = state.selection |
| 67 | if ($from.parent.type.spec.code || !$from.parent.inlineContent) return false |
| 68 | if (dispatch) dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView()) |
| 69 | return true |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | export function keys (schema, { save, linkPage = null }) { |
| 74 | const marks = markCommands(schema) |
| 75 | let escaped = false |
| 76 | const composing = command => (state, dispatch, view) => |
| 77 | view?.composing ? false : command(state, dispatch, view) |
| 78 | const bindings = keymap({ |
| 79 | Backspace: chainCommands(undoInputRule, unchip, deleteInColumns, emptyColumnBackspace, |
| 80 | layoutEdge(true)), |
| 81 | Delete: chainCommands(deleteInColumns, layoutEdge(false)), |
| 82 | // ⌥⌘→ and ⌥⌘←: the next or previous column (columns and icons §1.7). |
| 83 | 'Mod-Alt-ArrowRight': toColumn(1), |
| 84 | 'Mod-Alt-ArrowLeft': toColumn(-1), |
| 85 | 'Mod-z': safeUndo, |
| 86 | 'Shift-Mod-z': safeRedo, |
| 87 | 'Mod-y': safeRedo, |
| 88 | 'Mod-b': marks.strong, |
| 89 | 'Mod-i': marks.em, |
| 90 | 'Mod-e': marks.code, |
| 91 | 'Mod-s': () => { save(); return true }, |
| 92 | // ⌘L, taken while the page's text has focus: a command that answers |
| 93 | // true has its keydown's default prevented, so the browser does not |
| 94 | // also move to its address bar (build note 81). |
| 95 | 'Mod-l': linkPage ?? (() => false), |
| 96 | Enter: composing(chainCommands(nextItem(schema), leaveQuote(schema))), |
| 97 | 'Shift-Enter': composing(hardBreak(schema)), |
| 98 | 'Mod-[': liftListItem(schema.nodes.list_item), |
| 99 | 'Mod-]': sinkListItem(schema.nodes.list_item) |
| 100 | }) |
| 101 | const base = keymap({ ...baseKeymap, Enter: composing(baseKeymap.Enter) }) |
| 102 | // Esc arms the way out: the next Tab is the browser's. |
| 103 | const escape = new Plugin({ |
| 104 | props: { |
| 105 | handleKeyDown (view, event) { |
| 106 | if (event.key === 'Escape') { |
| 107 | escaped = true |
| 108 | return false |
| 109 | } |
| 110 | if (event.key !== 'Shift' && event.key !== 'Tab') escaped = false |
| 111 | if (event.key === 'Tab' && !escaped && inItem(view.state)) { |
| 112 | // Tab in a list never leaves, even when the item cannot move. |
| 113 | const moved = (event.shiftKey |
| 114 | ? liftListItem(schema.nodes.list_item) |
| 115 | : sinkListItem(schema.nodes.list_item))(view.state, view.dispatch) |
| 116 | event.preventDefault() |
| 117 | return moved || true |
| 118 | } |
| 119 | if (event.key === 'Tab') escaped = false |
| 120 | return false |
| 121 | } |
| 122 | } |
| 123 | }) |
| 124 | return [escape, bindings, base] |
| 125 | } |
| 126 | |