Commit 0df4959c authored by Robert Knight's avatar Robert Knight

Modernize PDFMetadata class

 - Convert `PDFMetadata` to an ES2015 class

 - Convert the ad-hoc type documentation to tool-readable JSDoc

 - Add a little more detail in doc comments about what
   `documentFingerprint` is
parent e94ea607
......@@ -3,15 +3,33 @@
const { normalizeURI } = require('../util/url');
/**
* This PDFMetadata service extracts metadata about a loading/loaded PDF
* document from a PDF.js PDFViewerApplication object.
* @typedef Link
* @prop {string} href
*/
/**
* @typedef Metadata
* @prop {string} title - The document title
* @prop {Link[]} link - Array of URIs associated with this document
* @prop {string} documentFingerprint - The fingerprint of this PDF. This is
* referred to as the "File Identifier" in the PDF spec. It may be a hash of
* part of the content if the PDF file does not have a File Identifier.
*/
/**
* PDFMetadata extracts metadata about a loading/loaded PDF document from a
* PDF.js PDFViewerApplication object.
*/
class PDFMetadata {
/**
* Construct a `PDFMetadata` that returns URIs/metadata associated with a
* given PDF viewer.
*
* This hides from users of this service the need to wait until the PDF document
* is loaded before extracting the relevant metadata.
* @param {PDFViewerApplication} app
*/
function PDFMetadata(app) {
this._loaded = new Promise(function (resolve) {
var finish = function () {
constructor(app) {
this._loaded = new Promise(resolve => {
var finish = () => {
window.removeEventListener('documentload', finish);
resolve(app);
};
......@@ -22,30 +40,36 @@ function PDFMetadata(app) {
window.addEventListener('documentload', finish);
}
});
}
}
/**
* Returns a promise of the URI of the loaded PDF.
/**
* Return the URI of the PDF.
*
* If the PDF is currently loading, the returned promise resolves once loading
* is complete.
*
* @return {Promise<string>}
*/
PDFMetadata.prototype.getUri = function () {
return this._loaded.then(function (app) {
getUri() {
return this._loaded.then(app => {
var uri = getPDFURL(app);
if (!uri) {
uri = fingerprintToURN(app.documentFingerprint);
}
return uri;
});
};
}
/**
* Returns a promise of a metadata object, containing:
/**
* Returns metadata about the document.
*
* If the PDF is currently loading, the returned promise resolves once loading
* is complete.
*
* title(string) - The document title
* link(array) - An array of link objects representing URIs for the document
* documentFingerprint(string) - The document fingerprint
* @return {Promise<Metadata>}
*/
PDFMetadata.prototype.getMetadata = function () {
return this._loaded.then(function (app) {
getMetadata() {
return this._loaded.then(app => {
var title = document.title;
if (app.metadata && app.metadata.has('dc:title') && app.metadata.get('dc:title') !== 'Untitled') {
......@@ -69,7 +93,8 @@ PDFMetadata.prototype.getMetadata = function () {
documentFingerprint: app.documentFingerprint,
};
});
};
}
}
function fingerprintToURN(fingerprint) {
return 'urn:x-pdf:' + String(fingerprint);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment