From d9934db192623f2ebc7d0db718b2ed90bdb948ca Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 22 Sep 2026 23:23:49 -0400 Subject: [PATCH] fix: Restore every comment anchor range --- .../models/helpers/ProseMirrorHelper.test.ts | 42 +++++++++++++++++++ server/models/helpers/ProsemirrorHelper.tsx | 9 ---- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/server/models/helpers/ProseMirrorHelper.test.ts b/server/models/helpers/ProseMirrorHelper.test.ts index aa55c6dec6..8ab069ac20 100644 --- a/server/models/helpers/ProseMirrorHelper.test.ts +++ b/server/models/helpers/ProseMirrorHelper.test.ts @@ -2383,6 +2383,48 @@ describe("ProsemirrorHelper", () => { }); }); + describe("restoreCommentMarks", () => { + it("restores a missing range when another range of the same comment remains", () => { + const commentMark = { + type: "comment", + attrs: { + id: "comment-1", + userId: "user-1", + draft: false, + resolved: false, + }, + }; + const paragraph = (text: string, marked: boolean) => ({ + type: "paragraph", + content: [ + { + type: "text", + text, + ...(marked ? { marks: [commentMark] } : {}), + }, + ], + }); + const previous = Node.fromJSON(schema, { + type: "doc", + content: [ + paragraph("First range", true), + paragraph("Second range", true), + ], + }); + const next = Node.fromJSON(schema, { + type: "doc", + content: [ + paragraph("First range", true), + paragraph("Second range", false), + ], + }); + + const restored = ProsemirrorHelper.restoreCommentMarks(previous, next); + + expect(restored.toJSON()).toEqual(previous.toJSON()); + }); + }); + describe("#applyCommentMarkByNode", () => { const buildDocState = (content: object[]) => { const doc = Node.fromJSON(schema, { type: "doc", content }); diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 0fc3e408d3..bcd2b494f3 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -1521,18 +1521,9 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { return next; } - const existing = new Set( - ProsemirrorHelper.getCommentAnchors(next).map( - (anchor) => anchor.mark.attrs.id - ) - ); const tr = new Transform(next); for (const { mark, text, prefix, suffix } of anchors) { - if (existing.has(mark.attrs.id)) { - continue; - } - const range = ProsemirrorHelper.findTextRange(next, text, { prefix, suffix }) ?? ProsemirrorHelper.findTextRange(next, text, { prefix }) ??