How can I set the cursor position after creating an EditorState with content and some decorators on Draft.js. It always starts on the position 0.
This is what happens:

This is what I want:

Here is how I am creating the state.
constructor(props) {
super(props)
this.state = { editorState: this.getEditorState() }
}
getEditorState() {
const { reply: { channel, userAccount } } = this.props
const content = this.getEditorContent({ channel, userAccount })
const decorators = this.getEditorDecorators(channel)
return EditorState.createWithContent(content, decorators)
}
getEditorContent({ channel, userAccount }) {
const content = channel && channel.prefill(userAccount)
return ContentState.createFromText(content || '')
}
getEditorDecorators({ decorators }) {
return getDecorators(decorators || [])
}
After reading the issue 224 from Draft.js repository, I've found a static method called moveSelectionToEnd. Everything that I need to do is wrap the brand new state with this method, this way.
getEditorState() {
const { reply: { channel, userAccount } } = this.props
const content = this.getEditorContent({ channel, userAccount })
const decorators = this.getEditorDecorators(channel)
const state = EditorState.createWithContent(content, decorators)
return EditorState.moveSelectionToEnd(state)
}
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