GithubHelp home page GithubHelp logo

Comments (16)

iphonic avatar iphonic commented on August 23, 2024 13

For ones who are wondering how to initialize it with HTML content instead of empty here is the way you need to generate editorState with the content you have, to init editorState there is a function in draft-js EditorState component and I am using a converter draft-js-import-html which allows to generate contentState out of HTML content, see below

import {EditorState, ContentState, convertFromHTML, convertToRaw} from 'draft-js';
import {stateFromHTML} from 'draft-js-import-html';

constructor(props) {
    super(props);
    var contentState = stateFromHTML(props.input.value);
    editorState = EditorState.createWithContent(contentState);
    this.state = {editorState: editorState};
}

Above works for me.

from react-draft-wysiwyg.

Ranganathan95 avatar Ranganathan95 commented on August 23, 2024 3

@jpuri & @iphonic , I tried statefromHTML and also convertFromHTML from draft-js. both were working fine, but when the content has underline/subscript/superscript content, it was rendered as normal text. Is there any work-around available?
Note: Bold and italic styles working fine.

from react-draft-wysiwyg.

elebumm avatar elebumm commented on August 23, 2024 2

@iphonic's result worked for me perfectly. I am using functional components though so here is my solution:

  const [editorState, setEditorState] = useState(EditorState.createEmpty());

  useEffect(() => {
   // automatedMessage is the html string I am fetching from my server
    if (automatedMessage) {
      let contentState = stateFromHTML(automatedMessage.message); //automatedMessage.message being "<p>Test</p>"
      setEditorState(EditorState.createWithContent(contentState));
    }
  }, [automatedMessage]);

from react-draft-wysiwyg.

ytwater avatar ytwater commented on August 23, 2024 1

I ran into the same problem where my editorContents was null the first time I rendered, then was populated on a second render. A quick and dirty fix of not rendering until I know the data is there worked for me.

` render() {

const { editorContents } = this.state;

if (!editorContents) {
  return <div />;
}

return (
  <div>
    <div className="demo-label">
      Editor with output generated in HTML.
    </div>
    <div className="demo-editorSection">
      <Editor
        toolbarClassName="demo-toolbar"
        wrapperClassName="demo-wrapper"
        editorClassName="demo-editor"
        rawContentState={editorContents}
        onChange={this.onEditorChange.bind(this)}
        toolbar={{
          options: ['inline', 'blockType', 'textAlign', 'link', 'remove', 'history'],
          inline: {
            options: ['bold', 'italic', 'underline', 'strikethrough'],
          },
        }}
      />
    </div>
  </div>
);

}`

from react-draft-wysiwyg.

jpuri avatar jpuri commented on August 23, 2024

Hi @alagos ,

Check similar example here: https://github.com/jpuri/react-draft-wysiwyg/blob/master/docs/src/components/Demo/index.js#L140

from react-draft-wysiwyg.

alagos avatar alagos commented on August 23, 2024

@jpuri yes, actually, that's the specific example I'm basing of. Instead of using sampleEditorContent to load the content in rawContentState={sampleEditorContent} I loaded it from the state and I almost copy/pasted the content from https://github.com/jpuri/react-draft-wysiwyg/blob/master/docs/src/util/sampleEditorContent.js to my _getInitialHTML function to load the initial data for the state, but is not working.
Not sure if has something to do, but I'm using 0.2.1 version.

from react-draft-wysiwyg.

jpuri avatar jpuri commented on August 23, 2024

ok try to do a console log and check whats coming in editorContent after this line const { editorContent } = this.state;.

from react-draft-wysiwyg.

alagos avatar alagos commented on August 23, 2024

That's the result with console.log(JSON.stringify(editorContent));:

{
  "entityMap": {},
  "blocks": [
    {
      "key": "1n9j8",
      "text": "Hello world",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    }
  ]
}

and after I click the editor to write something and render is reevaluated, the result is:

{
  "entityMap": {},
  "blocks": [
    {
      "key": "a5b87",
      "text": "",
      "type": "unstyled",
      "depth": 0,
      "inlineStyleRanges": [],
      "entityRanges": [],
      "data": {}
    }
  ]
}

from react-draft-wysiwyg.

jpuri avatar jpuri commented on August 23, 2024

Its looks like editor is re-mounting in each render - which I do not expect to be the case, it may be an issue with Modal component.

Ideally editor should have been initialized only once but apparently its content are reset second time also.

from react-draft-wysiwyg.

jpuri avatar jpuri commented on August 23, 2024

@ytwater: you can also check new contentState property (detailed in docs) it can be used not only for initializing but also changing editor content.

from react-draft-wysiwyg.

jpuri avatar jpuri commented on August 23, 2024

@alagos: is your issue resolved, can I do something more to help it ?

from react-draft-wysiwyg.

alagos avatar alagos commented on August 23, 2024

@jpuri at the end I moved to another editor as I couldn't figure out what was going on, sorry about that. Anyway thanks for the help :)

from react-draft-wysiwyg.

kamote avatar kamote commented on August 23, 2024

same issue here using react-bootstrap modal

from react-draft-wysiwyg.

lvian avatar lvian commented on August 23, 2024

@iphonic Thanks for posting, was having problems loading the content stored in the DB back into the editor and your post solved it.

from react-draft-wysiwyg.

andrew-w0711 avatar andrew-w0711 commented on August 23, 2024

@iphonic Thanks for your answer

from react-draft-wysiwyg.

swapster avatar swapster commented on August 23, 2024

when the content has underline/subscript/superscript content, it was rendered as normal text

The same problem. Is there any solution?

from react-draft-wysiwyg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.