DraftJS:如何以编程方式将焦点(光标)移动到右侧的某个字符?

acm*_*une 5 javascript draftjs

我使用以下命令在编辑器中插入一些文本:

_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()))
  }
Run Code Online (Sandbox Code Playgroud)

但插入后,光标停留在插入点的锚位置。我想将其移动到插入文本的末尾,这样我就可以继续编辑而无需手动移动光标。

请帮忙。

acm*_*une 3

修复了这个

_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))
  }
Run Code Online (Sandbox Code Playgroud)

如果有更好的解决方案,请告诉我。