Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly re-factor how the viewer initializes debug-only functionality #18061

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1510,8 +1510,14 @@ class PDFPageProxy {
internalRenderTask.capability.resolve();
}

this._stats?.timeEnd("Rendering");
this._stats?.timeEnd("Overall");
if (this._stats) {
this._stats.timeEnd("Rendering");
this._stats.timeEnd("Overall");

if (globalThis.Stats?.enabled) {
globalThis.Stats.add(this.pageNumber, this._stats);
}
}
};

const internalRenderTask = new InternalRenderTask({
Expand Down
60 changes: 22 additions & 38 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,27 @@ const PDFViewerApplication = {
const { mainContainer, viewerContainer } = this.appConfig,
params = parseQueryString(hash);

const loadPDFBug = async () => {
if (this._PDFBug) {
return;
}
const { PDFBug } =
typeof PDFJSDev === "undefined"
? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method
: await __non_webpack_import__(AppOptions.get("debuggerSrc"));

this._PDFBug = PDFBug;
};

if (params.get("disableworker") === "true") {
try {
await loadFakeWorker();
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");

if (typeof PDFJSDev === "undefined") {
globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js");
} else {
await __non_webpack_import__(PDFWorker.workerSrc);
}
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
}
Expand Down Expand Up @@ -306,7 +324,7 @@ const PDFViewerApplication = {
case "hover":
viewerContainer.classList.add(`textLayer-${params.get("textlayer")}`);
try {
await loadPDFBug(this);
await loadPDFBug();
this._PDFBug.loadCSS();
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
Expand All @@ -315,12 +333,11 @@ const PDFViewerApplication = {
}
}
if (params.has("pdfbug")) {
AppOptions.set("pdfBug", true);
AppOptions.set("fontExtraProperties", true);
AppOptions.setAll({ pdfBug: true, fontExtraProperties: true });

const enabled = params.get("pdfbug").split(",");
try {
await loadPDFBug(this);
await loadPDFBug();
this._PDFBug.init(mainContainer, enabled);
} catch (ex) {
console.error(`_parseHashParams: "${ex.message}".`);
Expand Down Expand Up @@ -1898,10 +1915,6 @@ const PDFViewerApplication = {
signal,
});

if (AppOptions.get("pdfBug")) {
eventBus._on("pagerendered", reportPageStatsPDFBug, { signal });
eventBus._on("pagechanging", reportPageStatsPDFBug, { signal });
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._on("fileinputchange", webViewerFileInputChange, { signal });
eventBus._on("openfile", webViewerOpenFile, { signal });
Expand Down Expand Up @@ -2168,35 +2181,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
};
}

async function loadFakeWorker() {
GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc");

if (typeof PDFJSDev === "undefined") {
globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js");
return;
}
await __non_webpack_import__(PDFWorker.workerSrc);
}

async function loadPDFBug(self) {
const { PDFBug } =
typeof PDFJSDev === "undefined"
? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method
: await __non_webpack_import__(AppOptions.get("debuggerSrc"));

self._PDFBug = PDFBug;
}

function reportPageStatsPDFBug({ pageNumber }) {
if (!globalThis.Stats?.enabled) {
return;
}
const pageView = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats);
}

function webViewerPageRender({ pageNumber }) {
// If the page is (the most) visible when it starts rendering,
// ensure that the page number input loading indicator is displayed.
Expand Down