diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 37e8576fbb..238cf55513 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -269,6 +269,11 @@ export class ProsemirrorHelper { if (node.marks) { node.marks = node.marks.filter((mark) => !marks.includes(mark.type)); } + if (node.attrs?.marks) { + node.attrs.marks = (node.attrs.marks as { type: string }[])?.filter( + (mark) => !marks.includes(mark.type) + ); + } if (node.content) { node.content.forEach(removeMarksInner); } diff --git a/server/utils/ProsemirrorHelper.test.ts b/server/utils/ProsemirrorHelper.test.ts index 02bd92e5d7..36a20bc5a2 100644 --- a/server/utils/ProsemirrorHelper.test.ts +++ b/server/utils/ProsemirrorHelper.test.ts @@ -1,4 +1,6 @@ +import crypto from "node:crypto"; import { Node } from "prosemirror-model"; +import { ProsemirrorHelper as ServerProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { schema } from "@server/editor"; @@ -181,4 +183,81 @@ describe("#ProsemirrorHelper", () => { }); }); }); + + describe("#removeMarks", () => { + it("preserves table cell background color when removing comment marks", () => { + const doc = Node.fromJSON(schema, { + type: "doc", + content: [ + { + type: "table", + content: [ + { + type: "tr", + content: [ + { + type: "td", + attrs: { + colspan: 1, + rowspan: 1, + alignment: null, + colwidth: null, + marks: [ + { + type: "background", + attrs: { color: "#e8f5e9" }, + }, + { + type: "comment", + attrs: { id: "comment-1" }, + }, + ], + }, + content: [{ type: "paragraph" }], + }, + ], + }, + ], + }, + ], + }); + + const result = ServerProsemirrorHelper.removeMarks(doc, ["comment"]); + const tdAttrsMarks = result.content?.[0]?.content?.[0]?.content?.[0] + ?.attrs?.marks as Array<{ type: string }> | undefined; + + expect(tdAttrsMarks?.find((m) => m.type === "background")).toBeDefined(); + expect(tdAttrsMarks?.find((m) => m.type === "comment")).toBeUndefined(); + }); + + it("removes comment marks from text nodes when duplicating", () => { + const doc = Node.fromJSON(schema, { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "Hello", + marks: [ + { + type: "comment", + attrs: { id: "comment-2", userId: crypto.randomUUID() }, + }, + ], + }, + ], + }, + ], + }); + + const result = ServerProsemirrorHelper.removeMarks(doc, ["comment"]); + const textMarks = result.content?.[0]?.content?.[0]?.marks as + | Array<{ type: string }> + | undefined; + + expect(textMarks?.find((m) => m.type === "comment")).toBeUndefined(); + }); + }); });