{"version":3,"file":"printPage.mjs","names":[],"sources":["../src/printPage.ts"],"sourcesContent":["/**\n * Copyright (c) 2021-present, NDLA.\n *\n * This source code is licensed under the GPLv3 license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\nfunction closePrint(this: any) {\n  document.body.removeChild(this.__container__);\n}\n\nfunction setPrint(this: any) {\n  const iframeWindow = this.contentWindow;\n  iframeWindow.__container__ = this;\n  iframeWindow.onbeforeunload = closePrint;\n  iframeWindow.onafterprint = closePrint;\n\n  // Wait until the content is fully ready to print\n  if (isContentReady(iframeWindow.document)) {\n    iframeWindow.focus();\n    iframeWindow.print();\n  } else {\n    // If content isn't ready, use MutationObserver to wait for it\n    const observer = new MutationObserver((_, obs) => {\n      if (isContentReady(iframeWindow.document)) {\n        obs.disconnect(); // Stop observing\n        iframeWindow.focus();\n        iframeWindow.print();\n      }\n    });\n\n    observer.observe(iframeWindow.document, { childList: true, subtree: true });\n  }\n}\n\nfunction isContentReady(doc: Document): boolean {\n  // Logic to determine if the iframe's content is ready\n  return doc.readyState === \"complete\";\n}\n\nexport function printPage(url: string): void {\n  const iframe = document.createElement(\"iframe\");\n  iframe.onload = setPrint;\n  iframe.src = url;\n  iframe.style.visibility = \"hidden\";\n  document.body.appendChild(iframe);\n}\n"],"mappings":";;;;;;;;AAQA,SAAS,aAAsB;AAC7B,UAAS,KAAK,YAAY,KAAK,cAAc;;AAG/C,SAAS,WAAoB;CAC3B,MAAM,eAAe,KAAK;AAC1B,cAAa,gBAAgB;AAC7B,cAAa,iBAAiB;AAC9B,cAAa,eAAe;AAG5B,KAAI,eAAe,aAAa,SAAS,EAAE;AACzC,eAAa,OAAO;AACpB,eAAa,OAAO;OAGH,KAAI,kBAAkB,GAAG,QAAQ;AAChD,MAAI,eAAe,aAAa,SAAS,EAAE;AACzC,OAAI,YAAY;AAChB,gBAAa,OAAO;AACpB,gBAAa,OAAO;;GAEtB,CAEO,QAAQ,aAAa,UAAU;EAAE,WAAW;EAAM,SAAS;EAAM,CAAC;;AAI/E,SAAS,eAAe,KAAwB;AAE9C,QAAO,IAAI,eAAe;;AAG5B,SAAgB,UAAU,KAAmB;CAC3C,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,QAAO,SAAS;AAChB,QAAO,MAAM;AACb,QAAO,MAAM,aAAa;AAC1B,UAAS,KAAK,YAAY,OAAO"}