Restore date too

This commit is contained in:
Marco Castelluccio 2026-01-21 17:06:13 +01:00
parent d9f67bd8ee
commit 84d15dc453
No known key found for this signature in database
GPG Key ID: 7EC7F621C5F89953
5 changed files with 45 additions and 8 deletions

View File

@ -314,6 +314,20 @@ class Comment {
this.#deleted = false; 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) { setInitialText(text, richText = null) {
this.#initialText = text; this.#initialText = text;
this.data = text; this.data = text;

View File

@ -1220,9 +1220,14 @@ class AnnotationEditor {
}; };
} }
set comment(text) { set comment(value) {
this.#comment ||= new Comment(this); 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) { if (this.hasComment) {
this.removeCommentButtonFromToolbar(); this.removeCommentButtonFromToolbar();
this.addStandaloneCommentButton(); this.addStandaloneCommentButton();

View File

@ -1180,11 +1180,11 @@ class AnnotationEditorUIManager {
/** /**
* Delete a comment from an editor with undo support. * Delete a comment from an editor with undo support.
* @param {AnnotationEditor} editor - The editor whose comment to delete. * @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 = () => { const undo = () => {
editor.comment = savedComment; editor.comment = savedData;
}; };
const cmd = () => { const cmd = () => {
this._editorUndoBar?.show(undo, "comment"); this._editorUndoBar?.show(undo, "comment");

View File

@ -999,6 +999,14 @@ describe("Comment", () => {
); );
await page.waitForSelector("#commentPopup", { visible: true }); 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 waitAndClick(page, "button.commentPopupDelete");
await page.waitForSelector("#editorUndoBar", { visible: true }); await page.waitForSelector("#editorUndoBar", { visible: true });
@ -1016,6 +1024,16 @@ describe("Comment", () => {
?.textContent ?.textContent
); );
expect(popupText).withContext(`In ${browserName}`).toEqual(comment); 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);
}) })
); );
}); });

View File

@ -982,11 +982,11 @@ class CommentPopup {
}, },
}, },
}); });
const savedComment = this.#editor.comment?.text;
const editor = this.#editor; const editor = this.#editor;
const savedData = editor.comment;
this.destroy(); this.destroy();
if (savedComment) { if (savedData?.text) {
editor._uiManager.deleteComment(editor, savedComment); editor._uiManager.deleteComment(editor, savedData);
} else { } else {
editor.comment = null; editor.comment = null;
} }