Skip to content

Commit

Permalink
Desktop: Fixes #9699: Beta editor: Fix o not working in Vim normal …
Browse files Browse the repository at this point in the history
…mode (#9700)
  • Loading branch information
personalizedrefrigerator authored Jan 18, 2024
1 parent dfe5383 commit fdbfa78
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down Expand Up @@ -354,12 +355,17 @@ export default class CodeMirror5Emulation extends BaseCodeMirror5Emulation {
public static commands = (() => {
const commands: Record<string, CodeMirror5Command> = {
...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,
Expand Down
20 changes: 20 additions & 0 deletions packages/editor/CodeMirror/editorCommands/insertLineAfter.test.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
23 changes: 23 additions & 0 deletions packages/editor/CodeMirror/editorCommands/insertLineAfter.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit fdbfa78

Please sign in to comment.