瀏覽代碼

Try waiting till done printing to clean up iframe

Saul Shanabrook 6 年之前
父節點
當前提交
8c7ea4cb42
共有 1 個文件被更改,包括 12 次插入2 次删除
  1. 12 2
      packages/apputils/src/printing.ts

+ 12 - 2
packages/apputils/src/printing.ts

@@ -86,7 +86,7 @@ export namespace Printing {
 
     const parent = window.document.body;
     parent.appendChild(iframe);
-
+    const printed = resolveAfterPrint(iframe);
     if (isText) {
       iframe.srcdoc = textOrEl as string;
       await resolveWhenLoaded(iframe);
@@ -97,6 +97,7 @@ export namespace Printing {
 
     launchPrint(iframe.contentWindow);
 
+    await printed;
     parent.removeChild(iframe);
   }
 
@@ -131,7 +132,16 @@ export namespace Printing {
    */
   function resolveWhenLoaded(iframe: HTMLIFrameElement): Promise<void> {
     return new Promise(resolve => {
-      iframe.addEventListener('load', () => resolve(), false);
+      iframe.addEventListener('load', () => resolve());
+    });
+  }
+
+  function resolveAfterPrint(iframe: HTMLIFrameElement): Promise<void> {
+    return new Promise(resolve => {
+      window.addEventListener('afterprint', () => {
+        console.log('on after print');
+        resolve();
+      });
     });
   }