diff --git a/packages/core/src/api/nodeConversions/nodeToBlock.ts b/packages/core/src/api/nodeConversions/nodeToBlock.ts index 3d90168824..5d25d1ea58 100644 --- a/packages/core/src/api/nodeConversions/nodeToBlock.ts +++ b/packages/core/src/api/nodeConversions/nodeToBlock.ts @@ -64,14 +64,40 @@ export function contentNodeToTableContent< } // Mark the cell as a header if it is a tableHeader node. headerMatrix[rowIndex][cellIndex] = cellNode.type.name === "tableHeader"; + // Convert cell content to inline content and merge adjacent styled text nodes + const content = cellNode.content.content + .map((child) => + contentNodeToInlineContent(child, inlineContentSchema, styleSchema) + ) + // The reason that we merge this content is that we allow table cells to contain multiple tableParagraph nodes + // So that we can leverage prosemirror-tables native merging + // If the schema only allowed a single tableParagraph node, then the merging would not work and cause prosemirror to fit the content into a new cell + .reduce((acc, contentPartial) => { + if (!acc.length) { + return contentPartial; + } + + const last = acc[acc.length - 1]; + const first = contentPartial[0]; + + // Only merge if the last and first content are both styled text nodes and have the same styles + if ( + isStyledTextInlineContent(last) && + isStyledTextInlineContent(first) && + JSON.stringify(last.styles) === JSON.stringify(first.styles) + ) { + // Join them together if they have the same styles + last.text += "\n" + first.text; + acc.push(...contentPartial.slice(1)); + return acc; + } + acc.push(...contentPartial); + return acc; + }, [] as InlineContent[]); return { type: "tableCell", - content: contentNodeToInlineContent( - cellNode.firstChild!, - inlineContentSchema, - styleSchema - ), + content, props: { colspan: cellNode.attrs.colspan, rowspan: cellNode.attrs.rowspan, diff --git a/packages/core/src/blocks/TableBlockContent/TableBlockContent.ts b/packages/core/src/blocks/TableBlockContent/TableBlockContent.ts index 309b0b5068..be59382502 100644 --- a/packages/core/src/blocks/TableBlockContent/TableBlockContent.ts +++ b/packages/core/src/blocks/TableBlockContent/TableBlockContent.ts @@ -159,10 +159,10 @@ export const Table = createBlockSpecFromStronglyTypedTiptapNode( TableExtension, TableParagraph, TableHeader.extend({ - content: "tableContent", + content: "tableContent*", }), TableCell.extend({ - content: "tableContent", + content: "tableContent*", }), TableRow, ]