Extending the Enter behaviour to keep format in new line

After reading a lot on the ProseMirror way of working, I’m confused about one point.

I’m trying to keep the format (both node and marks) after adding a new line with Enter. To do that, I first tried with a plugin, but I seems not easy to guess if the last transactions in the function “appendTransaction” includes a step which added a new line.

Then I binded the Enter key with a custom function which basically calls “splitBlockKeepMarks” and “setBlockType”.

It works great in many cases but I lost the correct behaviour in Lists and probably some other behaviours which was handled by the default binding.

My question is: What is the best way of doing this kind of stuff, maybe something which alllows to extends a command ?

The default binding for Enter tries newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock, in that order. So using that but replacing splitBlock with splitBlockKeepMarks should get you quite far. It sounds like you also had splitListItem in there (possibly via example-setup). Replacing that one with a mark-preserving variant is a bit more tricky, since no such command is provided by the library and it’s quite a tricky function, but maybe you can wrap splitListItem in a command that follows up the transaction it creates with some extra code.

Thanks for the quick answer!

   const onEnter = (EditorState, dispatch, EditorView) => {
      const blockType = EditorState.selection.$head.parent;
      chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock)(EditorState, dispatch)
      splitListItem(mySchema.nodes.list_item)(EditorState, dispatch)
      setBlockType(blockType.type, blockType.attrs)(window.view.state, window.view.dispatch, window.view)
    }

baseKeymap.Enter = undefined;
window.view = new EditorView(document.querySelector("#editor"), {
  state: EditorState.create({
    doc: DOMParser.fromSchema(mySchema).parse(document.querySelector("#content")),
    plugins: [keymap({"Enter": onEnter}), keymap(baseKeymap), menu, myPlugin]
  })
});

This Enter binding works, if it can help somebody.

I had to call splitListItem out of the chainCommands because splitBlock return true even without spliting anything so it does not execute splitListItem. Is that the expected behaviour ?

What if you just moved splitListItem to the beginning of your chained commands? You also shouldn’t need to explicitly set baseKeymap to undefined, as you are overwriting it anyway with your onEnter handler