diff --git a/src/display/editor/comment.js b/src/display/editor/comment.js index 86bf52ca7..5f73e3e5b 100644 --- a/src/display/editor/comment.js +++ b/src/display/editor/comment.js @@ -314,6 +314,20 @@ class Comment { this.#deleted = false; } + /** + * Restore the comment data (used for undo). + * @param {Object} data - The comment data to restore. + * @param {string} data.text - The comment text. + * @param {string|null} data.richText - The rich text content. + * @param {Date|null} data.date - The original date. + */ + restoreData({ text, richText, date }) { + this.#text = text; + this.#richText = richText; + this.#date = date; + this.#deleted = false; + } + setInitialText(text, richText = null) { this.#initialText = text; this.data = text; diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index db014c7e9..e330a4a94 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -1220,9 +1220,14 @@ class AnnotationEditor { }; } - set comment(text) { + set comment(value) { this.#comment ||= new Comment(this); - this.#comment.data = text; + if (typeof value === "object" && value !== null) { + // Restore full comment data (used for undo). + this.#comment.restoreData(value); + } else { + this.#comment.data = value; + } if (this.hasComment) { this.removeCommentButtonFromToolbar(); this.addStandaloneCommentButton(); diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index feafa2003..7767c024f 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -1180,11 +1180,11 @@ class AnnotationEditorUIManager { /** * Delete a comment from an editor with undo support. * @param {AnnotationEditor} editor - The editor whose comment to delete. - * @param {string} savedComment - The comment text to save for undo. + * @param {Object} savedData - The comment data to save for undo. */ - deleteComment(editor, savedComment) { + deleteComment(editor, savedData) { const undo = () => { - editor.comment = savedComment; + editor.comment = savedData; }; const cmd = () => { this._editorUndoBar?.show(undo, "comment"); diff --git a/test/integration/comment_spec.mjs b/test/integration/comment_spec.mjs index 36bebb3b6..2e4971c37 100644 --- a/test/integration/comment_spec.mjs +++ b/test/integration/comment_spec.mjs @@ -999,6 +999,14 @@ describe("Comment", () => { ); await page.waitForSelector("#commentPopup", { visible: true }); + + // Capture the date before deletion + const dateBefore = await page.evaluate( + () => + document.querySelector("#commentPopup .commentPopupTime") + ?.textContent + ); + await waitAndClick(page, "button.commentPopupDelete"); await page.waitForSelector("#editorUndoBar", { visible: true }); @@ -1016,6 +1024,16 @@ describe("Comment", () => { ?.textContent ); expect(popupText).withContext(`In ${browserName}`).toEqual(comment); + + // Check that the date is preserved + const dateAfter = await page.evaluate( + () => + document.querySelector("#commentPopup .commentPopupTime") + ?.textContent + ); + expect(dateAfter) + .withContext(`In ${browserName}`) + .toEqual(dateBefore); }) ); }); diff --git a/web/comment_manager.js b/web/comment_manager.js index 438d85f78..9da5b24fe 100644 --- a/web/comment_manager.js +++ b/web/comment_manager.js @@ -982,11 +982,11 @@ class CommentPopup { }, }, }); - const savedComment = this.#editor.comment?.text; const editor = this.#editor; + const savedData = editor.comment; this.destroy(); - if (savedComment) { - editor._uiManager.deleteComment(editor, savedComment); + if (savedData?.text) { + editor._uiManager.deleteComment(editor, savedData); } else { editor.comment = null; }