如何使用draft.js 更改光标位置? [英] How to change cursor position with draft.js?
问题描述
我想知道如何在对键盘命令进行文本插入后更改draft.js 中的光标位置.因此,我目前正在使用 _handleKeyCommand(cmd)
在用户按下特定按钮时插入自定义文本块.接下来我尝试了以下操作:
I am wondering how you can change the cursor position in draft.js after having done an text insertion on key command. Therefore I am currently using _handleKeyCommand(cmd)
to insert a custom text block whenever the user presses a specific button. Next I tried the following:
currentState = this.state.editorState;
var selectionState = this.state.editorState.getSelection().getStartKey();
this.setState({editorState: EditorState.forceSelection(currentState, selectionState)});
但是这会一直崩溃并显示错误消息
But this keeps crashing with the error message
TypeError: selection.getHasFocus 不是函数
TypeError: selection.getHasFocus is not a function
虽然我导入了 SelectionState
.那么如何正确更改光标位置(最好是插入前的位置)以及我在这里做错了什么?
although I imported SelectionState
. So how do I properly change the cursor position (preferably to the position before the insertion) and what am I doing wrong here?
推荐答案
var selectionState = this.state.editorState.getSelection().getStartKey();
应该是 var selectionState = this.state.editorState.getSelection()
这篇关于如何使用draft.js 更改光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!