Skip to content

[editor] Introduce a proper annotationEditorMode option/preference (PR 15075 follow-up) #15113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@
],
"default": 2
},
"annotationEditorEnabled": {
"type": "boolean",
"default": false
"annotationEditorMode": {
"type": "integer",
"default": -1
},
"enablePermissions": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion l10n/en-US/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ editor_free_text_label=FreeText Annotation
editor_ink.title=Add Ink Annotation
editor_ink_label=Ink Annotation

freetext_default_content=Enter some text…
freetext_default_content=Enter text…

# Editor Parameters
editor_free_text_font_color=Font Color
Expand Down
1 change: 1 addition & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const AnnotationMode = {
const AnnotationEditorPrefix = "pdfjs_internal_editor_";

const AnnotationEditorType = {
DISABLE: -1,
NONE: 0,
FREETEXT: 3,
INK: 15,
Expand Down
11 changes: 6 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
37 changes: 22 additions & 15 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ const PagesCountLimit = {
};

function isValidAnnotationEditorMode(mode) {
return Object.values(AnnotationEditorType).includes(mode);
return (
Object.values(AnnotationEditorType).includes(mode) &&
mode !== AnnotationEditorType.DISABLE
);
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -213,7 +217,7 @@ class PDFPageViewBuffer {
class BaseViewer {
#buffer = null;

#annotationEditorMode = AnnotationEditorType.NONE;
#annotationEditorMode = AnnotationEditorType.DISABLE;

#annotationEditorUIManager = null;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -560,7 +563,7 @@ class BaseViewer {
}

if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) {
params.annotationEditorMode = null;
params.annotationEditorMode = AnnotationEditorType.DISABLE;
}

if (
Expand Down Expand Up @@ -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}`);
}
}

Expand Down Expand Up @@ -885,9 +895,6 @@ class BaseViewer {
}

_resetView() {
if (this.#annotationEditorMode !== null) {
this.#annotationEditorMode = AnnotationEditorType.NONE;
}
this.#annotationEditorUIManager = null;
this._pages = [];
this._currentPageNumber = 1;
Expand Down Expand Up @@ -2142,7 +2149,7 @@ class BaseViewer {
}

/**
* @type {number | null}
* @type {number}
*/
get annotationEditorMode() {
return this.#annotationEditorMode;
Expand Down
2 changes: 1 addition & 1 deletion web/l10n_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const DEFAULT_L10N_STRINGS = {
web_fonts_disabled:
"Web fonts are disabled: unable to use embedded PDF fonts.",

freetext_default_content: "Enter some text…",
freetext_default_content: "Enter text…",
};

function getL10nFallback(key, args) {
Expand Down
8 changes: 4 additions & 4 deletions web/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@
<div class="editorParamsToolbarContainer">
<div class="editorParamsSetter">
<label for="editorFreeTextColor" class="editorParamsLabel" data-l10n-id="editor_free_text_font_color">Font Color</label>
<input type="color" id="editorFreeTextColor" name="fontColor" class="editorParamsColor" tabindex="100">
<input type="color" id="editorFreeTextColor" class="editorParamsColor" tabindex="100">
</div>
<div class="editorParamsSetter">
<label for="editorFreeTextFontSize" class="editorParamsLabel" data-l10n-id="editor_free_text_font_size">Font Size</label>
<input type="range" id="editorFreeTextFontSize" class="editorParamsSlider" name="fontSize" value="10" min="5" max="100" step="1" tabindex="101">
<input type="range" id="editorFreeTextFontSize" class="editorParamsSlider" value="10" min="5" max="100" step="1" tabindex="101">
</div>
</div>
</div>
Expand All @@ -164,11 +164,11 @@
<div class="editorParamsToolbarContainer">
<div class="editorParamsSetter">
<label for="editorInkColor" class="editorParamsLabel" data-l10n-id="editor_ink_line_color">Line Color</label>
<input type="color" id="editorInkColor" name="inkColor" class="editorParamsColor" tabindex="102">
<input type="color" id="editorInkColor" class="editorParamsColor" tabindex="102">
</div>
<div class="editorParamsSetter">
<label for="editorInkThickness" class="editorParamsLabel" data-l10n-id="editor_ink_line_thickness">Line Thickness</label>
<input type="range" id="editorInkThickness" class="editorParamsSlider" name="lineThickness" value="1" min="1" max="20" step="1" tabindex="103">
<input type="range" id="editorInkThickness" class="editorParamsSlider" value="1" min="1" max="20" step="1" tabindex="103">
</div>
</div>
</div>
Expand Down