Search code examples
angulartypescriptquill

Keypress events not registered in Quill Inline blot


I have a custom Inline Blot in my Quill editor. I would like to listen to keypress event on nodes. The problem is keypress event never gets fired, although click event works just fine.

Here is my code:

const Inline = Quill.import('blots/inline');

class Editable extends Inline {
  static create(value: any) {
    const node = super.create();
    node.setAttribute('contenteditable', true);
    node.setAttribute('editable', value);
    node.addEventListener('keypress', (e: any) => {
      alert('pressed'); // doesn't work
    });
    node.addEventListener('click', (e: any) => {
      alert('clicked'); // works
    });
    return node;
  }
}

Editable.blotName = 'editable';
Editable.tagName = 'edit';
Quill.register(Editable);

What am I doing wrong ?

UPDATE

After some research ended up with Keyboard Module

const bindings = {
  enter: {
    key: 13,
    shiftKey: null,
    handler: () => {
      const range = this.quill.getSelection(true);
      if (range && !this.editable(range.index)) {
        this.quill.insertText(range, '\n');
      }
    }
  }
};

this.quill = new Quill('#editor-container', {
  modules: {
    keyboard: {
      bindings
    },
    toolbar: '#toolbar'
  },
  theme: 'snow'
});

Solution

  • After some research ended up with Keyboard Module

    const bindings = {
      enter: {
        key: 13,
        shiftKey: null,
        handler: () => {
          const range = this.quill.getSelection(true);
          if (range && !this.editable(range.index)) {
            this.quill.insertText(range, '\n');
          }
        }
      }
    };
    
    this.quill = new Quill('#editor-container', {
      modules: {
        keyboard: {
          bindings
        },
        toolbar: '#toolbar'
      },
      theme: 'snow'
    });