From e819834505a8eb85367b33ee0587046b32778ebe Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 29 Jul 2022 18:00:52 +0200 Subject: [PATCH] [Editor] Add an editor in the annotation storage only when it's non-empty (#15241) --- src/display/annotation_storage.js | 9 +++++++++ src/display/editor/annotation_editor_layer.js | 14 ++++++++++++-- src/display/editor/editor.js | 7 +++++++ src/display/editor/freetext.js | 3 ++- src/display/editor/ink.js | 4 ++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/display/annotation_storage.js b/src/display/annotation_storage.js index 515869c13..6b558f8f0 100644 --- a/src/display/annotation_storage.js +++ b/src/display/annotation_storage.js @@ -102,6 +102,15 @@ class AnnotationStorage { } } + /** + * Check if the storage contains the given key. + * @param {string} key + * @returns {boolean} + */ + has(key) { + return this._storage.has(key); + } + getAll() { return this._storage.size > 0 ? objectFromMap(this._storage) : null; } diff --git a/src/display/editor/annotation_editor_layer.js b/src/display/editor/annotation_editor_layer.js index d1b031463..b4c1b4e79 100644 --- a/src/display/editor/annotation_editor_layer.js +++ b/src/display/editor/annotation_editor_layer.js @@ -32,7 +32,7 @@ import { InkEditor } from "./ink.js"; * @property {HTMLDivElement} div * @property {AnnotationEditorUIManager} uiManager * @property {boolean} enabled - * @property {AnnotationStorage} annotationStorag + * @property {AnnotationStorage} annotationStorage * @property {number} pageIndex * @property {IL10n} l10n */ @@ -426,7 +426,7 @@ class AnnotationEditorLayer { */ add(editor) { this.#changeParent(editor); - this.annotationStorage.setValue(editor.id, editor); + this.addToAnnotationStorage(editor); this.#uiManager.addEditor(editor); this.attach(editor); @@ -440,6 +440,16 @@ class AnnotationEditorLayer { editor.onceAdded(); } + /** + * Add an editor in the annotation storage. + * @param {AnnotationEditor} editor + */ + addToAnnotationStorage(editor) { + if (!editor.isEmpty() && !this.annotationStorage.has(editor.id)) { + this.annotationStorage.setValue(editor.id, editor); + } + } + /** * Add or rebuild depending if it has been removed or not. * @param {AnnotationEditor} editor diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index fcb912268..e3bbe5a66 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -136,6 +136,13 @@ class AnnotationEditor { } } + /** + * Commit the data contained in this editor. + */ + commit() { + this.parent.addToAnnotationStorage(this); + } + /** * We use drag-and-drop in order to move an editor on a page. * @param {DragEvent} event diff --git a/src/display/editor/freetext.js b/src/display/editor/freetext.js index dd0162006..ea51b9fb9 100644 --- a/src/display/editor/freetext.js +++ b/src/display/editor/freetext.js @@ -275,7 +275,7 @@ class FreeTextEditor extends AnnotationEditor { /** @inheritdoc */ isEmpty() { - return this.editorDiv.innerText.trim() === ""; + return !this.editorDiv || this.editorDiv.innerText.trim() === ""; } /** @inheritdoc */ @@ -320,6 +320,7 @@ class FreeTextEditor extends AnnotationEditor { * @returns {undefined} */ commit() { + super.commit(); if (!this.#hasAlreadyBeenCommitted) { // This editor has something and it's the first time // it's commited so we can add it in the undo/redo stack. diff --git a/src/display/editor/ink.js b/src/display/editor/ink.js index 5b1871d49..d6f7cad70 100644 --- a/src/display/editor/ink.js +++ b/src/display/editor/ink.js @@ -469,6 +469,8 @@ class InkEditor extends AnnotationEditor { return; } + super.commit(); + this.isEditing = false; this.disableEditMode(); @@ -572,6 +574,8 @@ class InkEditor extends AnnotationEditor { "pointermove", this.#boundCanvasPointermove ); + + this.parent.addToAnnotationStorage(this); } /**