Stumbled on this cool text editor, draft.js by Facebook. I tried to follow the example in Github, but I want to create an editor with content instead of an empty editor.
var EditorState = Draft.EditorState; var RichEditor = React.createClass({ getInitialState(){ return {editorState: EditorState.createWithContent("Hello")} //the example use this code to createEmpty editor // return ({editorState: EditorState.createEmpty()}) } });
When I run it, it throws an error with the following message "Uncaught TypeError: contentState.getBlockMap is not a function".
I've created a set of packages for DraftJS to help with importing and exporting content (HTML/Markdown). I use these in my project react-rte. The one you're probably looking for is: draft-js-import-html on npm.
For importing styled content Draft.js provides convertFromRaw and convertFromHTML utility functions. convertFromRaw function takes raw javascript object as parameter. Here, we are using a JSON stringified javascript object as content source:
Draft.js has the power to make text bold, italic, etc, but by default it won’t do any of that unless you tell it to. There are a few ways to go about adding this functionality. We could add some buttons that you click to insert formatting!
⚠️ Warning: Draft.js is a framework meant to be used with React, so if you already have a project you want to add a rich text editor to, but it’s written using a different library, like Vue, you may want to look at more suitable editor options. JefMari/awesome-wysiwyg is a great resource for looking at all your options.
The first argument to EditorState.createWithContent is a ContentState
, not a string. You need to import ContentState
var EditorState = Draft.EditorState; var ContentState = Draft.ContentState;
Use ContentState.createFromText And pass the result to EditorState.createWithContent.
return { editorState: EditorState.createWithContent(ContentState.createFromText('Hello')) };
I've created a set of packages for DraftJS to help with importing and exporting content (HTML/Markdown). I use these in my project react-rte. The one you're probably looking for is: draft-js-import-html on npm.
npm install draft-js-import-html
An example of how you might use it:
var stateFromHTML = require('draft-js-import-html').stateFromHTML; var EditorState = Draft.EditorState; var RichEditor = React.createClass({ getInitialState() { let contentState = stateFromHTML('<p>Hello</p>'); return { editorState: EditorState.createWithContent(contentState) }; } });
The modules I've published are:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With