Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const TemplateFooterPage: FunctionComponent = () => {

await setFieldValue("uploadedImageUrl", data.imageUrl);
}
copyToClipboard();
await copyToClipboard();
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@ import { useCallback } from "react";
export const useCopyToClipboard = (
footerTemplateRef: React.MutableRefObject<HTMLDivElement | null>
) => {
return useCallback(() => {
if (!footerTemplateRef) {
return;
}

return useCallback(async () => {
if (!footerTemplateRef || !footerTemplateRef.current) {
// eslint-disable-next-line no-console
console.error("Please try again later");

return;
}

const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(footerTemplateRef.current);
const htmlContent = footerTemplateRef.current.innerHTML;
const textContent =
footerTemplateRef.current.textContent ??
footerTemplateRef.current.innerText ??
"";

if (!selection) {
return;
}
const clipboardItem = new ClipboardItem({
"text/html": new Blob([htmlContent], { type: "text/html" }),
"text/plain": new Blob([textContent], { type: "text/plain" })
});

selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
selection.removeAllRanges();
await navigator.clipboard.write([clipboardItem]);
}, [footerTemplateRef]);
};