Merge pull request #16288 from Snuffleupagus/print-code-cleanup

Slightly modernize print-related code
This commit is contained in:
Tim van der Meij 2023-04-15 13:08:37 +02:00 committed by GitHub
commit 4d8a60b435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 73 deletions

View File

@ -114,7 +114,8 @@ function composePage(
};
}
function FirefoxPrintService(
class FirefoxPrintService {
constructor(
pdfDocument,
pagesOverview,
printContainer,
@ -132,7 +133,6 @@ function FirefoxPrintService(
printAnnotationStoragePromise || Promise.resolve();
}
FirefoxPrintService.prototype = {
layout() {
const {
pdfDocument,
@ -146,16 +146,13 @@ FirefoxPrintService.prototype = {
const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true);
const hasEqualPageSizes = this.pagesOverview.every(function (size) {
return (
size.width === this.pagesOverview[0].width &&
size.height === this.pagesOverview[0].height
const { width, height } = this.pagesOverview[0];
const hasEqualPageSizes = this.pagesOverview.every(
size => size.width === width && size.height === height
);
}, this);
if (!hasEqualPageSizes) {
console.warn(
"Not all pages have the same size. The printed " +
"result may be incorrect!"
"Not all pages have the same size. The printed result may be incorrect!"
);
}
@ -163,8 +160,7 @@ FirefoxPrintService.prototype = {
// set. Note that we assume that all pages have the same size, because
// variable-size pages are scaled down to the initial page size in Firefox.
this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0];
this.pageStyleSheet.textContent = `@page { size: ${pageSize.width}pt ${pageSize.height}pt;}`;
this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
body.append(this.pageStyleSheet);
if (pdfDocument.isPureXfa) {
@ -183,7 +179,7 @@ FirefoxPrintService.prototype = {
_printAnnotationStoragePromise
);
}
},
}
destroy() {
this.printContainer.textContent = "";
@ -195,8 +191,8 @@ FirefoxPrintService.prototype = {
this.pageStyleSheet.remove();
this.pageStyleSheet = null;
}
},
};
}
}
PDFPrintServiceFactory.instance = {
get supportsPrinting() {

View File

@ -62,7 +62,8 @@ function renderPage(
});
}
function PDFPrintService(
class PDFPrintService {
constructor(
pdfDocument,
pagesOverview,
printContainer,
@ -85,23 +86,19 @@ function PDFPrintService(
this.scratchCanvas = document.createElement("canvas");
}
PDFPrintService.prototype = {
layout() {
this.throwIfInactive();
const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true);
const hasEqualPageSizes = this.pagesOverview.every(function (size) {
return (
size.width === this.pagesOverview[0].width &&
size.height === this.pagesOverview[0].height
const { width, height } = this.pagesOverview[0];
const hasEqualPageSizes = this.pagesOverview.every(
size => size.width === width && size.height === height
);
}, this);
if (!hasEqualPageSizes) {
console.warn(
"Not all pages have the same size. The printed " +
"result may be incorrect!"
"Not all pages have the same size. The printed result may be incorrect!"
);
}
@ -115,11 +112,9 @@ PDFPrintService.prototype = {
// will be ignored and the user has to select the correct paper size in
// the UI if wanted.
this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0];
this.pageStyleSheet.textContent =
"@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
body.append(this.pageStyleSheet);
},
}
destroy() {
if (activeService !== this) {
@ -144,7 +139,7 @@ PDFPrintService.prototype = {
overlayManager.close(dialog);
}
});
},
}
renderPages() {
if (this.pdfDocument.isPureXfa) {
@ -177,7 +172,7 @@ PDFPrintService.prototype = {
}, reject);
};
return new Promise(renderNextPage);
},
}
useRenderedPage() {
this.throwIfInactive();
@ -200,7 +195,7 @@ PDFPrintService.prototype = {
img.onload = resolve;
img.onerror = reject;
});
},
}
performPrint() {
this.throwIfInactive();
@ -218,18 +213,18 @@ PDFPrintService.prototype = {
setTimeout(resolve, 20); // Tidy-up.
}, 0);
});
},
}
get active() {
return this === activeService;
},
}
throwIfInactive() {
if (!this.active) {
throw new Error("This print request was cancelled or completed.");
}
},
};
}
}
const print = window.print;
window.print = function () {