最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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()
发布评论

评论列表(0)

  1. 暂无评论