2020-11-07 17:59:37 +02:00
|
|
|
import ResourceEditWatcher from '@joplin/lib/services/ResourceEditWatcher/index';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2020-06-04 19:24:11 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const bridge = require('electron').remote.require('./bridge').default;
|
2020-05-09 20:18:41 +02:00
|
|
|
const Menu = bridge().Menu;
|
|
|
|
const MenuItem = bridge().MenuItem;
|
2020-11-07 17:59:37 +02:00
|
|
|
const Resource = require('@joplin/lib/models/Resource.js');
|
2020-05-09 20:18:41 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const { clipboard } = require('electron');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { toSystemSlashes } = require('@joplin/lib/path-utils');
|
2020-05-09 20:18:41 +02:00
|
|
|
|
|
|
|
export enum ContextMenuItemType {
|
|
|
|
None = '',
|
|
|
|
Image = 'image',
|
|
|
|
Resource = 'resource',
|
|
|
|
Text = 'text',
|
|
|
|
Link = 'link',
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ContextMenuOptions {
|
|
|
|
itemType: ContextMenuItemType,
|
|
|
|
resourceId: string,
|
2020-09-08 01:29:31 +02:00
|
|
|
linkToCopy: string,
|
2020-05-09 20:18:41 +02:00
|
|
|
textToCopy: string,
|
2020-08-02 13:16:42 +02:00
|
|
|
htmlToCopy: string,
|
|
|
|
insertContent: Function,
|
|
|
|
isReadOnly?: boolean,
|
2020-05-09 20:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ContextMenuItem {
|
|
|
|
label: string,
|
|
|
|
onAction: Function,
|
|
|
|
isActive: Function,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ContextMenuItems {
|
|
|
|
[key:string]: ContextMenuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resourceInfo(options:ContextMenuOptions):Promise<any> {
|
|
|
|
const resource = options.resourceId ? await Resource.load(options.resourceId) : null;
|
|
|
|
const resourcePath = resource ? Resource.fullPath(resource) : '';
|
|
|
|
return { resource, resourcePath };
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:21:37 +02:00
|
|
|
function handleCopyToClipboard(options:ContextMenuOptions) {
|
|
|
|
if (options.textToCopy) {
|
|
|
|
clipboard.writeText(options.textToCopy);
|
|
|
|
} else if (options.htmlToCopy) {
|
|
|
|
clipboard.writeHTML(options.htmlToCopy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:18:41 +02:00
|
|
|
export function menuItems():ContextMenuItems {
|
|
|
|
return {
|
|
|
|
open: {
|
|
|
|
label: _('Open...'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
2020-05-30 14:25:05 +02:00
|
|
|
try {
|
|
|
|
await ResourceEditWatcher.instance().openAndWatch(options.resourceId);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
2020-05-09 20:18:41 +02:00
|
|
|
},
|
|
|
|
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
|
|
|
|
},
|
|
|
|
saveAs: {
|
|
|
|
label: _('Save as...'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
|
|
|
const { resourcePath, resource } = await resourceInfo(options);
|
|
|
|
const filePath = bridge().showSaveDialog({
|
|
|
|
defaultPath: resource.filename ? resource.filename : resource.title,
|
|
|
|
});
|
|
|
|
if (!filePath) return;
|
|
|
|
await fs.copy(resourcePath, filePath);
|
|
|
|
},
|
|
|
|
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
|
|
|
|
},
|
|
|
|
revealInFolder: {
|
|
|
|
label: _('Reveal file in folder'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
|
|
|
const { resourcePath } = await resourceInfo(options);
|
|
|
|
bridge().showItemInFolder(resourcePath);
|
|
|
|
},
|
|
|
|
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
|
|
|
|
},
|
|
|
|
copyPathToClipboard: {
|
|
|
|
label: _('Copy path to clipboard'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
|
|
|
const { resourcePath } = await resourceInfo(options);
|
|
|
|
clipboard.writeText(toSystemSlashes(resourcePath));
|
|
|
|
},
|
|
|
|
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
|
|
|
|
},
|
2020-08-02 13:16:42 +02:00
|
|
|
cut: {
|
|
|
|
label: _('Cut'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
2020-10-23 14:21:37 +02:00
|
|
|
handleCopyToClipboard(options);
|
2020-08-02 13:16:42 +02:00
|
|
|
options.insertContent('');
|
|
|
|
},
|
|
|
|
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !options.isReadOnly && (!!options.textToCopy || !!options.htmlToCopy),
|
|
|
|
},
|
2020-05-09 20:18:41 +02:00
|
|
|
copy: {
|
|
|
|
label: _('Copy'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
2020-10-23 14:21:37 +02:00
|
|
|
handleCopyToClipboard(options);
|
2020-05-09 20:18:41 +02:00
|
|
|
},
|
2020-08-02 13:16:42 +02:00
|
|
|
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !!options.textToCopy || !!options.htmlToCopy,
|
|
|
|
},
|
|
|
|
paste: {
|
|
|
|
label: _('Paste'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
|
|
|
const content = clipboard.readHTML() ? clipboard.readHTML() : clipboard.readText();
|
|
|
|
options.insertContent(content);
|
|
|
|
},
|
|
|
|
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !options.isReadOnly && (!!clipboard.readText() || !!clipboard.readHTML()),
|
2020-05-09 20:18:41 +02:00
|
|
|
},
|
|
|
|
copyLinkUrl: {
|
|
|
|
label: _('Copy Link Address'),
|
|
|
|
onAction: async (options:ContextMenuOptions) => {
|
2020-09-08 01:29:31 +02:00
|
|
|
clipboard.writeText(options.linkToCopy !== null ? options.linkToCopy : options.textToCopy);
|
2020-05-09 20:18:41 +02:00
|
|
|
},
|
2020-10-22 17:32:13 +02:00
|
|
|
isActive: (itemType:ContextMenuItemType, options:ContextMenuOptions) => itemType === ContextMenuItemType.Link || !!options.linkToCopy,
|
2020-05-09 20:18:41 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function contextMenu(options:ContextMenuOptions) {
|
|
|
|
const menu = new Menu();
|
|
|
|
|
|
|
|
const items = menuItems();
|
|
|
|
|
2020-08-02 13:16:42 +02:00
|
|
|
if (!('readyOnly' in options)) options.isReadOnly = true;
|
|
|
|
|
2020-05-09 20:18:41 +02:00
|
|
|
for (const itemKey in items) {
|
|
|
|
const item = items[itemKey];
|
|
|
|
|
2020-08-02 13:16:42 +02:00
|
|
|
if (!item.isActive(options.itemType, options)) continue;
|
2020-05-09 20:18:41 +02:00
|
|
|
|
|
|
|
menu.append(new MenuItem({
|
|
|
|
label: item.label,
|
|
|
|
click: () => {
|
|
|
|
item.onAction(options);
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|