mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Convert the PDFDocumentProperties
class to use private methods
Given that none of these methods were ever intended to be accessed directly from the outside, we can use modern ECMAScript features to ensure that they are indeed private. This patch also makes `fieldData` private, to remove the old hack used to prevent it from being modified from the outside.
This commit is contained in:
parent
9e4aaf18f7
commit
ba8dae696a
1 changed files with 29 additions and 52 deletions
|
@ -52,6 +52,8 @@ function getPageName(size, isPortrait, pageNames) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class PDFDocumentProperties {
|
class PDFDocumentProperties {
|
||||||
|
#fieldData = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {PDFDocumentPropertiesOptions} options
|
* @param {PDFDocumentPropertiesOptions} options
|
||||||
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
|
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
|
||||||
|
@ -70,7 +72,7 @@ class PDFDocumentProperties {
|
||||||
this.overlayManager = overlayManager;
|
this.overlayManager = overlayManager;
|
||||||
this.l10n = l10n;
|
this.l10n = l10n;
|
||||||
|
|
||||||
this._reset();
|
this.#reset();
|
||||||
// Bind the event listener for the Close button.
|
// Bind the event listener for the Close button.
|
||||||
closeButton.addEventListener("click", this.close.bind(this));
|
closeButton.addEventListener("click", this.close.bind(this));
|
||||||
|
|
||||||
|
@ -97,15 +99,6 @@ class PDFDocumentProperties {
|
||||||
* Open the document properties overlay.
|
* Open the document properties overlay.
|
||||||
*/
|
*/
|
||||||
async open() {
|
async open() {
|
||||||
const freezeFieldData = data => {
|
|
||||||
Object.defineProperty(this, "fieldData", {
|
|
||||||
value: Object.freeze(data),
|
|
||||||
writable: false,
|
|
||||||
enumerable: true,
|
|
||||||
configurable: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.overlayManager.open(this.overlayName),
|
this.overlayManager.open(this.overlayName),
|
||||||
this._dataAvailableCapability.promise,
|
this._dataAvailableCapability.promise,
|
||||||
|
@ -116,11 +109,11 @@ class PDFDocumentProperties {
|
||||||
// If the document properties were previously fetched (for this PDF file),
|
// If the document properties were previously fetched (for this PDF file),
|
||||||
// just update the dialog immediately to avoid redundant lookups.
|
// just update the dialog immediately to avoid redundant lookups.
|
||||||
if (
|
if (
|
||||||
this.fieldData &&
|
this.#fieldData &&
|
||||||
currentPageNumber === this.fieldData._currentPageNumber &&
|
currentPageNumber === this.#fieldData._currentPageNumber &&
|
||||||
pagesRotation === this.fieldData._pagesRotation
|
pagesRotation === this.#fieldData._pagesRotation
|
||||||
) {
|
) {
|
||||||
this._updateUI();
|
this.#updateUI();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,16 +134,16 @@ class PDFDocumentProperties {
|
||||||
isLinearized,
|
isLinearized,
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
contentDispositionFilename || getPdfFilenameFromUrl(this.url),
|
contentDispositionFilename || getPdfFilenameFromUrl(this.url),
|
||||||
this._parseFileSize(contentLength),
|
this.#parseFileSize(contentLength),
|
||||||
this._parseDate(info.CreationDate),
|
this.#parseDate(info.CreationDate),
|
||||||
this._parseDate(info.ModDate),
|
this.#parseDate(info.ModDate),
|
||||||
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
|
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
|
||||||
return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
|
return this.#parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
|
||||||
}),
|
}),
|
||||||
this._parseLinearization(info.IsLinearized),
|
this.#parseLinearization(info.IsLinearized),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
freezeFieldData({
|
this.#fieldData = Object.freeze({
|
||||||
fileName,
|
fileName,
|
||||||
fileSize,
|
fileSize,
|
||||||
title: info.Title,
|
title: info.Title,
|
||||||
|
@ -168,7 +161,7 @@ class PDFDocumentProperties {
|
||||||
_currentPageNumber: currentPageNumber,
|
_currentPageNumber: currentPageNumber,
|
||||||
_pagesRotation: pagesRotation,
|
_pagesRotation: pagesRotation,
|
||||||
});
|
});
|
||||||
this._updateUI();
|
this.#updateUI();
|
||||||
|
|
||||||
// Get the correct fileSize, since it may not have been available
|
// Get the correct fileSize, since it may not have been available
|
||||||
// or could potentially be wrong.
|
// or could potentially be wrong.
|
||||||
|
@ -176,11 +169,11 @@ class PDFDocumentProperties {
|
||||||
if (contentLength === length) {
|
if (contentLength === length) {
|
||||||
return; // The fileSize has already been correctly set.
|
return; // The fileSize has already been correctly set.
|
||||||
}
|
}
|
||||||
const data = Object.assign(Object.create(null), this.fieldData);
|
const data = Object.assign(Object.create(null), this.#fieldData);
|
||||||
data.fileSize = await this._parseFileSize(length);
|
data.fileSize = await this.#parseFileSize(length);
|
||||||
|
|
||||||
freezeFieldData(data);
|
this.#fieldData = Object.freeze(data);
|
||||||
this._updateUI();
|
this.#updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -201,8 +194,8 @@ class PDFDocumentProperties {
|
||||||
*/
|
*/
|
||||||
setDocument(pdfDocument, url = null) {
|
setDocument(pdfDocument, url = null) {
|
||||||
if (this.pdfDocument) {
|
if (this.pdfDocument) {
|
||||||
this._reset();
|
this.#reset();
|
||||||
this._updateUI(true);
|
this.#updateUI(true);
|
||||||
}
|
}
|
||||||
if (!pdfDocument) {
|
if (!pdfDocument) {
|
||||||
return;
|
return;
|
||||||
|
@ -213,14 +206,11 @@ class PDFDocumentProperties {
|
||||||
this._dataAvailableCapability.resolve();
|
this._dataAvailableCapability.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
#reset() {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_reset() {
|
|
||||||
this.pdfDocument = null;
|
this.pdfDocument = null;
|
||||||
this.url = null;
|
this.url = null;
|
||||||
|
|
||||||
delete this.fieldData;
|
this.#fieldData = null;
|
||||||
this._dataAvailableCapability = createPromiseCapability();
|
this._dataAvailableCapability = createPromiseCapability();
|
||||||
this._currentPageNumber = 1;
|
this._currentPageNumber = 1;
|
||||||
this._pagesRotation = 0;
|
this._pagesRotation = 0;
|
||||||
|
@ -230,10 +220,9 @@ class PDFDocumentProperties {
|
||||||
* Always updates all of the dialog fields, to prevent inconsistent UI state.
|
* Always updates all of the dialog fields, to prevent inconsistent UI state.
|
||||||
* NOTE: If the contents of a particular field is neither a non-empty string,
|
* NOTE: If the contents of a particular field is neither a non-empty string,
|
||||||
* nor a number, it will fall back to `DEFAULT_FIELD_CONTENT`.
|
* nor a number, it will fall back to `DEFAULT_FIELD_CONTENT`.
|
||||||
* @private
|
|
||||||
*/
|
*/
|
||||||
_updateUI(reset = false) {
|
#updateUI(reset = false) {
|
||||||
if (reset || !this.fieldData) {
|
if (reset || !this.#fieldData) {
|
||||||
for (const id in this.fields) {
|
for (const id in this.fields) {
|
||||||
this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
|
this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
|
||||||
}
|
}
|
||||||
|
@ -245,16 +234,13 @@ class PDFDocumentProperties {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (const id in this.fields) {
|
for (const id in this.fields) {
|
||||||
const content = this.fieldData[id];
|
const content = this.#fieldData[id];
|
||||||
this.fields[id].textContent =
|
this.fields[id].textContent =
|
||||||
content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
|
content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async #parseFileSize(fileSize = 0) {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async _parseFileSize(fileSize = 0) {
|
|
||||||
const kb = fileSize / 1024,
|
const kb = fileSize / 1024,
|
||||||
mb = kb / 1024;
|
mb = kb / 1024;
|
||||||
if (!kb) {
|
if (!kb) {
|
||||||
|
@ -267,10 +253,7 @@ class PDFDocumentProperties {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async #parsePageSize(pageSizeInches, pagesRotation) {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async _parsePageSize(pageSizeInches, pagesRotation) {
|
|
||||||
if (!pageSizeInches) {
|
if (!pageSizeInches) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -364,10 +347,7 @@ class PDFDocumentProperties {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async #parseDate(inputDate) {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async _parseDate(inputDate) {
|
|
||||||
const dateObject = PDFDateString.toDateObject(inputDate);
|
const dateObject = PDFDateString.toDateObject(inputDate);
|
||||||
if (!dateObject) {
|
if (!dateObject) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -378,10 +358,7 @@ class PDFDocumentProperties {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
#parseLinearization(isLinearized) {
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_parseLinearization(isLinearized) {
|
|
||||||
return this.l10n.get(
|
return this.l10n.get(
|
||||||
`document_properties_linearized_${isLinearized ? "yes" : "no"}`
|
`document_properties_linearized_${isLinearized ? "yes" : "no"}`
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue