draft-js icon indicating copy to clipboard operation
draft-js copied to clipboard

Double symbol issue

Open vmakhaev opened this issue 10 years ago • 4 comments

I'm trying to make collaborative editing for Draft based on CRDT. For this I need to store content state in database and send it between clients, while selection state can be stored in component's state. For this I deconstruct entity state in onChange handler and then construct it again in render function. The problem is that on symbol input, it triggers onChange two times and and ends up with two symbols in editor instead of one. For your convenience, I created an example that reproduces problem. It is fork from rich example and saves everything in state.

vmakhaev avatar Mar 13 '16 12:03 vmakhaev

You might consider handleBeforeInput.

gscottolson avatar Mar 13 '16 13:03 gscottolson

@gscottolson I'm trying to use handleBeforeInput. I assume, that you mean that I should reject some symbols by returning true, but it is called just once, before onChange is called two times. So I can reject all input or nothing.

vmakhaev avatar Mar 13 '16 15:03 vmakhaev

I believe that modifying the editor state within your onChange handler might be contributing to your problem. Here is an excerpt from another issue:

When you changed to a new ContentState within handleChange, the nativelyRenderedContent value and your new ContentState did not match up. When this occurs, the component will re-render, because it believes that the current ContentState is not natively rendered. The event still occurs and the character is natively inserted, but your re-render also takes place. The character thus appears twice.

I think you can use EditorState.push instead of creating a new EditorState from scratch for each component render.

gscottolson avatar Mar 13 '16 18:03 gscottolson

@gscottolson EditorState.push works. Looks like I missed this moment in docs. Thanks.

vmakhaev avatar Mar 13 '16 19:03 vmakhaev