Skip to content

Commit

Permalink
Desktop: Right click on image to copy it to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Dec 28, 2021
1 parent 3b05e7e commit 297b992
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 8 additions & 8 deletions packages/app-desktop/bridge.ts
@@ -1,22 +1,18 @@
import ElectronAppWrapper from './ElectronAppWrapper';
import shim from '@joplin/lib/shim';
import { _, setLocale } from '@joplin/lib/locale';
import { BrowserWindow, nativeTheme, nativeImage } from 'electron';
const { dirname, toSystemSlashes } = require('@joplin/lib/path-utils');
const { BrowserWindow, nativeTheme } = require('electron');

interface LastSelectedPath {
file: string;
directory: string;
}

interface LastSelectedPaths {
[key: string]: LastSelectedPath;
}

export class Bridge {

private electronWrapper_: ElectronAppWrapper;
private lastSelectedPaths_: LastSelectedPaths;
private lastSelectedPaths_: LastSelectedPath;

constructor(electronWrapper: ElectronAppWrapper) {
this.electronWrapper_ = electronWrapper;
Expand Down Expand Up @@ -164,11 +160,11 @@ export class Bridge {
if (!options) options = {};
let fileType = 'file';
if (options.properties && options.properties.includes('openDirectory')) fileType = 'directory';
if (!('defaultPath' in options) && this.lastSelectedPaths_[fileType]) options.defaultPath = this.lastSelectedPaths_[fileType];
if (!('defaultPath' in options) && (this.lastSelectedPaths_ as any)[fileType]) options.defaultPath = (this.lastSelectedPaths_ as any)[fileType];
if (!('createDirectory' in options)) options.createDirectory = true;
const { filePaths } = await dialog.showOpenDialog(this.window(), options);
if (filePaths && filePaths.length) {
this.lastSelectedPaths_[fileType] = dirname(filePaths[0]);
(this.lastSelectedPaths_ as any)[fileType] = dirname(filePaths[0]);
}
return filePaths;
}
Expand Down Expand Up @@ -282,6 +278,10 @@ export class Bridge {
app.exit();
}

public createImageFromPath(path: string) {
return nativeImage.createFromPath(path);
}

}

let bridge_: Bridge = null;
Expand Down
12 changes: 10 additions & 2 deletions packages/app-desktop/gui/NoteEditor/utils/contextMenu.ts
@@ -1,8 +1,7 @@
import ResourceEditWatcher from '@joplin/lib/services/ResourceEditWatcher/index';
import { _ } from '@joplin/lib/locale';
import { copyHtmlToClipboard } from './clipboardUtils';

const bridge = require('@electron/remote').require('./bridge').default;
import bridge from '../../../services/bridge';
const Menu = bridge().Menu;
const MenuItem = bridge().MenuItem;
import Resource from '@joplin/lib/models/Resource';
Expand Down Expand Up @@ -131,6 +130,15 @@ export function menuItems(dispatch: Function): ContextMenuItems {
},
isActive: (itemType: ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
},
copyImage: {
label: _('Copy image'),
onAction: async (options: ContextMenuOptions) => {
const { resourcePath } = await resourceInfo(options);
const image = bridge().createImageFromPath(resourcePath);
clipboard.writeImage(image);
},
isActive: (itemType: ContextMenuItemType) => itemType === ContextMenuItemType.Image,
},
cut: {
label: _('Cut'),
onAction: async (options: ContextMenuOptions) => {
Expand Down

0 comments on commit 297b992

Please sign in to comment.