Is there a way to simulate enter keypress?

I would like to write a command that executes exactly what happens when I type the enter key. Is this possible in any way?

I know that I could copy the command from my default keymap. But as soon as you use e.g. lists or other nodes that register their own keymap (with an enter binding), it gets more complicated.

Creating a key event and passing it to the handleKeyDown prop should work—it’s also done by the view itself when recognizing an Enter-like DOM change on Android. Something like this…

function simulateKey(view, keyCode, key) {
  let event = document.createEvent("Event")
  event.initEvent("keydown", true, true)
  event.keyCode = keyCode
  event.key = event.code = key
  return view.someProp("handleKeyDown", f => f(view, event))
}

simulateKey(view, 13, "Enter")
1 Like

Great! This works fine! Thank you!

In tiptap 2 it will be possible to chain commands. This is super helpful to execute more complex things within a single transaction like:

editor
  .chain()
  .enter()
  .insertText('1')
  .enter()
  .insertText('2')
  .enter()
  .insertText('3')
  .run()