GithubHelp home page GithubHelp logo

Comments (6)

niuware avatar niuware commented on September 3, 2024 12

Thanks for using this component!
The MUIRichTextEditor component expects a stringified Draft.Model.Encoding.RawDraftContentState object, so you can do the following:

...
import { convertFromHTML, ContentState, convertToRaw } from 'draft-js'
import MUIRichTextEditor from '../../'

const sampleMarkup = '<b>Bold text</b>, <i>Italic text</i><br/ ><br />Other text<br /><br /><a href="http://myurl.com">Some link</a>'

// 1. Convert the HTML
const contentHTML = convertFromHTML(sampleMarkup)

// 2. Create the ContentState object
const state = ContentState.createFromBlockArray(contentHTML.contentBlocks, contentHTML.entityMap)

// 3. Stringify `state` object from a Draft.Model.Encoding.RawDraftContentState object
const content = JSON.stringify(convertToRaw(state))

...

<MUIRichTextEditor 
    value={content} 
/>

I have updated the examples directory as well if you want to take a look. Hope this helps you out!

from mui-rte.

niuware avatar niuware commented on September 3, 2024 3

Hi @felipenmoura
You have several alternatives for the task you want to accomplish.

  1. If you want to use an external button, please check the ref-save included example.

  2. If you want to use the included onSave property, note that the handler will receive the stringified content state, so you will have to use convertFromRaw to first parse it into a ContentState object, and then treat it accordingly, in this case pass it to the plugin you want to use.

  3. The last option is creating your own callback custom control so when the user clicks you can use the getCurrentContent method to get the ContentState and then use the plugin you want to use.

Hope one of this options helps you out!

from mui-rte.

felipenmoura avatar felipenmoura commented on September 3, 2024 2

Oh, I see, It worked!
Thanks for the answer, and for updating the documentation :)

from mui-rte.

felipenmoura avatar felipenmoura commented on September 3, 2024 1

Great, thanks, it helped a lot.

from mui-rte.

felipenmoura avatar felipenmoura commented on September 3, 2024

Sorry for bothering again @niuware !
Is there a way I can grab the final HTML content after the user has finished editing?
I see I can listen for each and every change (which gives me an object). I wanted to avoid having to parse it back to HTML on each keystroke just to be able to use it when the user clicks the "Finish" button. Any alternative?

Also, I noticed the onSave icon, I just want to trigger that event from a different button outside the editor.
As for the HTML content, I'm trying to use the module draft-js-export-html, but it explodes in an internal error! (TypeError: this.contentState.getBlocksAsArray is not a function)

Thanks once again :)

from mui-rte.

felipenmoura avatar felipenmoura commented on September 3, 2024

Ops, accidentally reopened the issue!

from mui-rte.

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.