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

Convert PDF.js types to TypeScript

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