diff --git a/app/components/Editor.tsx b/app/components/Editor.tsx index 19f6a7c4f0..e41ef39a51 100644 --- a/app/components/Editor.tsx +++ b/app/components/Editor.tsx @@ -205,7 +205,7 @@ function Editor(props: Props, ref: React.RefObject | null) {
{paragraphs.map((paragraph, index) => (

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

))}
diff --git a/app/models/Document.ts b/app/models/Document.ts index 57a530a93a..78be2fe01a 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -600,7 +600,7 @@ export default class Document extends ArchivableModel implements Searchable { */ getSummary = (blocks = 4) => ({ ...this.data, - content: this.data.content.slice(0, blocks), + content: this.data.content?.slice(0, blocks), }); @computed diff --git a/shared/types.ts b/shared/types.ts index dc45116750..3a6891db03 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -430,7 +430,7 @@ export type JSONObject = { [x: string]: JSONValue }; export type ProsemirrorData = { type: string; - content: ProsemirrorData[]; + content?: ProsemirrorData[]; text?: string; attrs?: JSONObject; marks?: { diff --git a/shared/utils/ProsemirrorHelper.test.ts b/shared/utils/ProsemirrorHelper.test.ts index 3974785c01..d557ad2b69 100644 --- a/shared/utils/ProsemirrorHelper.test.ts +++ b/shared/utils/ProsemirrorHelper.test.ts @@ -214,5 +214,23 @@ describe("ProsemirrorHelper", () => { const paragraphs = ProsemirrorHelper.getPlainParagraphs(data); expect(paragraphs).toBeUndefined(); }); + + it("should handle paragraph without content", async () => { + const data = { + type: "doc", + content: [ + { + type: "paragraph", + }, + ], + } as ProsemirrorData; + + const paragraphs = ProsemirrorHelper.getPlainParagraphs(data); + expect(paragraphs).toEqual([ + { + type: "paragraph", + }, + ]); + }); }); }); diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index 9b77644135..c20eacfeae 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -70,7 +70,7 @@ export class ProsemirrorHelper { return false; } - if (data.content.length === 1) { + if (data.content?.length === 1) { const node = data.content[0]; return ( node.type === "paragraph" && @@ -80,7 +80,7 @@ export class ProsemirrorHelper { ); } - return data.content.length === 0; + return !data.content || data.content.length === 0; } /** @@ -355,14 +355,19 @@ export class ProsemirrorHelper { * @returns An array of paragraph nodes or undefined */ static getPlainParagraphs(data: ProsemirrorData) { - const paragraphs = []; + const paragraphs: ProsemirrorData[] = []; + if (!data.content) { + return paragraphs; + } + for (const node of data.content) { if ( node.type === "paragraph" && - !node.content.some( - (item) => - item.type !== "text" || (item.marks && item.marks.length > 0) - ) + (!node.content || + !node.content.some( + (item) => + item.type !== "text" || (item.marks && item.marks.length > 0) + )) ) { paragraphs.push(node); } else {