Skip to content

Commit 41d4a46

Browse files
authored
fix: trying to add a row or column to an outer table caused it to be added into inner one (#139)
If you try to add a row or a column to a table with an inner table, it's gonna be added to the inner one instead.
1 parent dd873de commit 41d4a46

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/table-utils/commands/appendColumn.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {findChildTableCells, findChildTableRows} from '../utils';
1+
import {findChildTableCells, isTableBodyNode, isTableCellNode} from '../utils';
22
import {findChildIndex} from '../helpers';
33
import type {CommandWithAttrs} from '../../core';
4-
import {findParentNodeClosestToPos} from 'prosemirror-utils';
4+
import {findChildren, findParentNodeClosestToPos} from 'prosemirror-utils';
55
import {isTableNode, isTableRowNode} from '..';
66

77
export const appendColumn: CommandWithAttrs<{
@@ -20,15 +20,20 @@ export const appendColumn: CommandWithAttrs<{
2020
let parentCell;
2121
let parentRow;
2222

23+
const tableBody = findChildren(parentTable, isTableBodyNode, false).pop();
24+
if (!tableBody) return false;
25+
2326
if (columnNumber !== undefined) {
2427
parentCell = findChildTableCells(parentTable)[columnNumber];
2528
parentRow = findParentNodeClosestToPos(
2629
state.doc.resolve(tablePos + parentCell.pos + 1),
2730
isTableRowNode,
2831
);
2932
} else {
30-
parentCell = findChildTableCells(parentTable).pop();
31-
parentRow = findChildTableRows(parentTable).pop();
33+
parentRow = findChildren(tableBody.node, isTableRowNode, false).pop();
34+
if (!parentRow) return false;
35+
36+
parentCell = findChildren(parentRow.node, isTableCellNode, false).pop();
3237
}
3338

3439
if (!parentCell || !parentRow || !parentTable) {
@@ -42,14 +47,14 @@ export const appendColumn: CommandWithAttrs<{
4247
}
4348

4449
if (dispatch) {
45-
const allRows = findChildTableRows(parentTable);
50+
const allRows = findChildren(tableBody.node, isTableRowNode, false);
4651

4752
let tr = state.tr;
4853
for (const row of allRows) {
49-
const rowCells = findChildTableCells(row.node);
54+
const rowCells = findChildren(row.node, isTableCellNode, false);
5055
const cell = rowCells[parentCellIndex];
5156

52-
let position = tablePos + row.pos + cell.pos + 2;
57+
let position = tablePos + row.pos + cell.pos + 3;
5358
position += direction === 'before' ? 0 : cell.node.nodeSize;
5459

5560
tr = tr.insert(

src/table-utils/commands/appendRow.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Fragment, Node} from 'prosemirror-model';
2-
import {findParentNodeClosestToPos} from 'prosemirror-utils';
3-
import {findChildTableBody, findChildTableRows, isTableNode} from '..';
2+
import {findChildren, findParentNodeClosestToPos} from 'prosemirror-utils';
3+
import {findChildTableRows, isTableBodyNode, isTableNode, isTableRowNode} from '..';
44
import type {CommandWithAttrs} from '../../core';
55

66
export const appendRow: CommandWithAttrs<{
@@ -15,16 +15,20 @@ export const appendRow: CommandWithAttrs<{
1515
state.doc.resolve(tablePos + 1),
1616
isTableNode,
1717
)?.node;
18+
1819
if (!tableNode) return false;
20+
21+
const parentBody = findChildren(tableNode, isTableBodyNode, false).pop();
22+
if (!parentBody) return false;
23+
1924
let parentRow;
2025
if (rowNumber !== undefined) {
2126
parentRow = findChildTableRows(tableNode)[rowNumber];
2227
} else {
23-
parentRow = findChildTableRows(tableNode).pop();
28+
parentRow = findChildren(parentBody.node, isTableRowNode, false).pop();
2429
}
25-
const parentBody = findChildTableBody(tableNode).pop();
2630

27-
if (!parentRow || !parentBody) {
31+
if (!parentRow) {
2832
return false;
2933
}
3034

@@ -35,7 +39,7 @@ export const appendRow: CommandWithAttrs<{
3539
});
3640

3741
let position = tablePos + parentRow.pos;
38-
position += direction === 'before' ? 1 : parentRow.node.nodeSize;
42+
position += direction === 'before' ? 1 : parentRow.node.nodeSize + 2;
3943

4044
dispatch(state.tr.insert(position, parentRow.node.copy(Fragment.from(newCellNodes))));
4145
}

0 commit comments

Comments
 (0)