Skip to content

Commit 71376f0

Browse files
committed
[Editor] Remove the class fooEditing from the layer when destroying it
and simplify the way to handle the different types of editors.
1 parent 57d196e commit 71376f0

File tree

6 files changed

+47
-40
lines changed

6 files changed

+47
-40
lines changed

src/display/editor/annotation_editor_layer.js

+30-32
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class AnnotationEditorLayer {
7373

7474
static _initialized = false;
7575

76+
static #editorTypes = new Map(
77+
[FreeTextEditor, InkEditor, StampEditor].map(type => [
78+
type._editorType,
79+
type,
80+
])
81+
);
82+
7683
/**
7784
* @param {AnnotationEditorLayerOptions} options
7885
*/
@@ -85,7 +92,7 @@ class AnnotationEditorLayer {
8592
viewport,
8693
l10n,
8794
}) {
88-
const editorTypes = [FreeTextEditor, InkEditor, StampEditor];
95+
const editorTypes = [...AnnotationEditorLayer.#editorTypes.values()];
8996
if (!AnnotationEditorLayer._initialized) {
9097
AnnotationEditorLayer._initialized = true;
9198
for (const editorType of editorTypes) {
@@ -131,18 +138,13 @@ class AnnotationEditorLayer {
131138
}
132139

133140
if (mode !== AnnotationEditorType.NONE) {
134-
this.div.classList.toggle(
135-
"freeTextEditing",
136-
mode === AnnotationEditorType.FREETEXT
137-
);
138-
this.div.classList.toggle(
139-
"inkEditing",
140-
mode === AnnotationEditorType.INK
141-
);
142-
this.div.classList.toggle(
143-
"stampEditing",
144-
mode === AnnotationEditorType.STAMP
145-
);
141+
const { classList } = this.div;
142+
for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
143+
classList.toggle(
144+
`${editorType._type}Editing`,
145+
mode === editorType._editorType
146+
);
147+
}
146148
this.div.hidden = false;
147149
}
148150
}
@@ -262,6 +264,11 @@ class AnnotationEditorLayer {
262264
if (this.isEmpty) {
263265
this.div.hidden = true;
264266
}
267+
const { classList } = this.div;
268+
for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
269+
classList.remove(`${editorType._type}Editing`);
270+
}
271+
265272
this.#isDisabling = false;
266273
}
267274

@@ -458,15 +465,10 @@ class AnnotationEditorLayer {
458465
* @returns {AnnotationEditor}
459466
*/
460467
#createNewEditor(params) {
461-
switch (this.#uiManager.getMode()) {
462-
case AnnotationEditorType.FREETEXT:
463-
return new FreeTextEditor(params);
464-
case AnnotationEditorType.INK:
465-
return new InkEditor(params);
466-
case AnnotationEditorType.STAMP:
467-
return new StampEditor(params);
468-
}
469-
return null;
468+
const editorType = AnnotationEditorLayer.#editorTypes.get(
469+
this.#uiManager.getMode()
470+
);
471+
return editorType ? new editorType.prototype.constructor(params) : null;
470472
}
471473

