2024-12-21 17:48:27 +02:00

38 lines
983 B
TypeScript

import { t } from "./i18n.js";
import toastService from "./toast.js";
function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
try {
$imageWrapper.attr('contenteditable', 'true');
selectImage($imageWrapper.get(0));
const success = document.execCommand('copy');
if (success) {
toastService.showMessage(t("image.copied-to-clipboard"));
} else {
toastService.showAndLogError(t("image.cannot-copy"));
}
}
finally {
window.getSelection()?.removeAllRanges();
$imageWrapper.removeAttr('contenteditable');
}
}
function selectImage(element: HTMLElement | undefined) {
if (!element) {
return;
}
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
selection?.removeAllRanges();
selection?.addRange(range);
}
export default {
copyImageReferenceToClipboard
};