Search code examples
vue.jstiptapprose-mirror

TipTap/VueJS - How to detect a keypress


I have a collaborative chat application that uses tiptap for it's messaging area. I found it useful as it can support multiple formats already and can add some flexibility. However, I found myself stuck when looking for an event listener that listens for "enter" key. When the user hit the enter, I want to submit their chat and clear the editor.

I found this onUpdate event listener but I couldn't find exactly the place where it detects what key is pressed.

Sample code is below:

mounted() {
  let editor = new Editor({
    extensions: [StarterKit],
    content: this.value
  });

  editor.on("update", e => {
    console.log(e);
    this.$emit("input", this.editor.getHTML());
  });

  this.editor = editor;
}

I'm using Vue2, btw.

Thanks


Solution

  • In the editor props, pass in a callback

          handleDOMEvents: { 
            keydown: (view, event) => {
              if (event.key === "Enter") {
              }
              return false;
            }
          },
    

    https://www.tiptap.dev/api/editor/#editor-props https://prosemirror.net/docs/ref/#view.EditorProps