Skip to content

Commit 9c8cf0b

Browse files
authored
Merge pull request TriliumNext#452 from TriliumNext/fix-image-copy
Use the electron Clipboard module when using "Copy image to clipboard"
2 parents bafc556 + 9204f07 commit 9c8cf0b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/public/app/menus/image_context_menu.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,31 @@ function setupContextMenu($image) {
2222
command: "copyImageReferenceToClipboard",
2323
uiIcon: "bx bx-empty"
2424
},
25-
{title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty"},
25+
{ title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty" },
2626
],
27-
selectMenuItemHandler: ({command}) => {
27+
selectMenuItemHandler: async ({ command }) => {
2828
if (command === 'copyImageReferenceToClipboard') {
2929
imageService.copyImageReferenceToClipboard($image);
3030
} else if (command === 'copyImageToClipboard') {
31-
const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents();
32-
utils.dynamicRequire('electron');
33-
webContents.copyImageAt(e.pageX, e.pageY);
31+
try {
32+
const nativeImage = utils.dynamicRequire('electron').nativeImage;
33+
const clipboard = utils.dynamicRequire('electron').clipboard;
34+
35+
const response = await fetch(
36+
$image.attr('src')
37+
);
38+
const blob = await response.blob();
39+
40+
clipboard.writeImage(
41+
nativeImage.createFromBuffer(
42+
Buffer.from(
43+
await blob.arrayBuffer()
44+
)
45+
)
46+
);
47+
} catch (error) {
48+
console.error('Failed to copy image to clipboard:', error);
49+
}
3450
} else {
3551
throw new Error(`Unrecognized command '${command}'`);
3652
}
@@ -41,4 +57,4 @@ function setupContextMenu($image) {
4157

4258
export default {
4359
setupContextMenu
44-
};
60+
};

0 commit comments

Comments
 (0)