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.
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.
Share asked Dec 27, 2016 at 23:43 ryudiceryudice 37.5k33 gold badges121 silver badges164 bronze badges 1-
blockMap
is aOrderedMap
– Jiang YD Commented Dec 28, 2016 at 2:59
2 Answers
Reset to default 5Not 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/cczhL821/
you can get the caret positon like below
editorState.getSelection().getFocusOffset()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043795a4607969.html
评论列表(0条)