lexical: Bug: getTextContent Removes a New Line on Rich Text Editor

Lexical version: 0.8.0

Steps To Reproduce

Recorded a video

Basically the issue is that in rootNode.getTextContent() there’s a point where two of the same exact editor states result in two different text contents

Seems to be easy to reproduce by

1. Having at least two lines with text

Row 1
Row 2

The text content is Row1\n\nRow2

2. Add a new line between them

Row 1

Row 2

The text content is Row1\n\n\n\nRow2

3. On the 2nd line type anything

Row 1

dRow 2

The text content is Row1\n\ndRow2

Where did the two \n’s go?

Link to code example:

CodeSandbox

The current behavior

It’s possible to get two different text contents for the same editor state and it seems to remove some new lines

The expected behavior

An editor state should always return the same text content

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (2 by maintainers)

Most upvoted comments

This still repros in 0.11.1

useEffect(() => {
        return editor.registerCommand(
            KEY_ENTER_COMMAND,
            event => {
                if (event?.key == 'Enter') {
                    event?.preventDefault()
                    editor.dispatchCommand(INSERT_LINE_BREAK_COMMAND, false)
                    return true
                }
                return false
            },
            COMMAND_PRIORITY_HIGH
        )
    }, [editor, onSubmit, withShift])

I think we can take over Enter to fix this problem…

@etrepum i wish this issue wasn’t closed, because it is still a problem over here. The @jackple code did fix the issue, and I think it shows 2 things:

  1. The issue is real and reproduzable
  2. The fix should be something native from the library, since having two \n markers when it should be a single one is a bug in my opinion. If we need to take over the enter command and override the event function for it to work as intended, something is wrong.
useEffect(() => {
        return editor.registerCommand(
            KEY_ENTER_COMMAND,
            event => {
                if (event?.key == 'Enter') {
                    event?.preventDefault()
                    editor.dispatchCommand(INSERT_LINE_BREAK_COMMAND, false)
                    return true
                }
                return false
            },
            COMMAND_PRIORITY_HIGH
        )
    }, [editor, onSubmit, withShift])

I think we can take over Enter to fix this problem…

Still there in 0.12.4

Not sure if it helps but you can probably fork the example I made CodeSandbox and just replace RichTextPlugin with PlainTextPlugin