diff --git a/.eslintignore b/.eslintignore index 31d5e4f4f..659dce6d4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -246,7 +246,6 @@ packages/app-desktop/gui/Navigator.js packages/app-desktop/gui/NoteContentPropertiesDialog.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/Toolbar.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/index.js -packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/setupVim.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/types.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useContextMenu.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.js @@ -573,6 +572,8 @@ packages/editor/CodeMirror/configFromSettings.js packages/editor/CodeMirror/createEditor.test.js packages/editor/CodeMirror/createEditor.js packages/editor/CodeMirror/editorCommands/editorCommands.js +packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js +packages/editor/CodeMirror/editorCommands/insertLineAfter.js packages/editor/CodeMirror/editorCommands/supportsCommand.js packages/editor/CodeMirror/editorCommands/swapLine.js packages/editor/CodeMirror/getScrollFraction.js @@ -597,6 +598,7 @@ packages/editor/CodeMirror/testUtil/forceFullParse.js packages/editor/CodeMirror/testUtil/loadLanguages.js packages/editor/CodeMirror/theme.js packages/editor/CodeMirror/util/isInSyntaxNode.js +packages/editor/CodeMirror/util/setupVim.js packages/editor/SelectionFormatting.js packages/editor/events.js packages/editor/types.js diff --git a/.gitignore b/.gitignore index 7df852931..eafb8d23c 100644 --- a/.gitignore +++ b/.gitignore @@ -226,7 +226,6 @@ packages/app-desktop/gui/Navigator.js packages/app-desktop/gui/NoteContentPropertiesDialog.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/Toolbar.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/index.js -packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/setupVim.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/types.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useContextMenu.js packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.js @@ -553,6 +552,8 @@ packages/editor/CodeMirror/configFromSettings.js packages/editor/CodeMirror/createEditor.test.js packages/editor/CodeMirror/createEditor.js packages/editor/CodeMirror/editorCommands/editorCommands.js +packages/editor/CodeMirror/editorCommands/insertLineAfter.test.js +packages/editor/CodeMirror/editorCommands/insertLineAfter.js packages/editor/CodeMirror/editorCommands/supportsCommand.js packages/editor/CodeMirror/editorCommands/swapLine.js packages/editor/CodeMirror/getScrollFraction.js @@ -577,6 +578,7 @@ packages/editor/CodeMirror/testUtil/forceFullParse.js packages/editor/CodeMirror/testUtil/loadLanguages.js packages/editor/CodeMirror/theme.js packages/editor/CodeMirror/util/isInSyntaxNode.js +packages/editor/CodeMirror/util/setupVim.js packages/editor/SelectionFormatting.js packages/editor/events.js packages/editor/types.js diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useKeymap.ts b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useKeymap.ts index 0e13ef318..689067f79 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useKeymap.ts +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useKeymap.ts @@ -4,7 +4,7 @@ import KeymapService, { KeymapItem } from '@joplin/lib/services/KeymapService'; import { EditorCommand } from '../../../utils/types'; import shim from '@joplin/lib/shim'; import { reg } from '@joplin/lib/registry'; -import setupVim from './setupVim'; +import setupVim from '@joplin/editor/CodeMirror/util/setupVim'; import { EventName } from '@joplin/lib/eventManager'; export default function useKeymap(CodeMirror: any) { diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.tsx index 0b586cc45..c408e14f5 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/Editor.tsx @@ -8,7 +8,7 @@ import { PluginStates } from '@joplin/lib/services/plugins/reducer'; import { ContentScriptType } from '@joplin/lib/services/plugins/api/types'; import shim from '@joplin/lib/shim'; import PluginService from '@joplin/lib/services/plugins/PluginService'; -import setupVim from '../utils/setupVim'; +import setupVim from '@joplin/editor/CodeMirror/util/setupVim'; interface Props extends EditorProps { style: React.CSSProperties; diff --git a/packages/editor/CodeMirror/CodeMirror5Emulation/CodeMirror5Emulation.ts b/packages/editor/CodeMirror/CodeMirror5Emulation/CodeMirror5Emulation.ts index 3bb122fa8..af97a5ad0 100644 --- a/packages/editor/CodeMirror/CodeMirror5Emulation/CodeMirror5Emulation.ts +++ b/packages/editor/CodeMirror/CodeMirror5Emulation/CodeMirror5Emulation.ts @@ -7,6 +7,7 @@ import editorCommands from '../editorCommands/editorCommands'; import { StateEffect } from '@codemirror/state'; import { StreamParser } from '@codemirror/language'; import Decorator, { LineWidgetOptions } from './Decorator'; +import insertLineAfter from '../editorCommands/insertLineAfter'; const { pregQuote } = require('@joplin/lib/string-utils-common'); @@ -354,12 +355,17 @@ export default class CodeMirror5Emulation extends BaseCodeMirror5Emulation { public static commands = (() => { const commands: Record = { ...BaseCodeMirror5Emulation.commands, + + vimInsertListElement: (codeMirror: BaseCodeMirror5Emulation) => { + insertLineAfter(codeMirror.cm6); + Vim.handleKey(codeMirror, 'i', 'macro'); + }, }; for (const commandName in editorCommands) { const command = editorCommands[commandName as keyof typeof editorCommands]; - commands[commandName] = (codeMirror: CodeMirror5Emulation) => command(codeMirror.editor); + commands[commandName] = (codeMirror: BaseCodeMirror5Emulation) => command(codeMirror.cm6); } // as any: Required to properly extend the base class -- without this, diff --git a/packages/editor/CodeMirror/editorCommands/insertLineAfter.test.ts b/packages/editor/CodeMirror/editorCommands/insertLineAfter.test.ts new file mode 100644 index 000000000..02bf29faf --- /dev/null +++ b/packages/editor/CodeMirror/editorCommands/insertLineAfter.test.ts @@ -0,0 +1,20 @@ +import { EditorSelection } from '@codemirror/state'; +import createTestEditor from '../testUtil/createTestEditor'; +import insertLineAfter from './insertLineAfter'; + +describe('insertLineAfter', () => { + test('should continue lists', async () => { + const editor = await createTestEditor( + '- This\n- is\n- a test', + EditorSelection.cursor(1), + ['BulletList'], + ); + insertLineAfter(editor); + expect(editor.state.doc.toString()).toBe([ + '- This', + '- ', + '- is', + '- a test', + ].join('\n')); + }); +}); diff --git a/packages/editor/CodeMirror/editorCommands/insertLineAfter.ts b/packages/editor/CodeMirror/editorCommands/insertLineAfter.ts new file mode 100644 index 000000000..c8c84f6c0 --- /dev/null +++ b/packages/editor/CodeMirror/editorCommands/insertLineAfter.ts @@ -0,0 +1,23 @@ +import { insertNewlineAndIndent } from '@codemirror/commands'; +import { insertNewlineContinueMarkup } from '@codemirror/lang-markdown'; +import { EditorSelection, SelectionRange } from '@codemirror/state'; +import { EditorView } from '@codemirror/view'; + +const insertLineAfter = (view: EditorView) => { + const state = view.state; + view.dispatch(state.changeByRange((sel: SelectionRange) => { + const line = state.doc.lineAt(sel.anchor); + return { + range: EditorSelection.cursor(line.to), + }; + })); + + // insertNewlineContinueMarkup does nothing if not in markdown -- we thus + // need a fallback case + const addedNewLine = insertNewlineContinueMarkup(view); + if (!addedNewLine) { + insertNewlineAndIndent(view); + } +}; + +export default insertLineAfter; diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/setupVim.ts b/packages/editor/CodeMirror/util/setupVim.ts similarity index 89% rename from packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/setupVim.ts rename to packages/editor/CodeMirror/util/setupVim.ts index d2b4fe038..95c7670f1 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/setupVim.ts +++ b/packages/editor/CodeMirror/util/setupVim.ts @@ -1,4 +1,4 @@ -import CodeMirrorControl from '@joplin/editor/CodeMirror/CodeMirrorControl'; +import CodeMirrorControl from '../CodeMirrorControl'; const setupVim = (CodeMirror: CodeMirrorControl) => { CodeMirror.Vim.defineAction('swapLineDown', CodeMirror.commands.swapLineDown);