Split at paragraph boundaries leaves empty paragraphs before/after

Hello, I’m trying to perform a split from a selection and this works well… …unless a boundary of the selection is the start or the end of the paragraph. In such a case the split occurs but leaves the original paragraph empty.

For example, splitting “Hello” with “ell” selected produces paragraphs “h”, “ell”, “o”, but splitting it with “Hell” selected produces “”, “Hell”, “o” and splitting it with “ello” selected produces “H”, “ello”, “”

How can I manage to avoid empty paragraphs remaining?

You didn’t mention how you are doing the splitting, but if you’re making direct calls to Transform.split, could you just not split when the position is at the end or start of its parent node?

Yes I’m using Transform.split. I don’t think I could avoid splitting in such edge cases because:

  • when the position is at the start I still want to split at the end of the selection
  • when the position is at the end I still want to split at the beginning of the selection.

To make things more clear, the structure to split is:

  • simpleAnswer
    • paragraph
      • text(“Hello”)

and say I want to split some selection in paragraph, I’m trying to build:

  • simpleAnswer
    • paragraph only if text remaining at left side of selection is not empty
      • text(textRemainingAtLeftSideOfSelection)
  • simpleAnswer
    • paragraph
      • text(selection)
  • simpleAnswer
    • paragraph only if text remaining at right side of selection is not empty
      • paragraph
        • text(textRemainingAtRightSideOfSelection)

To do so I’m doing:

const currentSimpleAnswer: ContentNodeWithPos = findParentNodeOfType(this.schema.nodes.simpleAnswer)(selection);
const splitDepth = currentSimpleAnswer.depth + 1;  // Split at the above "simpleAnswer" level
tr.split(tr.mapping.map(selection.from), splitDepth);
tr.split(tr.mapping.map(selection.to), splitDepth);

(as you can see I’m using Atlassian’s prosemirror-utils to find parent node)

I do not see how this is a problem. Your code can check for these cases and do the appropriate amount of splitting.

Well, you are perfectly right :slight_smile: Sorry I didn’t realize that.

Now my code is:

if (selection.$from.parentOffset > 0) {
  tr.split(tr.mapping.map(selection.from), splitDepth);
}
if (selection.$to.parentOffset < selection.$to.node().content.size) {
  tr.split(tr.mapping.map(selection.to), splitDepth);
}

And it is working perfectly as expected. Thank you very much!