Skip to content

Question: Inserting a New Line. #3366

Closed

Description

@nrikiji

Is it possible to insert a line that preserves the status of the previous line when a new line is inserted with the enter key? If you have a sample code, could you please let me know?

That is not the behavior in Lexical's Playground. Also, the Draft.js demo has that behavior.

Activity

  1. zignis commented on Nov 21, 2022

    @zignis
    Contributor

    Is it possible to insert a line that preserves the status of the previous line when a new line is inserted with the enter key? If you have a sample code, could you please let me know?

    That is not the behavior in Lexical's Playground. Also, the Draft.js demo has that behavior.

    You can try line break node or $createTextNode with $createParagraphNode (kind of hack):

    import {
      $createTextNode,
      $createParagraphNode,
      $insertNodes,
    } from 'lexical';
    
    ...
    
    const textNode = $createTextNode('\n');
    const paragraphNode = $createParagraphNode();
    paragraphNode.append(textNode);
    
    $insertNodes([paragraphNode]);
  2. acywatson commented on Nov 23, 2022

    @acywatson
    Contributor

    Is it possible to insert a line that preserves the status of the previous line when a new line is inserted with the enter key? If you have a sample code, could you please let me know?

    That is not the behavior in Lexical's Playground. Also, the Draft.js demo has that behavior.

    When you say "preserves the status" what do you mean, exactly?

  3. nrikiji commented on Nov 23, 2022

    @nrikiji
    Author

    @zignis
    Thanks for the reply. Is it like adding a node by handling the enter key around the following?

    editor.registerUpdateListener(({ editorState }) => {
        ・・・
    });
    ・・・
    editor.update(() => {
        ・・・
    });
  4. nrikiji commented on Nov 23, 2022

    @nrikiji
    Author

    @acywatson
    Thanks for the reply. For example, we want to continue the selected font size and color in the next line.

  5. zignis commented on Nov 23, 2022

    @zignis
    Contributor

    @zignis Thanks for the reply. Is it like adding a node by handling the enter key around the following?

    editor.registerUpdateListener(({ editorState }) => {
        ・・・
    });
    ・・・
    editor.update(() => {
        ・・・
    });

    Yes, you may want to take a look at how the @lexical/code package does it: https://github.com/facebook/lexical/blob/main/packages/lexical-code/src/CodeHighlighter.ts#L515

  6. locked and limited conversation to collaborators on Nov 25, 2022
  7. converted this issue into a discussion #3423 on Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovement over existing feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions