38 lines
983 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 {
$imageWrapper.attr('contenteditable', 'true');
selectImage($imageWrapper.get(0));
const success = document.execCommand('copy');
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
}
}
finally {
2024-12-21 17:48:27 +02:00
window.getSelection()?.removeAllRanges();
2023-05-03 10:23:20 +02:00
$imageWrapper.removeAttr('contenteditable');
}
}
2024-12-21 17:48:27 +02:00
function selectImage(element: HTMLElement | undefined) {
if (!element) {
return;
}
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
};