Skip to content

Commit

Permalink
Fix deleteRow for cells with colspan greater than 1 (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
weijia18 committed Nov 28, 2023
1 parent b569c2f commit 0b47a0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,15 @@ export function removeRow(
const mapFrom = tr.mapping.maps.length;
tr.delete(rowPos + tableStart, nextRow + tableStart);

const seen = new Set<number>();

for (let col = 0, index = row * map.width; col < map.width; col++, index++) {
const pos = map.map[index];

// Skip cells that are checked already
if (seen.has(pos)) continue;
seen.add(pos);

if (row > 0 && pos == map.map[index - map.width]) {
// If this cell starts in the row above, simply reduce its rowspan
const attrs = table.nodeAt(pos)!.attrs as CellAttrs;
Expand Down
19 changes: 19 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ describe('deleteRow', () => {
table(tr(c11, cEmpty)),
));

it('moves the same cell with colspan greater than 1 that start in the deleted row only once', () =>
test(
table(tr(c(3, 2), c11, c(2, 2), cCursor), tr(c11, cEmpty)),
deleteRow,
table(tr(c(3, 1), c11, c(2, 1), cEmpty)),
));

it('deletes multiple rows when the start cell has a rowspan', () =>
test(
table(
Expand All @@ -437,6 +444,18 @@ describe('deleteRow', () => {
table(tr(c11, c11)),
));

it('moves the same cell with colspan greater than 1 that start in the deleted row only once when deleting multiple rows', () =>
test(
table(
tr(c(2, 4), td({ rowspan: 3 }, p('<cursor>')), c11),
tr(c11),
tr(c11),
tr(c11, c11),
),
deleteRow,
table(tr(c(2, 1), c11, c11)),
));

it('skips columns when adjusting rowspan', () =>
test(
table(tr(cCursor, c(2, 2)), tr(c11)),
Expand Down

0 comments on commit 0b47a0f

Please sign in to comment.