1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 06:38:07 +02:00

[api-major] Change viewer component render-methods to take parameter objects

This is nicer than a bunch of somewhat arbitrarily ordered parameters, and makes any future changes easier.
This commit is contained in:
Jonas Jenwald 2025-01-21 23:26:20 +01:00
parent 79a976ce9d
commit 7450457525
7 changed files with 83 additions and 25 deletions

View file

@ -42,6 +42,12 @@ import { GenericL10n } from "web-null_l10n";
* @property {function} [onAppend] * @property {function} [onAppend]
*/ */
/**
* @typedef {Object} AnnotationEditorLayerBuilderRenderOptions
* @property {PageViewport} viewport
* @property {string} [intent] - The default value is "display".
*/
class AnnotationEditorLayerBuilder { class AnnotationEditorLayerBuilder {
#annotationLayer = null; #annotationLayer = null;
@ -77,10 +83,10 @@ class AnnotationEditorLayerBuilder {
} }
/** /**
* @param {PageViewport} viewport * @param {AnnotationEditorLayerBuilderRenderOptions} options
* @param {string} intent (default value is 'display') * @returns {Promise<void>}
*/ */
async render(viewport, intent = "display") { async render({ viewport, intent = "display" }) {
if (intent !== "display") { if (intent !== "display") {
return; return;
} }

View file

@ -21,6 +21,8 @@
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */ /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./struct_tree_layer_builder.js").StructTreeLayerBuilder} StructTreeLayerBuilder */
// eslint-disable-next-line max-len
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */ /** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */ /** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
@ -47,6 +49,13 @@ import { PresentationModeState } from "./ui_utils.js";
* @property {function} [onAppend] * @property {function} [onAppend]
*/ */
/**
* @typedef {Object} AnnotationLayerBuilderRenderOptions
* @property {PageViewport} viewport
* @property {string} [intent] - The default value is "display".
* @property {StructTreeLayerBuilder} [structTreeLayer]
*/
class AnnotationLayerBuilder { class AnnotationLayerBuilder {
#onAppend = null; #onAppend = null;
@ -91,13 +100,11 @@ class AnnotationLayerBuilder {
} }
/** /**
* @param {PageViewport} viewport * @param {AnnotationLayerBuilderRenderOptions} options
* @param {Object} options
* @param {string} intent (default value is 'display')
* @returns {Promise<void>} A promise that is resolved when rendering of the * @returns {Promise<void>} A promise that is resolved when rendering of the
* annotations is complete. * annotations is complete.
*/ */
async render(viewport, options, intent = "display") { async render({ viewport, intent = "display", structTreeLayer = null }) {
if (this.div) { if (this.div) {
if (this._cancelled || !this.annotationLayer) { if (this._cancelled || !this.annotationLayer) {
return; return;
@ -137,7 +144,7 @@ class AnnotationLayerBuilder {
annotationEditorUIManager: this._annotationEditorUIManager, annotationEditorUIManager: this._annotationEditorUIManager,
page: this.pdfPage, page: this.pdfPage,
viewport: viewport.clone({ dontFlip: true }), viewport: viewport.clone({ dontFlip: true }),
structTreeLayer: options?.structTreeLayer || null, structTreeLayer,
}); });
await this.annotationLayer.render({ await this.annotationLayer.render({

View file

@ -20,6 +20,11 @@ import { DrawLayer } from "pdfjs-lib";
* @property {number} pageIndex * @property {number} pageIndex
*/ */
/**
* @typedef {Object} DrawLayerBuilderRenderOptions
* @property {string} [intent] - The default value is "display".
*/
class DrawLayerBuilder { class DrawLayerBuilder {
#drawLayer = null; #drawLayer = null;
@ -31,9 +36,10 @@ class DrawLayerBuilder {
} }
/** /**
* @param {string} intent (default value is 'display') * @param {DrawLayerBuilderRenderOptions} options
* @returns {Promise<void>}
*/ */
async render(intent = "display") { async render({ intent = "display" }) {
if (intent !== "display" || this.#drawLayer || this._cancelled) { if (intent !== "display" || this.#drawLayer || this._cancelled) {
return; return;
} }

View file

@ -396,11 +396,11 @@ class PDFPageView {
async #renderAnnotationLayer() { async #renderAnnotationLayer() {
let error = null; let error = null;
try { try {
await this.annotationLayer.render( await this.annotationLayer.render({
this.viewport, viewport: this.viewport,
{ structTreeLayer: this.structTreeLayer }, intent: "display",
"display" structTreeLayer: this.structTreeLayer,
); });
} catch (ex) { } catch (ex) {
console.error("#renderAnnotationLayer:", ex); console.error("#renderAnnotationLayer:", ex);
error = ex; error = ex;
@ -412,7 +412,10 @@ class PDFPageView {
async #renderAnnotationEditorLayer() { async #renderAnnotationEditorLayer() {
let error = null; let error = null;
try { try {
await this.annotationEditorLayer.render(this.viewport, "display"); await this.annotationEditorLayer.render({
viewport: this.viewport,
intent: "display",
});
} catch (ex) { } catch (ex) {
console.error("#renderAnnotationEditorLayer:", ex); console.error("#renderAnnotationEditorLayer:", ex);
error = ex; error = ex;
@ -423,7 +426,9 @@ class PDFPageView {
async #renderDrawLayer() { async #renderDrawLayer() {
try { try {
await this.drawLayer.render("display"); await this.drawLayer.render({
intent: "display",
});
} catch (ex) { } catch (ex) {
console.error("#renderDrawLayer:", ex); console.error("#renderDrawLayer:", ex);
} }
@ -432,7 +437,10 @@ class PDFPageView {
async #renderXfaLayer() { async #renderXfaLayer() {
let error = null; let error = null;
try { try {
const result = await this.xfaLayer.render(this.viewport, "display"); const result = await this.xfaLayer.render({
viewport: this.viewport,
intent: "display",
});
if (result?.textDivs && this._textHighlighter) { if (result?.textDivs && this._textHighlighter) {
// Given that the following method fetches the text asynchronously we // Given that the following method fetches the text asynchronously we
// can invoke it *before* appending the xfaLayer to the DOM (below), // can invoke it *before* appending the xfaLayer to the DOM (below),
@ -461,7 +469,9 @@ class PDFPageView {
let error = null; let error = null;
try { try {
await this.textLayer.render(this.viewport); await this.textLayer.render({
viewport: this.viewport,
});
} catch (ex) { } catch (ex) {
if (ex instanceof AbortException) { if (ex instanceof AbortException) {
return; return;

View file

@ -13,6 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
/** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
import { removeNullCharacters } from "./ui_utils.js"; import { removeNullCharacters } from "./ui_utils.js";
const PDF_ROLE_TO_HTML_ROLE = { const PDF_ROLE_TO_HTML_ROLE = {
@ -73,6 +75,12 @@ const PDF_ROLE_TO_HTML_ROLE = {
const HEADING_PATTERN = /^H(\d+)$/; const HEADING_PATTERN = /^H(\d+)$/;
/**
* @typedef {Object} StructTreeLayerBuilderOptions
* @property {PDFPageProxy} pdfPage
* @property {Object} rawDims
*/
class StructTreeLayerBuilder { class StructTreeLayerBuilder {
#promise; #promise;
@ -86,11 +94,17 @@ class StructTreeLayerBuilder {
#elementsToAddToTextLayer = null; #elementsToAddToTextLayer = null;
/**
* @param {StructTreeLayerBuilderOptions} options
*/
constructor(pdfPage, rawDims) { constructor(pdfPage, rawDims) {
this.#promise = pdfPage.getStructTree(); this.#promise = pdfPage.getStructTree();
this.#rawDims = rawDims; this.#rawDims = rawDims;
} }
/**
* @returns {Promise<void>}
*/
async render() { async render() {
if (this.#treePromise) { if (this.#treePromise) {
return this.#treePromise; return this.#treePromise;

View file

@ -29,9 +29,16 @@ import { removeNullCharacters } from "./ui_utils.js";
* @property {TextHighlighter} [highlighter] - Optional object that will handle * @property {TextHighlighter} [highlighter] - Optional object that will handle
* highlighting text from the find controller. * highlighting text from the find controller.
* @property {TextAccessibilityManager} [accessibilityManager] * @property {TextAccessibilityManager} [accessibilityManager]
* @property {boolean} [enablePermissions]
* @property {function} [onAppend] * @property {function} [onAppend]
*/ */
/**
* @typedef {Object} TextLayerBuilderRenderOptions
* @property {PageViewport} viewport
* @property {Object} [textContentParams]
*/
/** /**
* The text layer builder provides text selection functionality for the PDF. * The text layer builder provides text selection functionality for the PDF.
* It does this by creating overlay divs over the PDF's text. These divs * It does this by creating overlay divs over the PDF's text. These divs
@ -50,6 +57,9 @@ class TextLayerBuilder {
static #selectionChangeAbortController = null; static #selectionChangeAbortController = null;
/**
* @param {TextLayerBuilderOptions} options
*/
constructor({ constructor({
pdfPage, pdfPage,
highlighter = null, highlighter = null,
@ -70,10 +80,10 @@ class TextLayerBuilder {
/** /**
* Renders the text layer. * Renders the text layer.
* @param {PageViewport} viewport * @param {TextLayerBuilderRenderOptions} options
* @param {Object} [textContentParams] * @returns {Promise<void>}
*/ */
async render(viewport, textContentParams = null) { async render({ viewport, textContentParams = null }) {
if (this.#renderingDone && this.#textLayer) { if (this.#renderingDone && this.#textLayer) {
this.#textLayer.update({ this.#textLayer.update({
viewport, viewport,

View file

@ -30,6 +30,12 @@ import { XfaLayer } from "pdfjs-lib";
* @property {Object} [xfaHtml] * @property {Object} [xfaHtml]
*/ */
/**
* @typedef {Object} XfaLayerBuilderRenderOptions
* @property {PageViewport} viewport
* @property {string} [intent] - The default value is "display".
*/
class XfaLayerBuilder { class XfaLayerBuilder {
/** /**
* @param {XfaLayerBuilderOptions} options * @param {XfaLayerBuilderOptions} options
@ -50,13 +56,12 @@ class XfaLayerBuilder {
} }
/** /**
* @param {PageViewport} viewport * @param {XfaLayerBuilderRenderOptions} viewport
* @param {string} intent (default value is 'display')
* @returns {Promise<Object | void>} A promise that is resolved when rendering * @returns {Promise<Object | void>} A promise that is resolved when rendering
* of the XFA layer is complete. The first rendering will return an object * of the XFA layer is complete. The first rendering will return an object
* with a `textDivs` property that can be used with the TextHighlighter. * with a `textDivs` property that can be used with the TextHighlighter.
*/ */
async render(viewport, intent = "display") { async render({ viewport, intent = "display" }) {
if (intent === "print") { if (intent === "print") {
const parameters = { const parameters = {
viewport: viewport.clone({ dontFlip: true }), viewport: viewport.clone({ dontFlip: true }),