ckeditor / ckeditor5

Powerful rich text editor framework with a modular architecture, modern integrations, and features like collaborative editing.
https://ckeditor.com/ckeditor-5
Other
9.6k stars 3.7k forks source link

Operating on model text nodes is difficult / model.writer#updateText? #7400

Open scofalik opened 4 years ago

scofalik commented 4 years ago

šŸ“ Provide a description of the new feature

I wonder if we can do something with how difficult it is to operate on text nodes in the model. Operating straight on text nodes comes up rarely, that's true. Text nodes are mostly connected with typing.

However, from time to time, there is a case when we need to update the text of given text node.

First difficulty is that #data property on the text node is readonly. To update text node we need to remove the old text node and insert a new one. Another difficulty is that text nodes are joined together when they have same attributes, so following will throw an error:

writer.insertText( 'newText', oldTextNode.getAttributes(), oldTextNode, 'after' );
writer.remove( oldTextNode );

Because after calling insertText() oldTextNode is already removed (as it was joined with the inserted text). This is very unintuitive. You need to do that in the other order. So there's 50% chance that someone will write code like this and it will throw an error and the programmer will have no idea why.

This is why I propose introducing model.writer#updateText(). It will be a fairly simple method that just uses the existing code.

BTW. if you are updating text nodes while iterating a range, you may end up in an infinite loop if the new text is longer than the old text. Or probably in some other error if the new text is shorter ("jumping over" some text). However, this is not related, this is a typical problem that happens when you iterate over a some kind of a set and change that set in the same time. Well, I guess that maybe we would be able to fix that by getting rid of generators...


If you'd like to see this feature implemented, add a šŸ‘ reaction to this post.

chrismyer commented 4 years ago

As someone new to ckeditor, I would love this feature! How would you update the entire textNode rather than just adding some text to it? For instance, if I wanted to replace "Today is sunny." with "Today is cloudy."?

letsbug commented 3 years ago

Very, very, very good advice! Recently, I have a lot of new requirements, such as eliminating extra Spaces (first and last Spaces, more than 2 Spaces in the body), such as convert lowercase to uppercase, such as convert soft break to soft enter, such as conversion between full and half - corner characters. This API is significant.

xinglie commented 3 years ago

face to the same problem . so i have to insert text with different attributes , then remove old text , change the insert text to old text attributes