Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions packages/core/src/api/nodeConversions/nodeToBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, S>[]);

return {
type: "tableCell",
content: contentNodeToInlineContent(
cellNode.firstChild!,
inlineContentSchema,
styleSchema
),
content,
props: {
colspan: cellNode.attrs.colspan,
rowspan: cellNode.attrs.rowspan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ export const Table = createBlockSpecFromStronglyTypedTiptapNode(
TableExtension,
TableParagraph,
TableHeader.extend({
content: "tableContent",
content: "tableContent*",
}),
TableCell.extend({
content: "tableContent",
content: "tableContent*",
}),
TableRow,
]
Expand Down