Repository navigation
[lexical-table] Bug Fix: Leave a cut with neither end in the table to the editor - #9285
rinarakaki wants to merge 3 commits into
Conversation
… the editor ## Description Each table registers a `CUT_COMMAND` listener at `COMMAND_PRIORITY_HIGH` in `applyTableHandlers`. For any `RangeSelection` in the document it copied the range, called `selection.removeText()` and returned `true`, without checking that the range was in its table. So once a document held a table, no cut listener below HIGH ran for any range: - Rich text's own cut, which widens a whole-document range before removing it (meta#5835) and tags its updates `CUT_TAG` (meta#8649). A select-all cut over `h1 / table / p` left an empty heading, where the same document without the table leaves a paragraph. - Every NORMAL or LOW cut listener, such as a plugin that guards cuts (meta#7933). The listener now returns `false` for a range with neither end in its table, so the range goes on to the editor's own cut, as it would with no table in the document. The table still answers a table selection, and a range with an end in the table, which `$deleteCellHandler` widens around the table first. meta#6590 stays fixed: the table no longer copies such a range before the editor does, so it is copied once, and the clipboard gets its text. "Cut table in the middle of a range selection" relied on the table cutting a range with both ends outside it, in an editor with only `TablePlugin`. That range is now the editor's to cut, so the test registers rich text and waits for rich text's cut, which removes the range once its copy resolves. Its expected HTML is unchanged. Closes meta#9281 ## Test plan ### Before The new `TableCutScope.test.ts`, with the fix reverted: ``` $ pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts × select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true) × a range outside the table is copied once and cut by the editor (withTable: true) AssertionError: expected [ [ 'heading', [] ] ] to deeply equal [ [ 'paragraph', [] ] ] AssertionError: expected false to be true // Object.is equality Test Files 1 failed (1) Tests 2 failed | 5 passed (7) ``` ### After ``` $ pnpm exec vitest --project unit --no-watch --reporter=verbose packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: false) ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true) ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range outside the table is copied once and cut by the editor (withTable: false) ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range outside the table is copied once and cut by the editor (withTable: true) ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range in a cell is the table’s ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a table selection is the table’s ✓ |unit| packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts > the cuts a table answers (meta#9281) > a range from outside the table into a cell is the table’s Test Files 1 passed (1) Tests 7 passed (7) ``` ``` $ pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts packages/lexical-table/src/__tests__/unit/LexicalTableNode.test.tsx Test Files 2 passed (2) Tests 49 passed (49) ``` ``` $ pnpm run test-unit Test Files 370 passed (370) Tests 9018 passed | 1 skipped (9019) ``` ``` $ pnpm exec vitest --project browser --no-watch packages/lexical-table Test Files 4 passed (4) Tests 18 passed (18) ``` `pnpm run tsc` and `pnpm run tsc-test` pass, and prettier and eslint pass on the changed files. The browser tests ran in Chromium only. The e2e suite wasn't run; its one table cut test (`Tables.spec.mjs`, "cut them with the context menu") cuts a table selection, which stays the table's. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Thanks for the clear issue and reproducer. This looks good to me.
The table's CUT_COMMAND listener now handles only ranges that touch its own table (the new guard in packages/lexical-table/src/LexicalTableSelectionHelpers.ts, lines 904–910). A select-all cut over a heading, a table and a paragraph now goes through rich text's own cut: it leaves an empty paragraph, the update is tagged cut, and lower-priority cut listeners run. Table selections, ranges inside a cell, and ranges from outside the table into a cell behave as before. The new withTable: true cases fail before the fix and pass after it.
Optional, non-blocking suggestions:
-
Ranges with one end in the table (the open review comment). Such a range still takes the table's own path (
LexicalTableSelectionHelpers.ts, lines 913–922): it is widened around the table, its text is removed, and the listener returnstrue. So select all and cut on a document that starts or ends with a table, such as[heading, table], still leaves an empty heading, with nocuttag and no lower-priority listener call. Widening the range and then returningfalse, as suggested, would send both cases through rich text. It would also mean these changes:- flipping the
lowListenerRanexpectation atTableCutScope.test.ts:247; - giving the
TablePlugin-only tests "Cut table as last node in range selection" and "Cut table as first node in range selection" inLexicalTableNode.test.tsxthe same rich-text setup you gave "Cut table in the middle of a range selection"; - a range from a cell in one table into a cell in another would then remove both tables.
Would you like to handle this here, or in a follow-up issue? Since this PR closes #9281, a follow-up issue would keep it tracked.
- flipping the
-
Test title.
a range outside the table is copied once and cut by the editor(TableCutScope.test.ts:153) checks what was copied, not how many times; it would also pass with two identical clipboard writes. Renaming it, or counting the writes, would make the title match what it checks. -
History boundary. The issue cites the
CUT_TAGhistory boundary (#8609). An update listener assertingtags.has(CUT_TAG)would pin that boundary directly.
…he whole table, and leave it to the editor ## Description Follow-up to the review. A range with one end in a table and the other outside it still took the table's own cut path: the table copied the range as selected, widened it around the table, removed its text and returned `true`. So the clipboard didn't hold the whole table that went, the update had no `cut` tag, and lower-priority cut listeners didn't run. Select all on a document that starts or ends with a table, then cut, left an empty heading that way. The table's `CUT_COMMAND` listener now widens such a range over the whole table, as its delete listener already does, and returns `false`, so rich text's cut copies and removes the widened range with its `cut` tag. The widening is one function, `$selectAroundTable`, which both listeners call. The table keeps a table selection and a range inside it. A range from a cell of one table into a cell of another takes both tables, since each table widens its own end. Tests: - `TableCutScope.test.ts`: select all over a document that starts or ends with a table leaves an empty paragraph; a range from before the table into a cell, and from a cell to after the table, is cut by the editor with the whole table on the clipboard; a range across two tables takes both. `cut()` now counts text/plain writes, so "copied once" checks once, and reports whether an update carried `CUT_TAG`, the history boundary from meta#8609. - `LexicalTableNode.test.tsx`: "Cut table as last node in range selection" and "Cut table as first node in range selection" register rich text, like "Cut table in the middle of a range selection", since the editor now does that cut. ## Changeset `.changeset/table-cut-outside-table.md` ## Test plan ### Before `pnpm exec vitest --project unit --no-watch packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts`, with this PR's previous `LexicalTableSelectionHelpers.ts`: ``` × select all over a heading and a table leaves an empty paragraph × select all over a table and a paragraph leaves an empty paragraph × a range from before the table into a cell takes the whole table, and the editor cuts it × a range from a cell to after the table takes the whole table, and the editor cuts it × a range from a cell of one table into a cell of another takes both tables AssertionError: expected [ [ 'heading', [] ] ] to deeply equal [ [ 'paragraph', [] ] ] AssertionError: expected false to be true // Object.is equality AssertionError: expected false to be true // Object.is equality AssertionError: expected [ …(3) ] to deeply equal [ Array(1) ] Test Files 1 failed (1) Tests 5 failed | 6 passed (11) ``` With `main`'s: ``` × select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true) × select all over a heading and a table leaves an empty paragraph × select all over a table and a paragraph leaves an empty paragraph × a range outside the table is copied once and cut by the editor (withTable: true) × a range from before the table into a cell takes the whole table, and the editor cuts it × a range from a cell to after the table takes the whole table, and the editor cuts it × a range from a cell of one table into a cell of another takes both tables Test Files 1 failed (1) Tests 7 failed | 4 passed (11) ``` ### After ``` ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: false) ✓ select all over a heading, a table and a paragraph leaves an empty paragraph (withTable: true) ✓ select all over a heading and a table leaves an empty paragraph ✓ select all over a table and a paragraph leaves an empty paragraph ✓ a range outside the table is copied once and cut by the editor (withTable: false) ✓ a range outside the table is copied once and cut by the editor (withTable: true) ✓ a range in a cell is the table’s ✓ a table selection is the table’s ✓ a range from before the table into a cell takes the whole table, and the editor cuts it ✓ a range from a cell to after the table takes the whole table, and the editor cuts it ✓ a range from a cell of one table into a cell of another takes both tables Test Files 1 passed (1) Tests 11 passed (11) ``` `pnpm exec vitest --project unit --no-watch packages/lexical-table/ packages/lexical-rich-text/ packages/lexical-clipboard/ packages/lexical-playground/ packages/lexical-react/`: ``` Test Files 151 passed (151) Tests 1473 passed | 9 expected fail (1482) ``` `VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical-table/ packages/lexical-playground/src/__tests__/browser/TablesHTMLCopyAndPaste.test.ts packages/lexical-playground/src/__tests__/browser/HTMLCopyAndPaste.test.ts`: ``` Test Files 26 passed | 1 skipped (27) Tests 204 passed | 3 skipped (207) ``` `pnpm run tsc` and `pnpm run check-changesets` pass, and ESLint and Prettier are clean on the changed files. The E2E suite wasn't run; its table cut (`Tables.spec.mjs`, a context-menu cut of a table selection) takes the path this leaves unchanged.
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: |
|
Thanks, both. I merged
The description now describes the change as it stands, with the test runs. |
There was a problem hiding this comment.
Thanks for the fix, and for widening a range with one end in the table as suggested in review. This looks good to me.
The table's CUT_COMMAND listener now handles only what belongs to its table: a table selection, or a range inside one table. Every other range goes to the editor's own cut. A select-all cut over heading / table / paragraph therefore leaves an empty paragraph, as the same document without a table does, lower-priority cut listeners run, and the cut gets its own CUT_TAG history entry. Reusing the Backspace/Delete widening through $selectAroundTable keeps the two paths consistent. The two-table case (TableCutScope.test.ts:327) is a good addition too: on main the second table is left partly cut. The earlier note about counting clipboard writes is addressed by the plainTextWrites assertions.
Optional, non-blocking:
- Refresh the PR description and test plan. The Description still says the table "still answers ... a range with an end in the table", and the After output lists 7 tests, including "a range from outside the table into a cell is the table's". Since 5b2163c, such a range is widened and handed to the editor (
LexicalTableSelectionHelpers.ts:985-991), andTableCutScope.test.tshas 11 cases. AGENTS.md ("Commits and Pull Requests") asks the Test plan to carry the real Before and After output of the command that was run, so re-pasting it from this head would keep the record accurate.
The changeset follows CONTRIBUTING.md, including the Verify: paragraph a behavior change needs.
|
The description was refreshed after this review started: it describes the change at 5b2163c and pastes the Before and After of all 11 cases in |
Description
Each table registers a
CUT_COMMANDlistener atCOMMAND_PRIORITY_HIGHinapplyTableHandlers. For anyRangeSelectionin the document it copied the range, calledselection.removeText()and returnedtrue, without checking that the range was in its table. So once a document held a table, no cut listener below HIGH ran for any range:CUT_TAG([lexical][lexical-history][lexical-rich-text][lexical-plain-text] Feature: Snapshot history before cut/paste operations #8649). A select-all cut overh1 / table / pleft an empty heading, where the same document without the table leaves a paragraph.The listener now takes only the cuts that are wholly its table's: a table selection, and a range with both ends in the table. Any other range goes on to the editor's own cut, as it would with no table in the document. A range with one end in the table is first widened over the whole table, as the table's delete listener already does, so the editor copies the whole table it removes and tags the cut. That widening is one function,
$selectAroundTable, which both listeners call. A range from a cell of one table into a cell of another takes both tables, since each table widens its own end.#6590 stays fixed: the table no longer copies such a range before the editor does, so it is copied once, and the clipboard gets its text.
"Cut table in the middle of a range selection", "Cut table as last node in range selection" and "Cut table as first node in range selection" relied on the table cutting ranges with an end outside it, in an editor with only
TablePlugin. Those ranges are now the editor's to cut, so the tests register rich text and wait for rich text's cut, which removes the range once its copy resolves. Their expected HTML is unchanged.Closes #9281
Changeset
.changeset/table-cut-outside-table.mdTest plan
TableCutScope.test.tscuts with aClipboardEventand reports the text/plain copied, how many times it was written, whether a LOW-priority cut listener ran, and whether an update carriedCUT_TAG.Before
pnpm exec vitest --project unit --no-watch --reporter=verbose packages/lexical-table/src/__tests__/unit/TableCutScope.test.ts(describe prefix trimmed), withmain'sLexicalTableSelectionHelpers.ts:With the listener as this PR first had it, which left a range with one end in the table to the table:
After
pnpm exec vitest --project unit --no-watch packages/lexical-table/ packages/lexical-rich-text/ packages/lexical-clipboard/ packages/lexical-playground/ packages/lexical-react/:VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest --project browser --no-watch packages/lexical-table/ packages/lexical-playground/src/__tests__/browser/TablesHTMLCopyAndPaste.test.ts packages/lexical-playground/src/__tests__/browser/HTMLCopyAndPaste.test.ts:pnpm run tscandpnpm run check-changesetspass, and Prettier and ESLint are clean on the changed files. The E2E suite wasn't run; its one table cut test (Tables.spec.mjs, "cut them with the context menu") cuts a table selection, which stays the table's. macOS and Windows weren't run.