draftjs - 单击时为实体设置 SelectionState
问题描述
我有一个案例,我可以点击一个实体,然后选择新的内容来替换整个实体。例如:
Here is some text [special entity] and some more text
^ click anywhere inside the brackets
选择新内容后,替换旧的实体文本,变为:
Here is some text [my new entity] and some more text
基本上,我正在尝试做:
const newSelectionState = SelectionState.createEmpty(key).merge({
anchorOffset: entityStart,
focusOffset: entityEnd
});
我试过使用currentContentState.getSelectionBefore(key).getStartOffset()
,currentContentState.getSelectionAfter(key).getStartOffset()
但值似乎不可靠。
我应该如何获得实体的正确开始和结束偏移量?或者,有没有更好的方法来解决这个问题?
解决方案
我找到了一个名为draftjs-utils的库,它有一个名为的方法getEntityRange
,它能够为我提供所需的开始和结束偏移量。
推荐阅读
- vba - 确定一组文件中的最大值
- google-chrome - Chrome javascript调试器未显示悬停值
- swift - 重新加载collectionview后关闭视图
- r - 将函数应用于向量的索引列表?
- javascript - 如何在 Jest 中模拟 Mongoose 'find()' 函数?
- python-3.x - 为所有行复制excel countifs公式
- internet-explorer - 使用 Internet Explorer 在 login.microsoftonline.com 上的 http 400 错误请求,但刷新页面后登录有效
- php - foreach 循环给出不正确的结果
- .htaccess - 使用 htaccess 的页面重定向 301
- angular - 如何解决 npm 和密码中的“@”的代理问题?