A Feature request
There is a missing and needed Change method: "setTextByKey"
Currently there is no straight way to replace text in a Node.
I can append text to Node (insertTextByKey) or remove text from Node (removeTextByKey). However, for replacing the text, one can not merely remove the text and re-insert it. This is because removing the text of the Node will cause for its deletion (empty Nodes gets deleted automatically).
How about deleteAtRange(Range.create().moveToRangeOf(text.key), {normalize: false})).insertTextAtRange(Range.create().collapseToStartOf(text.key), "some texts")
I'd be open to a replaceTextByKey change method if someone wants to add it, it should just call into the existing removeTextByKey and then insertTextByKey, but it does seem like a nice convenience.
How about deleteAtRange(Range.create().moveToRangeOf(text.key), {normalize: false})).insertTextAtRange(Range.create().collapseToStartOf(text.key), "some texts")
The problem is, when users try to undo, they need to undo twice to revert back to previous text.
@benjycui Yes, I forgot that.... It should be removeTextByKey(....).insertTextByKey(...)
but I think it would be much better to have an replaceTextByKey. I will submit a PR about that.
Actually, I think having both replaceTextByKey (which takes a offset, length, text) and setTextByKey (which takes just a key) would both be useful. Open to PR's that implement either of them.
@ianstormtaylor How about another thing:
for insertTextAtRange and insertText, if range is expanded but startKey and endKey is the same, use replaceTextByKey and do not use snapshot and deleteAtRange
I think snapshot is perhaps unnecessary if the startText and endText is not deleted
That could work too. I'd rather find a way to get rid of snapshot I think, because it feels weird to need to do that.
@ianstormtaylor
About marks of setTextByKey, there are two choices. Which one would you prefer?
setTextByKey(key: string , text: string|Text)setTextByKey(key: string, text: string, marks?: Set<Marks>)Another question: Shall we inherit activeMarks in repalceTextByKey and insertTextByKey?
For setTextByKey, let's go with the same arguments as insertText (except without the offset).
And for replaceTextByKey, I think it would be good to inherit the active marks the same way as if the replacement happened with remove then insert.