Hon*_*iao 6 javascript reactjs draftjs
我正在尝试替换 Draft.js 中的最后一个插入
例如,
原始字符串 -> aaazzz
在中间插入 'bbb' 后 -> aaabbbzzz
用'ccc'替换最后一个插入-> aaaccczzz
用 'ddd' 替换最后一个插入 -> aaadddzzz
...
我认为的一种方法是保存插入起点。插入后,保存终点。然后我可以稍后更换范围
这是我的插入代码
const selection1 = editorState.getSelection();
const contentState1 = editorState.getCurrentContent();
const contentState2 = Modifier.insertText(contentState, selection, text);
const editorState2 = EditorState.push(editorState, newContentState);
const selection2 = newEditorState.getSelection();
// here I don't know how to get the range based on selection1 and selection2
Run Code Online (Sandbox Code Playgroud)
我可以用
const start = selection1.getStartOffset();
const end = selection2.getEndOffset();
Run Code Online (Sandbox Code Playgroud)
得到两个数字,它们是起点和终点。
基于定义
replaceText(
contentState: ContentState,
rangeToReplace: SelectionState,
text: string,
inlineStyle?: DraftInlineStyle,
entityKey?: ?string
): ContentState
Run Code Online (Sandbox Code Playgroud)
我需要一个新的选择。如何使用这两个数字或 selection1 和 selection2 创建选择?有没有类似的功能
createSelection(start, end) // not exist
Run Code Online (Sandbox Code Playgroud)
医生说:
var selectionState = SelectionState.createEmpty('blockkey');
var updatedSelection = selectionState.merge({
focusOffset: 0,
anchorOffset:20,
});
Run Code Online (Sandbox Code Playgroud)
所以你需要获取块密钥然后设置偏移量。开始/结束对应于锚点/焦点,因为在您的情况下。
我改变主意了。我的解决方案是
先存旧的editorState。
然后插入到当前的editorState.
当我想插入另一个时,editorState我不是插入到当前的,而是再次插入到旧的副本editorState。
希望这可以给有同样问题的人一些想法。
| 归档时间: |
|
| 查看次数: |
5634 次 |
| 最近记录: |