DraftJS:如何以编程方式将焦点(光标)移动到右侧的某个字符?
DraftJS: how to move the focus (cursor) to some character on the right, programmatically?
我正在使用这个在编辑器中插入一些文本:
_insertText(text) {
const { editorState, onChange } = this.props
const newContentState = Modifier.insertText(
editorState.getCurrentContent(),
editorState.getSelection(),
text
)
const newState = EditorState.createWithContent(newContentState)
onChange(EditorState.acceptSelection(newState, editorState.getSelection()))
}
但插入后,光标停留在锚点位置,在插入点。我想把它移到插入文本的末尾,这样我就可以继续编辑而不必手动移动光标。
请帮忙。
已修复
_insertText(text) {
const { editorState, onChange } = this.props
const newContentState = Modifier.insertText(
editorState.getCurrentContent(),
editorState.getSelection(),
text
)
const newState = EditorState.createWithContent(newContentState)
const nextOffSet = editorState.getSelection().getFocusOffset() + text.length
const newSelection = editorState.getSelection().merge({
focusOffset: nextOffSet,
anchorOffset: nextOffSet
})
onChange(EditorState.acceptSelection(newState, newSelection))
}
如果有更好的解决办法,请告诉我。
我正在使用这个在编辑器中插入一些文本:
_insertText(text) {
const { editorState, onChange } = this.props
const newContentState = Modifier.insertText(
editorState.getCurrentContent(),
editorState.getSelection(),
text
)
const newState = EditorState.createWithContent(newContentState)
onChange(EditorState.acceptSelection(newState, editorState.getSelection()))
}
但插入后,光标停留在锚点位置,在插入点。我想把它移到插入文本的末尾,这样我就可以继续编辑而不必手动移动光标。
请帮忙。
已修复
_insertText(text) {
const { editorState, onChange } = this.props
const newContentState = Modifier.insertText(
editorState.getCurrentContent(),
editorState.getSelection(),
text
)
const newState = EditorState.createWithContent(newContentState)
const nextOffSet = editorState.getSelection().getFocusOffset() + text.length
const newSelection = editorState.getSelection().merge({
focusOffset: nextOffSet,
anchorOffset: nextOffSet
})
onChange(EditorState.acceptSelection(newState, newSelection))
}
如果有更好的解决办法,请告诉我。