diff --git a/app/components/Editor.tsx b/app/components/Editor.tsx index c4d7ab92d6..8461d3b0c2 100644 --- a/app/components/Editor.tsx +++ b/app/components/Editor.tsx @@ -185,39 +185,46 @@ function Editor(props: Props, ref: React.RefObject | null) { [updateComments] ); - const paragraphs = - props.readOnly && typeof props.value === "object" - ? ProsemirrorHelper.getPlainParagrahs(props.value) - : undefined; - - console.log({ paragraphs }); - - if (paragraphs) { - return ( - - {paragraphs.map((paragraph, index) => ( -

{paragraph.content.map((content) => content.text)}

- ))} -
- ); - } + const paragraphs = React.useMemo(() => { + if (props.readOnly && typeof props.value === "object") { + return ProsemirrorHelper.getPlainParagrahs(props.value); + } + return undefined; + }, [props.readOnly, props.value]); return ( <> - + {paragraphs ? ( + +
+ {paragraphs.map((paragraph, index) => ( +

+ {paragraph.content.map((content) => content.text)} +

+ ))} +
+
+ ) : ( + + )} {props.editorStyle?.paddingBottom && !props.readOnly && ( .ProseMirror-yjs-cursor { display: none; } diff --git a/shared/utils/ProsemirrorHelper.test.ts b/shared/utils/ProsemirrorHelper.test.ts index 55c6d3d0f7..828114859c 100644 --- a/shared/utils/ProsemirrorHelper.test.ts +++ b/shared/utils/ProsemirrorHelper.test.ts @@ -1,3 +1,4 @@ +import { ProsemirrorData } from "../types"; import { CommentMark, ProsemirrorHelper } from "./ProsemirrorHelper"; describe("ProsemirrorHelper", () => { @@ -87,4 +88,131 @@ describe("ProsemirrorHelper", () => { expect(returnedAnchorText).toBeUndefined(); }); }); + + describe("getPlainParagrahs", () => { + it("should return an array of plain paragraphs", async () => { + const data = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in another paragraph", + }, + ], + }, + ], + } as ProsemirrorData; + + const paragraphs = ProsemirrorHelper.getPlainParagrahs(data); + + expect(paragraphs).toEqual([ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in another paragraph", + }, + ], + }, + ]); + }); + + it("should return undefined when data contains inline nodes", async () => { + const data = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + { + type: "emoji", + attrs: { + "data-name": "😆", + }, + }, + ], + }, + ], + } as ProsemirrorData; + + const paragraphs = ProsemirrorHelper.getPlainParagrahs(data); + expect(paragraphs).toBeUndefined(); + }); + + it("should return undefined when data contains block nodes", async () => { + const data = { + type: "doc", + content: [ + { + type: "blockquote", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + ], + }, + ], + } as ProsemirrorData; + + const paragraphs = ProsemirrorHelper.getPlainParagrahs(data); + expect(paragraphs).toBeUndefined(); + }); + + it("should return undefined when data contains marks", async () => { + const data = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + marks: [ + { + type: "bold", + }, + ], + }, + ], + }, + ], + } as ProsemirrorData; + + const paragraphs = ProsemirrorHelper.getPlainParagrahs(data); + expect(paragraphs).toBeUndefined(); + }); + }); }); diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index a731127cc5..fec2cb5a31 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -359,7 +359,10 @@ export class ProsemirrorHelper { for (const node of data.content) { if ( node.type === "paragraph" && - !node.content.some((item) => item.marks) + !node.content.some( + (item) => + item.type !== "text" || (item.marks && item.marks.length > 0) + ) ) { paragraphs.push(node); } else {