From dcf9ff2182becaecf59274d59b9b56fa6ce9aee8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 7 Dec 2022 16:12:48 +0100 Subject: [PATCH] Handle possibly undefined parameters *once* per `AnnotationLayer.render` invocation There's no reason to repeat this for every single annotation. Also, adds a couple of missing JSDoc-parameters. --- src/display/annotation_layer.js | 122 +++++++++++++++++--------------- web/annotation_layer_builder.js | 2 +- 2 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 9b6b7b4ed..f8b3fd55b 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -2562,13 +2562,16 @@ class FileAttachmentAnnotationElement extends AnnotationElement { * @property {PDFPageProxy} page * @property {IPDFLinkService} linkService * @property {IDownloadManager} downloadManager + * @property {AnnotationStorage} [annotationStorage] * @property {string} [imageResourcesPath] - Path for image resources, mainly * for annotation icons. Include trailing slash. * @property {boolean} renderForms * @property {boolean} [enableScripting] - Enable embedded script execution. * @property {boolean} [hasJSActions] - Some fields have JS actions. * The default value is `false`. + * @property {Object> | null} [fieldObjects] * @property {Map} [annotationCanvasMap] + * @property {TextAccessibilityManager} [accessibilityManager] */ class AnnotationLayer { @@ -2588,14 +2591,28 @@ class AnnotationLayer { /** * Render a new annotation layer with all annotation elements. * - * @public - * @param {AnnotationLayerParameters} parameters + * @param {AnnotationLayerParameters} params * @memberof AnnotationLayer */ - static render(parameters) { - const { annotations, div, viewport, accessibilityManager } = parameters; + static render(params) { + const { annotations, div, viewport, accessibilityManager } = params; setLayerDimensions(div, viewport); + const elementParams = { + data: null, + layer: div, + page: params.page, + viewport, + linkService: params.linkService, + downloadManager: params.downloadManager, + imageResourcesPath: params.imageResourcesPath || "", + renderForms: params.renderForms !== false, + svgFactory: new DOMSVGFactory(), + annotationStorage: params.annotationStorage || new AnnotationStorage(), + enableScripting: params.enableScripting === true, + hasJSActions: params.hasJSActions, + fieldObjects: params.fieldObjects, + }; let zIndex = 0; for (const data of annotations) { @@ -2605,73 +2622,60 @@ class AnnotationLayer { continue; // Ignore empty annotations. } } - const element = AnnotationElementFactory.create({ - data, - layer: div, - page: parameters.page, - viewport, - linkService: parameters.linkService, - downloadManager: parameters.downloadManager, - imageResourcesPath: parameters.imageResourcesPath || "", - renderForms: parameters.renderForms !== false, - svgFactory: new DOMSVGFactory(), - annotationStorage: - parameters.annotationStorage || new AnnotationStorage(), - enableScripting: parameters.enableScripting, - hasJSActions: parameters.hasJSActions, - fieldObjects: parameters.fieldObjects, - }); - if (element.isRenderable) { - const rendered = element.render(); - if (data.hidden) { - rendered.style.visibility = "hidden"; - } - if (Array.isArray(rendered)) { - for (const renderedElement of rendered) { - renderedElement.style.zIndex = zIndex++; - AnnotationLayer.#appendElement( - renderedElement, - data.id, - div, - accessibilityManager - ); - } - } else { - // The accessibility manager will move the annotation in the DOM in - // order to match the visual ordering. - // But if an annotation is above an other one, then we must draw it - // after the other one whatever the order is in the DOM, hence the - // use of the z-index. - rendered.style.zIndex = zIndex++; + elementParams.data = data; + const element = AnnotationElementFactory.create(elementParams); - if (element instanceof PopupAnnotationElement) { - // Popup annotation elements should not be on top of other - // annotation elements to prevent interfering with mouse events. - div.prepend(rendered); - } else { - AnnotationLayer.#appendElement( - rendered, - data.id, - div, - accessibilityManager - ); - } + if (!element.isRenderable) { + continue; + } + const rendered = element.render(); + if (data.hidden) { + rendered.style.visibility = "hidden"; + } + if (Array.isArray(rendered)) { + for (const renderedElement of rendered) { + renderedElement.style.zIndex = zIndex++; + AnnotationLayer.#appendElement( + renderedElement, + data.id, + div, + accessibilityManager + ); + } + } else { + // The accessibility manager will move the annotation in the DOM in + // order to match the visual ordering. + // But if an annotation is above an other one, then we must draw it + // after the other one whatever the order is in the DOM, hence the + // use of the z-index. + rendered.style.zIndex = zIndex++; + + if (element instanceof PopupAnnotationElement) { + // Popup annotation elements should not be on top of other + // annotation elements to prevent interfering with mouse events. + div.prepend(rendered); + } else { + AnnotationLayer.#appendElement( + rendered, + data.id, + div, + accessibilityManager + ); } } } - this.#setAnnotationCanvasMap(div, parameters.annotationCanvasMap); + this.#setAnnotationCanvasMap(div, params.annotationCanvasMap); } /** * Update the annotation elements on existing annotation layer. * - * @public - * @param {AnnotationLayerParameters} parameters + * @param {AnnotationLayerParameters} params * @memberof AnnotationLayer */ - static update(parameters) { - const { annotationCanvasMap, div, viewport } = parameters; + static update(params) { + const { annotationCanvasMap, div, viewport } = params; setLayerDimensions(div, { rotation: viewport.rotation }); this.#setAnnotationCanvasMap(div, annotationCanvasMap); diff --git a/web/annotation_layer_builder.js b/web/annotation_layer_builder.js index 5306ee47f..9d7e1b10d 100644 --- a/web/annotation_layer_builder.js +++ b/web/annotation_layer_builder.js @@ -42,7 +42,7 @@ import { PresentationModeState } from "./ui_utils.js"; * @property {Promise> | null>} * [fieldObjectsPromise] * @property {Map} [annotationCanvasMap] - * @property {TextAccessibilityManager} accessibilityManager + * @property {TextAccessibilityManager} [accessibilityManager] */ class AnnotationLayerBuilder {