"new Transform" with leaf text node

Background: I’m working on improving / fixing bugs in prosemirror-suggest-changes.

This is the Slice we’re working with:

{
  "content": [
    {
      "type": "text",
      "marks": [
        {
          "type": "em"
        },
        {
          "type": "strong"
        },
        {
          "type": "insertion",
          "attrs": {
            "id": "1"
          }
        }
      ],
      "text": "test paragraph"
    }
  ]
}

The code then loops through all the nodes in a slice and creates a new Transfrom for each children ( only 1 children in our example ):

  const tr = new Transform(node);

Then on that Transform it calls:

tr.removeMark(0, node.nodeSize, markTypeToApply);
// or
tr.removeNodeMark(0, markTypeToApply);

Both of these calls fail. The first one at nodesBetween because it doesn’t have any children ( okay ). The second one is because doc.nodeAt(0) doesn’t return a Node.

In the docs:

new Transform(doc: Node)
Create a transform that starts with the given document.

What we’re giving to the Transform is not a document, so I understand why this would not work.

My question is: how to do this correctly, what direction I should try?

You can create a transform with a non-document node, but addressing will work the same as with a document—position 0 refers to the start of the content of the top node, not to the top node itself. So if you’re trying to remove marks from that outer node, this will not work. But doing that is much easier if you just create a copy of the node without the mark, anyway.

1 Like