Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Fixes #10152: Fix broken plugin API: editor.execCommand #10153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ packages/app-mobile/components/NoteEditor/NoteEditor.js
packages/app-mobile/components/NoteEditor/SearchPanel.js
packages/app-mobile/components/NoteEditor/commandDeclarations.js
packages/app-mobile/components/NoteEditor/hooks/useCodeMirrorPlugins.js
packages/app-mobile/components/NoteEditor/hooks/useEditorCommandHandler.test.js
packages/app-mobile/components/NoteEditor/hooks/useEditorCommandHandler.js
packages/app-mobile/components/NoteEditor/hooks/useKeyboardVisible.js
packages/app-mobile/components/NoteEditor/types.js
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ packages/app-mobile/components/NoteEditor/NoteEditor.js
packages/app-mobile/components/NoteEditor/SearchPanel.js
packages/app-mobile/components/NoteEditor/commandDeclarations.js
packages/app-mobile/components/NoteEditor/hooks/useCodeMirrorPlugins.js
packages/app-mobile/components/NoteEditor/hooks/useEditorCommandHandler.test.js
packages/app-mobile/components/NoteEditor/hooks/useEditorCommandHandler.js
packages/app-mobile/components/NoteEditor/hooks/useKeyboardVisible.js
packages/app-mobile/components/NoteEditor/types.js
Expand Down
19 changes: 19 additions & 0 deletions jest.base-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ module.exports = () => {
global.console = jestConsole;
});
};

// jsdom extensions
if (typeof document !== 'undefined') {
// Prevents the CodeMirror error "getClientRects is undefined".
// See https://github.com/jsdom/jsdom/issues/3002#issue-652790925
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = jest.fn();
range.getClientRects = () => {
return {
length: 0,
item: () => null,
[Symbol.iterator]: jest.fn(),
};
};

return range;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import CommandService from '@joplin/lib/services/CommandService';
import useEditorCommandHandler from './useEditorCommandHandler';
import commandDeclarations from '../commandDeclarations';
import createTestEditorControl from '@joplin/editor/CodeMirror/testUtil/createEditorControl';
import { renderHook } from '@testing-library/react-native';
import { defaultState } from '@joplin/lib/reducer';


describe('useEditorCommandHandler', () => {
beforeAll(() => {
const storeMock = { getState: () => defaultState, dispatch: jest.fn() };
CommandService.instance().initialize(storeMock, false, ()=>({}));

for (const declaration of commandDeclarations) {
CommandService.instance().registerDeclaration(declaration);
}
});
it('should support running custom commands with editor.execCommand', async () => {
const editor = createTestEditorControl('Test.');
renderHook(() => useEditorCommandHandler(editor));

const testCommandCallback = jest.fn();
editor.registerCommand('myCommand', testCommandCallback);
expect(testCommandCallback).not.toHaveBeenCalled();

// Should support running commands with arguments
await CommandService.instance().execute('editor.execCommand', { name: 'myCommand', args: ['a', 'b', 'c'] });
expect(testCommandCallback).toHaveBeenCalledTimes(1);
expect(testCommandCallback).toHaveBeenLastCalledWith('a', 'b', 'c');

// Should support running commands without arguments
await CommandService.instance().execute('editor.execCommand', { name: 'myCommand' });
expect(testCommandCallback).toHaveBeenCalledTimes(2);
expect(testCommandCallback).toHaveBeenLastCalledWith();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CommandService, { CommandContext, CommandDeclaration } from '@joplin/lib/services/CommandService';
import { EditorControl } from '../types';
import { EditorControl } from '@joplin/editor/types';
import { useEffect } from 'react';
import commandDeclarations, { enabledCondition } from '../commandDeclarations';
import Logger from '@joplin/utils/Logger';
Expand All @@ -12,9 +12,13 @@ const commandRuntime = (declaration: CommandDeclaration, editor: EditorControl)
// Many editor CodeMirror commands are missing the editor. prefix.
let commandName = declaration.name.replace(/^editor\./, '');

if (declaration.name === 'editor.execCommand') {
commandName = args[0];
args = args.slice(1);
if (commandName === 'execCommand') {
commandName = args[0]?.name;
args = args[0]?.args ?? [];

if (!commandName) {
throw new Error('editor.execCommand is missing the name of the command to execute');
}
}

if (!(await editor.supportsCommand(commandName))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ describe('CodeMirror5Emulation', () => {
expect(testExtensionFn1).toHaveBeenCalledTimes(1);
expect(testExtensionFn2).toHaveBeenCalledTimes(1);
});

it('defineExtension should register an extension where this points to the editor', () => {
const codeMirror = makeCodeMirrorEmulation('Test...');
let lastThis = null;

codeMirror.defineExtension('testExtension', function() {
lastThis = this;
});
codeMirror.execCommand('testExtension');

expect(lastThis).toBe(codeMirror);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export default class CodeMirror5Emulation extends BaseCodeMirror5Emulation {
if (name in CodeMirror5Emulation.commands) {
return CodeMirror5Emulation.commands[name as (keyof typeof CodeMirror5Emulation.commands)](this);
} else if (typeof this._userExtensions[name] === 'function') {
return this._userExtensions[name](...args);
return this._userExtensions[name].call(this, ...args);
}
}
}
Expand Down
15 changes: 0 additions & 15 deletions packages/editor/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
require('../../jest.base-setup.js')();

// Prevents the CodeMirror error "getClientRects is undefined".
// See https://github.com/jsdom/jsdom/issues/3002#issue-652790925
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = jest.fn();
range.getClientRects = () => {
return {
length: 0,
item: () => null,
[Symbol.iterator]: jest.fn(),
};
};

return range;
};
Loading