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 insertTableColumn when rowSpan > 1 #4258

Merged
merged 3 commits into from
Apr 3, 2023
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
56 changes: 55 additions & 1 deletion packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,61 @@ test.describe('Tables', () => {
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
<td class="PlaygroundEditorTheme__tableCell"><br /></td>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
</tr>
</table>
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
`,
);
});

test('Insert column before (with selected cell with rowspan > 1)', async ({
page,
isPlainText,
}) => {
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, 2, 1);

await click(page, '.PlaygroundEditorTheme__tableCell');
await selectCellsFromTableCords(
page,
{x: 0, y: 0},
{x: 0, y: 1},
true,
true,
);
await mergeTableCells(page);
await insertTableColumnBefore(page);
await page.pause();

await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table">
<tr>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
rowspan="2">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</th>
</tr>
<tr>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
Expand Down
77 changes: 58 additions & 19 deletions packages/lexical-table/src/LexicalTableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type {Grid} from './LexicalTableSelection';
import type {ElementNode} from 'lexical';
import type {DEPRECATED_GridRowNode, ElementNode} from 'lexical';

import {$findMatchingParent} from '@lexical/utils';
import {
Expand Down Expand Up @@ -350,28 +350,67 @@ export function $insertTableColumn__EXPERIMENTAL(insertAfter = true): void {
);
const rowCount = gridMap.length;
const {startColumn: focusStartColumn} = focusCellMap;
if (insertAfter) {
const focusEndColumn = focusStartColumn + focusCell.__colSpan - 1;
for (let i = 0; i < rowCount; i++) {
const {cell, startColumn} = gridMap[i][focusEndColumn];
if (startColumn + cell.__colSpan - 1 <= focusEndColumn) {
cell.insertAfter($createTableCellNode(TableCellHeaderStates.NO_STATUS));
} else {
cell.setColSpan(cell.__colSpan + 1);
}
const insertAfterColumn = insertAfter
? focusStartColumn + focusCell.__colSpan - 1
: focusStartColumn - 1;
const gridFirstChild = grid.getFirstChild();
invariant(
DEPRECATED_$isGridRowNode(gridFirstChild),
'Expected firstTable child to be a row',
);
let firstInsertedCell: null | DEPRECATED_GridCellNode = null;
function $createTableCellNodeForInsertTableColumn() {
const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS).append(
$createParagraphNode(),
);
if (firstInsertedCell === null) {
firstInsertedCell = cell;
}
} else {
for (let i = 0; i < rowCount; i++) {
const {cell, startColumn} = gridMap[i][focusStartColumn];
if (startColumn === focusStartColumn) {
cell.insertBefore(
$createTableCellNode(TableCellHeaderStates.NO_STATUS),
);
} else {
cell.setColSpan(cell.__colSpan + 1);
return cell;
}
let loopRow: DEPRECATED_GridRowNode = gridFirstChild;
rowLoop: for (let i = 0; i < rowCount; i++) {
if (i !== 0) {
const currentRow = loopRow.getNextSibling();
invariant(
DEPRECATED_$isGridRowNode(currentRow),
'Expected row nextSibling to be a row',
);
loopRow = currentRow;
}
const rowMap = gridMap[i];
if (insertAfterColumn < 0) {
$insertFirst(loopRow, $createTableCellNodeForInsertTableColumn());
continue;
}
const {
cell: currentCell,
startColumn: currentStartColumn,
startRow: currentStartRow,
} = rowMap[insertAfterColumn];
if (currentStartColumn + currentCell.__colSpan - 1 <= insertAfterColumn) {
let insertAfterCell: DEPRECATED_GridCellNode = currentCell;
let insertAfterCellRowStart = currentStartRow;
let prevCellIndex = insertAfterColumn;
while (insertAfterCellRowStart !== i && insertAfterCell.__rowSpan > 1) {
prevCellIndex -= currentCell.__colSpan;
if (prevCellIndex >= 0) {
const {cell: cell_, startRow: startRow_} = rowMap[prevCellIndex];
insertAfterCell = cell_;
insertAfterCellRowStart = startRow_;
} else {
loopRow.append($createTableCellNodeForInsertTableColumn());
continue rowLoop;
}
}
insertAfterCell.insertAfter($createTableCellNodeForInsertTableColumn());
} else {
currentCell.setColSpan(currentCell.__colSpan + 1);
}
}
if (firstInsertedCell !== null) {
$moveSelectionToCell(firstInsertedCell);
}
}

export function $deleteTableColumn(
Expand Down