1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-mobile/components/NoteEditor/hooks/useEditorCommandHandler.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import CommandService, { CommandContext, CommandDeclaration } from '@joplin/lib/services/CommandService';
import { EditorControl } from '../types';
import { useEffect } from 'react';
import commandDeclarations, { enabledCondition } from '../commandDeclarations';
import Logger from '@joplin/utils/Logger';
const logger = Logger.create('useEditorCommandHandler');
const commandRuntime = (declaration: CommandDeclaration, editor: EditorControl) => {
return {
execute: async (_context: CommandContext, ...args: any[]) => {
// 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 (!(await editor.supportsCommand(commandName))) {
logger.warn('Command not supported by editor: ', commandName);
return;
}
return await editor.execCommand(commandName, ...args);
},
enabledCondition: enabledCondition(declaration.name),
};
};
const useEditorCommandHandler = (editorControl: EditorControl) => {
useEffect(() => {
const commandService = CommandService.instance();
for (const declaration of commandDeclarations) {
commandService.registerRuntime(declaration.name, commandRuntime(declaration, editorControl));
}
return () => {
for (const declaration of commandDeclarations) {
commandService.unregisterRuntime(declaration.name);
}
};
});
};
export default useEditorCommandHandler;