如何使用draft.js 更改光标位置? [英] How to change cursor position with draft.js?

查看:67
本文介绍了如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