【发布时间】:2020-08-25 13:21:30
【问题描述】:
我正在使用draftjs 创建一个富文本编辑器。这是最小的codesandbox,因此您可以了解问题所在。
所以我有一个辅助函数 getCurrentTextSelection 可以返回我选择的文本:
const getCurrentTextSelection = (editorState: EditorState): string => {
const selectionState = editorState.getSelection();
const anchorKey = selectionState.getAnchorKey();
const currentContent = editorState.getCurrentContent();
const currentContentBlock = currentContent.getBlockForKey(anchorKey);
const start = selectionState.getStartOffset();
const end = selectionState.getEndOffset();
const selectedText = currentContentBlock.getText().slice(start, end);
return selectedText;
};
当我在 TextEditor 外部单击时,焦点会丢失,因此不会选择文本(但对于 editorState 保持选中状态)。
是否有使用editorState 重新选择此文本的编程方式?这样当您单击Select text again 按钮时,TextEditor 中的文本就会被选中。
【问题讨论】:
标签: javascript reactjs typescript draftjs