keymap.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list';
  2. import type { Schema } from 'prosemirror-model';
  3. import { undo, redo } from 'prosemirror-history';
  4. import { undoInputRule } from 'prosemirror-inputrules';
  5. import type { Command } from 'prosemirror-state';
  6. import {
  7. toggleMark,
  8. selectParentNode,
  9. joinUp,
  10. joinDown,
  11. chainCommands,
  12. newlineInCode,
  13. createParagraphNear,
  14. liftEmptyBlock,
  15. splitBlockKeepMarks,
  16. } from 'prosemirror-commands';
  17. export const buildKeymap = (schema: Schema) => {
  18. const keys: Record<string, Command> = {};
  19. const bind = (key: string, cmd: Command) => (keys[key] = cmd);
  20. bind('Alt-ArrowUp', joinUp);
  21. bind('Alt-ArrowDown', joinDown);
  22. bind('Mod-z', undo);
  23. bind('Mod-y', redo);
  24. bind('Backspace', undoInputRule);
  25. bind('Escape', selectParentNode);
  26. bind('Mod-b', toggleMark(schema.marks.strong));
  27. bind('Mod-i', toggleMark(schema.marks.em));
  28. bind('Mod-u', toggleMark(schema.marks.underline));
  29. bind('Mod-d', toggleMark(schema.marks.strikethrough));
  30. bind('Mod-e', toggleMark(schema.marks.code));
  31. bind('Mod-;', toggleMark(schema.marks.superscript));
  32. bind(`Mod-'`, toggleMark(schema.marks.subscript));
  33. bind(
  34. 'Enter',
  35. chainCommands(
  36. splitListItem(schema.nodes.list_item),
  37. newlineInCode,
  38. createParagraphNear,
  39. liftEmptyBlock,
  40. splitBlockKeepMarks,
  41. ),
  42. );
  43. bind('Mod-[', liftListItem(schema.nodes.list_item));
  44. bind('Mod-]', sinkListItem(schema.nodes.list_item));
  45. bind('Tab', sinkListItem(schema.nodes.list_item));
  46. return keys;
  47. };