Transform.split with depth doesn't work when there are required nodes in content

I am endeavoring to split a block with depth which has a required element in it but get a transaction error. Is this a bug in prosemirror where it should auto-add the required element? How should I go about this?

Specifics My use case is a novel writing tool with scene and chapter blocks. The main doc schema has content "chapter+", the chapter content is "title scene+" and scene content is "paragraph+". I have an input rule that splits the scenes in a chapter when typing “***”. When I do something similar from within a scene paragraph to split the chapter, I get a TransformError. This is because title is a required element of chapter. If I change chapter’s content to "title? scene+" the split works great.

I’ve put together a testbed you can view the error in. There is also a transform error mentioned there when pressing Enter/Return from the title.

split doesn’t do anything clever, it just tries to split the given node at the given point, so that you’re getting an error here is expected behavior. (You can test in advance with canSplit whether a split is valid.)

If you want to create a transaction that does successfully split such a node, you’ll have to create a ReplaceStep that inserts the required node, along with the new node boundary, by hand.

1 Like

There is also a transform error mentioned there when pressing Enter/Return from the title in my example repo.

When pressing Enter after entering the chapter title it has a transform error. This is because it doesn’t navigate very well from the text in the title element across a deeper block (the scene) into the text of the paragraph below. Is this also expected behavior? See below:

<div class="chapter">
  <h1 class="title">My Title</h1>
  <div class="scene">

If the cursor is at the end of “My Title” and you press Enter there is a transform error, but the cursor does end up correctly inside the <p> afterwards.

The built-in commands should not raise errors, regardless of schema. If you can package this up as a small test case, I’ll take a look.

I already did. is a small test case. Thank you!!

Yeah, that was a bug. Does this patch help? It will cause splitBlock to do nothing in this case, since there is no straightforward way to split the given block.

The error no longer occurs, but the cursor doesn’t advance to the next available text node. In my use-case this is the expected behavior. It seems like it would be so in most cases, but I could be blinded by my own use-case.

That’d be a different command that you could bind to enter. splitBlock is for splitting blocks.

Then yes, it is fixed. Thank you!

Pressing Enter at the end of a Heading block creates a paragraph because of the createParagraphNear command, correct? So to do what I’m trying, I could create a command like that which can traverse up the parent chain before giving up? What do you think about making that change to the existing createParagraphNear command? Does that break existing functionality or have unintended consequences? (just wondering if it will make the command more robust or if it is better to do this as a one-off for my project)

Update never mind, I see it is a split as well, but because the heading and new paragraph share the same parent splitBlock works and my case cannot. A simple selection update will work fine for me.