admin管理员组文章数量:1363230
I am trying to replace the last insert in Draft.js
For example,
Orignal string -> aaazzz
After inserting 'bbb' in the middle -> aaabbbzzz
Replace last insert by 'ccc' -> aaaccczzz
Replace last insert by 'ddd' -> aaadddzzz
...
One way I thought is saving the insert start point. After inserting, save the end point. Then I can replace the range later
This is my inserting code
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
I can use
const start = selection1.getStartOffset();
const end = selection2.getEndOffset();
to get two numbers which are start and end points.
Based on the definition of
replaceText(
contentState: ContentState,
rangeToReplace: SelectionState,
text: string,
inlineStyle?: DraftInlineStyle,
entityKey?: ?string
): ContentState
I need get a new selection. How to create the selection using those two numbers OR selection1 and selection2? Is there any function like
createSelection(start, end) // not exist
I am trying to replace the last insert in Draft.js
For example,
Orignal string -> aaazzz
After inserting 'bbb' in the middle -> aaabbbzzz
Replace last insert by 'ccc' -> aaaccczzz
Replace last insert by 'ddd' -> aaadddzzz
...
One way I thought is saving the insert start point. After inserting, save the end point. Then I can replace the range later
This is my inserting code
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
I can use
const start = selection1.getStartOffset();
const end = selection2.getEndOffset();
to get two numbers which are start and end points.
Based on the definition of
replaceText(
contentState: ContentState,
rangeToReplace: SelectionState,
text: string,
inlineStyle?: DraftInlineStyle,
entityKey?: ?string
): ContentState
I need get a new selection. How to create the selection using those two numbers OR selection1 and selection2? Is there any function like
createSelection(start, end) // not exist
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Oct 26, 2017 at 23:41
Hongbo MiaoHongbo Miao
50.1k67 gold badges200 silver badges328 bronze badges
2 Answers
Reset to default 5the doc says:
var selectionState = SelectionState.createEmpty('blockkey');
var updatedSelection = selectionState.merge({
focusOffset: 0,
anchorOffset:20,
});
so you need to get the block key then set offset. the start/end correspond to anchor/focus because in your situation.
I changed my mind. My solution is
Save the old
editorState
first.Then insert to the current
editorState
.When I want to insert another one, instead of insert to the current
editorState
, I insert to the copy of the oldeditorState
again.
Hopefully, this can give someone who have the same question some ideas.
本文标签: javascriptHow to create a selection based on start and end in DraftjsStack Overflow
版权声明:本文标题:javascript - How to create a selection based on start and end in Draft.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743811513a2543154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论