[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.
This commit is contained in:
Jonas Jenwald 2022-06-29 11:13:03 +02:00
parent f1d4015508
commit 4a4c6b9851
5 changed files with 36 additions and 26 deletions

View File

@ -160,9 +160,9 @@
], ],
"default": 2 "default": 2
}, },
"annotationEditorEnabled": { "annotationEditorMode": {
"type": "boolean", "type": "integer",
"default": false "default": -1
}, },
"enablePermissions": { "enablePermissions": {
"type": "boolean", "type": "boolean",

View File

@ -55,6 +55,7 @@ const AnnotationMode = {
const AnnotationEditorPrefix = "pdfjs_internal_editor_"; const AnnotationEditorPrefix = "pdfjs_internal_editor_";
const AnnotationEditorType = { const AnnotationEditorType = {
DISABLE: -1,
NONE: 0, NONE: 0,
FREETEXT: 3, FREETEXT: 3,
INK: 15, INK: 15,

View File

@ -34,9 +34,8 @@ import {
SpreadMode, SpreadMode,
TextLayerMode, TextLayerMode,
} from "./ui_utils.js"; } from "./ui_utils.js";
import { AppOptions, OptionKind } from "./app_options.js";
import { AutomationEventBus, EventBus } from "./event_utils.js";
import { import {
AnnotationEditorType,
build, build,
createPromiseCapability, createPromiseCapability,
getDocument, getDocument,
@ -54,6 +53,8 @@ import {
UNSUPPORTED_FEATURES, UNSUPPORTED_FEATURES,
version, version,
} from "pdfjs-lib"; } 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 { CursorTool, PDFCursorTools } from "./pdf_cursor_tools.js";
import { LinkTarget, PDFLinkService } from "./pdf_link_service.js"; import { LinkTarget, PDFLinkService } from "./pdf_link_service.js";
import { AnnotationEditorParams } from "./annotation_editor_params.js"; import { AnnotationEditorParams } from "./annotation_editor_params.js";
@ -510,7 +511,7 @@ const PDFViewerApplication = {
const container = appConfig.mainContainer, const container = appConfig.mainContainer,
viewer = appConfig.viewerContainer; viewer = appConfig.viewerContainer;
const annotationEditorEnabled = AppOptions.get("annotationEditorEnabled"); const annotationEditorMode = AppOptions.get("annotationEditorMode");
const pageColors = { const pageColors = {
background: AppOptions.get("pageColorsBackground"), background: AppOptions.get("pageColorsBackground"),
foreground: AppOptions.get("pageColorsForeground"), foreground: AppOptions.get("pageColorsForeground"),
@ -534,7 +535,7 @@ const PDFViewerApplication = {
l10n: this.l10n, l10n: this.l10n,
textLayerMode: AppOptions.get("textLayerMode"), textLayerMode: AppOptions.get("textLayerMode"),
annotationMode: AppOptions.get("annotationMode"), annotationMode: AppOptions.get("annotationMode"),
annotationEditorEnabled, annotationEditorMode,
imageResourcesPath: AppOptions.get("imageResourcesPath"), imageResourcesPath: AppOptions.get("imageResourcesPath"),
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"), enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"), useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
@ -570,7 +571,7 @@ const PDFViewerApplication = {
this.findBar = new PDFFindBar(appConfig.findBar, eventBus, this.l10n); this.findBar = new PDFFindBar(appConfig.findBar, eventBus, this.l10n);
} }
if (annotationEditorEnabled) { if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
this.annotationEditorParams = new AnnotationEditorParams( this.annotationEditorParams = new AnnotationEditorParams(
appConfig.annotationEditorParams, appConfig.annotationEditorParams,
eventBus eventBus

View File

@ -59,11 +59,12 @@ const OptionKind = {
* primitive types and cannot rely on any imported types. * primitive types and cannot rely on any imported types.
*/ */
const defaultOptions = { const defaultOptions = {
annotationEditorEnabled: { annotationEditorMode: {
/** @type {boolean} */ /** @type {boolean} */
value: value:
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING")
PDFJSDev.test("!PRODUCTION || TESTING"), ? 0
: -1,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE, kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
}, },
annotationMode: { annotationMode: {

View File

@ -86,7 +86,10 @@ const PagesCountLimit = {
}; };
function isValidAnnotationEditorMode(mode) { 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; * 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 {boolean} [annotationEditorEnabled] - Enables the creation and * @property {boolean} [annotationEditorMode] - Enables the creation and editing
* editing of new Annotations. * 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 * @property {string} [imageResourcesPath] - Path for image resources, mainly
* mainly for annotation icons. Include trailing slash. * mainly for annotation icons. Include trailing slash.
* @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of * @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of
@ -213,7 +217,7 @@ class PDFPageViewBuffer {
class BaseViewer { class BaseViewer {
#buffer = null; #buffer = null;
#annotationEditorMode = AnnotationEditorType.NONE; #annotationEditorMode = AnnotationEditorType.DISABLE;
#annotationEditorUIManager = null; #annotationEditorUIManager = null;
@ -273,9 +277,8 @@ class BaseViewer {
this.textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE; this.textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE;
this.#annotationMode = this.#annotationMode =
options.annotationMode ?? AnnotationMode.ENABLE_FORMS; options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
this.#annotationEditorMode = options.annotationEditorEnabled this.#annotationEditorMode =
? AnnotationEditorType.NONE options.annotationEditorMode ?? AnnotationEditorType.DISABLE;
: null;
this.imageResourcesPath = options.imageResourcesPath || ""; this.imageResourcesPath = options.imageResourcesPath || "";
this.enablePrintAutoRotate = options.enablePrintAutoRotate || false; this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
this.renderer = options.renderer || RendererType.CANVAS; this.renderer = options.renderer || RendererType.CANVAS;
@ -560,7 +563,7 @@ class BaseViewer {
} }
if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) { if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) {
params.annotationEditorMode = null; params.annotationEditorMode = AnnotationEditorType.DISABLE;
} }
if ( if (
@ -710,19 +713,26 @@ class BaseViewer {
const { annotationEditorMode, annotationMode, textLayerMode } = const { annotationEditorMode, annotationMode, textLayerMode } =
this.#initializePermissions(permissions); this.#initializePermissions(permissions);
if (annotationEditorMode !== null) { if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
const mode = annotationEditorMode;
if (isPureXfa) { if (isPureXfa) {
console.warn("Warning: XFA-editing is not implemented."); console.warn("Warning: XFA-editing is not implemented.");
} else { } else if (isValidAnnotationEditorMode(mode)) {
// Ensure that the Editor buttons, in the toolbar, are updated. // Ensure that the Editor buttons, in the toolbar, are updated.
this.eventBus.dispatch("annotationeditormodechanged", { this.eventBus.dispatch("annotationeditormodechanged", {
source: this, source: this,
mode: annotationEditorMode, mode,
}); });
this.#annotationEditorUIManager = new AnnotationEditorUIManager( this.#annotationEditorUIManager = new AnnotationEditorUIManager(
this.eventBus this.eventBus
); );
if (mode !== AnnotationEditorType.NONE) {
this.#annotationEditorUIManager.updateMode(mode);
}
} else {
console.error(`Invalid AnnotationEditor mode: ${mode}`);
} }
} }
@ -885,9 +895,6 @@ class BaseViewer {
} }
_resetView() { _resetView() {
if (this.#annotationEditorMode !== null) {
this.#annotationEditorMode = AnnotationEditorType.NONE;
}
this.#annotationEditorUIManager = null; this.#annotationEditorUIManager = null;
this._pages = []; this._pages = [];
this._currentPageNumber = 1; this._currentPageNumber = 1;
@ -2142,7 +2149,7 @@ class BaseViewer {
} }
/** /**
* @type {number | null} * @type {number}
*/ */
get annotationEditorMode() { get annotationEditorMode() {
return this.#annotationEditorMode; return this.#annotationEditorMode;