Repository navigation
[lexical-table] Bug Fix: Plain text pasted into a table selection made right to left or upwards starts at its top-left cell - #9774
Conversation
…e right to left or upwards starts at its top-left cell
## Description
Plain text pasted into a table selection (tab- and newline-separated values, as "paste and match style" delivers them) is spread over the cells by `TableSelection.insertRawText`, growing the table when the text has more rows or columns. It started at the selection's anchor, the cell the selection was started from. For a selection made from its right or bottom end the anchor is that end, so the values landed right of or below the selection, over cells it does not hold (their text was lost), and the table grew. For example, selecting three cells of a row from right to left and pasting `X⇥Y⇥Z` wrote over the selected right cell and the two cells after it.
Now a grid of values is laid over the selection's rectangle from its top-left cell (after padding a ragged table, as `$insertTableIntoGrid` does anyway), whichever corner the selection was made from, as it already was for a selection made from its top-left cell and as a pasted table is. Non-table content that the table's clipboard handler turns into such a grid (several paragraphs of HTML) is placed the same way. A single value, and a paste into a single selected cell, still go to the anchor cell. A rectangle that a merged cell stretches beyond both ends of the selection starts at the rectangle's top-left.
## Changeset
`.changeset/table-plain-paste-top-left.md` (`@lexical/table` patch, `[fix]`).
## Test plan
- `packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts` (new, 15 tests): a row, a column and a block selected from every corner; a selection made right to left starting on a merged cell; a selection made upwards in a ragged table; a rectangle a row span stretches upwards; a grid larger than the selection pasted whole; HTML without a table (one formatted word, several paragraphs); a single value and a single selected cell keep the anchor.
### Before
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
AssertionError: expected [ [ 'a', 'b', 'X', 'Y', 'Z' ], …(2) ] to deeply equal [ [ 'X', 'Y', 'Z', 'd' ], …(2) ]
AssertionError: expected [ [ 't0', 't1', 'X', 'Y', 'Z' ], …(1) ] to deeply equal [ [ 'X', 'Y', 'Z', '', 't3' ], …(1) ]
Tests 10 failed | 5 passed (15)
```
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
Tests 15 passed (15)
```
The `@lexical/table`, `@lexical/clipboard`, `lexical` and playground unit tests pass with no change from main. Fingerprints of every anchor/focus pair (556) on 11 table shapes (plain, merged cells, covered rows, ragged rows, cells with lists and nested tables), covering the table selection's nodes, text, all three clipboard formats, delete, backspace, cut, paste back of its own Lexical copy and of its own plain text: identical to main except plain-text pastes from selections made from a corner other than their top-left, plus 5 made from the top-left whose rectangle a merged cell stretches. WebKit, Chromium and Firefox e2e were not run for this change (the paths are not reached by the playground's e2e specs).
## Review
Independent review with gpt-6.1-sol over two rounds (the last clean); its first round found that single values pasted as HTML moved, which this keeps at the anchor.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
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: |
There was a problem hiding this comment.
This looks good to me. Thanks for the clear reproduction and test plan.
TableSelection.insertRawText now starts the pasted grid at the top-left cell of the selection's rectangle, so a selection made right to left or upwards no longer overwrites cells outside it or grows the table. The new TableSelectionPlainPasteStart.test.ts cases that cover selections made from other corners fail against main's implementation and pass with this change, and the cases that must not move (top-left selections, a single value, a single cell) pass on both. The changeset follows CONTRIBUTING.md: [fix], patch, and a Verify: paragraph.
Optional, non-blocking:
- For a selection made from a corner other than the top-left, the caret after the paste now lands in the top-left cell rather than the anchor cell (
packages/lexical-table/src/LexicalTableSelection.ts, line 271). One assertion on$getSelection()after a backward paste in the new test would pin that too.
… from the bottom right of a table selection
## Description
Adds a test to this pull request's plain-paste tests: after plain text is pasted into a table selection made from its bottom-right cell, the caret is collapsed at the start of the selection rectangle's top-left cell, where the pasted grid starts. Test-only; no source change.
## Test plan
### Before
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
× the caret lands at the start of the pasted grid after a paste from the bottom right
Tests 11 failed | 5 passed (16)
```
### After
```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts
Tests 16 passed (16)
```
|
The optional item from the review is taken in 9874461 (test only, no source change): |
There was a problem hiding this comment.
This looks good to me, thanks for the careful reproduction.
TableSelection.insertRawText now starts a pasted grid at the top-left cell of the selection's rectangle, so a selection made right to left or upwards no longer overwrites cells outside it or grows the table, matching how a pasted table is already placed. The new TableSelectionPlainPasteStart.test.ts cases for selections made from other corners fail against main and pass with this change, while the cases that must not move (a selection made from its top-left, a single value, a single selected cell) pass on both. The changeset follows CONTRIBUTING.md: @lexical/table as patch, a [fix] summary, and a Verify: paragraph.
Optional, non-blocking:
- The Test plan in the description still describes the first commit ("new, 15 tests", Before
10 failed | 5 passed (15), After15 passed (15)). With the caret test from 9874461 the file has 16 tests, 11 of which fail onmain, as your later comment says. AGENTS.md asks the Test plan to carry the real Before/After output, and the description usually becomes the squash commit message, so updating those three lines keeps the record accurate.
|
The Test plan in the description now matches the file at 9874461: 16 tests, 11 fail on main, 16 pass here. |
Description
Plain text pasted into a table selection (tab- and newline-separated values, as "paste and match style" delivers them) is spread over the cells by
TableSelection.insertRawText, growing the table when the text has more rows or columns. It started at the selection's anchor, the cell the selection was started from. For a selection made from its right or bottom end the anchor is that end, so the values landed right of or below the selection, over cells it does not hold (their text was lost), and the table grew. For example, selecting three cells of a row from right to left and pastingX⇥Y⇥Zwrote over the selected right cell and the two cells after it.Now a grid of values is laid over the selection's rectangle from its top-left cell (after padding a ragged table, as
$insertTableIntoGriddoes anyway), whichever corner the selection was made from, as it already was for a selection made from its top-left cell and as a pasted table is. Non-table content that the table's clipboard handler turns into such a grid (several paragraphs of HTML) is placed the same way. A single value, and a paste into a single selected cell, still go to the anchor cell. A rectangle that a merged cell stretches beyond both ends of the selection starts at the rectangle's top-left.Changeset
.changeset/table-plain-paste-top-left.md(@lexical/tablepatch,[fix]).Test plan
packages/lexical-table/src/__tests__/unit/TableSelectionPlainPasteStart.test.ts(new, 16 tests): a row, a column and a block selected from every corner; a selection made right to left starting on a merged cell; a selection made upwards in a ragged table; a rectangle a row span stretches upwards; a grid larger than the selection pasted whole; HTML without a table (one formatted word, several paragraphs); a single value and a single selected cell keep the anchor; the caret lands at the start of the pasted grid after a paste from the bottom right.Before
After
The
@lexical/table,@lexical/clipboard,lexicaland playground unit tests pass with no change from main. Fingerprints of every anchor/focus pair (556) on 11 table shapes (plain, merged cells, covered rows, ragged rows, cells with lists and nested tables), covering the table selection's nodes, text, all three clipboard formats, delete, backspace, cut, paste back of its own Lexical copy and of its own plain text: identical to main except plain-text pastes from selections made from a corner other than their top-left, plus 5 made from the top-left whose rectangle a merged cell stretches. WebKit, Chromium and Firefox e2e were not run for this change (the paths are not reached by the playground's e2e specs).Review
Independent review with gpt-6.1-sol over two rounds (the last clean); its first round found that single values pasted as HTML moved, which this keeps at the anchor.