2023-09-21 10:12:40 +02:00
|
|
|
import { EditorView } from '@codemirror/view';
|
|
|
|
import { EditorCommandType, ListType } from '../../types';
|
2024-08-22 22:52:22 +02:00
|
|
|
import { undo, redo, selectAll, indentSelection, cursorDocStart, cursorDocEnd, cursorLineStart, cursorLineEnd, deleteToLineStart, deleteToLineEnd, undoSelection, redoSelection, cursorPageDown, cursorPageUp, cursorCharRight, cursorCharLeft, insertNewlineAndIndent, cursorLineDown, cursorLineUp, toggleComment, deleteLine, moveLineUp, moveLineDown } from '@codemirror/commands';
|
2023-09-21 10:12:40 +02:00
|
|
|
import {
|
|
|
|
decreaseIndent, increaseIndent,
|
2024-09-21 14:00:43 +02:00
|
|
|
insertHorizontalRule,
|
2023-09-21 10:12:40 +02:00
|
|
|
toggleBolded, toggleCode,
|
|
|
|
toggleHeaderLevel, toggleItalicized,
|
|
|
|
toggleList, toggleMath,
|
|
|
|
} from '../markdown/markdownCommands';
|
2024-03-02 17:58:15 +02:00
|
|
|
import duplicateLine from './duplicateLine';
|
|
|
|
import sortSelectedLines from './sortSelectedLines';
|
2023-09-21 10:12:40 +02:00
|
|
|
import { closeSearchPanel, findNext, findPrevious, openSearchPanel, replaceAll, replaceNext } from '@codemirror/search';
|
2024-04-01 16:34:22 +02:00
|
|
|
import { focus } from '@joplin/lib/utils/focusHandler';
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-03-11 17:02:15 +02:00
|
|
|
export type EditorCommandFunction = (editor: EditorView, ...args: any[])=> void|any;
|
|
|
|
|
|
|
|
const replaceSelectionCommand = (editor: EditorView, toInsert: string) => {
|
|
|
|
editor.dispatch(editor.state.replaceSelection(toInsert));
|
|
|
|
};
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
const editorCommands: Record<EditorCommandType, EditorCommandFunction> = {
|
|
|
|
[EditorCommandType.Undo]: undo,
|
|
|
|
[EditorCommandType.Redo]: redo,
|
|
|
|
[EditorCommandType.SelectAll]: selectAll,
|
2024-04-01 16:34:22 +02:00
|
|
|
[EditorCommandType.Focus]: editor => focus('editorCommands::focus', editor),
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
[EditorCommandType.ToggleBolded]: toggleBolded,
|
|
|
|
[EditorCommandType.ToggleItalicized]: toggleItalicized,
|
|
|
|
[EditorCommandType.ToggleCode]: toggleCode,
|
|
|
|
[EditorCommandType.ToggleMath]: toggleMath,
|
2024-03-02 17:58:15 +02:00
|
|
|
[EditorCommandType.ToggleComment]: toggleComment,
|
|
|
|
[EditorCommandType.DuplicateLine]: duplicateLine,
|
|
|
|
[EditorCommandType.SortSelectedLines]: sortSelectedLines,
|
2023-09-21 10:12:40 +02:00
|
|
|
[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),
|
2024-09-21 14:00:43 +02:00
|
|
|
[EditorCommandType.InsertHorizontalRule]: insertHorizontalRule,
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
[EditorCommandType.ScrollSelectionIntoView]: editor => {
|
|
|
|
editor.dispatch(editor.state.update({
|
|
|
|
scrollIntoView: true,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
[EditorCommandType.DeleteToLineEnd]: deleteToLineEnd,
|
|
|
|
[EditorCommandType.DeleteToLineStart]: deleteToLineStart,
|
2024-03-02 17:58:15 +02:00
|
|
|
[EditorCommandType.DeleteLine]: deleteLine,
|
2023-09-21 10:12:40 +02:00
|
|
|
[EditorCommandType.IndentMore]: increaseIndent,
|
|
|
|
[EditorCommandType.IndentLess]: decreaseIndent,
|
|
|
|
[EditorCommandType.IndentAuto]: indentSelection,
|
|
|
|
[EditorCommandType.InsertNewlineAndIndent]: insertNewlineAndIndent,
|
2024-08-22 22:52:22 +02:00
|
|
|
[EditorCommandType.SwapLineUp]: moveLineUp,
|
|
|
|
[EditorCommandType.SwapLineDown]: moveLineDown,
|
2023-09-21 10:12:40 +02:00
|
|
|
[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,
|
2024-03-11 17:02:15 +02:00
|
|
|
|
|
|
|
// Getter commands
|
|
|
|
// Note that these commands aren't strictly CodeMirror 6 commands as they produce
|
|
|
|
// output.
|
|
|
|
[EditorCommandType.SelectedText]: editor => {
|
|
|
|
const selection = editor.state.selection;
|
|
|
|
return editor.state.sliceDoc(selection.main.from, selection.main.to);
|
|
|
|
},
|
|
|
|
[EditorCommandType.InsertText]: replaceSelectionCommand,
|
|
|
|
[EditorCommandType.ReplaceSelection]: replaceSelectionCommand,
|
2024-04-03 19:56:54 +02:00
|
|
|
|
|
|
|
[EditorCommandType.SetText]: (editor, text: string) => {
|
|
|
|
editor.dispatch({
|
|
|
|
changes: [{
|
|
|
|
from: 0,
|
|
|
|
to: editor.state.doc.length,
|
|
|
|
insert: text,
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
};
|
|
|
|
export default editorCommands;
|
|
|
|
|