import dayjs from "dayjs";
import html2canvas from "html2canvas-pro";
export default async function exportImage(docid, filename = "file") {
const target = document.getElementById(docid);
const ignored = target.querySelectorAll(".ignore");
ignored.forEach((el) => el.style.setProperty("display", "none", "important"));
// Xóa cache ảnh để lần export sau không bị lỗi
target.querySelectorAll("img").forEach((img) => (img.src = `${img.src}`));
const restoreIcons = swapIconsForExport(target);
try {
await new Promise((r) => requestAnimationFrame(r));
const canvas = await html2canvas(target);
const link = document.createElement("a");
link.href = canvas.toDataURL("image/png");
link.download = `${filename}-${dayjs().format("YYYYMMDDHHmmss")}.png`;
link.click();
link.remove();
} finally {
restoreIcons();
ignored.forEach((el) => el.style.removeProperty("display"));
}
return filename;
}