|
| 1 | +--- |
| 2 | +title: ShortcutsPlugin |
| 3 | +--- |
| 4 | + |
| 5 | +## Description |
| 6 | +Plugin for adding markdown shortcuts. |
| 7 | + |
| 8 | +## Usage |
| 9 | +```tsx |
| 10 | +import { KonaEditor, ShortcutsPlugin, CodeBlockPlugin } from "@kona/editor"; |
| 11 | +import { getShortcuts } from "./getShortcuts"; |
| 12 | + |
| 13 | +const shortcutsPlugin = new ShortcutsPlugin({ |
| 14 | + shortcuts: getShortcuts(), |
| 15 | + ignoreNodes: [CodeBlockPlugin.CODE_LINE_ELEMENT] |
| 16 | +}) |
| 17 | +``` |
| 18 | + |
| 19 | +I usually store shortcuts in a separate file because the list can be pretty large. |
| 20 | +The order of the shortcuts matters because the first one found will be applied and the later will be ignored. |
| 21 | + |
| 22 | +```ts |
| 23 | +import { Editor, Transforms } from 'slate'; |
| 24 | +import { type ChangeMatch, HeadingsPlugin, type Shortcut } from '../plugins'; |
| 25 | + |
| 26 | +export const getShortcuts = (): Shortcut[] => { |
| 27 | + return [ |
| 28 | + { |
| 29 | + trigger: ' ', |
| 30 | + before: /\*\*/g, |
| 31 | + after: /\*\*/g, |
| 32 | + change(editor: Editor, match) { |
| 33 | + replaceWithMarkedText(editor, match, 'bold'); |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + trigger: ' ', |
| 38 | + before: /\*/g, |
| 39 | + after: /\*/g, |
| 40 | + change(editor: Editor, match) { |
| 41 | + replaceWithMarkedText(editor, match, 'italic'); |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + trigger: ' ', |
| 46 | + before: /~~/g, |
| 47 | + after: /~~/g, |
| 48 | + change(editor: Editor, match) { |
| 49 | + replaceWithMarkedText(editor, match, 'strikethrough'); |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + trigger: ' ', |
| 54 | + before: /^###/g, |
| 55 | + change(editor, match) { |
| 56 | + replaceWithHeading(editor, match, HeadingsPlugin.HeadingLevel3); |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + trigger: ' ', |
| 61 | + before: /^##/g, |
| 62 | + change(editor, match) { |
| 63 | + replaceWithHeading(editor, match, HeadingsPlugin.HeadingLevel2); |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + trigger: ' ', |
| 68 | + before: /^#/g, |
| 69 | + change(editor, match) { |
| 70 | + replaceWithHeading(editor, match, HeadingsPlugin.HeadingLevel1); |
| 71 | + }, |
| 72 | + }, |
| 73 | + ]; |
| 74 | +}; |
| 75 | + |
| 76 | +const replaceWithHeading = ( |
| 77 | + editor: Editor, |
| 78 | + match: ChangeMatch, |
| 79 | + type: string, |
| 80 | +) => { |
| 81 | + Editor.withoutNormalizing(editor, () => { |
| 82 | + const { selection } = editor; |
| 83 | + |
| 84 | + if (selection) { |
| 85 | + Transforms.delete(editor, { |
| 86 | + at: selection.focus, |
| 87 | + distance: match.before?.[0].length, |
| 88 | + reverse: true, |
| 89 | + unit: 'character', |
| 90 | + }); |
| 91 | + |
| 92 | + Transforms.setNodes(editor, { |
| 93 | + type, |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +const replaceWithMarkedText = ( |
| 100 | + editor: Editor, |
| 101 | + match: ChangeMatch, |
| 102 | + mark: string, |
| 103 | +) => { |
| 104 | + Editor.withoutNormalizing(editor, () => { |
| 105 | + if (!editor.selection) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + Transforms.delete(editor, { |
| 110 | + at: editor.selection?.focus, |
| 111 | + distance: match.text?.length || 0, |
| 112 | + reverse: true, |
| 113 | + unit: 'character', |
| 114 | + }); |
| 115 | + Transforms.insertText(editor, match.cleanText || ''); |
| 116 | + Transforms.setSelection(editor, { |
| 117 | + anchor: editor.selection?.focus, |
| 118 | + focus: { |
| 119 | + path: editor.selection?.focus.path, |
| 120 | + offset: editor.selection?.focus.offset - (match.cleanText?.length || 0), |
| 121 | + }, |
| 122 | + }); |
| 123 | + Editor.addMark(editor, mark, true); |
| 124 | + Transforms.setSelection(editor, { |
| 125 | + anchor: editor.selection.anchor, |
| 126 | + focus: editor.selection.anchor, |
| 127 | + }); |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +``` |
0 commit comments