Use let/const instead of var in the printing code

This commit is contained in:
Jonas Jenwald 2017-10-22 16:13:14 +02:00
parent a0ec980e63
commit d14cb5eb27
2 changed files with 50 additions and 50 deletions

View File

@ -19,11 +19,11 @@ import { shadow } from 'pdfjs-lib';
// 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(pdfDocument, pageNumber, size, printContainer) { function composePage(pdfDocument, pageNumber, size, printContainer) {
var canvas = document.createElement('canvas'); let canvas = document.createElement('canvas');
// The size of the canvas in pixels for printing. // The size of the canvas in pixels for printing.
var PRINT_RESOLUTION = 150; const PRINT_RESOLUTION = 150;
var PRINT_UNITS = PRINT_RESOLUTION / 72.0; const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
canvas.width = Math.floor(size.width * PRINT_UNITS); canvas.width = Math.floor(size.width * PRINT_UNITS);
canvas.height = Math.floor(size.height * PRINT_UNITS); canvas.height = Math.floor(size.height * PRINT_UNITS);
@ -31,13 +31,13 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
canvas.style.width = Math.floor(size.width * CSS_UNITS) + 'px'; canvas.style.width = Math.floor(size.width * CSS_UNITS) + 'px';
canvas.style.height = Math.floor(size.height * CSS_UNITS) + 'px'; canvas.style.height = Math.floor(size.height * CSS_UNITS) + 'px';
var canvasWrapper = document.createElement('div'); let canvasWrapper = document.createElement('div');
canvasWrapper.appendChild(canvas); canvasWrapper.appendChild(canvas);
printContainer.appendChild(canvasWrapper); printContainer.appendChild(canvasWrapper);
canvas.mozPrintCallback = function(obj) { canvas.mozPrintCallback = function(obj) {
// Printing/rendering the page. // Printing/rendering the page.
var ctx = obj.context; let ctx = obj.context;
ctx.save(); ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)'; ctx.fillStyle = 'rgb(255, 255, 255)';
@ -45,7 +45,7 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
ctx.restore(); ctx.restore();
pdfDocument.getPage(pageNumber).then(function(pdfPage) { pdfDocument.getPage(pageNumber).then(function(pdfPage) {
var renderContext = { let renderContext = {
canvasContext: ctx, canvasContext: ctx,
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0], transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
viewport: pdfPage.getViewport(1, size.rotation), viewport: pdfPage.getViewport(1, size.rotation),
@ -76,12 +76,12 @@ function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
FirefoxPrintService.prototype = { FirefoxPrintService.prototype = {
layout() { layout() {
var pdfDocument = this.pdfDocument; let pdfDocument = this.pdfDocument;
var printContainer = this.printContainer; let printContainer = this.printContainer;
var body = document.querySelector('body'); let body = document.querySelector('body');
body.setAttribute('data-pdfjsprinting', true); body.setAttribute('data-pdfjsprinting', true);
for (var i = 0, ii = this.pagesOverview.length; i < ii; ++i) { for (let i = 0, ii = this.pagesOverview.length; i < ii; ++i) {
composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer); composePage(pdfDocument, i + 1, this.pagesOverview[i], printContainer);
} }
}, },
@ -93,8 +93,8 @@ FirefoxPrintService.prototype = {
PDFPrintServiceFactory.instance = { PDFPrintServiceFactory.instance = {
get supportsPrinting() { get supportsPrinting() {
var canvas = document.createElement('canvas'); let canvas = document.createElement('canvas');
var value = 'mozPrintCallback' in canvas; let value = 'mozPrintCallback' in canvas;
return shadow(this, 'supportsPrinting', value); return shadow(this, 'supportsPrinting', value);
}, },

View File

@ -23,26 +23,26 @@ let overlayManager = null;
// Renders the page to the canvas of the given print service, and returns // Renders the page to the canvas of the given print service, and returns
// the suggested dimensions of the output page. // the suggested dimensions of the output page.
function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) { function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) {
var scratchCanvas = activeService.scratchCanvas; let scratchCanvas = activeService.scratchCanvas;
// The size of the canvas in pixels for printing. // The size of the canvas in pixels for printing.
var PRINT_RESOLUTION = 150; const PRINT_RESOLUTION = 150;
var PRINT_UNITS = PRINT_RESOLUTION / 72.0; const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
scratchCanvas.width = Math.floor(size.width * PRINT_UNITS); scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
scratchCanvas.height = Math.floor(size.height * PRINT_UNITS); scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
// The physical size of the img as specified by the PDF document. // The physical size of the img as specified by the PDF document.
var width = Math.floor(size.width * CSS_UNITS) + 'px'; let width = Math.floor(size.width * CSS_UNITS) + 'px';
var height = Math.floor(size.height * CSS_UNITS) + 'px'; let height = Math.floor(size.height * CSS_UNITS) + 'px';
var ctx = scratchCanvas.getContext('2d'); let ctx = scratchCanvas.getContext('2d');
ctx.save(); ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)'; ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, scratchCanvas.width, scratchCanvas.height); ctx.fillRect(0, 0, scratchCanvas.width, scratchCanvas.height);
ctx.restore(); ctx.restore();
return pdfDocument.getPage(pageNumber).then(function(pdfPage) { return pdfDocument.getPage(pageNumber).then(function(pdfPage) {
var renderContext = { let renderContext = {
canvasContext: ctx, canvasContext: ctx,
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0], transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
viewport: pdfPage.getViewport(1, size.rotation), viewport: pdfPage.getViewport(1, size.rotation),
@ -71,10 +71,10 @@ PDFPrintService.prototype = {
layout() { layout() {
this.throwIfInactive(); this.throwIfInactive();
var body = document.querySelector('body'); let body = document.querySelector('body');
body.setAttribute('data-pdfjsprinting', true); body.setAttribute('data-pdfjsprinting', true);
var hasEqualPageSizes = this.pagesOverview.every(function (size) { let hasEqualPageSizes = this.pagesOverview.every(function(size) {
return size.width === this.pagesOverview[0].width && return size.width === this.pagesOverview[0].width &&
size.height === this.pagesOverview[0].height; size.height === this.pagesOverview[0].height;
}, this); }, this);
@ -93,7 +93,7 @@ PDFPrintService.prototype = {
// https://bugzil.la/851441), the next stylesheet will be ignored and the // https://bugzil.la/851441), the next stylesheet will be ignored and the
// user has to select the correct paper size in the UI if wanted. // user has to select the correct paper size in the UI if wanted.
this.pageStyleSheet = document.createElement('style'); this.pageStyleSheet = document.createElement('style');
var pageSize = this.pagesOverview[0]; let pageSize = this.pagesOverview[0];
this.pageStyleSheet.textContent = this.pageStyleSheet.textContent =
// "size:<width> <height>" is what we need. But also add "A4" because // "size:<width> <height>" is what we need. But also add "A4" because
// Firefox incorrectly reports support for the other value. // Firefox incorrectly reports support for the other value.
@ -127,15 +127,15 @@ PDFPrintService.prototype = {
}, },
renderPages() { renderPages() {
var pageCount = this.pagesOverview.length; let pageCount = this.pagesOverview.length;
var renderNextPage = (resolve, reject) => { let renderNextPage = (resolve, reject) => {
this.throwIfInactive(); this.throwIfInactive();
if (++this.currentPage >= pageCount) { if (++this.currentPage >= pageCount) {
renderProgress(pageCount, pageCount, this.l10n); renderProgress(pageCount, pageCount, this.l10n);
resolve(); resolve();
return; return;
} }
var index = this.currentPage; let index = this.currentPage;
renderProgress(index, pageCount, this.l10n); renderProgress(index, pageCount, this.l10n);
renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index]) renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index])
.then(this.useRenderedPage.bind(this)) .then(this.useRenderedPage.bind(this))
@ -148,11 +148,11 @@ PDFPrintService.prototype = {
useRenderedPage(printItem) { useRenderedPage(printItem) {
this.throwIfInactive(); this.throwIfInactive();
var img = document.createElement('img'); let img = document.createElement('img');
img.style.width = printItem.width; img.style.width = printItem.width;
img.style.height = printItem.height; img.style.height = printItem.height;
var scratchCanvas = this.scratchCanvas; let scratchCanvas = this.scratchCanvas;
if (('toBlob' in scratchCanvas) && !PDFJS.disableCreateObjectURL) { if (('toBlob' in scratchCanvas) && !PDFJS.disableCreateObjectURL) {
scratchCanvas.toBlob(function(blob) { scratchCanvas.toBlob(function(blob) {
img.src = URL.createObjectURL(blob); img.src = URL.createObjectURL(blob);
@ -161,7 +161,7 @@ PDFPrintService.prototype = {
img.src = scratchCanvas.toDataURL(); img.src = scratchCanvas.toDataURL();
} }
var wrapper = document.createElement('div'); let wrapper = document.createElement('div');
wrapper.appendChild(img); wrapper.appendChild(img);
this.printContainer.appendChild(wrapper); this.printContainer.appendChild(wrapper);
@ -201,7 +201,7 @@ PDFPrintService.prototype = {
}; };
var print = window.print; let print = window.print;
window.print = function print() { window.print = function print() {
if (activeService) { if (activeService) {
console.warn('Ignored window.print() because of a pending print job.'); console.warn('Ignored window.print() because of a pending print job.');
@ -225,7 +225,7 @@ window.print = function print() {
}); });
return; // eslint-disable-line no-unsafe-finally return; // eslint-disable-line no-unsafe-finally
} }
var activeServiceOnEntry = activeService; let activeServiceOnEntry = activeService;
activeService.renderPages().then(function() { activeService.renderPages().then(function() {
return activeServiceOnEntry.performPrint(); return activeServiceOnEntry.performPrint();
}).catch(function() { }).catch(function() {
@ -244,7 +244,7 @@ window.print = function print() {
}; };
function dispatchEvent(eventType) { function dispatchEvent(eventType) {
var event = document.createEvent('CustomEvent'); let event = document.createEvent('CustomEvent');
event.initCustomEvent(eventType, false, false, 'custom'); event.initCustomEvent(eventType, false, false, 'custom');
window.dispatchEvent(event); window.dispatchEvent(event);
} }
@ -257,10 +257,10 @@ function abort() {
} }
function renderProgress(index, total, l10n) { function renderProgress(index, total, l10n) {
var progressContainer = document.getElementById('printServiceOverlay'); let progressContainer = document.getElementById('printServiceOverlay');
var progress = Math.round(100 * index / total); let progress = Math.round(100 * index / total);
var progressBar = progressContainer.querySelector('progress'); let progressBar = progressContainer.querySelector('progress');
var progressPerc = progressContainer.querySelector('.relative-progress'); let progressPerc = progressContainer.querySelector('.relative-progress');
progressBar.value = progress; progressBar.value = progress;
l10n.get('print_progress_percent', { progress, }, progress + '%'). l10n.get('print_progress_percent', { progress, }, progress + '%').
then((msg) => { then((msg) => {
@ -268,7 +268,7 @@ function renderProgress(index, total, l10n) {
}); });
} }
var hasAttachEvent = !!document.attachEvent; let hasAttachEvent = !!document.attachEvent;
window.addEventListener('keydown', function(event) { window.addEventListener('keydown', function(event) {
// Intercept Cmd/Ctrl + P in all browsers. // Intercept Cmd/Ctrl + P in all browsers.
@ -303,7 +303,7 @@ if (hasAttachEvent) {
if ('onbeforeprint' in window) { if ('onbeforeprint' in window) {
// Do not propagate before/afterprint events when they are not triggered // Do not propagate before/afterprint events when they are not triggered
// from within this polyfill. (FF/IE). // from within this polyfill. (FF/IE).
var stopPropagationIfNeeded = function(event) { let stopPropagationIfNeeded = function(event) {
if (event.detail !== 'custom' && event.stopImmediatePropagation) { if (event.detail !== 'custom' && event.stopImmediatePropagation) {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
} }
@ -312,7 +312,7 @@ if ('onbeforeprint' in window) {
window.addEventListener('afterprint', stopPropagationIfNeeded); window.addEventListener('afterprint', stopPropagationIfNeeded);
} }
var overlayPromise; let overlayPromise;
function ensureOverlay() { function ensureOverlay() {
if (!overlayPromise) { if (!overlayPromise) {
overlayManager = PDFViewerApplication.overlayManager; overlayManager = PDFViewerApplication.overlayManager;