Get caret position (line number) in draft.js

天大地大妈咪最大 提交于 2021-01-29 03:06:25

问题


How do you get the caret position in draft.js? I guess you can get the block from the selectionstate and then get the block array and see at which position the block array is, but I'm not sure this is a reliable way, or even the best way.


回答1:


Not sure if this is what you mean, but you can get the index of the current block like this:

const currentBlockKey = editorState.getSelection().getStartKey()
const currentBlockIndex = editorState.getCurrentContent().getBlockMap()
    .keySeq().findIndex(k => k === currentBlockKey)

Here's a fiddle with a full example: https://jsfiddle.net/cczhL821/



来源:https://stackoverflow.com/questions/41353569/get-caret-position-line-number-in-draft-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!