Repository navigation
[lexical-table] Bug Fix: A table selection in a ragged table no longer throws, and deleting or pasting it keeps the cells it does not hold #9749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+3,253
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@lexical/table': patch | ||
| --- | ||
|
|
||
| [fix] A table selection in a table whose rows have different numbers of cells no longer throws (`Cannot destructure property 'cell' of 'map[i][j]'`), and deleting, copying or pasting it keeps the cells it does not hold | ||
|
|
||
| Such a ragged table can stay that way after collaborators insert a row and a column at the same time, or after loading JSON that has it. `TableSelection.getNodes()`, aligning a table selection and exporting a table to HTML now skip the grid positions that no cell covers, and a selected row with no cell of its own stays in the copy (a blank line in its plain text) when a later selected row has one, so rows keep their places when it is pasted. Deleting a table selection removes the table only when it holds every cell of the table, not whenever it holds the first and last cells, which also stops a rectangular table from being removed when a cell spanning rows in another column is not selected. A selection with a short first row holds a merged cell further right whole. Pasting a copied ragged selection takes the widest row and leaves a target cell at a position the pasted table has no cell for as it is. | ||
|
|
||
| Verify: in a table with rows of 2, 1, 3 and 2 cells, select from the first cell to the last, press Delete and check that the third cell of the third row keeps its text; copy and paste the selection into another table and check that nothing throws and the rows keep their places. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
packages/lexical-table/src/__tests__/unit/RaggedTableSelection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| */ | ||
|
|
||
| import {$getClipboardDataFromSelection} from '@lexical/clipboard'; | ||
| import {buildEditorFromExtensions} from '@lexical/extension'; | ||
| import { | ||
| $createTableCellNode, | ||
| $createTableNode, | ||
| $createTableRowNode, | ||
| $createTableSelection, | ||
| $isTableCellNode, | ||
| $isTableNode, | ||
| $padTableRows, | ||
| TableExtension, | ||
| type TableNode, | ||
| } from '@lexical/table'; | ||
| import { | ||
| $createParagraphNode, | ||
| $createTextNode, | ||
| $getRoot, | ||
| $getSelection, | ||
| $setSelection, | ||
| defineExtension, | ||
| type ElementNode, | ||
| FORMAT_ELEMENT_COMMAND, | ||
| type LexicalEditor, | ||
| } from 'lexical'; | ||
| import {describe, expect, test} from 'vitest'; | ||
|
|
||
| // A table whose rows have different lengths, as a merge of concurrent row and | ||
| // column inserts by collaborators can leave it (remote updates skip the table | ||
| // transform, and the transform leaves rows it did not change alone). Cells | ||
| // are named `r<row>c<index>`; `rowSpan` cells are `[text, rowSpan]`. | ||
| type Spec = (string | [string, number])[][]; | ||
|
|
||
| function buildEditor(rows: Spec) { | ||
| const built = buildEditorFromExtensions( | ||
| defineExtension({ | ||
| afterRegistration(editor) { | ||
| // The table selection observer, which handles FORMAT_ELEMENT_COMMAND, | ||
| // needs the editor to have a root element. | ||
| const container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| editor.setRootElement(container); | ||
| return () => document.body.removeChild(container); | ||
| }, | ||
| dependencies: [TableExtension], | ||
| name: '[ragged-table-selection]', | ||
| onError: (error: Error) => { | ||
| throw error; | ||
| }, | ||
| }), | ||
| ); | ||
| built.update( | ||
| () => { | ||
| const table = $createTableNode(); | ||
| for (const cells of rows) { | ||
| const row = $createTableRowNode(); | ||
| for (const spec of cells) { | ||
| const [text, rowSpan] = typeof spec === 'string' ? [spec, 1] : spec; | ||
| row.append( | ||
| $createTableCellNode() | ||
| .setRowSpan(rowSpan) | ||
| .append($createParagraphNode().append($createTextNode(text))), | ||
| ); | ||
| } | ||
| table.append(row); | ||
| } | ||
| $getRoot().clear().append(table, $createParagraphNode()); | ||
| }, | ||
| // As a remote update applies it: without the table transform. | ||
| {discrete: true, skipTransforms: true}, | ||
| ); | ||
| return built; | ||
| } | ||
|
|
||
| const $table = () => $getRoot().getChildren().find($isTableNode) as TableNode; | ||
| const $cellNamed = (name: string) => | ||
| $table() | ||
| .getAllTextNodes() | ||
| .find(node => node.getTextContent() === name)! | ||
| .getParentOrThrow() | ||
| .getParentOrThrow(); | ||
| const names = (rows: Spec) => | ||
| rows.flat().map(spec => (typeof spec === 'string' ? spec : spec[0])); | ||
|
|
||
| // The texts of the cells a table selection from `anchor` to `focus` holds. | ||
| function selectedCells( | ||
| editor: LexicalEditor, | ||
| anchor: string, | ||
| focus: string, | ||
| ): string[] { | ||
| let cells: string[] = []; | ||
| editor.update( | ||
| () => { | ||
| const selection = $createTableSelection(); | ||
| selection.set( | ||
| $table().getKey(), | ||
| $cellNamed(anchor).getKey(), | ||
| $cellNamed(focus).getKey(), | ||
| ); | ||
| $setSelection(selection); | ||
| const tableSelection = $getSelection()!; | ||
| cells = tableSelection | ||
| .getNodes() | ||
| .filter($isTableCellNode) | ||
| .map(cell => cell.getTextContent()); | ||
| // Copying it reads the same nodes. | ||
| const copied = $getClipboardDataFromSelection(tableSelection); | ||
| expect(Object.keys(copied).length).toBeGreaterThan(0); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| return cells; | ||
| } | ||
|
|
||
| const SHAPES: [string, Spec][] = [ | ||
| [ | ||
| 'a short middle row', | ||
| [ | ||
| ['r0c0', 'r0c1', 'r0c2', 'r0c3'], | ||
| ['r1c0', 'r1c1'], | ||
| ['r2c0', 'r2c1', 'r2c2', 'r2c3'], | ||
| ], | ||
| ], | ||
| [ | ||
| 'a short first row', | ||
| [ | ||
| ['r0c0', 'r0c1'], | ||
| ['r1c0', 'r1c1', 'r1c2', 'r1c3'], | ||
| ['r2c0', 'r2c1', 'r2c2'], | ||
| ], | ||
| ], | ||
| [ | ||
| 'a short last row under a row span', | ||
| [['r0c0', ['r0c1', 2], 'r0c2'], ['r1c0', 'r1c1'], ['r2c0']], | ||
| ], | ||
| ]; | ||
|
|
||
| describe('a table selection in a ragged table', () => { | ||
| for (const [label, rows] of SHAPES) { | ||
| test(`holds the cells it holds once the table is padded: ${label}`, () => { | ||
| const ragged = buildEditor(rows); | ||
| const padded = buildEditor(rows); | ||
| padded.update(() => $padTableRows($table()), {discrete: true}); | ||
| for (const anchor of names(rows)) { | ||
| for (const focus of names(rows)) { | ||
| const expected = selectedCells(padded, anchor, focus).filter( | ||
| text => text !== '', | ||
| ); | ||
| expect(selectedCells(ragged, anchor, focus)).toEqual(expected); | ||
| } | ||
| } | ||
| ragged.dispose(); | ||
| padded.dispose(); | ||
| }); | ||
|
|
||
| test(`aligns every cell it holds: ${label}`, () => { | ||
| for (const anchor of names(rows)) { | ||
| for (const focus of names(rows)) { | ||
| const editor = buildEditor(rows); | ||
| const cells = selectedCells(editor, anchor, focus); | ||
| editor.update( | ||
| () => { | ||
| editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'center'); | ||
| }, | ||
| {discrete: true}, | ||
| ); | ||
| const centered = editor.read(() => | ||
| $table() | ||
| .getAllTextNodes() | ||
| .filter(node => { | ||
| const paragraph = node.getParentOrThrow(); | ||
| const cell = paragraph.getParentOrThrow() as ElementNode; | ||
| // The cell and the paragraph in it are both aligned. | ||
| return ( | ||
| cell.getFormatType() === 'center' && | ||
| paragraph.getFormatType() === 'center' | ||
| ); | ||
| }) | ||
| .map(node => node.getTextContent()), | ||
| ); | ||
| if (editor.read(() => $table().getFormatType()) === 'center') { | ||
| // A selection spanning the table's first row and every row | ||
| // formats the table itself. | ||
| expect(centered).toEqual([]); | ||
| } else { | ||
| expect(centered.sort()).toEqual([...cells].sort()); | ||
| } | ||
| editor.dispose(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional, non-blocking: when the last row the selection spans has only a hole and row-spanned cells in the rectangle, this row is added after every cell, so
getNodes()ends on aTableRowNode.$insertNodeToNearestRootstarts from the last node (packages/lexical-utils/src/index.ts:750-753), and the node it inserts never reaches the document. Repro:[['OUT', A (rowSpan 2), 'B'], ['OUT2']], selectA→B, insert a paragraph.mainthrew here, so this is not a regression, but a follow-up could keep the last node inside a cell or give$insertNodeToNearestRoota focus-cell path forTableSelection.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not worse than main: there the same selection throws in
getNodes(), so the insert is lost with an error. The trailing row is kept on purpose (a copy needs it so that a pasted row span is not cut short). Filed as #9756 to fix in$insertNodeToNearestRootorgetNodes()separately; listed under still open in the description.