Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draftjs how to initiate an editor with content

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".

like image 913
vdj4y Avatar asked Mar 09 '16 06:03

vdj4y


People also ask

Is there a way to import and export HTML content using draftjs?

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.

How to import styled content in draft using JSON stringified JavaScript Object?

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:

How do I make text bold in draftdraft JS?

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!

Can I add a rich text editor to a React project?

⚠️ 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.


2 Answers

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')) }; 
like image 58
Brigand Avatar answered Nov 04 '22 09:11

Brigand


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:

  • draft-js-import-html
  • draft-js-export-html
  • draft-js-import-markdown
  • draft-js-export-markdown
like image 34
sstur Avatar answered Nov 04 '22 10:11

sstur