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

[lexical] Bug Fix: Fix Table formatting when no TextNodes present #6675

Merged
merged 7 commits into from
Sep 29, 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
199 changes: 199 additions & 0 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,205 @@ test.describe.parallel('Tables', () => {
},
);

test(`Can style on empty table cells and paragraphs with no text`, async ({
page,
isPlainText,
isCollab,
}) => {
await initialize({isCollab, page});
test.skip(isPlainText);

await focusEditor(page);
await insertTable(page, 2, 3);
await selectAll(page);

// Apply style on empty table
await clickSelectors(page, ['.bold']);

// Add text after applying styles
await click(page, 'div[contenteditable="true"] p:first-of-type');
await page.keyboard.type('abc');

await click(page, 'th p:first-of-type');
await fillTablePartiallyWithText(page);

// Check that the character styles are applied.
await assertHTML(
page,
html`
<p dir="ltr"><strong data-lexical-text="true">abc</strong></p>
<table>
<colgroup>
<col style="width: 92px" />
<col style="width: 92px" />
<col style="width: 92px" />
</colgroup>
<tr>
<th>
<p dir="ltr"><strong data-lexical-text="true">a</strong></p>
</th>
<th>
<p dir="ltr"><strong data-lexical-text="true">bb</strong></p>
</th>
<th>
<p dir="ltr"><strong data-lexical-text="true">cc</strong></p>
</th>
</tr>
<tr>
<th>
<p dir="ltr"><strong data-lexical-text="true">d</strong></p>
</th>
<td>
<p dir="ltr"><strong data-lexical-text="true">e</strong></p>
</td>
<td>
<p dir="ltr"><strong data-lexical-text="true">f</strong></p>
</td>
</tr>
</table>
<p><br /></p>
`,
html`
<p dir="ltr"><strong data-lexical-text="true">abc</strong></p>
<table>
<colgroup>
<col style="width: 92px" />
<col style="width: 92px" />
<col style="width: 92px" />
</colgroup>
<tr>
<th>
<p dir="ltr"><strong data-lexical-text="true">a</strong></p>
</th>
<th>
<p dir="ltr"><strong data-lexical-text="true">bb</strong></p>
</th>
<th>
<p dir="ltr"><strong data-lexical-text="true">cc</strong></p>
</th>
</tr>
<tr>
<th>
<p dir="ltr"><strong data-lexical-text="true">d</strong></p>
</th>
<td>
<p dir="ltr"><strong data-lexical-text="true">e</strong></p>
</td>
<td>
<p dir="ltr"><strong data-lexical-text="true">f</strong></p>
</td>
</tr>
</table>
<p><br /></p>
`,
{ignoreClasses: true},
);
});

test(`Align selection style for table cells`, async ({
page,
isPlainText,
isCollab,
}) => {
await initialize({isCollab, page});
test.skip(isPlainText);

await focusEditor(page);
await insertTable(page, 2, 3);

// Add text in bold to first cell
await click(page, 'th p:first-of-type');
await page.keyboard.type('a');
await page.keyboard.down('Shift');
await page.keyboard.press('ArrowLeft');
await page.keyboard.up('Shift');
await clickSelectors(page, ['.bold']);

// Apply bold style to whole table
// Bold style shouldn't be applied to any paragraphs and removed from all cells
await selectAll(page);
await clickSelectors(page, ['.bold']);

// Add text after applying styles
await click(page, 'div[contenteditable="true"] p:first-of-type');
await page.keyboard.type('abc');

await click(page, 'th p:first-of-type');
await fillTablePartiallyWithText(page);

// None of the paragraphs have style applied
await assertHTML(
page,
html`
<p dir="ltr"><span data-lexical-text="true">abc</span></p>
<table>
<colgroup>
<col style="width: 92px" />
<col style="width: 92px" />
<col style="width: 92px" />
</colgroup>
<tr>
<th>
<p dir="ltr"><span data-lexical-text="true">aa</span></p>
</th>
<th>
<p><span data-lexical-text="true">bb</span></p>
</th>
<th>
<p><span data-lexical-text="true">cc</span></p>
</th>
</tr>
<tr>
<th>
<p><span data-lexical-text="true">d</span></p>
</th>
<td>
<p><span data-lexical-text="true">e</span></p>
</td>
<td>
<p><span data-lexical-text="true">f</span></p>
</td>
</tr>
</table>
<p><br /></p>
`,
html`
<p dir="ltr"><span data-lexical-text="true">abc</span></p>
<table>
<colgroup>
<col style="width: 92px" />
<col style="width: 92px" />
<col style="width: 92px" />
</colgroup>
<tr>
<th>
<p dir="ltr"><span data-lexical-text="true">aa</span></p>
</th>
<th>
<p><span data-lexical-text="true">bb</span></p>
</th>
<th>
<p><span data-lexical-text="true">cc</span></p>
</th>
</tr>
<tr>
<th>
<p><span data-lexical-text="true">d</span></p>
</th>
<td>
<p><span data-lexical-text="true">e</span></p>
</td>
<td>
<p><span data-lexical-text="true">f</span></p>
</td>
</tr>
</table>
<p><br /></p>
`,
{ignoreClasses: true},
);
});

test(
`Can copy + paste (internal) using Table selection`,
{
Expand Down
16 changes: 10 additions & 6 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,12 +1194,21 @@ export class RangeSelection implements BaseSelection {
selectedTextNodes.push(selectedNode);
}
}
const applyFormatToParagraphs = (alignWith: number | null) => {
selectedNodes.forEach((node) => {
if ($isParagraphNode(node)) {
const newFormat = node.getFormatFlags(formatType, alignWith);
node.setTextFormat(newFormat);
}
});
};

const selectedTextNodesLength = selectedTextNodes.length;
if (selectedTextNodesLength === 0) {
this.toggleFormat(formatType);
// When changing format, we should stop composition
$setCompositionKey(null);
applyFormatToParagraphs(alignWithFormat);
return;
}

Expand Down Expand Up @@ -1231,12 +1240,7 @@ export class RangeSelection implements BaseSelection {
formatType,
alignWithFormat,
);
selectedNodes.forEach((node) => {
if ($isParagraphNode(node)) {
const newFormat = node.getFormatFlags(formatType, firstNextFormat);
node.setTextFormat(newFormat);
}
});
applyFormatToParagraphs(firstNextFormat);

const lastIndex = selectedTextNodesLength - 1;
let lastNode = selectedTextNodes[lastIndex];
Expand Down
Loading