37 lines
975 B
TypeScript
Raw Normal View History

2024-12-21 17:48:27 +02:00
import { t } from "./i18n.js";
2023-05-03 10:23:20 +02:00
import toastService from "./toast.js";
2024-12-21 17:48:27 +02:00
function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
2023-05-03 10:23:20 +02:00
try {
2025-01-09 18:07:02 +02:00
$imageWrapper.attr("contenteditable", "true");
2023-05-03 10:23:20 +02:00
selectImage($imageWrapper.get(0));
2025-01-09 18:07:02 +02:00
const success = document.execCommand("copy");
2023-05-03 10:23:20 +02:00
if (success) {
2024-10-20 02:06:08 +03:00
toastService.showMessage(t("image.copied-to-clipboard"));
2023-05-03 10:23:20 +02:00
} else {
2024-10-20 02:06:08 +03:00
toastService.showAndLogError(t("image.cannot-copy"));
2023-05-03 10:23:20 +02:00
}
2025-01-09 18:07:02 +02:00
} finally {
2024-12-21 17:48:27 +02:00
window.getSelection()?.removeAllRanges();
2025-01-09 18:07:02 +02:00
$imageWrapper.removeAttr("contenteditable");
2023-05-03 10:23:20 +02:00
}
}
2024-12-21 17:48:27 +02:00
function selectImage(element: HTMLElement | undefined) {
if (!element) {
return;
}
2025-01-09 18:07:02 +02:00
2023-05-03 10:23:20 +02:00
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
2024-12-21 17:48:27 +02:00
selection?.removeAllRanges();
selection?.addRange(range);
2023-05-03 10:23:20 +02:00
}
export default {
copyImageReferenceToClipboard
};