mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-15 09:04:04 +02:00
51732a5adb
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
153 lines
5.1 KiB
TypeScript
153 lines
5.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { FormNote } from './types';
|
|
const BaseItem = require('lib/models/BaseItem');
|
|
const { _ } = require('lib/locale');
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
const Resource = require('lib/models/Resource.js');
|
|
const { bridge } = require('electron').remote.require('./bridge');
|
|
const { urlDecode } = require('lib/string-utils');
|
|
const urlUtils = require('lib/urlUtils');
|
|
const ResourceFetcher = require('lib/services/ResourceFetcher.js');
|
|
const Menu = bridge().Menu;
|
|
const MenuItem = bridge().MenuItem;
|
|
const fs = require('fs-extra');
|
|
const { clipboard } = require('electron');
|
|
const { toSystemSlashes } = require('lib/path-utils');
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
export default function useMessageHandler(scrollWhenReady:any, setScrollWhenReady:Function, editorRef:any, setLocalSearchResultCount:Function, dispatch:Function, formNote:FormNote) {
|
|
return useCallback(async (event: any) => {
|
|
const msg = event.channel ? event.channel : '';
|
|
const args = event.args;
|
|
const arg0 = args && args.length >= 1 ? args[0] : null;
|
|
|
|
if (msg !== 'percentScroll') console.info(`Got ipc-message: ${msg}`, args);
|
|
|
|
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') {
|
|
const itemType = arg0 && arg0.type;
|
|
|
|
const menu = new Menu();
|
|
|
|
if (itemType === 'image' || itemType === 'resource') {
|
|
const resource = await Resource.load(arg0.resourceId);
|
|
const resourcePath = Resource.fullPath(resource);
|
|
|
|
menu.append(
|
|
new MenuItem({
|
|
label: _('Open...'),
|
|
click: async () => {
|
|
const ok = bridge().openExternal(`file://${resourcePath}`);
|
|
if (!ok) bridge().showErrorMessageBox(_('This file could not be opened: %s', resourcePath));
|
|
},
|
|
})
|
|
);
|
|
|
|
menu.append(
|
|
new MenuItem({
|
|
label: _('Save as...'),
|
|
click: async () => {
|
|
const filePath = bridge().showSaveDialog({
|
|
defaultPath: resource.filename ? resource.filename : resource.title,
|
|
});
|
|
if (!filePath) return;
|
|
await fs.copy(resourcePath, filePath);
|
|
},
|
|
})
|
|
);
|
|
|
|
menu.append(
|
|
new MenuItem({
|
|
label: _('Copy path to clipboard'),
|
|
click: async () => {
|
|
clipboard.writeText(toSystemSlashes(resourcePath));
|
|
},
|
|
})
|
|
);
|
|
} else if (itemType === 'text') {
|
|
menu.append(
|
|
new MenuItem({
|
|
label: _('Copy'),
|
|
click: async () => {
|
|
clipboard.writeText(arg0.textToCopy);
|
|
},
|
|
})
|
|
);
|
|
} else if (itemType === 'link') {
|
|
menu.append(
|
|
new MenuItem({
|
|
label: _('Copy Link Address'),
|
|
click: async () => {
|
|
clipboard.writeText(arg0.textToCopy);
|
|
},
|
|
})
|
|
);
|
|
} else {
|
|
reg.logger().error(`Unhandled item type: ${itemType}`);
|
|
return;
|
|
}
|
|
|
|
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;
|
|
}
|
|
const filePath = Resource.fullPath(item);
|
|
bridge().openItem(filePath);
|
|
} else if (item.type_ === BaseModel.TYPE_NOTE) {
|
|
dispatch({
|
|
type: 'FOLDER_AND_NOTE_SELECT',
|
|
folderId: item.parent_id,
|
|
noteId: item.id,
|
|
hash: resourceUrlInfo.hash,
|
|
historyNoteAction: {
|
|
id: formNote.id,
|
|
parent_id: formNote.parent_id,
|
|
},
|
|
});
|
|
} 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));
|
|
}
|
|
}, [dispatch, setLocalSearchResultCount, scrollWhenReady, formNote]);
|
|
}
|