From cae8fe4c7e930f63b2c03a95e23f8cfbaa2a2602 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 2 Jul 2023 11:12:27 +0200 Subject: [PATCH 1/2] Move the `setTitleUsingUrl`-call into `PDFViewerApplication.initPassiveLoading` This seems overall nicer, rather than having to "manually" call this when initializing passive loading. --- web/app.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/web/app.js b/web/app.js index af7f06870288f..3c607a014f687 100644 --- a/web/app.js +++ b/web/app.js @@ -768,13 +768,15 @@ const PDFViewerApplication = { return this.externalServices.supportedMouseWheelZoomModifierKeys; }, - initPassiveLoading() { + initPassiveLoading(file) { if ( typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL || CHROME") ) { throw new Error("Not implemented: initPassiveLoading"); } + this.setTitleUsingUrl(file, /* downloadUrl = */ file); + this.externalServices.initPassiveLoading({ onOpenWithTransport: range => { this.open({ range }); @@ -2285,8 +2287,7 @@ function webViewerInitialized() { PDFViewerApplication._hideViewBookmark(); } } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { - PDFViewerApplication.setTitleUsingUrl(file, /* downloadUrl = */ file); - PDFViewerApplication.initPassiveLoading(); + PDFViewerApplication.initPassiveLoading(file); } else { throw new Error("Not implemented: webViewerInitialized"); } From 58252a528d48cb7cd2ab72e6aeb8ffd6f06b6a53 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 2 Jul 2023 11:36:10 +0200 Subject: [PATCH 2/2] Inline the `webViewerInitialized` function in `PDFViewerApplication.run` Given the size of this function respectively method, it seems reasonable to simply inline the `webViewerInitialized`-code here. --- web/app.js | 202 ++++++++++++++++++++++++++--------------------------- 1 file changed, 101 insertions(+), 101 deletions(-) diff --git a/web/app.js b/web/app.js index 3c607a014f687..9b2b9a7c50c57 100644 --- a/web/app.js +++ b/web/app.js @@ -685,8 +685,107 @@ const PDFViewerApplication = { } }, - run(config) { - this.initialize(config).then(webViewerInitialized); + async run(config) { + await this.initialize(config); + + const { appConfig, eventBus, l10n } = this; + let file; + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + const queryString = document.location.search.substring(1); + const params = parseQueryString(queryString); + file = params.get("file") ?? AppOptions.get("defaultUrl"); + validateFileURL(file); + } else if (PDFJSDev.test("MOZCENTRAL")) { + file = window.location.href; + } else if (PDFJSDev.test("CHROME")) { + file = AppOptions.get("defaultUrl"); + } + + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + const fileInput = appConfig.openFileInput; + fileInput.value = null; + + fileInput.addEventListener("change", function (evt) { + const { files } = evt.target; + if (!files || files.length === 0) { + return; + } + eventBus.dispatch("fileinputchange", { + source: this, + fileInput: evt.target, + }); + }); + + // Enable dragging-and-dropping a new PDF file onto the viewerContainer. + appConfig.mainContainer.addEventListener("dragover", function (evt) { + evt.preventDefault(); + + evt.dataTransfer.dropEffect = + evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move"; + }); + appConfig.mainContainer.addEventListener("drop", function (evt) { + evt.preventDefault(); + + const { files } = evt.dataTransfer; + if (!files || files.length === 0) { + return; + } + eventBus.dispatch("fileinputchange", { + source: this, + fileInput: evt.dataTransfer, + }); + }); + } + + if (!this.supportsDocumentFonts) { + AppOptions.set("disableFontFace", true); + l10n.get("web_fonts_disabled").then(msg => { + console.warn(msg); + }); + } + + if (!this.supportsPrinting) { + appConfig.toolbar?.print?.classList.add("hidden"); + appConfig.secondaryToolbar?.printButton.classList.add("hidden"); + } + + if (!this.supportsFullscreen) { + appConfig.secondaryToolbar?.presentationModeButton.classList.add( + "hidden" + ); + } + + if (this.supportsIntegratedFind) { + appConfig.toolbar?.viewFind?.classList.add("hidden"); + } + + appConfig.mainContainer.addEventListener( + "transitionend", + function (evt) { + if (evt.target === /* mainContainer */ this) { + eventBus.dispatch("resize", { source: this }); + } + }, + true + ); + + try { + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + if (file) { + this.open({ url: file }); + } else { + this._hideViewBookmark(); + } + } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { + this.initPassiveLoading(file); + } else { + throw new Error("Not implemented: run"); + } + } catch (reason) { + l10n.get("loading_error").then(msg => { + this._documentError(msg, reason); + }); + } }, get initialized() { @@ -2199,105 +2298,6 @@ function reportPageStatsPDFBug({ pageNumber }) { globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats); } -function webViewerInitialized() { - const { appConfig, eventBus, l10n } = PDFViewerApplication; - let file; - if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - const queryString = document.location.search.substring(1); - const params = parseQueryString(queryString); - file = params.get("file") ?? AppOptions.get("defaultUrl"); - validateFileURL(file); - } else if (PDFJSDev.test("MOZCENTRAL")) { - file = window.location.href; - } else if (PDFJSDev.test("CHROME")) { - file = AppOptions.get("defaultUrl"); - } - - if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - const fileInput = appConfig.openFileInput; - fileInput.value = null; - - fileInput.addEventListener("change", function (evt) { - const { files } = evt.target; - if (!files || files.length === 0) { - return; - } - eventBus.dispatch("fileinputchange", { - source: this, - fileInput: evt.target, - }); - }); - - // Enable dragging-and-dropping a new PDF file onto the viewerContainer. - appConfig.mainContainer.addEventListener("dragover", function (evt) { - evt.preventDefault(); - - evt.dataTransfer.dropEffect = - evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move"; - }); - appConfig.mainContainer.addEventListener("drop", function (evt) { - evt.preventDefault(); - - const { files } = evt.dataTransfer; - if (!files || files.length === 0) { - return; - } - eventBus.dispatch("fileinputchange", { - source: this, - fileInput: evt.dataTransfer, - }); - }); - } - - if (!PDFViewerApplication.supportsDocumentFonts) { - AppOptions.set("disableFontFace", true); - l10n.get("web_fonts_disabled").then(msg => { - console.warn(msg); - }); - } - - if (!PDFViewerApplication.supportsPrinting) { - appConfig.toolbar?.print?.classList.add("hidden"); - appConfig.secondaryToolbar?.printButton.classList.add("hidden"); - } - - if (!PDFViewerApplication.supportsFullscreen) { - appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden"); - } - - if (PDFViewerApplication.supportsIntegratedFind) { - appConfig.toolbar?.viewFind?.classList.add("hidden"); - } - - appConfig.mainContainer.addEventListener( - "transitionend", - function (evt) { - if (evt.target === /* mainContainer */ this) { - eventBus.dispatch("resize", { source: this }); - } - }, - true - ); - - try { - if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - if (file) { - PDFViewerApplication.open({ url: file }); - } else { - PDFViewerApplication._hideViewBookmark(); - } - } else if (PDFJSDev.test("MOZCENTRAL || CHROME")) { - PDFViewerApplication.initPassiveLoading(file); - } else { - throw new Error("Not implemented: webViewerInitialized"); - } - } catch (reason) { - l10n.get("loading_error").then(msg => { - PDFViewerApplication._documentError(msg, reason); - }); - } -} - function webViewerPageRender({ pageNumber }) { // If the page is (the most) visible when it starts rendering, // ensure that the page number input loading indicator is displayed.