Slate: How to insert void <br> node

Created on 12 Feb 2017  路  2Comments  路  Source: ianstormtaylor/slate

How should be a definition for a line break node rendering <br> element look like?

What I desire is similar behaviour to slate-soft-break but it would insert '<br>' instead of '\n'.

Should it be an Inline node with isVoid:true? But all Block and Inline nodes contains by default a Text node. So how to prevevent directional keys from "entering" this node and just "skip" it.

question

Most helpful comment

Since the reason for all this was to add soft break on shift+enter and to make Slate output a HTML markup with <br/> elements instead of \n, there was no reason to serialize and deserialize <br/> elements into some kind of node. It is sufficient to deserialize the HTML <br/> element into a Text node with content set to \n and afterwards serialize those Text nodes back into <br/> element.

With this solution, we can caontinue using the slate-soft-break plugin and just add following rules into our schema:

// Rule handling line breaks
  {
    deserialize(el) {
      if (el.tagName !== 'br') return;

      return {
        kind: 'text',
        text: '\n',
      };
    },
    serialize(object, children) {
      if (object.type !== 'text') return;
      if (children !== '\n') return;

      return (
        <br/>
      );
    },
  },

All 2 comments

So basically the question is how to create an empty, non-focusable node.

Since the reason for all this was to add soft break on shift+enter and to make Slate output a HTML markup with <br/> elements instead of \n, there was no reason to serialize and deserialize <br/> elements into some kind of node. It is sufficient to deserialize the HTML <br/> element into a Text node with content set to \n and afterwards serialize those Text nodes back into <br/> element.

With this solution, we can caontinue using the slate-soft-break plugin and just add following rules into our schema:

// Rule handling line breaks
  {
    deserialize(el) {
      if (el.tagName !== 'br') return;

      return {
        kind: 'text',
        text: '\n',
      };
    },
    serialize(object, children) {
      if (object.type !== 'text') return;
      if (children !== '\n') return;

      return (
        <br/>
      );
    },
  },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ezakto picture ezakto  路  3Comments

bunterWolf picture bunterWolf  路  3Comments

varoot picture varoot  路  3Comments

ianstormtaylor picture ianstormtaylor  路  3Comments

YurkaninRyan picture YurkaninRyan  路  3Comments