lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Feature: Support for replace with multiple nodes

Open yudori opened this issue 4 years ago • 4 comments

There currently is a way to replace a LexicalNode with another node. It would be great to extend this functionality to be able to replace with a list of nodes.

yudori avatar May 17 '22 16:05 yudori

Would ElementNode.splice meet this use case?

acywatson avatar May 17 '22 19:05 acywatson

@acywatson Hmmm, thanks! It does look like I would be able to achieve that using ElementNode.splice with the help of LexicalNode.getParent() and LexicalNode.getIndexWithinParent().

I think that makes this less of an issue, however it would still be easier/nice to have to do the replace at the child LexicalNode level like LexicalNode.replace does, perhaps by changing that to replace<N extends LexicalNode>(replaceWith: Array<N>): Array<N>

yudori avatar May 17 '22 20:05 yudori

Yea, I think that does make sense. I momentarily forgot that replace is a method on LexicalNode. I think it makes sense to have an interface that supports replacing a single node with multiple.

Maybe even

replace<N extends LexicalNode>(replaceWith: Array<N> | N): Array<N>

for backwards compat.

acywatson avatar May 17 '22 20:05 acywatson

I don't think we need to expand core functionality on this given you can achieve this easily in user-land today.

trueadm avatar Nov 24 '22 13:11 trueadm