0

我有一个协作聊天应用程序,它使用tiptap作为它的消息传递区域。我发现它很有用,因为它已经可以支持多种格式并且可以增加一些灵活性。但是,在寻找侦听“输入”键的事件侦听器时,我发现自己卡住了。当用户按回车键时,我想提交他们的聊天并清除编辑器。

我找到了这个onUpdate事件监听器,但我找不到它检测到按下什么键的确切位置。

示例代码如下:

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;
}

我正在使用 Vue2,顺便说一句。

谢谢

4

3 回答 3

1

在编辑器道具中,传入一个回调

      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

于 2021-08-19T01:31:25.657 回答
1
  editorProps: {
    handleDOMEvents: {
      keypress: (view, event) => {
        if (event.key === 'Enter') {
          console.log('Heyyyy')
        }
      },
    },
  },

您可以将其作为道具传递给 new Editor()

于 2022-02-01T09:21:43.283 回答
0

作为参考,我设法做了类似的事情。我使用 VueJS 原生 keydown 事件来检测按下了哪个键。

<EditorContent class="editor" :editor="editor" @keydown.native="onKeydown" />
methods: {
  onKeydown(e) {
    if (!e.shiftKey && e.which == 13) {
      this.editor.commands.undo(); // i just "undo" the enter event to remove the space it made
      this.$emit("onEnter");
    }
  }
}

参考: https ://www.tiptap.dev/installation/vue2#5-use-v-model-optional

于 2021-08-18T01:00:30.400 回答