在Draft.js编辑器中第一行开头的光标

时间:2016-11-02 00:44:46

标签: reactjs draftjs

我有一个onUpArrow处理程序需要检查游标是否位于Draft.js编辑器中第一行的最开头。编辑器可能包含多行/块。

我发现SelectionStategetAnchorOffset()getStartOffset()方法可以通过返回0告诉我光标位于一行的开头,但该值是在任何行/块的开头返回,而不仅仅是编辑器中的第一行。

issue引用"获取文档的开头或结尾或获取确切的光标位置"但它并没有出现在草案来源中。

有没有人知道如何检测光标是否在编辑器内容的最开头?

2 个答案:

答案 0 :(得分:2)

根据您的支票和editorState.getCurrentContent().getBlockMap().first().getKey() === selectionState.getAnchor/FocusKey()

答案 1 :(得分:1)

以下是我如何使用标准handleKeyCommand函数检测我是否处于DraftJS的第一行开始的示例编辑:

handleKeyCommand(command, editorState) {

  const selectionState = editorState.getSelection()

  const firstline = (editorState.getCurrentContent().getBlockMap().first().getKey() == selectionState.getFocusKey())

  const startofline = (selectionState.getAnchorOffset() == 0)

  if (firstline && startofline) {
    // Your caret is at the start of the first line of your DraftJS editor.
  }
}
相关问题