Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
resolve: preview load speed
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Oct 14, 2022
1 parent a505b47 commit 95f4d0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions addon/chrome/content/previewPDF.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
19 changes: 3 additions & 16 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 95f4d0f

@1067237855
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐂🍺大佬太牛了,预览加载速度快的飞起!!!!

Please sign in to comment.