React-quill: Question: is it possible to implement new line in editor.

Created on 21 Aug 2019  路  2Comments  路  Source: zenoamaro/react-quill

Is it possible to implement a new line <br /> on shift+enter like in general editing?

At the moment all new lines are wrapped in paragraphs which add top and bottom margin which is fine.

How do I achieve adittional functionality of new line with br tag?

Most helpful comment

Solved it like this:

const Delta = Quill.import("delta");
const Break = Quill.import("blots/break");
const Embed = Quill.import("blots/embed");

const lineBreakMatcher = () => {
  let newDelta = new Delta();
  newDelta.insert({ break: "" });
  return newDelta;
};

class SmartBreak extends Break {
  length() {
    return 1;
  }
  value() {
    return "\n";
  }

  insertInto(parent, ref) {
    Embed.prototype.insertInto.call(this, parent, ref);
  }
}

SmartBreak.blotName = "break";
SmartBreak.tagName = "BR";
Quill.register(SmartBreak);

<ReactQuill
  modules={{
    clipboard: {
      matchers: [["BR", lineBreakMatcher]],
      matchVisual: false
    },
    keyboard: {
      bindings: {
        linebreak: {
          key: 13,
          shiftKey: true,
          handler: function(range) {
            const currentLeaf = this.quill.getLeaf(range.index)[0];
            const nextLeaf = this.quill.getLeaf(range.index + 1)[0];
            this.quill.insertEmbed(range.index, "break", true, "user");
            // Insert a second break if:
            // At the end of the editor, OR next leaf has a different parent (<p>)
            if (nextLeaf === null || currentLeaf.parent !== nextLeaf.parent) {
              this.quill.insertEmbed(range.index, "break", true, "user");
            }
            // Now that we've inserted a line break, move the cursor forward
            this.quill.setSelection(range.index + 1, Quill.sources.SILENT);
          }
        }
      }
    }
  }}
/>

All 2 comments

@alexkrolick @daggmano @notseenee any ideas?

Solved it like this:

const Delta = Quill.import("delta");
const Break = Quill.import("blots/break");
const Embed = Quill.import("blots/embed");

const lineBreakMatcher = () => {
  let newDelta = new Delta();
  newDelta.insert({ break: "" });
  return newDelta;
};

class SmartBreak extends Break {
  length() {
    return 1;
  }
  value() {
    return "\n";
  }

  insertInto(parent, ref) {
    Embed.prototype.insertInto.call(this, parent, ref);
  }
}

SmartBreak.blotName = "break";
SmartBreak.tagName = "BR";
Quill.register(SmartBreak);

<ReactQuill
  modules={{
    clipboard: {
      matchers: [["BR", lineBreakMatcher]],
      matchVisual: false
    },
    keyboard: {
      bindings: {
        linebreak: {
          key: 13,
          shiftKey: true,
          handler: function(range) {
            const currentLeaf = this.quill.getLeaf(range.index)[0];
            const nextLeaf = this.quill.getLeaf(range.index + 1)[0];
            this.quill.insertEmbed(range.index, "break", true, "user");
            // Insert a second break if:
            // At the end of the editor, OR next leaf has a different parent (<p>)
            if (nextLeaf === null || currentLeaf.parent !== nextLeaf.parent) {
              this.quill.insertEmbed(range.index, "break", true, "user");
            }
            // Now that we've inserted a line break, move the cursor forward
            this.quill.setSelection(range.index + 1, Quill.sources.SILENT);
          }
        }
      }
    }
  }}
/>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

prosenjitchy picture prosenjitchy  路  3Comments

jaimefps picture jaimefps  路  3Comments

Fensterbank picture Fensterbank  路  3Comments

levous picture levous  路  3Comments

aliciawood picture aliciawood  路  3Comments