DEV Community

Discussion on: Setting up a basic rich text editor in React

Collapse
 
johnniepop profile image
Ivan Popov • Edited

Nice intro to CKEdit 5 for React.
I have a question though. Let's say I have an instance of this rich text editor on a page and a user tries to paste a large chunk of text (+5K lines for instance). The browser starts sweating. Well in about a minute or two it eventually pastes the text in a pretty huge, extended field. So my question is how would you prevent such situation?
One strategy would be to act early, catch the pasting event and restrict the length of the inserted text at that point. The CKEditor 5 API seem to have Clipboard and Clipboardobserver classes with the latter having a clipboardInput event but they don't seem to be easily accessible by React ... at least for me, since I'm still a noob in the front-end field.

Collapse
 
johnniepop profile image
Ivan Popov

At the moment I'm not sure if asked a too stupid question, or this feat is really difficult ...

Collapse
 
crimsonmed profile image
Médéric Burlet

The idea is indeed to restrict the length. Have you tried working with CKEdit plugins?
ckeditor.com/old/forums/CKEditor-3...

This looks promising for limiting the number of characters.

CKEdit clipboard is an extension that you also have to install:
ckeditor.com/docs/ckeditor5/latest...

You could also check this:
stackoverflow.com/questions/497090...

They look at pasting the clipboard content as text. This would remove any formatting that needs to happen and might lighten the load a lot.

Thread Thread
 
johnniepop profile image
Ivan Popov

Hi Médéric,
Thanks for your time.

I've seen some of the resources you provided. The point is that all of those, and the other working solutions I've found are vanilla javascript, and I can't figure how to reuse them in my react component. I mean, I already have the length altering function but I can't figure out with what prop to access it.

As for the plugin, yes it was one of the first things I tried but weirdly, after a seemingly successful installation, my project stops compiling (the '@@ckeditor/ckeditor5-clipboard' package is seen from the editor but not from the compiler).

Here (github.com/ckeditor/ckeditor5-reac...) I found a piece of useful example code but still not sure how to link it to the CKEditor component. I might ask a question directly in the project.

Cheers!

Thread Thread
 
johnniepop profile image
Ivan Popov
Thread Thread
 
johnniepop profile image
Ivan Popov • Edited

So a CKEditor dev gave some useful hints and I managed to fix my problem. I posted my solution also on stackoverflow: stackoverflow.com/questions/652636...