From 4a4c6b98515ed233b3f078422ad805beee1cd760 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 29 Jun 2022 11:13:03 +0200 Subject: [PATCH] [editor] Introduce a proper `annotationEditorMode` option/preference (PR 15075 follow-up) This replaces the boolean `annotationEditorEnabled` option/preference with a "proper" `annotationEditorMode` one. This way it's not only possible for the user to control if Editing is enabled/disabled, but also which *specific* Editing-mode should become enabled upon PDF document load. Given that Editing is not enabled/released yet, I cannot imagine that changing the name and type of the option/preference should be an issue. --- extensions/chromium/preferences_schema.json | 6 ++-- src/shared/util.js | 1 + web/app.js | 11 +++--- web/app_options.js | 7 ++-- web/base_viewer.js | 37 ++++++++++++--------- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/extensions/chromium/preferences_schema.json b/extensions/chromium/preferences_schema.json index 371da256f..4b8d68623 100644 --- a/extensions/chromium/preferences_schema.json +++ b/extensions/chromium/preferences_schema.json @@ -160,9 +160,9 @@ ], "default": 2 }, - "annotationEditorEnabled": { - "type": "boolean", - "default": false + "annotationEditorMode": { + "type": "integer", + "default": -1 }, "enablePermissions": { "type": "boolean", diff --git a/src/shared/util.js b/src/shared/util.js index d82053890..2586bdc45 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -55,6 +55,7 @@ const AnnotationMode = { const AnnotationEditorPrefix = "pdfjs_internal_editor_"; const AnnotationEditorType = { + DISABLE: -1, NONE: 0, FREETEXT: 3, INK: 15, diff --git a/web/app.js b/web/app.js index cb5b4f7d9..5482ed7d6 100644 --- a/web/app.js +++ b/web/app.js @@ -34,9 +34,8 @@ import { SpreadMode, TextLayerMode, } from "./ui_utils.js"; -import { AppOptions, OptionKind } from "./app_options.js"; -import { AutomationEventBus, EventBus } from "./event_utils.js"; import { + AnnotationEditorType, build, createPromiseCapability, getDocument, @@ -54,6 +53,8 @@ import { UNSUPPORTED_FEATURES, version, } from "pdfjs-lib"; +import { AppOptions, OptionKind } from "./app_options.js"; +import { AutomationEventBus, EventBus } from "./event_utils.js"; import { CursorTool, PDFCursorTools } from "./pdf_cursor_tools.js"; import { LinkTarget, PDFLinkService } from "./pdf_link_service.js"; import { AnnotationEditorParams } from "./annotation_editor_params.js"; @@ -510,7 +511,7 @@ const PDFViewerApplication = { const container = appConfig.mainContainer, viewer = appConfig.viewerContainer; - const annotationEditorEnabled = AppOptions.get("annotationEditorEnabled"); + const annotationEditorMode = AppOptions.get("annotationEditorMode"); const pageColors = { background: AppOptions.get("pageColorsBackground"), foreground: AppOptions.get("pageColorsForeground"), @@ -534,7 +535,7 @@ const PDFViewerApplication = { l10n: this.l10n, textLayerMode: AppOptions.get("textLayerMode"), annotationMode: AppOptions.get("annotationMode"), - annotationEditorEnabled, + annotationEditorMode, imageResourcesPath: AppOptions.get("imageResourcesPath"), enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"), useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"), @@ -570,7 +571,7 @@ const PDFViewerApplication = { this.findBar = new PDFFindBar(appConfig.findBar, eventBus, this.l10n); } - if (annotationEditorEnabled) { + if (annotationEditorMode !== AnnotationEditorType.DISABLE) { this.annotationEditorParams = new AnnotationEditorParams( appConfig.annotationEditorParams, eventBus diff --git a/web/app_options.js b/web/app_options.js index 220975859..3178b0434 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -59,11 +59,12 @@ const OptionKind = { * primitive types and cannot rely on any imported types. */ const defaultOptions = { - annotationEditorEnabled: { + annotationEditorMode: { /** @type {boolean} */ value: - typeof PDFJSDev === "undefined" || - PDFJSDev.test("!PRODUCTION || TESTING"), + typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING") + ? 0 + : -1, kind: OptionKind.VIEWER + OptionKind.PREFERENCE, }, annotationMode: { diff --git a/web/base_viewer.js b/web/base_viewer.js index d49db8093..1e5128ddb 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -86,7 +86,10 @@ const PagesCountLimit = { }; function isValidAnnotationEditorMode(mode) { - return Object.values(AnnotationEditorType).includes(mode); + return ( + Object.values(AnnotationEditorType).includes(mode) && + mode !== AnnotationEditorType.DISABLE + ); } /** @@ -113,8 +116,9 @@ function isValidAnnotationEditorMode(mode) { * being rendered. The constants from {@link AnnotationMode} should be used; * see also {@link RenderParameters} and {@link GetOperatorListParameters}. * The default value is `AnnotationMode.ENABLE_FORMS`. - * @property {boolean} [annotationEditorEnabled] - Enables the creation and - * editing of new Annotations. + * @property {boolean} [annotationEditorMode] - Enables the creation and editing + * of new Annotations. The constants from {@link AnnotationEditorType} should + * be used. The default value is `AnnotationEditorType.DISABLE`. * @property {string} [imageResourcesPath] - Path for image resources, mainly * mainly for annotation icons. Include trailing slash. * @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of @@ -213,7 +217,7 @@ class PDFPageViewBuffer { class BaseViewer { #buffer = null; - #annotationEditorMode = AnnotationEditorType.NONE; + #annotationEditorMode = AnnotationEditorType.DISABLE; #annotationEditorUIManager = null; @@ -273,9 +277,8 @@ class BaseViewer { this.textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE; this.#annotationMode = options.annotationMode ?? AnnotationMode.ENABLE_FORMS; - this.#annotationEditorMode = options.annotationEditorEnabled - ? AnnotationEditorType.NONE - : null; + this.#annotationEditorMode = + options.annotationEditorMode ?? AnnotationEditorType.DISABLE; this.imageResourcesPath = options.imageResourcesPath || ""; this.enablePrintAutoRotate = options.enablePrintAutoRotate || false; this.renderer = options.renderer || RendererType.CANVAS; @@ -560,7 +563,7 @@ class BaseViewer { } if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) { - params.annotationEditorMode = null; + params.annotationEditorMode = AnnotationEditorType.DISABLE; } if ( @@ -710,19 +713,26 @@ class BaseViewer { const { annotationEditorMode, annotationMode, textLayerMode } = this.#initializePermissions(permissions); - if (annotationEditorMode !== null) { + if (annotationEditorMode !== AnnotationEditorType.DISABLE) { + const mode = annotationEditorMode; + if (isPureXfa) { console.warn("Warning: XFA-editing is not implemented."); - } else { + } else if (isValidAnnotationEditorMode(mode)) { // Ensure that the Editor buttons, in the toolbar, are updated. this.eventBus.dispatch("annotationeditormodechanged", { source: this, - mode: annotationEditorMode, + mode, }); this.#annotationEditorUIManager = new AnnotationEditorUIManager( this.eventBus ); + if (mode !== AnnotationEditorType.NONE) { + this.#annotationEditorUIManager.updateMode(mode); + } + } else { + console.error(`Invalid AnnotationEditor mode: ${mode}`); } } @@ -885,9 +895,6 @@ class BaseViewer { } _resetView() { - if (this.#annotationEditorMode !== null) { - this.#annotationEditorMode = AnnotationEditorType.NONE; - } this.#annotationEditorUIManager = null; this._pages = []; this._currentPageNumber = 1; @@ -2142,7 +2149,7 @@ class BaseViewer { } /** - * @type {number | null} + * @type {number} */ get annotationEditorMode() { return this.#annotationEditorMode;