Skip to content

fix(6763): copy and paste with newline at the beginning - #7184

Closed
ozanilkumar23 wants to merge 2 commits into
meta:mainfrom
ozanilkumar23:6763-copy-issue
Closed

ozanilkumar23 wants to merge 2 commits into
meta:mainfrom
ozanilkumar23:6763-copy-issue

Conversation

@ozanilkumar23

Copy link
Copy Markdown

[Lexical] : Fix duplicate content on copy and paste with newline at the beginning

Bug Fix

Description

  • When content with a newline at the start is copied and pasted again, it creates duplicate content, instead of replacing it by appending the text.
  • This PR will fix the duplicate text issue and it will replace the whole text.

Describe the changes in this pull request

This PR checks if the first block is empty and it has a newline char then it will call this.insertParagraph().

Closes #6763

Test plan

Before

There is no change in behavior due to the fix.

After

There is no change in behavior due to the fix.

@vercel

vercel Bot commented Feb 16, 2025 •

Copy link
Copy Markdown

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 Feb 17, 2025 3:28am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 17, 2025 3:28am

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Hi @ozanilkumar23!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does not pass the style checks (npm run ci-check; the prettier formatting and the lint against using the ?. operator), does not have any new tests to confirm that it is solving the issue, and probably fails other tests.

I did already fix this particular issue more systematically in #7155, which is waiting for review

(!firstBlock.isEmpty() || firstBlock.canMergeWhenEmpty());

const shouldInsert = !$isElementNode(firstBlock) || !firstBlock.isEmpty();
let isFirstBlockEmpty = (firstBlock as ElementNode)?.isEmpty();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The INTERNAL_$isBlock guard will also return true for a top-level decorator nodes, so casting here is not safe

let isFirstBlockEmpty = (firstBlock as ElementNode)?.isEmpty();
// if first block is empty check its has newline chars
if(isFirstBlockEmpty) {
const firstBlockParentContent = firstBlock?.getParent()?.getTextContent()?.split('\n') || [];

@etrepum etrepum Feb 16, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a very inefficient approach to solving this problem, as this could generally produce the textContent of the entire document just to look for a LineBreakNode. I also generally don't think it works because newlines are produced by any block ElementNode here so this would also be triggered by an empty paragraph block before the firstBlock in this selection.

edit: of course this is looking specifically for an empty paragraph, mistake in my comment above

This branch was successfully deployed

2 active deployments
Preview – lexical — 7960a855 Deployed Feb 17, 2025 by vercel[bot]
Preview – lexical-playground — 7960a855 Deployed Feb 17, 2025 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Pasting over content starting with an empty node appends instead of replace

4 participants