Skip to content

Commit

Permalink
Fixed column header on merged cells (#5230)
Browse files Browse the repository at this point in the history
  • Loading branch information
icrosil authored Nov 16, 2023
1 parent df79e71 commit 83b8317
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 4 deletions.
95 changes: 95 additions & 0 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
selectFromAdditionalStylesDropdown,
setBackgroundColor,
test,
toggleColumnHeader,
unmergeTableCell,
} from '../utils/index.mjs';

Expand Down Expand Up @@ -1961,4 +1962,98 @@ test.describe('Tables', () => {
`,
);
});

test('Add column header after merging cells #4378', async ({
page,
isPlainText,
isCollab,
}) => {
await initialize({isCollab, page});
test.skip(isPlainText);
if (IS_COLLAB) {
// The contextual menu positioning needs fixing (it's hardcoded to show on the right side)
page.setViewportSize({height: 1000, width: 3000});
}

await focusEditor(page);

await insertTable(page, 4, 4);
await selectCellsFromTableCords(
page,
{x: 1, y: 2},
{x: 3, y: 3},
false,
false,
);
await mergeTableCells(page);
await selectCellsFromTableCords(
page,
{x: 3, y: 1},
{x: 3, y: 1},
false,
false,
);
await toggleColumnHeader(page);
await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
</tr>
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
</tr>
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<td
class="PlaygroundEditorTheme__tableCell"
colspan="3"
rowspan="2">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
</tr>
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
</tr>
</table>
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
`,
);
});
});
5 changes: 5 additions & 0 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,11 @@ export async function unmergeTableCell(page) {
await click(page, '.item[data-test-id="table-unmerge-cells"]');
}

export async function toggleColumnHeader(page) {
await click(page, '.table-cell-action-button-container');
await click(page, '.item[data-test-id="table-column-header"]');
}

export async function deleteTableRows(page) {
await click(page, '.table-cell-action-button-container');
await click(page, '.item[data-test-id="table-delete-rows"]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ function TableActionMenu({
$getTableColumnIndexFromTableCellNode(tableCellNode);

const tableRows = tableNode.getChildren();
const maxRowsLength = Math.max(
...tableRows.map((row) => row.getChildren().length),
);

if (tableColumnIndex >= maxRowsLength || tableColumnIndex < 0) {
throw new Error('Expected table cell to be inside of table row.');
}

for (let r = 0; r < tableRows.length; r++) {
const tableRow = tableRows[r];
Expand All @@ -441,9 +448,9 @@ function TableActionMenu({
}

const tableCells = tableRow.getChildren();

if (tableColumnIndex >= tableCells.length || tableColumnIndex < 0) {
throw new Error('Expected table cell to be inside of table row.');
if (tableColumnIndex >= tableCells.length) {
// if cell is outside of bounds for the current row (for example various merge cell cases) we shouldn't highlight it
continue;
}

const tableCell = tableCells[tableColumnIndex];
Expand Down Expand Up @@ -598,7 +605,10 @@ function TableActionMenu({
row header
</span>
</button>
<button className="item" onClick={() => toggleTableColumnIsHeader()}>
<button
className="item"
onClick={() => toggleTableColumnIsHeader()}
data-test-id="table-column-header">
<span className="text">
{(tableCellNode.__headerState & TableCellHeaderStates.COLUMN) ===
TableCellHeaderStates.COLUMN
Expand Down

2 comments on commit 83b8317

@vercel
Copy link

@vercel vercel bot commented on 83b8317 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-git-main-fbopensource.vercel.app
lexical-fbopensource.vercel.app
lexical.dev
lexicaljs.com
www.lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 83b8317 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground.vercel.app
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev

Please sign in to comment.