Code Monkey home page Code Monkey logo

Comments (14)

davisml avatar davisml commented on September 21, 2024 12

To retrieve the raw data call this:

var rawData = Draft.convertToRaw(editorState.getCurrentContent())

To create an EditorState from the raw data:

var contentState = Draft.ContentState.createFromBlockArray(Draft.convertFromRaw(rawData))
var editorState = Draft.EditorState.createWithContent(contentState)

from draft-js.

c-h- avatar c-h- commented on September 21, 2024 6

Not sure why but the only way I was able to restore state was by skipping the middle step in the above comment like so:

var rawData = Draft.convertToRaw(editorState.getCurrentContent())
var editorState = Draft.EditorState.createWithContent(Draft.convertFromRaw(rawData))

Just in case anyone else comes here and has issues when trying to use Draft.ContentState.createFromBlockArray!

from draft-js.

hellendag avatar hellendag commented on September 21, 2024 5

Since we don't use markup or markdown to store rich content at Facebook, I haven't built any such utilities for this repo. (Since I wouldn't want to build and release something we aren't using in prod on FB.)

Feel free to create such a utility, though -- I'm sure there will be others who need it too.

If you just need to render your content statically within React, you can use the readOnly prop on Editor.

from draft-js.

davisml avatar davisml commented on September 21, 2024 5

Here is a gist that converts DraftJS raw block data to HTML:

https://gist.github.com/davisml/2b5184f64c59eb3745fd

Supports H1, H2, blockquote, ul, ol, strong, em and underline styles mapped to the rich text example. Uses a modified version of AttributedString.js (https://github.com/cohitre/attributedString.js) to format the style ranges.

from draft-js.

whogopu avatar whogopu commented on September 21, 2024 2

@davisml

To retrieve the raw data call this:

var rawData = Draft.convertToRaw(editorState.getCurrentContent())

To create an EditorState from the raw data:

var contentState = Draft.convertFromRaw(rawData) // convertFromRow already returns contentState, no need to 'createFromBlockArray()'
var editorState = Draft.EditorState.createWithContent(contentState)

from draft-js.

jamesbrewerdev avatar jamesbrewerdev commented on September 21, 2024 1

@hellendag @cogell I'm going to start working on conversion utilities for both Markdown and HTML this evening. I'll submit a PR when they're done, in case they are useful for the project.

from draft-js.

brandondurham avatar brandondurham commented on September 21, 2024

Fabulous. Thanks so much!

from draft-js.

hellendag avatar hellendag commented on September 21, 2024

:)

from draft-js.

cogell avatar cogell commented on September 21, 2024

Is there a way to get rawContent mapped to HTML with our already defined decorators, styleMap, blockStyleFn, etc

from draft-js.

cogell avatar cogell commented on September 21, 2024

Thanks for your quick reply. Will do!

from draft-js.

zhang-z avatar zhang-z commented on September 21, 2024

@cogell , +1 for retrieve HTML content util
@hellendag , wondering how you handle rich content at FB :)

from draft-js.

cogell avatar cogell commented on September 21, 2024

@BRWR Thanks, I haven't had time to start anything on that utility. Happy to review and be of assistance since I'll need this moving forward on my project as well.

from draft-js.

Rahulgarg30591 avatar Rahulgarg30591 commented on September 21, 2024

Hello, I have also tried draft-Js-export stateToHtml function, but the function is not working in IE is there any other way to get HTML from DraftJS editor?

from draft-js.

dmanohar2k9 avatar dmanohar2k9 commented on September 21, 2024

Manohar D

from draft-js.

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.