Transaction.replaceSelection() > only replaces the text from first Node in the Slice

I implemented a custom handlePaste method using EditorProps interface where I use Transaction.replaceSelection(), pretty similar way to the original pasting implementation, I just update a couple nodes in the slice before doing the replacement.

However I noticed that the first Node in my Slice has its attributes overriden by those of the Node that is the selection (they share the same NodeType). Every other node in the slice keeps their attributes as expected.

I was diving a bit into the code with breakpoints and noticed that it happens within Fitter.findFittable(), most specifically in this highlighted line:

It makes so the fragment being added (for the first line) is the TextNode and not the Node itself. So when pasting a number of nodes, the first one loses all its data, only the TextNode is preserved.

Is this expected behavior when using replaceSelection? If so, how can I work around it so the entire pasted content remains unchanged, wherever it gets pasted?

Is this expected behavior when using replaceSelection?

Yes, if the selection starts inside a given node, and the slice you provide it starts with an open node, the replace fitter will try to fit the content of that open node into the parent of the start position. Your problem might be with the openStart value of your slice. replaceSelection goes through replaceRange, which will do some magic to expand the replaced range when appropriate (for example when the selection starts at the start of a node and ends at the end of a node, and the replaced content could replace that node), but if the replacing nodes are open that won’t kick in.

1 Like

I tried forcing openStart to 0 when pasting into an empty line (node.content.size === 0) and it seems to work as I expect. Haven’t run into side effects so far.