question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to allow HTML blocks to breakout on enter?

See original GitHub issue

How can we allow HTML blocks to ‘break out’ when the user hits enter? In essence, when in an h1, if I hit enter, I want to set the default behavior to ‘break out’ of the h1 into a p.

  1. Open editor.
  2. Begin an h1 block and type.
  3. Hit enter.
  4. Continue typing.
    • Desired behavior: second line should be in a <p>, not an <h1>.
    • Actual behavior: second line is in an <h1>

This is handled in Draft.js with this plugin: https://github.com/icelab/draft-js-block-breakout-plugin.

block

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
PaddyManncommented, Nov 30, 2020

I had issues with some of the solutions above:

  • The solution from @simpleshadow (using setTimeout) leads to a flicker effect in the UI
  • The solution from @zxkxyz works well if pressing enter at the end of some text, but not if you press enter in the middle of some text.

To fix this:

if (event.key === 'Enter') {
  const selectedElement = Node.descendant(editor, editor.selection.anchor.path.slice(0, -1));

  // Replace 'title' with the type of the element which you wish to "break out" from
  if (selectedElement.type === 'title') {
    event.preventDefault();
    const selectedLeaf = Node.descendant(editor, editor.selection.anchor.path);

    if (selectedLeaf.text.length === editor.selection.anchor.offset) {
      Transforms.insertNodes(editor, {
        type: 'paragraph',
        children: [{text: '', marks: []}],
      });
    }
    else {
      Transforms.splitNodes(editor);
      Transforms.setNodes(editor, {type: 'paragraph'});
    }
  }
}
8reactions
zxkxyzcommented, Jan 11, 2020

@simpleshadow I did something similar but eliminated the need for a setTimeout because it was causing the new line to temporarily glitch. I just override the default behavior of the enter key and instead manually insert a new paragraph node with empty text:

if (event.key === "Enter" && !event.shiftKey) {
    event.preventDefault();
    const newLine = {
        type: "paragraph",
        children: [
            {
                text: "",
                marks: []
            }
        ]
    };
    Transforms.insertNodes(editor, newLine);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Where Lines Break is Complicated. Here's all the ... - CSS-Tricks
Here's all the Related CSS and HTML. ... This is what a break-out text situation might be like: ... Allows words to be...
Read more >
Break out of parent div's - css - Stack Overflow
Let's add another parent and move the position:relative one level up (or, in your context, you could maybe simply use an existing upper...
Read more >
JavaScript break Statement - W3Schools
The break statement breaks out of a switch or a loop. In a switch, it breaks out of the switch block. This stops...
Read more >
break - JavaScript - MDN Web Docs - Mozilla
let i = 0; ... The labeled statement can be any block statement; ... label statement that the break statement is intended to...
Read more >
A guide to creating HTML emails | Help Center - Intercom
Add a HTML block to your email ... When you need to break out from the regular formatting options that the composer offers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found