Files
d9-eql/web/themes/custom/eql/scripts/paged-setup.js
T
2025-07-21 18:39:34 +02:00

142 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// window.runPagedIfPrinting = function () {
// console.log("📄 Impression via iframe");
// // 1. Créer une iframe cachée
// const iframe = document.createElement("iframe");
// iframe.style.position = "fixed";
// iframe.style.left = "-100vw"; // hors de la vue
// iframe.style.top = "-100vh";
// iframe.style.width = "0";
// iframe.style.height = "0";
// iframe.style.visibility = "hidden";
// iframe.setAttribute("aria-hidden", "true");
// document.body.appendChild(iframe);
// // 2. Copier tout le document dans liframe
// const clonedHTML = document.documentElement.outerHTML;
// const doc = iframe.contentWindow.document;
// doc.open();
// doc.write(clonedHTML);
// doc.close();
// // 3. Attendre que tout soit chargé
// iframe.onload = () => {
// console.log("📦 Iframe chargée, injection manuelle de Paged.js");
// const pagedScript = iframe.contentDocument.createElement("script");
// pagedScript.src = "https://unpkg.com/pagedjs/dist/paged.js";
// pagedScript.onload = () => {
// console.log("📚 Paged.js chargé, génération des pages...");
// const previewer = new iframe.contentWindow.Paged.Previewer();
// previewer.preview().then(() => {
// console.log("🖨️ Aperçu prêt, impression dans iframe…");
// setTimeout(() => {
// const printScript = iframe.contentDocument.createElement("script");
// printScript.textContent = `
// window.addEventListener("load", () => {
// setTimeout(() => {
// window.print();
// window.onafterprint = () => window.close(); // facultatif
// }, 100);
// });
// `;
// iframe.contentDocument.body.appendChild(printScript);
// }, 300);
// });
// };
// // Injecte le script dans <head> de liframe
// iframe.contentDocument.head.appendChild(pagedScript);
// };
// };
// // 📎 Listeners impression
// function setupPagedPrintListeners() {
// window.addEventListener("beforeprint", (e) => {
// e.preventDefault(); // empêcher la version native
// });
// if (window.matchMedia) {
// window.matchMedia("print").addEventListener("change", (e) => {
// if (e.matches) {
// window.runPagedIfPrinting();
// }
// });
// }
// console.log("✅ Paged.js listeners (iframe) installés.");
// }
// if (document.readyState !== "loading") {
// setupPagedPrintListeners();
// } else {
// document.addEventListener("DOMContentLoaded", setupPagedPrintListeners);
// }
window.runPagedIfPrinting = function () {
console.log("📄 Impression détectée — Paged.js va s'exécuter");
if (typeof Paged !== "undefined" && Paged.Previewer) {
// Sauvegarde du DOM actuel
const original = document.body.cloneNode(true);
const previewer = new Paged.Previewer();
previewer.preview().then(() => {
console.log("📃 Paged.js rendu terminé");
const restoreDOM = () => {
console.log("🔄 Restauration du DOM après impression");
document.body.replaceWith(original);
window.removeEventListener("afterprint", restoreDOM);
};
// ✅ Restauration après impression (ou annulation)
window.addEventListener("afterprint", restoreDOM);
});
} else {
console.warn("❌ Paged.js non chargé ou Previewer indisponible !");
}
};
// Initialisation automatique
function setupPagedPrintListeners() {
// Classique
window.addEventListener("beforeprint", window.runPagedIfPrinting);
// Pour certains navigateurs modernes
if (window.matchMedia) {
const mediaQueryList = window.matchMedia("print");
if (mediaQueryList.addEventListener) {
mediaQueryList.addEventListener("change", (e) => {
if (e.matches) {
window.runPagedIfPrinting();
}
});
} else {
// Safari fallback
mediaQueryList.addListener((e) => {
if (e.matches) {
window.runPagedIfPrinting();
}
});
}
}
console.log("✅ Paged.js listeners installés.");
}
// Démarrage au bon moment
if (document.readyState !== "loading") {
setupPagedPrintListeners();
} else {
document.addEventListener("DOMContentLoaded", setupPagedPrintListeners);
}