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()))
  }

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

请帮忙。

用这个修复

_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))
  }

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

最新更新