2020-05-02 17:41:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2020-05-03 19:44:49 +02:00
|
|
|
import { FormNote } from './types';
|
2021-05-13 10:34:03 +02:00
|
|
|
import contextMenu, { openItemById } from './contextMenu';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
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';
|
2021-11-23 14:12:27 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2021-10-01 20:35:27 +02:00
|
|
|
const bridge = require('@electron/remote').require('./bridge').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const { urlDecode } = require('@joplin/lib/string-utils');
|
|
|
|
const urlUtils = require('@joplin/lib/urlUtils');
|
2021-11-22 19:17:28 +02:00
|
|
|
const { fileUriToPath } = require('@joplin/lib/urlUtils');
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function useMessageHandler(scrollWhenReady: any, setScrollWhenReady: Function, editorRef: any, setLocalSearchResultCount: Function, dispatch: Function, formNote: FormNote) {
|
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;
|
|
|
|
|
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 };
|
|
|
|
setScrollWhenReady(null);
|
|
|
|
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,
|
|
|
|
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'); },
|
2021-05-13 10:34:03 +02:00
|
|
|
}, dispatch);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
menu.popup(bridge().window());
|
|
|
|
} else if (msg.indexOf('joplin://') === 0) {
|
2021-05-13 10:34:03 +02:00
|
|
|
const { itemId, hash } = urlUtils.parseResourceUrl(msg);
|
|
|
|
await openItemById(itemId, dispatch, hash);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
} else if (urlUtils.urlProtocol(msg)) {
|
|
|
|
if (msg.indexOf('file://') === 0) {
|
2021-06-04 13:34:30 +02:00
|
|
|
// When using the file:// protocol, openPath doesn't work (does
|
|
|
|
// nothing) with URL-encoded paths.
|
|
|
|
//
|
|
|
|
// shell.openPath seems to work with file:// urls on Windows,
|
|
|
|
// but doesn't on macOS, so we need to convert it to a path
|
|
|
|
// before passing it to openPath.
|
2021-11-23 14:12:27 +02:00
|
|
|
const decodedPath = fileUriToPath(urlDecode(msg), shim.platformName());
|
2021-06-04 13:34:30 +02:00
|
|
|
require('electron').shell.openPath(decodedPath);
|
2020-05-02 17:41:07 +02:00
|
|
|
} else {
|
|
|
|
require('electron').shell.openExternal(msg);
|
|
|
|
}
|
|
|
|
} 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') {
|
|
|
|
void PostMessageService.instance().postMessage(arg0);
|
2020-05-02 17:41:07 +02:00
|
|
|
} else {
|
|
|
|
bridge().showErrorMessageBox(_('Unsupported link or message: %s', msg));
|
|
|
|
}
|
2020-05-03 19:44:49 +02:00
|
|
|
}, [dispatch, setLocalSearchResultCount, scrollWhenReady, formNote]);
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|