From 760f765e5690790194e8bd737c13b6aca3e75216 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 Dec 2021 12:18:12 +0100 Subject: [PATCH] Move the /Lang handling into the `BaseViewer` (PR 14114 follow-up) In PR 14114 this was only added to the default viewer, which means that in the viewer components the user would need to *manually* implement /Lang handling. This was (obviously) a bad choice, since the viewer components already support e.g. structTrees by default; sorry about overlooking this! To avoid having to make *two* `getMetadata` API-calls[1] very early during initialization, in the default viewer, the API will now cache its result. This will also come in handy elsewhere in the default viewer, e.g. by reducing parsing when opening the "document properties" dialog. --- [1] This not only includes a round-trip to the worker-thread, but also having to re-parse the /Metadata-entry when it exists. --- src/display/api.js | 8 ++++++-- web/app.js | 5 ----- web/base_viewer.js | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 8661dc2be..416e8b0ca 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2406,6 +2406,8 @@ class WorkerTransport { #pagePromises = new Map(); + #metadataPromise = null; + constructor(messageHandler, loadingTask, networkStream, params) { this.messageHandler = messageHandler; this.loadingTask = loadingTask; @@ -2530,6 +2532,7 @@ class WorkerTransport { Promise.all(waitOn).then(() => { this.commonObjs.clear(); this.fontLoader.clear(); + this.#metadataPromise = null; this._getFieldObjectsPromise = null; this._hasJSActionsPromise = null; @@ -3063,7 +3066,7 @@ class WorkerTransport { } getMetadata() { - return this.messageHandler + return (this.#metadataPromise ||= this.messageHandler .sendWithPromise("GetMetadata", null) .then(results => { return { @@ -3072,7 +3075,7 @@ class WorkerTransport { contentDispositionFilename: this._fullReader?.filename ?? null, contentLength: this._fullReader?.contentLength ?? null, }; - }); + })); } getMarkInfo() { @@ -3098,6 +3101,7 @@ class WorkerTransport { if (!keepLoadedFonts) { this.fontLoader.clear(); } + this.#metadataPromise = null; this._getFieldObjectsPromise = null; this._hasJSActionsPromise = null; } diff --git a/web/app.js b/web/app.js index 451151b86..7f6422b14 100644 --- a/web/app.js +++ b/web/app.js @@ -810,7 +810,6 @@ const PDFViewerApplication = { const { container } = this.appConfig.errorWrapper; container.hidden = true; } - this.appConfig.viewerContainer.removeAttribute("lang"); if (!this.pdfLoadingTask) { return; @@ -1542,10 +1541,6 @@ const PDFViewerApplication = { `${(info.Producer || "-").trim()} / ${(info.Creator || "-").trim()}] ` + `(PDF.js: ${version || "-"})` ); - - if (info.Language) { - this.appConfig.viewerContainer.lang = info.Language; - } let pdfTitle = info?.Title; const metadataTitle = metadata?.get("dc:title"); diff --git a/web/base_viewer.js b/web/base_viewer.js index 9a23974a8..affa751c4 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -486,10 +486,7 @@ class BaseViewer { /** * Currently only *some* permissions are supported. */ - #initializePermissions(permissions, pdfDocument) { - if (pdfDocument !== this.pdfDocument) { - return; // The document was closed while the permissions resolved. - } + #initializePermissions(permissions) { if (!permissions) { return; } @@ -603,9 +600,12 @@ class BaseViewer { // viewport for all pages Promise.all([firstPagePromise, permissionsPromise]) .then(([firstPdfPage, permissions]) => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the first page resolved. + } this._firstPageCapability.resolve(firstPdfPage); this._optionalContentConfigPromise = optionalContentConfigPromise; - this.#initializePermissions(permissions, pdfDocument); + this.#initializePermissions(permissions); const viewerElement = this._scrollMode === ScrollMode.PAGE ? null : this.viewer; @@ -719,6 +719,15 @@ class BaseViewer { this.eventBus.dispatch("pagesinit", { source: this }); + pdfDocument.getMetadata().then(({ info }) => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the metadata resolved. + } + if (info.Language) { + this.viewer.lang = info.Language; + } + }); + if (this.defaultRenderingQueue) { this.update(); } @@ -789,6 +798,7 @@ class BaseViewer { // ... and reset the Scroll mode CSS class(es) afterwards. this._updateScrollMode(); + this.viewer.removeAttribute("lang"); // Reset all PDF document permissions. this.viewer.classList.remove(ENABLE_PERMISSIONS_CLASS);