Replace the IPDFPrintServiceFactory interface with an abstract BasePrintServiceFactory class

This should help reduce the maintenance burden of the code, since you no longer need to remember to update separate code when touching the different `PDFPrintServiceFactory` classes.
This commit is contained in:
Jonas Jenwald 2026-01-30 15:51:22 +01:00
parent 50a12e3e67
commit ff7f87fc21
5 changed files with 31 additions and 34 deletions

View File

@ -2456,10 +2456,7 @@ const PDFViewerApplication = {
}; };
initCom(PDFViewerApplication); initCom(PDFViewerApplication);
PDFPrintServiceFactory.initGlobals(PDFViewerApplication);
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
PDFPrintServiceFactory.initGlobals(PDFViewerApplication);
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const HOSTED_VIEWER_ORIGINS = new Set([ const HOSTED_VIEWER_ORIGINS = new Set([

View File

@ -19,7 +19,10 @@ import {
RenderingCancelledException, RenderingCancelledException,
shadow, shadow,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { getXfaHtmlForPrinting } from "./print_utils.js"; import {
BasePrintServiceFactory,
getXfaHtmlForPrinting,
} from "./print_utils.js";
// Creates a placeholder with div and canvas with right size for the page. // Creates a placeholder with div and canvas with right size for the page.
function composePage( function composePage(
@ -194,10 +197,7 @@ class FirefoxPrintService {
} }
} }
/** class PDFPrintServiceFactory extends BasePrintServiceFactory {
* @implements {IPDFPrintServiceFactory}
*/
class PDFPrintServiceFactory {
static get supportsPrinting() { static get supportsPrinting() {
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
return shadow(this, "supportsPrinting", "mozPrintCallback" in canvas); return shadow(this, "supportsPrinting", "mozPrintCallback" in canvas);

View File

@ -71,19 +71,4 @@ class IDownloadManager {
download(data, url, filename) {} download(data, url, filename) {}
} }
/** export { IDownloadManager, IRenderableView };
* @interface
*/
class IPDFPrintServiceFactory {
static initGlobals() {}
static get supportsPrinting() {
return false;
}
static createPrintService() {
throw new Error("Not implemented: createPrintService");
}
}
export { IDownloadManager, IPDFPrintServiceFactory, IRenderableView };

View File

@ -13,16 +13,16 @@
* limitations under the License. * limitations under the License.
*/ */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces.js").IPDFPrintServiceFactory} IPDFPrintServiceFactory */
import { import {
AnnotationMode, AnnotationMode,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
shadow, shadow,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { getXfaHtmlForPrinting } from "./print_utils.js"; import {
BasePrintServiceFactory,
getXfaHtmlForPrinting,
} from "./print_utils.js";
let activeService = null; let activeService = null;
let dialog = null; let dialog = null;
@ -371,10 +371,7 @@ function ensureOverlay() {
return overlayPromise; return overlayPromise;
} }
/** class PDFPrintServiceFactory extends BasePrintServiceFactory {
* @implements {IPDFPrintServiceFactory}
*/
class PDFPrintServiceFactory {
static initGlobals(app) { static initGlobals(app) {
viewerApp = app; viewerApp = app;
} }

View File

@ -17,6 +17,24 @@ import { getXfaPageViewport, PixelsPerInch } from "pdfjs-lib";
import { SimpleLinkService } from "./pdf_link_service.js"; import { SimpleLinkService } from "./pdf_link_service.js";
import { XfaLayerBuilder } from "./xfa_layer_builder.js"; import { XfaLayerBuilder } from "./xfa_layer_builder.js";
class BasePrintServiceFactory {
constructor() {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
throw new Error("Cannot initialize BasePrintServiceFactory.");
}
}
static initGlobals(app) {}
static get supportsPrinting() {
throw new Error("Not implemented: supportsPrinting");
}
static createPrintService(params) {
throw new Error("Not implemented: createPrintService");
}
}
function getXfaHtmlForPrinting(printContainer, pdfDocument) { function getXfaHtmlForPrinting(printContainer, pdfDocument) {
const xfaHtml = pdfDocument.allXfaHtml; const xfaHtml = pdfDocument.allXfaHtml;
const linkService = new SimpleLinkService(); const linkService = new SimpleLinkService();
@ -40,4 +58,4 @@ function getXfaHtmlForPrinting(printContainer, pdfDocument) {
} }
} }
export { getXfaHtmlForPrinting }; export { BasePrintServiceFactory, getXfaHtmlForPrinting };