reactjsdraftjs

Strip all styling from pasted text in DraftJS?


When pasting text from word or another source into draftjs the formatting comes along for the ride, I tried stripping the styling data like so:

onChange={(newEditorState) => {
                        const raw = convertToRaw(newEditorState.getCurrentContent())
                        for (let i = 0; i < raw.blocks.length; i++){
                            raw.blocks[i].type = "unstyled"
                        }
                        let newContent = convertFromRaw(raw)
                        newEditorState
                        const newState = EditorState.push(state, newContent, "change-block-type")

                        setState(newState)
                    }} />

Which worked except typing ended up being reversed on input after that, which was very confusing.


Solution

  • It seems like the stripPastedStyles option is what you're looking for:

    Set whether to remove all information except plaintext from pasted content.

    This should be used if your editor does not support rich styles.

    Default is false.