1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Plugins: Add command "editorSetText" for desktop app

This commit is contained in:
Laurent Cozic 2020-11-13 22:50:03 +00:00
parent 872d2942b4
commit aaaa4e4569
3 changed files with 21 additions and 4 deletions

View File

@ -230,7 +230,16 @@ function NoteEditor(props: NoteEditorProps) {
}
}, [handleProvisionalFlag, formNote, isNewNote, titleHasBeenManuallyChanged]);
useWindowCommandHandler({ dispatch: props.dispatch, formNote, setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef, saveNoteAndWait });
useWindowCommandHandler({
dispatch: props.dispatch,
formNote,
setShowLocalSearch,
noteSearchBarRef,
editorRef,
titleInputRef,
saveNoteAndWait,
setFormNote,
});
const onDrop = useDropHandler({ editorRef });

View File

@ -89,6 +89,9 @@ const declarations: CommandDeclaration[] = [
{
name: 'replaceSelection',
},
{
name: 'editorSetText',
},
];
export default declarations;

View File

@ -19,9 +19,10 @@ interface HookDependencies {
editorRef: any;
titleInputRef: any;
saveNoteAndWait: Function;
setFormNote: Function;
}
function editorCommandRuntime(declaration: CommandDeclaration, editorRef: any): CommandRuntime {
function editorCommandRuntime(declaration: CommandDeclaration, editorRef: any, setFormNote: Function): CommandRuntime {
return {
execute: async (_context: CommandContext, ...args: any[]) => {
if (!editorRef.current.execCommand) {
@ -39,6 +40,10 @@ function editorCommandRuntime(declaration: CommandDeclaration, editorRef: any):
type: ScrollOptionTypes.Hash,
value: args[0],
});
} else if (declaration.name === 'editorSetText') {
setFormNote((prev: FormNote) => {
return { ...prev, body: args[0] };
});
} else {
return editorRef.current.execCommand({
name: declaration.name,
@ -51,11 +56,11 @@ function editorCommandRuntime(declaration: CommandDeclaration, editorRef: any):
}
export default function useWindowCommandHandler(dependencies: HookDependencies) {
const { setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef } = dependencies;
const { setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef, setFormNote } = dependencies;
useEffect(() => {
for (const declaration of editorCommandDeclarations) {
CommandService.instance().registerRuntime(declaration.name, editorCommandRuntime(declaration, editorRef));
CommandService.instance().registerRuntime(declaration.name, editorCommandRuntime(declaration, editorRef, setFormNote));
}
const dependencies = {