forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editorCommands.ts
75 lines (69 loc) · 3.6 KB
/
editorCommands.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { EditorView } from '@codemirror/view';
import { EditorCommandType, ListType } from '../../types';
import { undo, redo, selectAll, indentSelection, cursorDocStart, cursorDocEnd, cursorLineStart, cursorLineEnd, deleteToLineStart, deleteToLineEnd, undoSelection, redoSelection, cursorPageDown, cursorPageUp, cursorCharRight, cursorCharLeft, insertNewlineAndIndent, cursorLineDown, cursorLineUp, toggleComment, deleteLine } from '@codemirror/commands';
import {
decreaseIndent, increaseIndent,
toggleBolded, toggleCode,
toggleHeaderLevel, toggleItalicized,
toggleList, toggleMath,
} from '../markdown/markdownCommands';
import swapLine, { SwapLineDirection } from './swapLine';
import duplicateLine from './duplicateLine';
import sortSelectedLines from './sortSelectedLines';
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
export type EditorCommandFunction = (editor: EditorView)=> void;
const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
[EditorCommandType.Undo]: undo,
[EditorCommandType.Redo]: redo,
[EditorCommandType.SelectAll]: selectAll,
[EditorCommandType.Focus]: editor => editor.focus(),
[EditorCommandType.ToggleBolded]: toggleBolded,
[EditorCommandType.ToggleItalicized]: toggleItalicized,
[EditorCommandType.ToggleCode]: toggleCode,
[EditorCommandType.ToggleMath]: toggleMath,
[EditorCommandType.ToggleComment]: toggleComment,
[EditorCommandType.DuplicateLine]: duplicateLine,
[EditorCommandType.SortSelectedLines]: sortSelectedLines,
[EditorCommandType.ToggleNumberedList]: toggleList(ListType.OrderedList),
[EditorCommandType.ToggleBulletedList]: toggleList(ListType.UnorderedList),
[EditorCommandType.ToggleCheckList]: toggleList(ListType.CheckList),
[EditorCommandType.ToggleHeading]: toggleHeaderLevel(2),
[EditorCommandType.ToggleHeading1]: toggleHeaderLevel(1),
[EditorCommandType.ToggleHeading2]: toggleHeaderLevel(2),
[EditorCommandType.ToggleHeading3]: toggleHeaderLevel(3),
[EditorCommandType.ToggleHeading4]: toggleHeaderLevel(4),
[EditorCommandType.ToggleHeading5]: toggleHeaderLevel(5),
[EditorCommandType.ScrollSelectionIntoView]: editor => {
editor.dispatch(editor.state.update({
scrollIntoView: true,
}));
},
[EditorCommandType.DeleteToLineEnd]: deleteToLineEnd,
[EditorCommandType.DeleteToLineStart]: deleteToLineStart,
[EditorCommandType.DeleteLine]: deleteLine,
[EditorCommandType.IndentMore]: increaseIndent,
[EditorCommandType.IndentLess]: decreaseIndent,
[EditorCommandType.IndentAuto]: indentSelection,
[EditorCommandType.InsertNewlineAndIndent]: insertNewlineAndIndent,
[EditorCommandType.SwapLineUp]: swapLine(SwapLineDirection.Up),
[EditorCommandType.SwapLineDown]: swapLine(SwapLineDirection.Down),
[EditorCommandType.GoDocEnd]: cursorDocEnd,
[EditorCommandType.GoDocStart]: cursorDocStart,
[EditorCommandType.GoLineStart]: cursorLineStart,
[EditorCommandType.GoLineEnd]: cursorLineEnd,
[EditorCommandType.GoLineUp]: cursorLineUp,
[EditorCommandType.GoLineDown]: cursorLineDown,
[EditorCommandType.GoPageUp]: cursorPageUp,
[EditorCommandType.GoPageDown]: cursorPageDown,
[EditorCommandType.GoCharLeft]: cursorCharLeft,
[EditorCommandType.GoCharRight]: cursorCharRight,
[EditorCommandType.UndoSelection]: undoSelection,
[EditorCommandType.RedoSelection]: redoSelection,
[EditorCommandType.ShowSearch]: openSearchPanel,
[EditorCommandType.HideSearch]: closeSearchPanel,
[EditorCommandType.FindNext]: findNext,
[EditorCommandType.FindPrevious]: findPrevious,
[EditorCommandType.ReplaceNext]: replaceNext,
[EditorCommandType.ReplaceAll]: replaceAll,
};
export default editorCommands;