2023-12-28 00:02:09 +01:00
|
|
|
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"
|
|
|
|
},
|
2024-09-30 19:43:44 +00:00
|
|
|
{ title: "Copy image to clipboard", command: "copyImageToClipboard", uiIcon: "bx bx-empty" },
|
2023-12-28 00:02:09 +01:00
|
|
|
],
|
2024-09-30 19:43:44 +00:00
|
|
|
selectMenuItemHandler: async ({ command }) => {
|
2023-12-28 00:02:09 +01:00
|
|
|
if (command === 'copyImageReferenceToClipboard') {
|
|
|
|
imageService.copyImageReferenceToClipboard($image);
|
|
|
|
} else if (command === 'copyImageToClipboard') {
|
2024-09-28 17:41:59 -07:00
|
|
|
try {
|
|
|
|
const nativeImage = utils.dynamicRequire('electron').nativeImage;
|
|
|
|
const clipboard = utils.dynamicRequire('electron').clipboard;
|
2024-09-30 19:43:44 +00:00
|
|
|
|
|
|
|
const response = await fetch(
|
|
|
|
$image.attr('src')
|
|
|
|
);
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
clipboard.writeImage(
|
|
|
|
nativeImage.createFromBuffer(
|
|
|
|
Buffer.from(
|
|
|
|
await blob.arrayBuffer()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2024-09-28 17:41:59 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to copy image to clipboard:', error);
|
|
|
|
}
|
2023-12-28 00:02:09 +01:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unrecognized command '${command}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
setupContextMenu
|
2024-09-28 17:41:59 -07:00
|
|
|
};
|