[api-minor] Remove the annotationLayerFactory in the viewer

Please note that this functionality has never really mattered for the Firefox PDF Viewer, the GENERIC viewer, or even the "simpleviewer"/"singlepageviewer" component-examples. Hence, in practice this means that only the "pageviewer" component-example[1] have ever really utilized this.

Using factories to initialize various layers in the viewer, rather than simply invoking the relevant code directly, seems (at least to me) like a somewhat roundabout way of doing things.
Not only does this lead to more code, both to write and maintain, but since many of the layers have common parameters (e.g. an `AnnotationStorage`-instance) there's also some duplication.

Hence this patch, which removes the `annotationLayerFactory` and instead uses a lookup-function in the `PDFPageView`-class to access the external viewer-properties as necessary.
Note that this should even be an improvement for the "pageviewer" component-example, since most layers will now work by default rather than require manual configuration.

---
[1] In practice we generally suggest using the "simpleviewer", or "singlepageviewer", since it does *most* things out-of-the-box and given that a lot of functionality really require *a viewer* and not just a single page in order to work.
This commit is contained in:
Jonas Jenwald 2022-12-06 23:34:55 +01:00
parent 7aedb8ed7a
commit ca69da735e
5 changed files with 62 additions and 185 deletions

View File

@ -19,9 +19,6 @@
/** @typedef {import("./event_utils").EventBus} EventBus */ /** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */ /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
/** @typedef {import("./interfaces").IL10n} IL10n */ /** @typedef {import("./interfaces").IL10n} IL10n */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFAnnotationLayerFactory} IPDFAnnotationLayerFactory */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */ /** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */
@ -32,70 +29,11 @@
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */ /** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { NullL10n } from "./l10n_utils.js";
import { SimpleLinkService } from "./pdf_link_service.js"; import { SimpleLinkService } from "./pdf_link_service.js";
import { StructTreeLayerBuilder } from "./struct_tree_layer_builder.js"; import { StructTreeLayerBuilder } from "./struct_tree_layer_builder.js";
import { TextLayerBuilder } from "./text_layer_builder.js"; import { TextLayerBuilder } from "./text_layer_builder.js";
import { XfaLayerBuilder } from "./xfa_layer_builder.js"; import { XfaLayerBuilder } from "./xfa_layer_builder.js";
/**
* @implements IPDFAnnotationLayerFactory
*/
class DefaultAnnotationLayerFactory {
/**
* @typedef {Object} CreateAnnotationLayerBuilderParameters
* @property {HTMLDivElement} pageDiv
* @property {PDFPageProxy} pdfPage
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
* data in forms.
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} renderForms
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
* annotation ids with canvases used to render them.
* @property {TextAccessibilityManager} [accessibilityManager]
*/
/**
* @param {CreateAnnotationLayerBuilderParameters}
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder({
pageDiv,
pdfPage,
annotationStorage = null,
imageResourcesPath = "",
renderForms = true,
l10n = NullL10n,
enableScripting = false,
hasJSActionsPromise = null,
fieldObjectsPromise = null,
annotationCanvasMap = null,
accessibilityManager = null,
}) {
return new AnnotationLayerBuilder({
pageDiv,
pdfPage,
imageResourcesPath,
renderForms,
linkService: new SimpleLinkService(),
l10n,
annotationStorage,
enableScripting,
hasJSActionsPromise,
fieldObjectsPromise,
annotationCanvasMap,
accessibilityManager,
});
}
}
/** /**
* @implements IPDFStructTreeLayerFactory * @implements IPDFStructTreeLayerFactory
*/ */
@ -163,7 +101,6 @@ class DefaultXfaLayerFactory {
} }
export { export {
DefaultAnnotationLayerFactory,
DefaultStructTreeLayerFactory, DefaultStructTreeLayerFactory,
DefaultTextLayerFactory, DefaultTextLayerFactory,
DefaultXfaLayerFactory, DefaultXfaLayerFactory,

View File

@ -18,8 +18,6 @@
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */ /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./annotation_layer_builder").AnnotationLayerBuilder} AnnotationLayerBuilder */
// eslint-disable-next-line max-len
/** @typedef {import("./struct_tree_builder").StructTreeLayerBuilder} StructTreeLayerBuilder */ /** @typedef {import("./struct_tree_builder").StructTreeLayerBuilder} StructTreeLayerBuilder */
/** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */ /** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
@ -181,48 +179,6 @@ class IPDFTextLayerFactory {
}) {} }) {}
} }
/**
* @interface
*/
class IPDFAnnotationLayerFactory {
/**
* @typedef {Object} CreateAnnotationLayerBuilderParameters
* @property {HTMLDivElement} pageDiv
* @property {PDFPageProxy} pdfPage
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
* data in forms.
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} renderForms
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
* annotation ids with canvases used to render them.
* @property {TextAccessibilityManager} [accessibilityManager]
*/
/**
* @param {CreateAnnotationLayerBuilderParameters}
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder({
pageDiv,
pdfPage,
annotationStorage = null,
imageResourcesPath = "",
renderForms = true,
l10n = undefined,
enableScripting = false,
hasJSActionsPromise = null,
fieldObjectsPromise = null,
annotationCanvasMap = null,
accessibilityManager = null,
}) {}
}
/** /**
* @interface * @interface
*/ */
@ -321,7 +277,6 @@ class IL10n {
export { export {
IDownloadManager, IDownloadManager,
IL10n, IL10n,
IPDFAnnotationLayerFactory,
IPDFLinkService, IPDFLinkService,
IPDFStructTreeLayerFactory, IPDFStructTreeLayerFactory,
IPDFTextLayerFactory, IPDFTextLayerFactory,

View File

@ -20,8 +20,6 @@
/** @typedef {import("./event_utils").EventBus} EventBus */ /** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IL10n} IL10n */ /** @typedef {import("./interfaces").IL10n} IL10n */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFAnnotationLayerFactory} IPDFAnnotationLayerFactory */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */ /** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFTextLayerFactory} IPDFTextLayerFactory */ /** @typedef {import("./interfaces").IPDFTextLayerFactory} IPDFTextLayerFactory */
@ -51,8 +49,10 @@ import {
TextLayerMode, TextLayerMode,
} from "./ui_utils.js"; } from "./ui_utils.js";
import { AnnotationEditorLayerBuilder } from "./annotation_editor_layer_builder.js"; import { AnnotationEditorLayerBuilder } from "./annotation_editor_layer_builder.js";
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { compatibilityParams } from "./app_options.js"; import { compatibilityParams } from "./app_options.js";
import { NullL10n } from "./l10n_utils.js"; import { NullL10n } from "./l10n_utils.js";
import { SimpleLinkService } from "./pdf_link_service.js";
import { TextAccessibilityManager } from "./text_accessibility.js"; import { TextAccessibilityManager } from "./text_accessibility.js";
/** /**
@ -75,7 +75,6 @@ import { TextAccessibilityManager } from "./text_accessibility.js";
* being rendered. The constants from {@link AnnotationMode} should be used; * being rendered. The constants from {@link AnnotationMode} should be used;
* see also {@link RenderParameters} and {@link GetOperatorListParameters}. * see also {@link RenderParameters} and {@link GetOperatorListParameters}.
* The default value is `AnnotationMode.ENABLE_FORMS`. * The default value is `AnnotationMode.ENABLE_FORMS`.
* @property {IPDFAnnotationLayerFactory} [annotationLayerFactory]
* @property {IPDFXfaLayerFactory} [xfaLayerFactory] * @property {IPDFXfaLayerFactory} [xfaLayerFactory]
* @property {IPDFStructTreeLayerFactory} [structTreeLayerFactory] * @property {IPDFStructTreeLayerFactory} [structTreeLayerFactory]
* @property {Object} [textHighlighterFactory] * @property {Object} [textHighlighterFactory]
@ -104,6 +103,12 @@ const DEFAULT_LAYER_PROPERTIES = () => {
} }
return { return {
annotationEditorUIManager: null, annotationEditorUIManager: null,
annotationStorage: null,
downloadManager: null,
enableScripting: false,
fieldObjectsPromise: null,
hasJSActionsPromise: null,
linkService: new SimpleLinkService(),
}; };
}; };
@ -153,7 +158,6 @@ class PDFPageView {
this.eventBus = options.eventBus; this.eventBus = options.eventBus;
this.renderingQueue = options.renderingQueue; this.renderingQueue = options.renderingQueue;
this.textLayerFactory = options.textLayerFactory; this.textLayerFactory = options.textLayerFactory;
this.annotationLayerFactory = options.annotationLayerFactory;
this.xfaLayerFactory = options.xfaLayerFactory; this.xfaLayerFactory = options.xfaLayerFactory;
this._textHighlighterFactory = options.textHighlighterFactory; this._textHighlighterFactory = options.textHighlighterFactory;
this.structTreeLayerFactory = options.structTreeLayerFactory; this.structTreeLayerFactory = options.structTreeLayerFactory;
@ -778,20 +782,34 @@ class PDFPageView {
} }
if ( if (
this.#annotationMode !== AnnotationMode.DISABLE && !this.annotationLayer &&
this.annotationLayerFactory this.#annotationMode !== AnnotationMode.DISABLE
) { ) {
const {
annotationStorage,
downloadManager,
enableScripting,
fieldObjectsPromise,
hasJSActionsPromise,
linkService,
} = this.#layerProperties();
this._annotationCanvasMap ||= new Map(); this._annotationCanvasMap ||= new Map();
this.annotationLayer ||= this.annotationLayer = new AnnotationLayerBuilder({
this.annotationLayerFactory.createAnnotationLayerBuilder({ pageDiv: div,
pageDiv: div, pdfPage,
pdfPage, annotationStorage,
imageResourcesPath: this.imageResourcesPath, imageResourcesPath: this.imageResourcesPath,
renderForms: this.#annotationMode === AnnotationMode.ENABLE_FORMS, renderForms: this.#annotationMode === AnnotationMode.ENABLE_FORMS,
l10n: this.l10n, linkService,
annotationCanvasMap: this._annotationCanvasMap, downloadManager,
accessibilityManager: this._accessibilityManager, l10n: this.l10n,
}); enableScripting,
hasJSActionsPromise,
fieldObjectsPromise,
annotationCanvasMap: this._annotationCanvasMap,
accessibilityManager: this._accessibilityManager,
});
} }
if (this.xfaLayer?.div) { if (this.xfaLayer?.div) {

View File

@ -14,7 +14,6 @@
*/ */
import { import {
DefaultAnnotationLayerFactory,
DefaultStructTreeLayerFactory, DefaultStructTreeLayerFactory,
DefaultTextLayerFactory, DefaultTextLayerFactory,
DefaultXfaLayerFactory, DefaultXfaLayerFactory,
@ -51,6 +50,16 @@ const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD"); const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
class DefaultAnnotationLayerFactory {
constructor() {
throw new Error(
"The `DefaultAnnotationLayerFactory` has been removed, " +
"please use the `annotationMode` option when initializing " +
"the `PDFPageView`-instance to control AnnotationLayer rendering."
);
}
}
export { export {
AnnotationLayerBuilder, AnnotationLayerBuilder,
DefaultAnnotationLayerFactory, DefaultAnnotationLayerFactory,

View File

@ -22,9 +22,6 @@
/** @typedef {import("./event_utils").EventBus} EventBus */ /** @typedef {import("./event_utils").EventBus} EventBus */
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */ /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
/** @typedef {import("./interfaces").IL10n} IL10n */ /** @typedef {import("./interfaces").IL10n} IL10n */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFAnnotationLayerFactory} IPDFAnnotationLayerFactory */
// eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */ /** @typedef {import("./interfaces").IPDFStructTreeLayerFactory} IPDFStructTreeLayerFactory */
@ -68,7 +65,6 @@ import {
VERTICAL_PADDING, VERTICAL_PADDING,
watchScroll, watchScroll,
} from "./ui_utils.js"; } from "./ui_utils.js";
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { NullL10n } from "./l10n_utils.js"; import { NullL10n } from "./l10n_utils.js";
import { PDFPageView } from "./pdf_page_view.js"; import { PDFPageView } from "./pdf_page_view.js";
import { PDFRenderingQueue } from "./pdf_rendering_queue.js"; import { PDFRenderingQueue } from "./pdf_rendering_queue.js";
@ -210,7 +206,6 @@ class PDFPageViewBuffer {
/** /**
* Simple viewer control to display PDF content/pages. * Simple viewer control to display PDF content/pages.
* *
* @implements {IPDFAnnotationLayerFactory}
* @implements {IPDFStructTreeLayerFactory} * @implements {IPDFStructTreeLayerFactory}
* @implements {IPDFTextLayerFactory} * @implements {IPDFTextLayerFactory}
* @implements {IPDFXfaLayerFactory} * @implements {IPDFXfaLayerFactory}
@ -562,6 +557,24 @@ class PDFViewer {
get annotationEditorUIManager() { get annotationEditorUIManager() {
return self.#annotationEditorUIManager; return self.#annotationEditorUIManager;
}, },
get annotationStorage() {
return self.pdfDocument?.annotationStorage;
},
get downloadManager() {
return self.downloadManager;
},
get enableScripting() {
return !!self._scriptingManager;
},
get fieldObjectsPromise() {
return self.pdfDocument?.getFieldObjects();
},
get hasJSActionsPromise() {
return self.pdfDocument?.hasJSActions();
},
get linkService() {
return self.linkService;
},
}; };
} }
@ -776,8 +789,6 @@ class PDFViewer {
textLayerFactory: textLayerFactory:
textLayerMode !== TextLayerMode.DISABLE ? this : null, textLayerMode !== TextLayerMode.DISABLE ? this : null,
textLayerMode, textLayerMode,
annotationLayerFactory:
annotationMode !== AnnotationMode.DISABLE ? this : null,
annotationMode, annotationMode,
xfaLayerFactory: this, xfaLayerFactory: this,
textHighlighterFactory: this, textHighlighterFactory: this,
@ -1696,59 +1707,6 @@ class PDFViewer {
}); });
} }
/**
* @typedef {Object} CreateAnnotationLayerBuilderParameters
* @property {HTMLDivElement} pageDiv
* @property {PDFPageProxy} pdfPage
* @property {AnnotationStorage} [annotationStorage] - Storage for annotation
* data in forms.
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} renderForms
* @property {IL10n} l10n
* @property {boolean} [enableScripting]
* @property {Promise<boolean>} [hasJSActionsPromise]
* @property {Promise<Object<string, Array<Object>> | null>}
* [fieldObjectsPromise]
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap] - Map some
* annotation ids with canvases used to render them.
* @property {TextAccessibilityManager} [accessibilityManager]
*/
/**
* @param {CreateAnnotationLayerBuilderParameters}
* @returns {AnnotationLayerBuilder}
*/
createAnnotationLayerBuilder({
pageDiv,
pdfPage,
annotationStorage = this.pdfDocument?.annotationStorage,
imageResourcesPath = "",
renderForms = true,
l10n = NullL10n,
enableScripting = this.enableScripting,
hasJSActionsPromise = this.pdfDocument?.hasJSActions(),
fieldObjectsPromise = this.pdfDocument?.getFieldObjects(),
annotationCanvasMap = null,
accessibilityManager = null,
}) {
return new AnnotationLayerBuilder({
pageDiv,
pdfPage,
annotationStorage,
imageResourcesPath,
renderForms,
linkService: this.linkService,
downloadManager: this.downloadManager,
l10n,
enableScripting,
hasJSActionsPromise,
fieldObjectsPromise,
annotationCanvasMap,
accessibilityManager,
});
}
/** /**
* @typedef {Object} CreateXfaLayerBuilderParameters * @typedef {Object} CreateXfaLayerBuilderParameters
* @property {HTMLDivElement} pageDiv * @property {HTMLDivElement} pageDiv