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

Table insert column w/ span support #4074

Merged
merged 2 commits into from
Mar 11, 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
59 changes: 57 additions & 2 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
insertHorizontalRule,
insertSampleImage,
insertTable,
insertTableColumnBefore,
insertTableRowBelow,
IS_COLLAB,
mergeTableCells,
Expand Down Expand Up @@ -1119,7 +1120,6 @@ test.describe('Tables', () => {
await insertTable(page, 2, 2);

await click(page, '.PlaygroundEditorTheme__tableCell');
await moveDown(page, 1);
await selectCellsFromTableCords(
page,
{x: 1, y: 0},
Expand All @@ -1130,7 +1130,6 @@ test.describe('Tables', () => {
await mergeTableCells(page);

await moveLeft(page, 1);
await page.pause();
await insertTableRowBelow(page);

await assertHTML(
Expand Down Expand Up @@ -1163,4 +1162,60 @@ test.describe('Tables', () => {
`,
);
});

test('Insert column before (with conflicting merged cell)', 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, 2);

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

await moveDown(page, 1);
await moveRight(page, 1);
await insertTableColumnBefore(page);

await assertHTML(
page,
html`
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
<table class="PlaygroundEditorTheme__table">
<tr>
<th
class="PlaygroundEditorTheme__tableCell PlaygroundEditorTheme__tableCellHeader"
colspan="3">
<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"><br /></td>
<td class="PlaygroundEditorTheme__tableCell">
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
</td>
</tr>
</table>
<p class="PlaygroundEditorTheme__paragraph"><br /></p>
`,
);
});
});
10 changes: 10 additions & 0 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,16 @@ export async function insertTableRowBelow(page) {
await click(page, '.item[data-test-id="table-insert-row-below"]');
}

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

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

export async function mergeTableCells(page) {
await click(page, '.table-cell-action-button-container');
await click(page, '.item[data-test-id="table-merge-cells"]');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import useLexicalEditable from '@lexical/react/useLexicalEditable';
import {
$deleteTableColumn,
$getElementGridForTableNode,
$getTableCellNodeFromLexicalNode,
$getTableColumnIndexFromTableCellNode,
$getTableNodeFromLexicalNodeOrThrow,
$getTableRowIndexFromTableCellNode,
$insertTableColumn,
$insertTableColumn__EXPERIMENTAL,
$insertTableRow__EXPERIMENTAL,
$isTableCellNode,
$isTableRowNode,
Expand Down Expand Up @@ -243,44 +242,11 @@ function TableActionMenu({
const insertTableColumnAtSelection = useCallback(
(shouldInsertAfter: boolean) => {
editor.update(() => {
const selection = $getSelection();

const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);

let tableColumnIndex;

if (DEPRECATED_$isGridSelection(selection)) {
const selectionShape = selection.getShape();
tableColumnIndex = shouldInsertAfter
? selectionShape.toX
: selectionShape.fromX;
} else {
tableColumnIndex =
$getTableColumnIndexFromTableCellNode(tableCellNode);
}

const grid = $getElementGridForTableNode(editor, tableNode);

$insertTableColumn(
tableNode,
tableColumnIndex,
shouldInsertAfter,
selectionCounts.columns,
grid,
);

clearTableSelection();

$insertTableColumn__EXPERIMENTAL(shouldInsertAfter);
onClose();
});
},
[
editor,
tableCellNode,
selectionCounts.columns,
clearTableSelection,
onClose,
],
[editor, onClose],
);

const deleteTableRowAtSelection = useCallback(() => {
Expand Down Expand Up @@ -431,7 +397,7 @@ function TableActionMenu({
<button
className="item"
onClick={() => insertTableColumnAtSelection(false)}
data-test-id="table-insert-column-left">
data-test-id="table-insert-column-before">
<span className="text">
Insert{' '}
{selectionCounts.columns === 1
Expand All @@ -443,7 +409,7 @@ function TableActionMenu({
<button
className="item"
onClick={() => insertTableColumnAtSelection(true)}
data-test-id="table-insert-column-right">
data-test-id="table-insert-column-after">
<span className="text">
Insert{' '}
{selectionCounts.columns === 1
Expand Down
42 changes: 40 additions & 2 deletions packages/lexical-table/src/LexicalTableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ export function $insertTableRow__EXPERIMENTAL(insertAfter = true): void {
focusCell,
focusCell,
);
const columnCount = gridMap[0].length;
const {startRow: focusStartRow} = focusCellMap;
if (insertAfter) {
const focusEndRow = focusStartRow + focusCell.__rowSpan - 1;
const focusEndRowMap = gridMap[focusEndRow];
const newRow = $createTableRowNode();
const columnCount = gridMap[0].length;
for (let i = 0; i < columnCount; i++) {
const {cell, startRow} = focusEndRowMap[i];
if (startRow + cell.__rowSpan - 1 <= focusEndRow) {
Expand All @@ -262,7 +262,6 @@ export function $insertTableRow__EXPERIMENTAL(insertAfter = true): void {
} else {
const focusStartRowMap = gridMap[focusStartRow];
const newRow = $createTableRowNode();
const columnCount = gridMap[0].length;
for (let i = 0; i < columnCount; i++) {
const {cell, startRow} = focusStartRowMap[i];
if (startRow === focusStartRow) {
Expand Down Expand Up @@ -334,6 +333,45 @@ export function $insertTableColumn(
return tableNode;
}

export function $insertTableColumn__EXPERIMENTAL(insertAfter = true): void {
const selection = $getSelection();
invariant(
$isRangeSelection(selection) || DEPRECATED_$isGridSelection(selection),
'Expected a RangeSelection or GridSelection',
);
const focus = selection.focus.getNode();
const [focusCell, , grid] = DEPRECATED_$getNodeTriplet(focus);
const [gridMap, focusCellMap] = DEPRECATED_$computeGridMap(
grid,
focusCell,
focusCell,
);
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);
}
}
} 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);
}
}
}
}

export function $deleteTableColumn(
tableNode: TableNode,
targetIndex: number,
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
$getTableRowIndexFromTableCellNode,
$getTableRowNodeFromTableCellNodeOrThrow,
$insertTableColumn,
$insertTableColumn__EXPERIMENTAL,
$insertTableRow,
$insertTableRow__EXPERIMENTAL,
$removeTableRowAtIndex,
Expand All @@ -66,6 +67,7 @@ export {
$getTableRowIndexFromTableCellNode,
$getTableRowNodeFromTableCellNodeOrThrow,
$insertTableColumn,
$insertTableColumn__EXPERIMENTAL,
$insertTableRow,
$insertTableRow__EXPERIMENTAL,
$isTableCellNode,
Expand Down