fix: Restore every comment anchor range

This commit is contained in:
Tom Moor committed 2026-09-22 23:23:49 -04:00
1 parent 04103b7cf4
commit d9934db192
2 files changed
+42 -9

No files matched your search

@@ -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 });
@@ -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 }) ??