Commit 986588ad authored by Robert Knight's avatar Robert Knight

Convert PDF.js types to TypeScript

parent d674e11b
......@@ -16,20 +16,20 @@
* Document metadata parsed from the PDF's _metadata stream_.
*
* See `Metadata` class from `display/metadata.js` in PDF.js.
*
* @typedef Metadata
* @prop {(name: string) => string} get
* @prop {(name: string) => boolean} has
*/
export type Metadata = {
get(name: string): string;
has(name: string): boolean;
};
/**
* Document metadata parsed from the PDF's _document info dictionary_.
*
* See `PDFDocument#documentInfo` in PDF.js.
*
* @typedef PDFDocumentInfo
* @prop {string} [Title]
*/
export type PDFDocumentInfo = {
Title?: string;
};
/**
* An object containing metadata about the PDF. This includes information from:
......@@ -41,113 +41,138 @@
*
* See the "Metadata" section (14.3) in the PDF 1.7 reference for details of
* the _metadata stream_ and _document info dictionary_.
*
* @typedef PDFDocumentMetadata
* @prop {Metadata|null} metadata
* @prop {PDFDocumentInfo} [info]
* @prop {string|null} contentDispositionFilename - The `filename` directive from
* the `Content-Disposition` header
*/
export type PDFDocumentMetadata = {
metadata: Metadata | null;
info?: PDFDocumentInfo;
/**
* The `filename` directive from the `Content-Disposition` header
*/
contentDispositionFilename: string | null;
};
/**
* @typedef PDFDocument
* @prop {string} [fingerprint] - PDF fingerprint in PDF.js before v2.10.377.
export type PDFDocument = {
/**
* PDF fingerprint in PDF.js before v2.10.377.
* May exist in later versions depending on the PDF.js build.
* @prop {[string, string|null]} [fingerprints] - PDF fingerprints in PDF.js after
* v2.10.377. See https://github.com/mozilla/pdf.js/pull/13661. The first
* entry of this array is the "original" fingerprint and the same as the
* `fingerprint` property in older versions. The second entry is the "modified"
*/
fingerprint?: string;
/**
* PDF fingerprints in PDF.js after v2.10.377. See
* https://github.com/mozilla/pdf.js/pull/13661. The first entry of this
* array is the "original" fingerprint and the same as the `fingerprint`
* property in older versions. The second entry is the "modified"
* fingerprint. See "File Identifiers" section in the PDF spec.
* @prop {() => Promise<PDFDocumentMetadata>} getMetadata
*/
fingerprints?: [string, string | null];
getMetadata(): Promise<PDFDocumentMetadata>;
};
/**
* @typedef GetTextContentParameters
* @prop {boolean} normalizeWhitespace - Whether to convert all whitespace to
* an ASCII space char. Obsolete since https://github.com/mozilla/pdf.js/pull/14527.
export type GetTextContentParameters = {
/**
* Whether to convert all whitespace to an ASCII space char.
* Obsolete since https://github.com/mozilla/pdf.js/pull/14527.
*/
normalizeWhitespace: boolean;
};
/**
* @typedef TextContentItem
* @prop {string} str
*/
export type TextContentItem = {
str: string;
};
/**
* @typedef TextContent
* @prop {TextContentItem[]} items
*/
export type TextContent = {
items: TextContentItem[];
};
/**
* @typedef PDFPageProxy
* @prop {(o?: GetTextContentParameters) => Promise<TextContent>} getTextContent
*/
export type PDFPageProxy = {
getTextContent(o?: GetTextContentParameters): Promise<TextContent>;
};
/**
* @typedef PDFPageView
* @prop {HTMLElement} div - Container element for the PDF page
* @prop {PDFPageProxy} pdfPage
* @prop {TextLayer|null} textLayer
* @prop {number} renderingState - See `RenderingStates` enum in src/annotator/anchoring/pdf.js
*/
export type PDFPageView = {
/** Container element for the PDF page. */
div: HTMLElement;
pdfPage: PDFPageProxy;
textLayer: TextLayer | null;
/** See `RenderingStates` enum in src/annotator/anchoring/pdf.js */
renderingState: number;
};
/**
* @typedef PDFViewer
*
* Defined in `web/pdf_viewer.js` in the PDF.js source.
*
* @prop {string} currentScaleValue - Zoom level/mode. This can be a string representation
* of a float or a special constant ("auto", "page-fit", "page-width" and more)
* @prop {number} pagesCount
* @prop {EventBus} eventBus -
*/
export type PDFViewer = {
/**
* Zoom level/mode. This can be a string representation of a float or a special constant
* ("auto", "page-fit", "page-width" and more)
*/
currentScaleValue: string;
pagesCount: number;
/**
* Reference to the global event bus. Added in PDF.js v1.6.210.
* @prop {(page: number) => PDFPageView|null} getPageView
* @prop {HTMLElement} viewer - DOM element containing the main content of the document
* @prop {() => void} update - Re-render the current view
*/
eventBus: EventBus;
getPageView(page: number): PDFPageView | null;
/** DOM element containing the main content of the document. */
viewer: HTMLElement;
/** Re-render the current view. */
update(): void;
};
/**
* Defined in `web/ui_utils.js` in the PDF.js source.
*
* @typedef EventBus
* @prop {(event: string, listener: Function) => void} on
* @prop {(event: string, listener: Function) => void} off
*/
export type EventBus = {
on(event: string, listener: () => void): void;
off(event: string, listener: () => void): void;
};
/**
* Object containing references to various DOM elements that make up the PDF.js viewer UI,
* as well as a few other global objects used by the viewer.
*
* @typedef AppConfig
* @prop {HTMLElement} appContainer
* Object containing references to various DOM elements that make up the PDF.js
* viewer UI, as well as a few other global objects used by the viewer.
*/
export type AppConfig = {
appContainer: HTMLElement;
};
/**
* The `PDFViewerApplication` global which is the entry-point for accessing PDF.js.
*
* Defined in `web/app.js` in the PDF.js source.
*
* @typedef PDFViewerApplication
* @prop {AppConfig} [appConfig] - Viewer DOM elements. Since v1.5.188.
* @prop {EventBus} [eventBus] -
*/
export type PDFViewerApplication = {
/**
* Viewer DOM elements. Since v1.5.188.
*/
appConfig?: AppConfig;
/**
* Global event bus. Since v1.6.210. This is not available until the PDF viewer
* has been initialized. See `initialized` and `initializedPromise` properties.
* @prop {PDFDocument} pdfDocument
* @prop {PDFViewer} pdfViewer
* @prop {boolean} downloadComplete
* @prop {PDFDocumentInfo} documentInfo
* @prop {Metadata} metadata
* @prop {boolean} initialized - Indicates that the PDF viewer is initialized.
* @prop {Promise<void>} [initializedPromise] -
*/
eventBus?: EventBus;
pdfDocument: PDFDocument;
pdfViewer: PDFViewer;
downloadComplete: boolean;
documentInfo: PDFDocumentInfo;
metadata: Metadata;
/**
* Indicates that the PDF viewer is initialized.
*/
initialized: boolean;
/**
* Promise that resolves when PDF.js is initialized. Since v2.4.456.
* See https://github.com/mozilla/pdf.js/wiki/Third-party-viewer-usage#initialization-promise.
* @prop {string} url - The URL of the loaded PDF file
*/
initializedPromise?: Promise<void>;
/** The URL of the loaded PDF file. */
url: string;
};
/**
* @typedef TextLayer
* @prop {boolean} renderingDone
* @prop {HTMLElement} [div] - New name for root element of text layer in PDF.js >= v3.2.146
* @prop {HTMLElement} [textLayerDiv] - Old name for root element of text layer
export type TextLayer = {
renderingDone: boolean;
/**
* New name for root element of text layer in PDF.js >= v3.2.146
*/
export {};
div?: HTMLElement;
/** Old name for root element of text layer. */
textLayerDiv?: HTMLElement;
};
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