2020-05-02 17:41:07 +02:00
|
|
|
import { useEffect } from 'react';
|
2020-10-09 19:35:46 +02:00
|
|
|
import { FormNote, ScrollOptionTypes } from './types';
|
2021-02-06 18:01:06 +02:00
|
|
|
import editorCommandDeclarations from '../commands/editorCommandDeclarations';
|
2021-01-23 17:51:19 +02:00
|
|
|
import CommandService, { CommandDeclaration, CommandRuntime, CommandContext } from '@joplin/lib/services/CommandService';
|
2021-01-22 19:41:11 +02:00
|
|
|
import time from '@joplin/lib/time';
|
2021-01-29 20:45:11 +02:00
|
|
|
import { reg } from '@joplin/lib/registry';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
const commandsWithDependencies = [
|
|
|
|
require('../commands/showLocalSearch'),
|
|
|
|
require('../commands/focusElementNoteTitle'),
|
|
|
|
require('../commands/focusElementNoteBody'),
|
|
|
|
];
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
interface HookDependencies {
|
2020-11-12 21:29:22 +02:00
|
|
|
formNote: FormNote;
|
|
|
|
setShowLocalSearch: Function;
|
|
|
|
dispatch: Function;
|
|
|
|
noteSearchBarRef: any;
|
|
|
|
editorRef: any;
|
|
|
|
titleInputRef: any;
|
|
|
|
saveNoteAndWait: Function;
|
2020-11-14 00:50:03 +02:00
|
|
|
setFormNote: Function;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:50:03 +02:00
|
|
|
function editorCommandRuntime(declaration: CommandDeclaration, editorRef: any, setFormNote: Function): CommandRuntime {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (_context: CommandContext, ...args: any[]) => {
|
2021-01-08 00:03:13 +02:00
|
|
|
if (!editorRef.current) {
|
|
|
|
reg.logger().warn('Received command, but editor is gone', declaration.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
if (!editorRef.current.execCommand) {
|
|
|
|
reg.logger().warn('Received command, but editor cannot execute commands', declaration.name);
|
2020-10-18 22:52:10 +02:00
|
|
|
return;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
if (declaration.name === 'insertDateTime') {
|
|
|
|
return editorRef.current.execCommand({
|
|
|
|
name: 'insertText',
|
|
|
|
value: time.formatMsToLocal(new Date().getTime()),
|
|
|
|
});
|
|
|
|
} else if (declaration.name === 'scrollToHash') {
|
|
|
|
return editorRef.current.scrollTo({
|
|
|
|
type: ScrollOptionTypes.Hash,
|
|
|
|
value: args[0],
|
|
|
|
});
|
2020-11-14 12:57:09 +02:00
|
|
|
} else if (declaration.name === 'editor.setText') {
|
2020-11-14 00:50:03 +02:00
|
|
|
setFormNote((prev: FormNote) => {
|
|
|
|
return { ...prev, body: args[0] };
|
|
|
|
});
|
2020-10-18 22:52:10 +02:00
|
|
|
} else {
|
|
|
|
return editorRef.current.execCommand({
|
|
|
|
name: declaration.name,
|
|
|
|
value: args[0],
|
|
|
|
});
|
|
|
|
}
|
2020-07-03 23:32:39 +02:00
|
|
|
},
|
2020-10-18 22:52:10 +02:00
|
|
|
enabledCondition: '!modalDialogVisible && markdownEditorPaneVisible && oneNoteSelected && noteIsMarkdown',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
}
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function useWindowCommandHandler(dependencies: HookDependencies) {
|
2021-02-06 18:01:06 +02:00
|
|
|
const { setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef, setFormNote } = dependencies;
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
useEffect(() => {
|
2021-02-06 18:01:06 +02:00
|
|
|
for (const declaration of editorCommandDeclarations) {
|
2020-11-14 00:50:03 +02:00
|
|
|
CommandService.instance().registerRuntime(declaration.name, editorCommandRuntime(declaration, editorRef, setFormNote));
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
const dependencies = {
|
|
|
|
editorRef,
|
|
|
|
setShowLocalSearch,
|
|
|
|
noteSearchBarRef,
|
|
|
|
titleInputRef,
|
|
|
|
};
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
for (const command of commandsWithDependencies) {
|
|
|
|
CommandService.instance().registerRuntime(command.declaration.name, command.runtime(dependencies));
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
return () => {
|
2021-02-06 18:01:06 +02:00
|
|
|
for (const declaration of editorCommandDeclarations) {
|
2020-07-03 23:32:39 +02:00
|
|
|
CommandService.instance().unregisterRuntime(declaration.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const command of commandsWithDependencies) {
|
|
|
|
CommandService.instance().unregisterRuntime(command.declaration.name);
|
|
|
|
}
|
|
|
|
};
|
2021-02-06 18:01:06 +02:00
|
|
|
}, [editorRef, setShowLocalSearch, noteSearchBarRef, titleInputRef]);
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|