From 898172e9d2f48fd40535554ed3b47f1da2a62bdc Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 7 Feb 2024 13:53:29 +0100 Subject: [PATCH] Re-factor `PDFPrintServiceFactory` to use import maps This is very old code, which can (ever so slightly) be simplified now that import maps are available. --- web/app.js | 17 ++++------------- web/firefox_print_service.js | 22 +++++++++++----------- web/interfaces.js | 21 ++++++++++++++++++++- web/pdf_print_service.js | 21 +++++++++++++-------- web/viewer-geckoview.js | 1 - web/viewer.js | 1 - 6 files changed, 48 insertions(+), 35 deletions(-) diff --git a/web/app.js b/web/app.js index f1736f04b..ea7ec64d1 100644 --- a/web/app.js +++ b/web/app.js @@ -70,6 +70,7 @@ import { PDFHistory } from "./pdf_history.js"; import { PDFLayerViewer } from "web-pdf_layer_viewer"; import { PDFOutlineViewer } from "web-pdf_outline_viewer"; import { PDFPresentationMode } from "web-pdf_presentation_mode"; +import { PDFPrintServiceFactory } from "web-print_service"; import { PDFRenderingQueue } from "./pdf_rendering_queue.js"; import { PDFScriptingManager } from "./pdf_scripting_manager.js"; import { PDFSidebar } from "web-pdf_sidebar"; @@ -731,7 +732,7 @@ const PDFViewerApplication = { }, get supportsPrinting() { - return PDFPrintServiceFactory.instance.supportsPrinting; + return PDFPrintServiceFactory.supportsPrinting; }, get supportsFullscreen() { @@ -1786,7 +1787,7 @@ const PDFViewerApplication = { const optionalContentConfigPromise = this.pdfViewer.optionalContentConfigPromise; - const printService = PDFPrintServiceFactory.instance.createPrintService( + const printService = PDFPrintServiceFactory.createPrintService( this.pdfDocument, pagesOverview, printContainer, @@ -3234,14 +3235,4 @@ function webViewerReportTelemetry({ details }) { PDFViewerApplication.externalServices.reportTelemetry(details); } -/* Abstract factory for the print service. */ -const PDFPrintServiceFactory = { - instance: { - supportsPrinting: false, - createPrintService() { - throw new Error("Not implemented: createPrintService"); - }, - }, -}; - -export { PDFPrintServiceFactory, PDFViewerApplication }; +export { PDFViewerApplication }; diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index 476839c83..aa1428458 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -20,7 +20,6 @@ import { shadow, } from "pdfjs-lib"; import { getXfaHtmlForPrinting } from "./print_utils.js"; -import { PDFPrintServiceFactory } from "./app.js"; // Creates a placeholder with div and canvas with right size for the page. function composePage( @@ -194,15 +193,16 @@ class FirefoxPrintService { } } -PDFPrintServiceFactory.instance = { - get supportsPrinting() { +/** + * @implements {IPDFPrintServiceFactory} + */ +class PDFPrintServiceFactory { + static get supportsPrinting() { const canvas = document.createElement("canvas"); - const value = "mozPrintCallback" in canvas; + return shadow(this, "supportsPrinting", "mozPrintCallback" in canvas); + } - return shadow(this, "supportsPrinting", value); - }, - - createPrintService( + static createPrintService( pdfDocument, pagesOverview, printContainer, @@ -218,7 +218,7 @@ PDFPrintServiceFactory.instance = { optionalContentConfigPromise, printAnnotationStoragePromise ); - }, -}; + } +} -export { FirefoxPrintService }; +export { PDFPrintServiceFactory }; diff --git a/web/interfaces.js b/web/interfaces.js index 1be741d2e..10c9bb023 100644 --- a/web/interfaces.js +++ b/web/interfaces.js @@ -217,4 +217,23 @@ class IL10n { resume() {} } -export { IDownloadManager, IL10n, IPDFLinkService, IRenderableView }; +/** + * @interface + */ +class IPDFPrintServiceFactory { + static get supportsPrinting() { + return false; + } + + static createPrintService() { + throw new Error("Not implemented: createPrintService"); + } +} + +export { + IDownloadManager, + IL10n, + IPDFLinkService, + IPDFPrintServiceFactory, + IRenderableView, +}; diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 845444573..389418f66 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -13,9 +13,9 @@ * limitations under the License. */ -import { AnnotationMode, PixelsPerInch } from "pdfjs-lib"; -import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js"; +import { AnnotationMode, PixelsPerInch, shadow } from "pdfjs-lib"; import { getXfaHtmlForPrinting } from "./print_utils.js"; +import { PDFViewerApplication } from "./app.js"; let activeService = null; let dialog = null; @@ -355,10 +355,15 @@ function ensureOverlay() { return overlayPromise; } -PDFPrintServiceFactory.instance = { - supportsPrinting: true, +/** + * @implements {IPDFPrintServiceFactory} + */ +class PDFPrintServiceFactory { + static get supportsPrinting() { + return shadow(this, "supportsPrinting", true); + } - createPrintService( + static createPrintService( pdfDocument, pagesOverview, printContainer, @@ -378,7 +383,7 @@ PDFPrintServiceFactory.instance = { printAnnotationStoragePromise ); return activeService; - }, -}; + } +} -export { PDFPrintService }; +export { PDFPrintServiceFactory }; diff --git a/web/viewer-geckoview.js b/web/viewer-geckoview.js index 39edaf9fb..735f66af9 100644 --- a/web/viewer-geckoview.js +++ b/web/viewer-geckoview.js @@ -14,7 +14,6 @@ */ import "web-com"; -import "web-print_service"; import { RenderingStates, ScrollMode, SpreadMode } from "./ui_utils.js"; import { AppOptions } from "./app_options.js"; import { LinkTarget } from "./pdf_link_service.js"; diff --git a/web/viewer.js b/web/viewer.js index fea12640f..dcd13132a 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -14,7 +14,6 @@ */ import "web-com"; -import "web-print_service"; import { RenderingStates, ScrollMode, SpreadMode } from "./ui_utils.js"; import { AppOptions } from "./app_options.js"; import { LinkTarget } from "./pdf_link_service.js";