472474
/**
@@ -497,18 +499,14 @@ class AnnotationEditorLayer {
497499
/**
498500
* Create a new editor
499501
* @param {Object} data
500-
* @returns {AnnotationEditor}
502+
* @returns {AnnotationEditor | null}
501503
*/
502504
deserialize(data) {
503-
switch (data.annotationType ?? data.annotationEditorType) {
504-
case AnnotationEditorType.FREETEXT:
505-
return FreeTextEditor.deserialize(data, this, this.#uiManager);
506-
case AnnotationEditorType.INK:
507-
return InkEditor.deserialize(data, this, this.#uiManager);
508-
case AnnotationEditorType.STAMP:
509-
return StampEditor.deserialize(data, this, this.#uiManager);
510-
}
511-
return null;
505+
return (
506+
AnnotationEditorLayer.#editorTypes
507+
.get(data.annotationType ?? data.annotationEditorType)
508+
?.deserialize(data, this, this.#uiManager) || null
509+
);
512510
}
513511

514512
/**

src/display/editor/editor.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ class AnnotationEditor {
941941

942942
/**
943943
* Render this editor in a div.
944-
* @returns {HTMLDivElement}
944+
* @returns {HTMLDivElement | null}
945945
*/
946946
render() {
947947
this.div = document.createElement("div");
@@ -1192,8 +1192,9 @@ class AnnotationEditor {
11921192
* new annotation to add to the pdf document.
11931193
*
11941194
* To implement in subclasses.
1195-
* @param {boolean} isForCopying
1196-
* @param {Object} [context]
1195+
* @param {boolean} [isForCopying]
1196+
* @param {Object | null} [context]
1197+
* @returns {Object | null}
11971198
*/
11981199
serialize(isForCopying = false, context = null) {
11991200
unreachable("An editor must be serializable");
@@ -1206,7 +1207,7 @@ class AnnotationEditor {
12061207
* @param {Object} data
12071208
* @param {AnnotationEditorLayer} parent
12081209
* @param {AnnotationEditorUIManager} uiManager
1209-
* @returns {AnnotationEditor}
1210+
* @returns {AnnotationEditor | null}
12101211
*/
12111212
static deserialize(data, parent, uiManager) {
12121213
const editor = new this.prototype.constructor({
@@ -1327,6 +1328,7 @@ class AnnotationEditor {
13271328

13281329
/**
13291330
* Get the div which really contains the displayed content.
1331+
* @returns {HTMLDivElement | undefined}
13301332
*/
13311333
get contentDiv() {
13321334
return this.div;

src/display/editor/freetext.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class FreeTextEditor extends AnnotationEditor {
132132

133133
static _type = "freetext";
134134

135+
static _editorType = AnnotationEditorType.FREETEXT;
136+
135137
constructor(params) {
136138
super({ ...params, name: "freeTextEditor" });
137139
this.#color =
@@ -335,7 +337,7 @@ class FreeTextEditor extends AnnotationEditor {
335337

336338
// In case the blur callback hasn't been called.
337339
this.isEditing = false;
338-
this.parent.div.classList.add("freeTextEditing");
340+
this.parent.div.classList.add("freetextEditing");
339341
}
340342

341343
/** @inheritdoc */
@@ -374,7 +376,7 @@ class FreeTextEditor extends AnnotationEditor {
374376
this.isEditing = false;
375377
if (this.parent) {
376378
this.parent.setEditingState(true);
377-
this.parent.div.classList.add("freeTextEditing");
379+
this.parent.div.classList.add("freetextEditing");
378380
}
379381
super.remove();
380382
}
@@ -508,7 +510,7 @@ class FreeTextEditor extends AnnotationEditor {
508510
}
509511

510512
editorDivInput(event) {
511-
this.parent.div.classList.toggle("freeTextEditing", this.isEmpty());
513+
this.parent.div.classList.toggle("freetextEditing", this.isEmpty());
512514
}
513515

514516
/** @inheritdoc */
@@ -649,6 +651,7 @@ class FreeTextEditor extends AnnotationEditor {
649651
}
650652
}
651653

654+
/** @inheritdoc */
652655
get contentDiv() {
653656
return this.editorDiv;
654657
}

src/display/editor/ink.js

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class InkEditor extends AnnotationEditor {
6363

6464
static _type = "ink";
6565

66+
static _editorType = AnnotationEditorType.INK;
67+
6668
constructor(params) {
6769
super({ ...params, name: "inkEditor" });
6870
this.color = params.color || null;

src/display/editor/stamp.js

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class StampEditor extends AnnotationEditor {
4444

4545
static _type = "stamp";
4646

47+
static _editorType = AnnotationEditorType.STAMP;
48+
4749
constructor(params) {
4850
super({ ...params, name: "stampEditor" });
4951
this.#bitmapUrl = params.bitmapUrl;

web/annotation_editor_layer_builder.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
height: 100%;
122122
}
123123

124-
.annotationEditorLayer.freeTextEditing {
124+
.annotationEditorLayer.freetextEditing {
125125
cursor: var(--editorFreeText-editing-cursor);
126126
}
127127

0 commit comments

Comments
 (0)