Skip to content
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

[lexical-react]: Fix incorrect addition of empty cells on table paste #6578

Merged
merged 4 commits into from
Sep 1, 2024

Conversation

Shubhankerism
Copy link
Collaborator

This PR fixes the issue with empty cells getting incorrectly added to a table upon paste.

Before

When a table with rows with unequal sizes and merged cells is pasted, we add the empty cells in the wrong row.
For example, a table like this:
image

table-transform-before

After

Empty cells are now added to the correct row.

table-transform-after

Copy link

vercel bot commented Aug 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 31, 2024 8:58am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 31, 2024 8:58am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 30, 2024
Copy link

github-actions bot commented Aug 30, 2024

size-limit report 📦

Path Size
lexical - cjs 29.38 KB (0%)
lexical - esm 29.22 KB (0%)
@lexical/rich-text - cjs 37.87 KB (0%)
@lexical/rich-text - esm 31.08 KB (0%)
@lexical/plain-text - cjs 36.45 KB (0%)
@lexical/plain-text - esm 28.44 KB (0%)
@lexical/react - cjs 39.64 KB (0%)
@lexical/react - esm 32.52 KB (0%)

@ivailop7
Copy link
Collaborator

Fantastic! Well done! Please add a test for this copy-paste use case, there are quite a few to take inspiration from, so it should be easy to add one.

Copy link
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to see some tests with various merged cell conditions, I'm not familiar enough with this code to judge if it's a general fix or only handles the cases you ran into. The indexing seems maybe a bit suspicious (e.g. if there are merges in the middle of the table), but I didn't review $computeTableMapSkipCellCheck

Comment on lines 91 to 92
const row = gridMap[i].filter((cell) => cell);
const rowLength = row.length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're not using this array it would be more efficient to calculate it directly

Suggested change
const row = gridMap[i].filter((cell) => cell);
const rowLength = row.length;
const rowLength = gridMap[i].reduce((acc, cell) => cell ? 1 + acc : acc, 0);

if (rowLength === maxRowLength) {
continue;
}
const lastCellMap = gridMap[i][rowLength - 1];
const lastRowCell = lastCellMap.cell;
const rowNode: TableRowNode | null = node.getChildAtIndex(i);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more efficient to compute node.getChildren() outside of the loop and index it directly down here since each getChildAtIndex is a linked list traversal in a loop this ends up being geometric instead of linear.

Could also do the if (rowNode) here and continue or wrap it around the loop so you can skip the loop if it is merged.

@ivailop7
Copy link
Collaborator

ivailop7 commented Sep 1, 2024

@etrepum good with this one?

@ivailop7 ivailop7 added this pull request to the merge queue Sep 1, 2024
Merged via the queue into facebook:main with commit f07f0d6 Sep 1, 2024
44 checks passed
@Shubhankerism Shubhankerism deleted the table-transform-fix branch September 2, 2024 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants