1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-03 22:37:35 +02:00
joplin/ElectronClient/gui/NoteEditor/utils/useWindowCommandHandler.ts
Laurent Cozic 51732a5adb Desktop: Fixed various bugs and regressions following note editor refactoring
Squashed commit of the following:

commit 5fde36f5c3fa7c7efbce6d81f48fe841c823e88c
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 18:43:20 2020 +0100

    Cannot fix for now

commit 251284db3c8b7da6db83f3e06fd19bfdc8b8dd3f
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 18:31:08 2020 +0100

    Fixed print to multiple PDFs logic

commit 00d9557996fb984b400fe650594150ae2681e03f
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 17:49:20 2020 +0100

    Fixed local search in TinyMCE

commit 46778bf0a79f3bba9ddbc27389fadce4f8944507
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 17:37:31 2020 +0100

    Restored note toolbar buttons

commit 3e623c98f0a1cf08bf7d0136f0c8982c5e1ddcd8
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 12:30:57 2020 +0100

    Various fixes and moved note toolbar to left of title

commit 790262fe9df5b08d4a619e5244d2906047b39855
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 11:21:11 2020 +0100

    Clean up

commit cea30f42e69014ecabda6fa5083199a1ba7b7510
Author: Laurent Cozic <laurent@cozic.net>
Date:   Sun May 3 11:08:23 2020 +0100

    Fixed note loading in TinyMCE
2020-05-03 18:44:49 +01:00

109 lines
3.1 KiB
TypeScript

import { useEffect } from 'react';
import { FormNote, EditorCommand } from './types';
const { time } = require('lib/time-utils.js');
const { reg } = require('lib/registry.js');
const NoteListUtils = require('../../utils/NoteListUtils');
interface HookDependencies {
windowCommand: any,
formNote:FormNote,
setShowLocalSearch:Function,
dispatch:Function,
noteSearchBarRef:any,
editorRef:any,
titleInputRef:any,
saveNoteAndWait: Function,
}
export default function useWindowCommandHandler(dependencies:HookDependencies) {
const { windowCommand, dispatch, formNote, setShowLocalSearch, noteSearchBarRef, editorRef, titleInputRef, saveNoteAndWait } = dependencies;
useEffect(() => {
async function processCommand() {
const command = windowCommand;
if (!command || !formNote) return;
reg.logger().debug('NoteEditor::useWindowCommandHandler:', command);
const editorCmd: EditorCommand = { name: '', value: command.value };
let fn: Function = null;
// These commands can be forwarded directly to the note body editor
// without transformation.
const directMapCommands = [
'textCode',
'textBold',
'textItalic',
'textLink',
'attachFile',
'textNumberedList',
'textBulletedList',
'textCheckbox',
'textHeading',
'textHorizontalRule',
];
if (directMapCommands.includes(command.name)) {
editorCmd.name = command.name;
} else if (command.name === 'commandStartExternalEditing') {
fn = async () => {
await saveNoteAndWait(formNote);
NoteListUtils.startExternalEditing(formNote.id);
};
} else if (command.name === 'commandStopExternalEditing') {
fn = () => {
NoteListUtils.stopExternalEditing(formNote.id);
};
} else if (command.name === 'insertDateTime') {
editorCmd.name = 'insertText',
editorCmd.value = time.formatMsToLocal(new Date().getTime());
} else if (command.name === 'showLocalSearch') {
if (editorRef.current && editorRef.current.supportsCommand('search')) {
editorCmd.name = 'search';
} else {
setShowLocalSearch(true);
if (noteSearchBarRef.current) noteSearchBarRef.current.wrappedInstance.focus();
}
} else if (command.name === 'insertTemplate') {
editorCmd.name = 'insertText',
editorCmd.value = time.formatMsToLocal(new Date().getTime());
}
if (command.name === 'focusElement' && command.target === 'noteTitle') {
fn = () => {
if (!titleInputRef.current) return;
titleInputRef.current.focus();
};
}
if (command.name === 'focusElement' && command.target === 'noteBody') {
editorCmd.name = 'focus';
}
reg.logger().debug('NoteEditor::useWindowCommandHandler: Dispatch:', editorCmd, fn);
if (!editorCmd.name && !fn) return;
dispatch({
type: 'WINDOW_COMMAND',
name: null,
});
requestAnimationFrame(() => {
if (fn) {
fn();
} else {
if (!editorRef.current.execCommand) {
reg.logger().warn('Received command, but editor cannot execute commands', editorCmd);
} else {
editorRef.current.execCommand(editorCmd);
}
}
});
}
processCommand();
}, [windowCommand, dispatch, formNote, saveNoteAndWait]);
}