2025-01-09 18:07:02 +02:00
|
|
|
import { t } from "../services/i18n.js";
|
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";
|
|
|
|
|
2024-12-22 17:46:30 +02:00
|
|
|
function setupContextMenu($image: JQuery<HTMLElement>) {
|
2023-12-28 00:02:09 +01:00
|
|
|
if (!utils.isElectron() || $image.prop(PROP_NAME)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image.prop(PROP_NAME, true);
|
2025-01-09 18:07:02 +02:00
|
|
|
$image.on("contextmenu", (e) => {
|
2023-12-28 00:02:09 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
contextMenu.show({
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
items: [
|
|
|
|
{
|
2024-11-25 17:41:00 +08:00
|
|
|
title: t("image_context_menu.copy_reference_to_clipboard"),
|
2023-12-28 00:02:09 +01:00
|
|
|
command: "copyImageReferenceToClipboard",
|
2024-11-20 10:13:52 +02:00
|
|
|
uiIcon: "bx bx-directions"
|
|
|
|
},
|
2024-11-25 17:41:00 +08:00
|
|
|
{
|
|
|
|
title: t("image_context_menu.copy_image_to_clipboard"),
|
2024-11-20 10:13:52 +02:00
|
|
|
command: "copyImageToClipboard",
|
2024-11-25 17:41:00 +08:00
|
|
|
uiIcon: "bx bx-copy"
|
2025-01-09 18:07:02 +02:00
|
|
|
}
|
2023-12-28 00:02:09 +01:00
|
|
|
],
|
2024-09-30 19:43:44 +00:00
|
|
|
selectMenuItemHandler: async ({ command }) => {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (command === "copyImageReferenceToClipboard") {
|
2023-12-28 00:02:09 +01:00
|
|
|
imageService.copyImageReferenceToClipboard($image);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (command === "copyImageToClipboard") {
|
2024-09-28 17:41:59 -07:00
|
|
|
try {
|
2025-01-09 18:07:02 +02:00
|
|
|
const nativeImage = utils.dynamicRequire("electron").nativeImage;
|
|
|
|
const clipboard = utils.dynamicRequire("electron").clipboard;
|
2024-09-30 19:43:44 +00:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const src = $image.attr("src");
|
2024-12-22 17:46:30 +02:00
|
|
|
if (!src) {
|
|
|
|
console.error("Missing src");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(src);
|
2024-09-30 19:43:44 +00:00
|
|
|
const blob = await response.blob();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
clipboard.writeImage(nativeImage.createFromBuffer(Buffer.from(await blob.arrayBuffer())));
|
2024-09-28 17:41:59 -07:00
|
|
|
} catch (error) {
|
2025-01-09 18:07:02 +02:00
|
|
|
console.error("Failed to copy image to clipboard:", error);
|
2024-09-28 17:41:59 -07:00
|
|
|
}
|
2023-12-28 00:02:09 +01:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unrecognized command '${command}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
setupContextMenu
|
2024-11-25 17:41:00 +08:00
|
|
|
};
|