fix: Error rendering read only editor with paragraph without text content (#8730)

This commit is contained in:
Tom Moor authored and GitHub committed 2025-03-20 13:46:53 +00:00
1 parent 0fe50c179c
commit 08f91aa60c
5 files changed
+33 -10

No files matched your search

+1 -1
View File
@@ -205,7 +205,7 @@ function Editor(props: Props, ref: React.RefObject<SharedEditor> | null) {
<div className="ProseMirror">
{paragraphs.map((paragraph, index) => (
<p key={index} dir="auto">
{paragraph.content.map((content) => content.text)}
{paragraph.content?.map((content) => content.text)}
</p>
))}
</div>
+1 -1
View File
@@ -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
+1 -1
View File
@@ -430,7 +430,7 @@ export type JSONObject = { [x: string]: JSONValue };
export type ProsemirrorData = {
type: string;
content: ProsemirrorData[];
content?: ProsemirrorData[];
text?: string;
attrs?: JSONObject;
marks?: {
+18
View File
@@ -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",
},
]);
});
});
});
+12 -7
View File
@@ -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 {