2020-05-02 17:41:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2020-05-03 19:44:49 +02:00
|
|
|
import { FormNote } from './types';
|
2020-05-09 20:18:41 +02:00
|
|
|
import contextMenu from './contextMenu';
|
2020-11-07 17:59:37 +02:00
|
|
|
import ResourceEditWatcher from '@joplin/lib/services/ResourceEditWatcher/index';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
const BaseItem = require('@joplin/lib/models/BaseItem');
|
|
|
|
const BaseModel = require('@joplin/lib/BaseModel').default;
|
|
|
|
const Resource = require('@joplin/lib/models/Resource.js');
|
2020-10-09 19:35:46 +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');
|
|
|
|
const ResourceFetcher = require('@joplin/lib/services/ResourceFetcher.js');
|
|
|
|
const { reg } = require('@joplin/lib/registry.js');
|
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;
|
|
|
|
|
2020-07-23 21:56:53 +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}`);
|
|
|
|
ResourceFetcher.instance().markForDownload(s[1]);
|
|
|
|
} 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'); },
|
2020-05-09 20:18:41 +02:00
|
|
|
});
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
menu.popup(bridge().window());
|
|
|
|
} else if (msg.indexOf('joplin://') === 0) {
|
|
|
|
const resourceUrlInfo = urlUtils.parseResourceUrl(msg);
|
|
|
|
const itemId = resourceUrlInfo.itemId;
|
|
|
|
const item = await BaseItem.loadItemById(itemId);
|
|
|
|
|
|
|
|
if (!item) throw new Error(`No item with ID ${itemId}`);
|
|
|
|
|
|
|
|
if (item.type_ === BaseModel.TYPE_RESOURCE) {
|
|
|
|
const localState = await Resource.localState(item);
|
|
|
|
if (localState.fetch_status !== Resource.FETCH_STATUS_DONE || !!item.encryption_blob_encrypted) {
|
|
|
|
if (localState.fetch_status === Resource.FETCH_STATUS_ERROR) {
|
|
|
|
bridge().showErrorMessageBox(`${_('There was an error downloading this attachment:')}\n\n${localState.fetch_error}`);
|
|
|
|
} else {
|
|
|
|
bridge().showErrorMessageBox(_('This attachment is not downloaded or not decrypted yet'));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-05-30 14:25:05 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await ResourceEditWatcher.instance().openAndWatch(item.id);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
2020-05-02 17:41:07 +02:00
|
|
|
} else if (item.type_ === BaseModel.TYPE_NOTE) {
|
|
|
|
dispatch({
|
|
|
|
type: 'FOLDER_AND_NOTE_SELECT',
|
|
|
|
folderId: item.parent_id,
|
|
|
|
noteId: item.id,
|
|
|
|
hash: resourceUrlInfo.hash,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unsupported item type: ${item.type_}`);
|
|
|
|
}
|
|
|
|
} else if (urlUtils.urlProtocol(msg)) {
|
|
|
|
if (msg.indexOf('file://') === 0) {
|
|
|
|
// When using the file:// protocol, openExternal doesn't work (does nothing) with URL-encoded paths
|
|
|
|
require('electron').shell.openExternal(urlDecode(msg));
|
|
|
|
} 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
|
|
|
|
} 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
|
|
|
}
|