jpuri / react-draft-wysiwyg

A Wysiwyg editor build on top of ReactJS and DraftJS. https://jpuri.github.io/react-draft-wysiwyg
MIT License
6.38k stars 1.16k forks source link

nbsp not working when adding the HTML back into Wysiwyg #1418

Open TrofinSorin opened 5 months ago

TrofinSorin commented 5 months ago

I'm saving the HTML to my database and when entering the modal that contains this WYSIWYG.

I need to add back to the WYSIWYG this HTML: <h1>1</h1>\n<h1>&nbsp;&nbsp;2</h1>\n<h1>3</h1>\n

Which will look something like this:

 1
   2
 3 

This is what I'm using to set the editor state to populate the WYSIWYG.

const [editorState, setEditorState] = useState(EditorState.createEmpty());
const blocksFromHTML = convertFromHTML(messageFromBackend);
      const contentState = ContentState.createFromBlockArray(
        blocksFromHTML.contentBlocks,
        blocksFromHTML.entityMap
      );

// Update the editorState with the new content
setEditorState(EditorState.createWithContent(contentState));

This is the HTML:

  <Editor
          editorState={editorState}
          wrapperClassName="demo-wrapper"
          editorClassName="demo-editor"
          onEditorStateChange={onEditorStateChange}
        />

I need the nbsp to show inside wysiwyg when setting the state with what i've saved from WYSIWYG initiality