From cfc32a14e0dc3f5fea48f40f6bbe4eb50bc547e2 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Sat, 28 Sep 2024 17:41:59 -0700 Subject: [PATCH 1/2] Use the electron Clipboard module when using "Copy image to clipboard" --- src/public/app/menus/image_context_menu.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/public/app/menus/image_context_menu.js b/src/public/app/menus/image_context_menu.js index 3b2abdca8..342dc5778 100644 --- a/src/public/app/menus/image_context_menu.js +++ b/src/public/app/menus/image_context_menu.js @@ -24,13 +24,23 @@ function setupContextMenu($image) { }, {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 imageUrl = $image.attr('src'); + const response = await fetch(imageUrl); + const blob = await response.blob(); + const arrayBuffer = await blob.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + const nativeImage = utils.dynamicRequire('electron').nativeImage; + const clipboard = utils.dynamicRequire('electron').clipboard; + const image = nativeImage.createFromBuffer(buffer); + clipboard.writeImage(image); + } catch (error) { + console.error('Failed to copy image to clipboard:', error); + } } else { throw new Error(`Unrecognized command '${command}'`); } @@ -41,4 +51,4 @@ function setupContextMenu($image) { export default { setupContextMenu -}; +}; \ No newline at end of file From 9204f0735c2cb01995f2e82a4d616bb0f27a4908 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Mon, 30 Sep 2024 19:43:44 +0000 Subject: [PATCH 2/2] use fewer const --- src/public/app/menus/image_context_menu.js | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/public/app/menus/image_context_menu.js b/src/public/app/menus/image_context_menu.js index 342dc5778..d45e35f62 100644 --- a/src/public/app/menus/image_context_menu.js +++ b/src/public/app/menus/image_context_menu.js @@ -22,22 +22,28 @@ 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: async ({command}) => { + selectMenuItemHandler: async ({ command }) => { if (command === 'copyImageReferenceToClipboard') { imageService.copyImageReferenceToClipboard($image); } else if (command === 'copyImageToClipboard') { try { - const imageUrl = $image.attr('src'); - const response = await fetch(imageUrl); - const blob = await response.blob(); - const arrayBuffer = await blob.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); const nativeImage = utils.dynamicRequire('electron').nativeImage; const clipboard = utils.dynamicRequire('electron').clipboard; - const image = nativeImage.createFromBuffer(buffer); - clipboard.writeImage(image); + + 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); }