From 95f4d0f2c8801ff7c3dcab2246a1fc708f6881ea Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Fri, 14 Oct 2022 20:24:27 +0800 Subject: [PATCH] resolve: preview load speed --- addon/chrome/content/previewPDF.html | 2 ++ src/events.ts | 29 ++++++++++++++++++++++++++++ src/preview.ts | 19 +++--------------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/addon/chrome/content/previewPDF.html b/addon/chrome/content/previewPDF.html index 805fe5c..a5ec848 100644 --- a/addon/chrome/content/previewPDF.html +++ b/addon/chrome/content/previewPDF.html @@ -481,6 +481,8 @@ } else if (e.data.type === "updateWidth") { cachedData.viewportWidth = e.data.width; renderDocument(cachedData.itemID); + } else if (e.data.type === "updateToolbar") { + updateToolbar(e.data.previewType); } } diff --git a/src/events.ts b/src/events.ts index 342a931..f487e60 100644 --- a/src/events.ts +++ b/src/events.ts @@ -9,6 +9,9 @@ class AddonEvents extends AddonModule { this.previewSplitCollapsed = false; Zotero.debug("PDFPreview: init called"); await Zotero.uiReadyPromise; + if (!Zotero.Prefs.get("pdfpreview.enable")) { + return; + } this.initItemSelectListener(); this.initPreviewResizeListener(); this.initTabSelectListener(); @@ -18,6 +21,11 @@ class AddonEvents extends AddonModule { this.updatePreviewAttachmentSplit(); this.updatePreviewTab(); this.updatePreviewTabName(); + let type = PreviewType.info; + while (type !== PreviewType.null) { + await this.initPreview(type); + type++; + } } private initItemSelectListener() { @@ -346,6 +354,27 @@ class AddonEvents extends AddonModule { ); }); } + + public async initPreview(type: PreviewType) { + console.log("init preview iframe"); + this._Addon.preview._initPromise = Zotero.Promise.defer(); + const { container } = this._Addon.preview.getPreviewElements(type); + if (!container) { + return; + } + const iframe = document.createElement("iframe"); + iframe.setAttribute("id", this._Addon.preview.getPreviewIds(type).iframeId); + iframe.setAttribute("src", "chrome://PDFPreview/content/previewPDF.html"); + container.appendChild(iframe); + await this._Addon.preview._initPromise; + iframe.contentWindow.postMessage( + { + type: "updateToolbar", + previewType: type, + }, + "*" + ); + } } export default AddonEvents; diff --git a/src/preview.ts b/src/preview.ts index 80bfa06..3bde857 100644 --- a/src/preview.ts +++ b/src/preview.ts @@ -55,7 +55,7 @@ class AddonPreview extends AddonModule { return new Uint8Array(buf).buffer; } - private getPreviewIds(type: PreviewType) { + public getPreviewIds(type: PreviewType) { let iframeId = ""; let containerId = ""; const splitType: "before" | "after" = Zotero.Prefs.get( @@ -74,7 +74,7 @@ class AddonPreview extends AddonModule { return { iframeId, containerId }; } - private getPreviewElements(type: PreviewType) { + public getPreviewElements(type: PreviewType) { const { iframeId, containerId } = this.getPreviewIds(type); return { iframe: document.getElementById(iframeId) as HTMLIFrameElement, @@ -117,25 +117,12 @@ class AddonPreview extends AddonModule { item = this.item; } - let { iframe: iframe, container } = this.getPreviewElements(type); + let { iframe } = this.getPreviewElements(type); if (item) { if (this._loadingPromise) { await this._loadingPromise.promise; } if (!iframe) { - console.log("init preview iframe"); - this._initPromise = Zotero.Promise.defer(); - iframe = window.document.createElement("iframe"); - iframe.setAttribute("id", this.getPreviewIds(type).iframeId); - iframe.setAttribute( - "src", - "chrome://PDFPreview/content/previewPDF.html" - ); - - if (!container) { - return; - } - container.appendChild(iframe); } iframe.hidden = false; // Reset the width to allow parentElement to shrink