From 6dc39cb873160a3bfffece6e98e5cc3decb9dd8b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 17 Dec 2020 12:55:58 +0100 Subject: [PATCH] Tweak the new `mouseState` parameter, and its usage, in the viewer components and the `AnnotationLayer` - Actually remove the `isDown` property when destroying the scripting-instance. - Mark all `mouseState` usage as "private" in the various classes. - Ensure that the `AnnotationLayer` actually treats the parameter as properly *optional*, the same way that the viewer components do. - For now remove the `mouseState` parameter from the `PDFPageView` class, and keep it only on the `BaseViewer`, since it's questionable if all of the scripting-functionality will work all that well without e.g. a full `BaseViewer`. - Append the `mouseState` to the JSDoc for the `AnnotationElement` class, and just move its definition into the base-`AnnotationElement` class. --- src/display/annotation_layer.js | 7 ++++--- web/annotation_layer_builder.js | 1 + web/app.js | 1 + web/base_viewer.js | 8 ++++---- web/pdf_page_view.js | 4 +--- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 8a6f90072..80ad7554d 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -47,6 +47,7 @@ import { ColorConverters } from "../shared/scripting_utils.js"; * @property {Object} svgFactory * @property {boolean} [enableScripting] * @property {boolean} [hasJSActions] + * @property {Object} [mouseState] */ class AnnotationElementFactory { @@ -155,6 +156,7 @@ class AnnotationElement { this.annotationStorage = parameters.annotationStorage; this.enableScripting = parameters.enableScripting; this.hasJSActions = parameters.hasJSActions; + this._mouseState = parameters.mouseState; if (isRenderable) { this.container = this._createContainer(ignoreBorder); @@ -590,7 +592,6 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement { parameters.renderInteractiveForms || (!parameters.data.hasAppearance && !!parameters.data.fieldValue); super(parameters, { isRenderable }); - this.mouseState = parameters.mouseState; } render() { @@ -734,7 +735,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement { const _blurListener = blurListener; blurListener = null; element.addEventListener("blur", event => { - if (this.mouseState.isDown) { + if (this._mouseState.isDown) { // Focus out using the mouse: data are committed elementData.userValue = event.target.value; window.dispatchEvent( @@ -1951,7 +1952,7 @@ class AnnotationLayer { parameters.annotationStorage || new AnnotationStorage(), enableScripting: parameters.enableScripting, hasJSActions: parameters.hasJSActions, - mouseState: parameters.mouseState, + mouseState: parameters.mouseState || { isDown: false }, }); if (element.isRenderable) { const rendered = element.render(); diff --git a/web/annotation_layer_builder.js b/web/annotation_layer_builder.js index e81b7a554..761ad5086 100644 --- a/web/annotation_layer_builder.js +++ b/web/annotation_layer_builder.js @@ -30,6 +30,7 @@ import { SimpleLinkService } from "./pdf_link_service.js"; * @property {IL10n} l10n - Localization service. * @property {boolean} [enableScripting] * @property {Promise} [hasJSActionsPromise] + * @property {Object} [mouseState] */ class AnnotationLayerBuilder { diff --git a/web/app.js b/web/app.js index df37f0c66..0b8de60c1 100644 --- a/web/app.js +++ b/web/app.js @@ -794,6 +794,7 @@ const PDFViewerApplication = { } events.clear(); + delete this._mouseState.isDown; this._scriptingInstance = null; }, diff --git a/web/base_viewer.js b/web/base_viewer.js index df50b3761..0103797ba 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -79,7 +79,8 @@ const DEFAULT_CACHE_SIZE = 10; * @property {IL10n} l10n - Localization service. * @property {boolean} [enableScripting] - Enable embedded script execution. * The default value is `false`. - * @property {Object} [mouseState] - The mouse button state. + * @property {Object} [mouseState] - The mouse button state. The default value + * is `null`. */ function PDFPageViewBuffer(size) { @@ -195,7 +196,7 @@ class BaseViewer { this.maxCanvasPixels = options.maxCanvasPixels; this.l10n = options.l10n || NullL10n; this.enableScripting = options.enableScripting || false; - this.mouseState = options.mouseState || null; + this._mouseState = options.mouseState || null; this.defaultRenderingQueue = !options.renderingQueue; if (this.defaultRenderingQueue) { @@ -540,7 +541,6 @@ class BaseViewer { maxCanvasPixels: this.maxCanvasPixels, l10n: this.l10n, enableScripting: this.enableScripting, - mouseState: this.mouseState, }); this._pages.push(pageView); } @@ -1301,7 +1301,7 @@ class BaseViewer { enableScripting, hasJSActionsPromise: hasJSActionsPromise || this.pdfDocument?.hasJSActions(), - mouseState, + mouseState: mouseState || this._mouseState, }); } diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 0aa427178..da8d86a6d 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -63,7 +63,6 @@ import { viewerCompatibilityParams } from "./viewer_compatibility.js"; * @property {IL10n} l10n - Localization service. * @property {boolean} [enableScripting] - Enable embedded script execution. * The default value is `false`. - * @property {Object} [mouseState] - The mouse button state. */ const MAX_CANVAS_PIXELS = viewerCompatibilityParams.maxCanvasPixels || 16777216; @@ -110,7 +109,6 @@ class PDFPageView { this.enableWebGL = options.enableWebGL || false; this.l10n = options.l10n || NullL10n; this.enableScripting = options.enableScripting || false; - this.mouseState = options.mouseState || null; this.paintTask = null; this.paintedViewportMap = new WeakMap(); @@ -554,7 +552,7 @@ class PDFPageView { this.l10n, this.enableScripting, /* hasJSActionsPromise = */ null, - this.mouseState + /* mouseState = */ null ); } this._renderAnnotationLayer();