javascript - Get caret position (line number) in draft.js - Stack Overflow

How do you get the caret position in draft.js? I guess you can get the block from the selectionstate an

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 a OrderedMap – Jiang YD Commented Dec 28, 2016 at 2:59
Add a ment  | 

2 Answers 2

Reset to default 5

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/cczhL821/

you can get the caret positon like below

editorState.getSelection().getFocusOffset()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043795a4607969.html

相关推荐

  • javascript - Get caret position (line number) in draft.js - Stack Overflow

    How do you get the caret position in draft.js? I guess you can get the block from the selectionstate an

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信