diff --git a/src/display/editor/comment.js b/src/display/editor/comment.js index d32087818..6bfc468dd 100644 --- a/src/display/editor/comment.js +++ b/src/display/editor/comment.js @@ -135,6 +135,10 @@ class Comment { return this.#deleted || this.#text === ""; } + isEmpty() { + return this.#text === null; + } + hasBeenEdited() { return this.isDeleted() || this.#text !== this.#initialText; } diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index bda3f3b89..8e84e0edb 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -1103,6 +1103,17 @@ class AnnotationEditor { return this._editToolbar; } + addCommentButtonInToolbar() { + if (!this._editToolbar) { + return; + } + this._editToolbar.addButtonBefore( + "comment", + this.addCommentButton(), + ".deleteButton" + ); + } + removeEditToolbar() { if (!this._editToolbar) { return; @@ -1214,10 +1225,14 @@ class AnnotationEditor { } set comment(text) { - if (!this.#comment) { - this.#comment = new Comment(this); - } + this.#comment ||= new Comment(this); this.#comment.data = text; + if (this.hasComment) { + this.addStandaloneCommentButton(); + } else { + this.addCommentButtonInToolbar(); + this.removeStandaloneCommentButton(); + } } setCommentData({ comment, richText }) { @@ -1232,7 +1247,9 @@ class AnnotationEditor { } get hasComment() { - return !!this.#comment && !this.#comment.isDeleted(); + return ( + !!this.#comment && !this.#comment.isEmpty() && !this.#comment.isDeleted() + ); } async editComment() { diff --git a/src/display/editor/toolbar.js b/src/display/editor/toolbar.js index bf961fe01..2ad428144 100644 --- a/src/display/editor/toolbar.js +++ b/src/display/editor/toolbar.js @@ -158,7 +158,7 @@ class EditorToolbar { this.#altText = altText; } - addComment(comment) { + addComment(comment, beforeElement = null) { if (this.#comment) { return; } @@ -167,7 +167,12 @@ class EditorToolbar { return; } this.#addListenersToElement(button); - this.#buttons.append(button, this.#divider); + if (!beforeElement) { + this.#buttons.append(button, this.#divider); + } else { + this.#buttons.insertBefore(button, beforeElement); + this.#buttons.insertBefore(this.#divider, beforeElement); + } this.#comment = comment; comment.toolbar = this; } @@ -209,6 +214,16 @@ class EditorToolbar { } } + async addButtonBefore(name, tool, beforeSelector) { + const beforeElement = this.#buttons.querySelector(beforeSelector); + if (!beforeElement) { + return; + } + if (name === "comment") { + this.addComment(tool, beforeElement); + } + } + updateEditSignatureButton(description) { if (this.#signatureDescriptionButton) { this.#signatureDescriptionButton.title = description;