【发布时间】:2016-08-03 16:51:25
【问题描述】:
我在 editorState 上做了一些棘手的状态突变,但我失去了选择。
我需要获取 currentText(),使用一些魔术库转换为 HTML,然后将其转换回 editorState。效果很好,只是选择太难了。
现在,我正在尝试在第一次开始时进行选择,然后执行 forceSelection,但由于与 selection.hasFocus() 相关的一些错误而失败(这似乎并不真正相关......)。
我猜我需要根据锚点和偏移量计算“新”选择,但不太确定,有什么想法吗?
现在我的代码如下所示:
// onChangeHandler:
const currentContentState = editorState.getCurrentContent()
const selectionState = editorState.getSelection()
const plainHtml = magicOperation(currentContentState.getPlainText())
const currentContentBlocks = convertFromHTML(plainHtml)
const contentState = ContentState.createFromBlockArray(currentContentBlocks)
const newEditorState = EditorState.createWithContent(contentState)
this.setState({
editorState: EditorState.forceSelection(
newEditorState,
selectionState
)
})
是一个 hack,我知道我只是在玩 DraftJS 如果我能做到这一点,如果我让它顺利运行,我肯定会使用装饰器在 editorState 中添加 HTML。
感谢您的宝贵时间!
【问题讨论】: