Skip to content

Commit

Permalink
Merge pull request #452 from TriliumNext/fix-image-copy
Browse files Browse the repository at this point in the history
Use the electron Clipboard module when using "Copy image to clipboard"
  • Loading branch information
eliandoran authored Oct 11, 2024
2 parents bafc556 + 9204f07 commit 9c8cf0b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/public/app/menus/image_context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,31 @@ function setupContextMenu($image) {
command: "copyImageReferenceToClipboard",
uiIcon: "bx bx-empty"
},
{title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty"},
{ title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty" },
],
selectMenuItemHandler: ({command}) => {
selectMenuItemHandler: async ({ 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);
try {
const nativeImage = utils.dynamicRequire('electron').nativeImage;
const clipboard = utils.dynamicRequire('electron').clipboard;

const response = await fetch(
$image.attr('src')
);
const blob = await response.blob();

clipboard.writeImage(
nativeImage.createFromBuffer(
Buffer.from(
await blob.arrayBuffer()
)
)
);
} catch (error) {
console.error('Failed to copy image to clipboard:', error);
}
} else {
throw new Error(`Unrecognized command '${command}'`);
}
Expand All @@ -41,4 +57,4 @@ function setupContextMenu($image) {

export default {
setupContextMenu
};
};

0 comments on commit 9c8cf0b

Please sign in to comment.