File size: 1,566 Bytes
c20f20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list';
import type { Schema } from 'prosemirror-model';
import { undo, redo } from 'prosemirror-history';
import { undoInputRule } from 'prosemirror-inputrules';
import type { Command } from 'prosemirror-state';
import {
  toggleMark,
  selectParentNode,
  joinUp,
  joinDown,
  chainCommands,
  newlineInCode,
  createParagraphNear,
  liftEmptyBlock,
  splitBlockKeepMarks,
} from 'prosemirror-commands';

export const buildKeymap = (schema: Schema) => {
  const keys: Record<string, Command> = {};
  const bind = (key: string, cmd: Command) => (keys[key] = cmd);

  bind('Alt-ArrowUp', joinUp);
  bind('Alt-ArrowDown', joinDown);
  bind('Mod-z', undo);
  bind('Mod-y', redo);
  bind('Backspace', undoInputRule);
  bind('Escape', selectParentNode);
  bind('Mod-b', toggleMark(schema.marks.strong));
  bind('Mod-i', toggleMark(schema.marks.em));
  bind('Mod-u', toggleMark(schema.marks.underline));
  bind('Mod-d', toggleMark(schema.marks.strikethrough));
  bind('Mod-e', toggleMark(schema.marks.code));
  bind('Mod-;', toggleMark(schema.marks.superscript));
  bind(`Mod-'`, toggleMark(schema.marks.subscript));
  bind(
    'Enter',
    chainCommands(
      splitListItem(schema.nodes.list_item),
      newlineInCode,
      createParagraphNear,
      liftEmptyBlock,
      splitBlockKeepMarks,
    ),
  );
  bind('Mod-[', liftListItem(schema.nodes.list_item));
  bind('Mod-]', sinkListItem(schema.nodes.list_item));
  bind('Tab', sinkListItem(schema.nodes.list_item));

  return keys;
};