在 Draft.js 中获取插入符位置(行号)

ryu*_*ice 2 javascript reactjs draftjs

如何获得 Draft.js 中的插入符号位置?我想你可以从选择状态中获取块,然后获取块数组并查看块数组位于哪个位置,但我不确定这是一种可靠的方法,甚至是最好的方法。

tob*_*sen 5

不确定这是否是您的意思,但您可以像这样获取当前块的索引:

const currentBlockKey = editorState.getSelection().getStartKey()
const currentBlockIndex = editorState.getCurrentContent().getBlockMap()
    .keySeq().findIndex(k => k === currentBlockKey)
Run Code Online (Sandbox Code Playgroud)

这是一个完整示例的小提琴:https ://jsfiddle.net/cczhL821/