Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TableCell child nodes on paste #5951

Merged
merged 2 commits into from
Apr 25, 2024
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
22 changes: 12 additions & 10 deletions packages/lexical-playground/__tests__/e2e/CodeBlock.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ test.describe('CodeBlock', () => {
</p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph">
<p class="PlaygroundEditorTheme__paragraph" style="text-align: right">
<span
class="PlaygroundEditorTheme__textUnderline"
data-lexical-text="true">
Expand All @@ -1030,15 +1030,17 @@ test.describe('CodeBlock', () => {
<span data-lexical-text="true">XDS_RICH_TEXT_AREA</span>
</p>
</td>
<td
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">sdvd</span>
<strong
class="PlaygroundEditorTheme__textBold"
data-lexical-text="true">
sdfvsfs
</strong>
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">sdvd</span>
<strong
class="PlaygroundEditorTheme__textBold"
data-lexical-text="true">
sdfvsfs
</strong>
</p>
</td>
</tr>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,30 @@ test.describe('HTML Tables CopyAndPaste', () => {
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">a</span>
</p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">b</span>
</p>
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">b</span>
</p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">c</span>
</p>
</td>
Expand All @@ -145,21 +149,24 @@ test.describe('HTML Tables CopyAndPaste', () => {
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">d</span>
</p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">e</span>
</p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
dir="ltr"
style="text-align: left;">
<span data-lexical-text="true">f</span>
</p>
</td>
Expand Down
30 changes: 27 additions & 3 deletions packages/lexical-table/src/LexicalTableCellNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DOMConversionOutput,
DOMExportOutput,
EditorConfig,
ElementFormatType,
LexicalEditor,
LexicalNode,
NodeKey,
Expand All @@ -22,6 +23,7 @@ import {addClassNamesToElement} from '@lexical/utils';
import {
$applyNodeReplacement,
$createParagraphNode,
$isBlockElementNode,
$isElementNode,
$isLineBreakNode,
$isTextNode,
Expand Down Expand Up @@ -323,17 +325,39 @@ export function convertTableCellNodeElement(
const hasLinethroughTextDecoration = textDecoration.includes('line-through');
const hasItalicFontStyle = style.fontStyle === 'italic';
const hasUnderlineTextDecoration = textDecoration.includes('underline');

const textAlign = style.textAlign;
return {
after: (childLexicalNodes) => {
const children = [];
let paragraphNode = $createParagraphNode().setFormat(
textAlign as ElementFormatType,
);
if (childLexicalNodes.length === 0) {
childLexicalNodes.push($createParagraphNode());
children.push(paragraphNode);
}

childLexicalNodes.forEach((node) => {
if ($isBlockElementNode(node)) {
if (paragraphNode.getChildrenSize() !== 0) {
children.push(paragraphNode);
paragraphNode = $createParagraphNode().setFormat(
textAlign as ElementFormatType,
);
}
children.push(node);
} else {
paragraphNode.append(node);
}
});
if (paragraphNode.getChildrenSize() !== 0) {
children.push(paragraphNode);
}
return childLexicalNodes;
return children;
},
forChild: (lexicalNode, parentLexicalNode) => {
if ($isTableCellNode(parentLexicalNode) && !$isElementNode(lexicalNode)) {
const paragraphNode = $createParagraphNode();
paragraphNode.setFormat(textAlign as ElementFormatType);
if (
$isLineBreakNode(lexicalNode) &&
lexicalNode.getTextContent() === '\n'
Expand Down
Loading