我有一个需要多个文本输入的应用程序,为了格式化和自定义,我选择了 draft-js
作为我的编辑器,但是我遇到了一个非常令人困惑的输入问题。
当我在编辑器中输入时,最近按下的键被打印在编辑器的开头,颠倒了我的整个输入,就好像插入符总是在行的第一个索引处。
我有3个编辑器,每个编辑器都有一个 onChange,它会使用当前编辑器的 contentState
更新 redux 存储。当页面重新渲染时,每个编辑器都会呈现其相应的 contentState
转换为 EditorState
。
这是我的代码:
main.js
render() {
/* Each Editor has a similar block as below */
let coverEditorState = EditorState.createEmpty()
let coverContentState = _.get(data, 'details.message.cover.contentState')
if (typeof coverContentHTML != "undefined"){
coverEditorState = EditorState.createWithContent(coverContentState)
}
return (
...
<Composer
editorState={coverEditorState}
onChange={this._handleCoveringLetterChange.bind(this)}
/>
...
)
}
Composer.js
class Composer extends Component {
constructor() {
super()
this.state = { editorState: EditorState.createEmpty(), styleMap: {} }
}
componentWillReceiveProps( nextProps ) {
this.setState({ editorState: nextProps.editorState })
}
onChange( editorState ) {
let nextState = Object.assign({}, this.state, { editorState })
let currentContentState = editorState.getCurrentContent()
let changeObject = {
contentState: currentContentState
}
this.setState(nextState)
this.props.onChange(changeObject)
}
render() {
return (
<Editor
editorState={this.state.editorState}
onChange={this.onChange.bind(this)}
/>
)
}
}
我尝试返回SelectionState
和ContentState
,将两者合并并重新渲染,但这只会导致更多的问题和错误。