在 draft.js 中获取插入符位置(行号)
Get caret position (line number) in draft.js
如何获得 draft.js 中的插入符位置?我猜你可以从selectionstate中获取block,然后获取block数组,看看block数组在哪个位置,但我不确定这是一个可靠的方式,甚至是最好的方式。
不确定这是否是你的意思,但你可以像这样获取当前块的索引:
const currentBlockKey = editorState.getSelection().getStartKey()
const currentBlockIndex = editorState.getCurrentContent().getBlockMap()
.keySeq().findIndex(k => k === currentBlockKey)
这里有一个 fiddle 和一个完整的例子:https://jsfiddle.net/cczhL821/
如何获得 draft.js 中的插入符位置?我猜你可以从selectionstate中获取block,然后获取block数组,看看block数组在哪个位置,但我不确定这是一个可靠的方式,甚至是最好的方式。
不确定这是否是你的意思,但你可以像这样获取当前块的索引:
const currentBlockKey = editorState.getSelection().getStartKey()
const currentBlockIndex = editorState.getCurrentContent().getBlockMap()
.keySeq().findIndex(k => k === currentBlockKey)
这里有一个 fiddle 和一个完整的例子:https://jsfiddle.net/cczhL821/