2020-05-02 17:41:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2024-05-21 02:28:19 +02:00
|
|
|
import { FormNote, HtmlToMarkdownHandler, MarkupToHtmlHandler, ScrollOptions } from './types';
|
2021-12-27 18:44:53 +02:00
|
|
|
import contextMenu from './contextMenu';
|
2020-12-11 14:00:24 +02:00
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
2021-01-12 01:33:10 +02:00
|
|
|
import PostMessageService from '@joplin/lib/services/PostMessageService';
|
2021-11-22 19:17:28 +02:00
|
|
|
import ResourceFetcher from '@joplin/lib/services/ResourceFetcher';
|
|
|
|
import { reg } from '@joplin/lib/registry';
|
2024-11-08 17:32:05 +02:00
|
|
|
import bridge from '../../../services/bridge';
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
export default function useMessageHandler(
|
|
|
|
scrollWhenReady: ScrollOptions|null,
|
|
|
|
clearScrollWhenReady: ()=> void,
|
|
|
|
windowId: string,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
|
|
|
editorRef: any,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
|
|
|
setLocalSearchResultCount: Function,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
|
|
|
dispatch: Function,
|
|
|
|
formNote: FormNote,
|
|
|
|
htmlToMd: HtmlToMarkdownHandler,
|
|
|
|
mdToHtml: MarkupToHtmlHandler,
|
|
|
|
) {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-05-02 17:41:07 +02:00
|
|
|
return useCallback(async (event: any) => {
|
|
|
|
const msg = event.channel ? event.channel : '';
|
|
|
|
const args = event.args;
|
|
|
|
const arg0 = args && args.length >= 1 ? args[0] : null;
|
|
|
|
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2021-11-22 19:17:28 +02:00
|
|
|
if (msg !== 'percentScroll') console.info(`Got ipc-message: ${msg}`, arg0);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
if (msg.indexOf('error:') === 0) {
|
|
|
|
const s = msg.split(':');
|
|
|
|
s.splice(0, 1);
|
|
|
|
reg.logger().error(s.join(':'));
|
|
|
|
} else if (msg === 'noteRenderComplete') {
|
|
|
|
if (scrollWhenReady) {
|
|
|
|
const options = { ...scrollWhenReady };
|
2024-05-21 02:28:19 +02:00
|
|
|
clearScrollWhenReady();
|
2020-05-02 17:41:07 +02:00
|
|
|
editorRef.current.scrollTo(options);
|
|
|
|
}
|
|
|
|
} else if (msg === 'setMarkerCount') {
|
|
|
|
setLocalSearchResultCount(arg0);
|
|
|
|
} else if (msg.indexOf('markForDownload:') === 0) {
|
|
|
|
const s = msg.split(':');
|
|
|
|
if (s.length < 2) throw new Error(`Invalid message: ${msg}`);
|
2021-01-22 19:41:11 +02:00
|
|
|
void ResourceFetcher.instance().markForDownload(s[1]);
|
2020-05-02 17:41:07 +02:00
|
|
|
} else if (msg === 'contextMenu') {
|
2020-05-09 20:18:41 +02:00
|
|
|
const menu = await contextMenu({
|
|
|
|
itemType: arg0 && arg0.type,
|
|
|
|
resourceId: arg0.resourceId,
|
2022-03-28 18:10:29 +02:00
|
|
|
filename: arg0.filename,
|
|
|
|
mime: arg0.mime,
|
2020-05-09 20:18:41 +02:00
|
|
|
textToCopy: arg0.textToCopy,
|
2020-10-22 17:32:13 +02:00
|
|
|
linkToCopy: arg0.linkToCopy || null,
|
2020-08-02 13:16:42 +02:00
|
|
|
htmlToCopy: '',
|
|
|
|
insertContent: () => { console.warn('insertContent() not implemented'); },
|
2023-02-15 15:59:32 +02:00
|
|
|
fireEditorEvent: () => { console.warn('fireEditorEvent() not implemented'); },
|
2024-01-26 21:11:05 +02:00
|
|
|
htmlToMd,
|
|
|
|
mdToHtml,
|
2021-05-13 10:34:03 +02:00
|
|
|
}, dispatch);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
menu.popup({ window: bridge().activeWindow() });
|
2020-05-02 17:41:07 +02:00
|
|
|
} else if (msg.indexOf('#') === 0) {
|
|
|
|
// This is an internal anchor, which is handled by the WebView so skip this case
|
2020-12-11 14:00:24 +02:00
|
|
|
} else if (msg === 'contentScriptExecuteCommand') {
|
|
|
|
const commandName = arg0.name;
|
|
|
|
const commandArgs = arg0.args || [];
|
|
|
|
void CommandService.instance().execute(commandName, ...commandArgs);
|
2021-01-12 01:33:10 +02:00
|
|
|
} else if (msg === 'postMessageService.message') {
|
2024-11-08 17:32:05 +02:00
|
|
|
void PostMessageService.instance().postMessage({ ...arg0, windowId });
|
2022-09-11 15:58:32 +02:00
|
|
|
} else if (msg === 'openPdfViewer') {
|
|
|
|
await CommandService.instance().execute('openPdfViewer', arg0.resourceId, arg0.pageNo);
|
2020-05-02 17:41:07 +02:00
|
|
|
} else {
|
2021-12-27 18:44:53 +02:00
|
|
|
await CommandService.instance().execute('openItem', msg);
|
|
|
|
// bridge().showErrorMessageBox(_('Unsupported link or message: %s', msg));
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-05-03 19:44:49 +02:00
|
|
|
}, [dispatch, setLocalSearchResultCount, scrollWhenReady, formNote]);
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|