I have this react component that has to abstract draft-js
editor.
My redux store will have this description
field that must be a regular HTML string. The editor should be able to take a value
HTML string and parse it into its own internal things (draftjs stuff). When changing the content, it should fire onChange
prop with the final HTML content. In other words, it should be transparent to the outside world what's going on inside this component.
Right now, my component looks like this:
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { Editor, EditorState, ContentState, convertFromHTML } from 'draft-js'
import { stateToHTML } from 'draft-js-export-html'
export const getStateFromHTML = (html) => {
const blocksFromHTML = html && convertFromHTML(html)
if (blocksFromHTML) {
const state = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap,
)
return EditorState.createWithContent(state)
}
return EditorState.createEmpty()
}
export default class WYSIWYG extends Component {
static propTypes = {
value: PropTypes.string,
onChange: PropTypes.func,
}
static defaultProps = {
value: '',
onChange: Function.prototype,
}
state = { editorState: null }
componentWillMount() {
this.setEditorState(this.props.value)
}
componentWillReceiveProps({ value }) {
this.setEditorState(value)
}
onChange = (editorState) => {
this.setState({ editorState })
const rawData = stateToHTML(editorState.getCurrentContent())
this.props.onChange(rawData)
}
setEditorState(value) {
this.setState({ editorState: getStateFromHTML(value) })
}
render() {
return (
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
/>
)
}
}
By some reason, this is not working. It seems that getCurrentContent
is called before the state is actually updated. I'm not really able to think this through.
I'm open to any [new] ways to handle this situation; I just need to be able to send an HTML value in and get an HTML value out. I also accept indications of other WYSIWYG plugins that will do the job.