Skip to content

Commit

Permalink
add "copy image" context menu to also attachments, fixes #4514
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Dec 27, 2023
1 parent a378313 commit 8dbc592
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
44 changes: 44 additions & 0 deletions src/public/app/menus/image_context_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import utils from "../services/utils.js";
import contextMenu from "./context_menu.js";
import imageService from "../services/image.js";

const PROP_NAME = "imageContextMenuInstalled";

function setupContextMenu($image) {
if (!utils.isElectron() || $image.prop(PROP_NAME)) {
return;
}

$image.prop(PROP_NAME, true);
$image.on('contextmenu', e => {
e.preventDefault();

contextMenu.show({
x: e.pageX,
y: e.pageY,
items: [
{
title: "Copy reference to clipboard",
command: "copyImageReferenceToClipboard",
uiIcon: "bx bx-empty"
},
{title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty"},
],
selectMenuItemHandler: ({command}) => {
if (command === 'copyImageReferenceToClipboard') {
imageService.copyImageReferenceToClipboard($image);
} else if (command === 'copyImageToClipboard') {
const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
utils.dynamicRequire('electron');
webContents.copyImageAt(e.pageX, e.pageY);
} else {
throw new Error(`Unrecognized command '${command}'`);
}
}
});
});
}

export default {
setupContextMenu
};
3 changes: 3 additions & 0 deletions src/public/app/services/content_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import linkService from "./link.js";
import treeService from "./tree.js";
import FNote from "../entities/fnote.js";
import FAttachment from "../entities/fattachment.js";
import imageContextMenuService from "../menus/image_context_menu.js";

let idCounter = 1;

Expand Down Expand Up @@ -148,6 +149,8 @@ function renderImage(entity, $renderedContent, options = {}) {
});
});
}

imageContextMenuService.setupContextMenu($img);
}

function renderFile(entity, type, $renderedContent) {
Expand Down
33 changes: 2 additions & 31 deletions src/public/app/widgets/type_widgets/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import utils from "../../services/utils.js";
import TypeWidget from "./type_widget.js";
import libraryLoader from "../../services/library_loader.js";
import contextMenu from "../../menus/context_menu.js";
import imageContextMenuService from "../../menus/image_context_menu.js";
import imageService from "../../services/image.js";

const TPL = `
Expand Down Expand Up @@ -55,36 +55,7 @@ class ImageTypeWidget extends TypeWidget {
});
});

if (utils.isElectron()) {
// for browser, we want to let the native menu
this.$imageView.on('contextmenu', e => {
e.preventDefault();

contextMenu.show({
x: e.pageX,
y: e.pageY,
items: [
{
title: "Copy reference to clipboard",
command: "copyImageReferenceToClipboard",
uiIcon: "bx bx-empty"
},
{title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty"},
],
selectMenuItemHandler: ({command}) => {
if (command === 'copyImageReferenceToClipboard') {
imageService.copyImageReferenceToClipboard(this.$imageWrapper);
} else if (command === 'copyImageToClipboard') {
const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
utils.dynamicRequire('electron');
webContents.copyImageAt(e.pageX, e.pageY);
} else {
throw new Error(`Unrecognized command '${command}'`);
}
}
});
});
}
imageContextMenuService.setupContextMenu(this.$imageView);

super.doRender();
}
Expand Down

0 comments on commit 8dbc592

Please sign in to comment.