Repository navigation
[lexical-table] Bug Fix: Deleting a row no longer drops a merged cell changed earlier in the same update - #9851
Conversation
… changed earlier in the same update
## Description
`$deleteTableRowAtSelection` trims the row span of a merged cell that reaches past the deleted rows, and moves the cell into the next row when the deleted rows include its first row. It read `cell.__rowSpan` again after `cell.setRowSpan(...)`, and a cell spanning several deleted rows was visited once per row.
When the cell is not yet writable in the update, `setRowSpan` clones it, so `cell` keeps the old span and this works. When the cell is already writable (for example `$mergeCells` and `$deleteTableRowAtSelection` called in one `editor.update`), `setRowSpan` changes it in place:
- Deleting the first row of a 2-row merged cell skips the move, so the merged cell and its text are removed with the row. With `TableExtension`, the row is padded with empty cells.
- Deleting two middle rows of a 4-row merged cell trims the span twice (4 -> 2 -> 1), leaving the row below one cell short.
Each cell is now handled once, from the span it had before the delete. Deleting rows in their own update, as the table menu does, already worked and is unchanged. `$deleteTableColumnAtSelection` reads the span before changing it and does not have this problem.
## Test plan
New `DeleteRowMergedCellSameUpdate.test.ts` runs three cases (2-row merge, 2x2 merge, 4-row merge with two rows deleted) with the merge and the delete in separate updates and in the same update.
`pnpm exec vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRowMergedCellSameUpdate.test.ts`
### Before
```
× deleting the top row of a 2-row merged …, in the same update as the merge
× deleting the top row of a 2x2 merged ce…, in the same update as the merge
× deleting two middle rows of a 4-row mer…, in the same update as the merge
Tests 3 failed | 3 passed (6)
- Expected
+ Received
[
- "A2 | B1 B2",
+ "A2",
]
[
- "A2 | B1 C1 B2 C2 cs2",
+ "A2 | | ",
"A3 | B3 | C3",
]
[
- "A1 | B1 B2 B3 B4 rs2",
- "A4",
+ "A1 | B1 B2 B3 B4",
+ "A4 | ",
]
```
### After
```
Tests 6 passed (6)
```
`pnpm exec vitest run --project unit packages/lexical-table`: 38 files passed, 395 passed | 9 expected fail. `pnpm run tsc`, ESLint and Prettier on the changed files, and `pnpm run check-changesets` pass. Flow, browser and e2e tests were not run; the change has no API or DOM impact.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks for the fix, this looks good to me. Reading __rowSpan once before setRowSpan and handling each cell once in $deleteTableRowAtSelection (packages/lexical-table/src/LexicalTableUtils.ts:669-706) gives the same result the separate-update path already produced, and the new DeleteRowMergedCellSameUpdate.test.ts fails on the old implementation with exactly the output in your Before block and passes with the change. The changeset follows .changeset/README.md, including the Verify: paragraph AGENTS.md asks for on a fix that changes observable output.
One optional, non-blocking suggestion:
- The same pattern is still in the sibling commands. The description says
$deleteTableColumnAtSelectiondoes not have this problem, but its loop visits a row-spanning cell once per row and re-readscell.__colSpaneach time (LexicalTableUtils.ts:783-806). Merging B1:C2 in a 3x3 table and deleting column B (selection in B3) in the sameeditor.updateremoves the merged cell and its text through theelsebranch at line 803; with an A1:C2 merge in a 3x4 table the span is trimmed twice (colSpan 1 instead of 2).$insertTableRowAtNode(setRowSpan(cell.__rowSpan + 1)at lines 345 and 374, once per spanned column) and$insertTableColumnAtNode(setColSpan(currentCell.__colSpan + 1)at line 590, once per spanned row) over-grow a merged cell the same way: rowSpan 4 instead of 3 and colSpan 4 instead of 3 after merging B1:C2 in the same update. These behave the same onmain, so this PR does not cause them and a follow-up is fine; please drop the sentence about$deleteTableColumnAtSelectionfrom the description, or extend the same read-once, once-per-cell fix to these three commands here.
|
Hi, could a maintainer approve the workflow runs on this one when you have a moment? The Lexical Tests runs are still waiting for approval because this is my first PR here. The fix is in |
|
Thanks for the approval, @potatowagon! @zurfyx @acywatson @ivailop7 @etrepum, the workflow runs on this PR (Lexical Tests, Extended, Changesets and others) are still waiting for approval because it's my first PR here, so no checks have run yet. Could one of you approve them when you have a moment? The fix is in |
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
Description
$deleteTableRowAtSelectiontrims the row span of a merged cell that reaches past the deleted rows, and moves the cell into the next row when the deleted rows include its first row. It readcell.__rowSpanagain aftercell.setRowSpan(...), and a cell spanning several deleted rows was visited once per row.When the cell is not yet writable in the update,
setRowSpanclones it, socellkeeps the old span and this works. When the cell is already writable (for example$mergeCellsand$deleteTableRowAtSelectioncalled in oneeditor.update),setRowSpanchanges it in place:TableExtension, the row is padded with empty cells.Each cell is now handled once, from the span it had before the delete. Deleting rows in their own update, as the table menu does, already worked and is unchanged.
$deleteTableColumnAtSelectionreads the span before changing it and does not have this problem.Changeset
.changeset/table-delete-row-merged-cell-same-update.md(@lexical/tablepatch,[fix]with aVerify:paragraph).Test plan
New
DeleteRowMergedCellSameUpdate.test.tsruns three cases (2-row merge, 2x2 merge, 4-row merge with two rows deleted) with the merge and the delete in separate updates and in the same update.pnpm exec vitest run --project unit packages/lexical-table/src/__tests__/unit/DeleteRowMergedCellSameUpdate.test.tsBefore
After
pnpm exec vitest run --project unit packages/lexical-table: 38 files passed, 395 passed | 9 expected fail.pnpm run tsc, ESLint and Prettier on the changed files, andpnpm run check-changesetspass. Flow, browser and e2e tests were not run; the change has no API or DOM impact.