Javascript 如何在js草案中使用replaceWithFragment?
我需要替换“前缀”什么用户类型的自动完成文本。 在文档中我发现了这个,但我真的不知道如何使用它Javascript 如何在js草案中使用replaceWithFragment?,javascript,reactjs,autocomplete,editor,draftjs,Javascript,Reactjs,Autocomplete,Editor,Draftjs,我需要替换“前缀”什么用户类型的自动完成文本。 在文档中我发现了这个,但我真的不知道如何使用它 replaceWithFragment( contentState: ContentState, targetRange: SelectionState, fragment: BlockMap ): ContentState 我尝试键入此解决方案,但在从用户处获得带有“前缀”的选择之前,我是如何解释的 例如,当我键入“SEL”时,我的自动完成为我查找 将单击该建议,在编辑器中我得到了“
replaceWithFragment(
contentState: ContentState,
targetRange: SelectionState,
fragment: BlockMap
): ContentState
我尝试键入此解决方案,但在从用户处获得带有“前缀”的选择之前,我是如何解释的
例如,当我键入“SEL”时,我的自动完成为我查找
将单击该建议,在编辑器中我得到了“SELECT”
当我键入“SEL”我的自动完成为我查找“SELECT”时,我会点击那个建议,在编辑器中我得到了“SELSELECT”。我为它创建了一个方法。。。。 不知道如何使用草稿
gettingSuggestion = (str, str2) =>{
let remain ="";
str = str.toUpperCase();//for compare
for( let i = 0 ; i<str2.length ; i++){
if(str2[i]!==str[i]){ //if not equal -> we are searching for remaining characters
remain= remain.concat(str2[i]);//remain + suggestion
}
}
return(remain);
}
suggestionSelected(value){
const { editorState } = this.state;
const values =this.value.split(" ");//for up to date values
const remainer = this.gettingSuggestion(values[this.index], value);//remaining values
const pastedBlocks = ContentState.createFromText(remainer).blockMap; //get blockMap
const newState = Modifier.replaceWithFragment( //replaceWithFragment -> set new state editor
editorState.getCurrentContent(),//ContentState
editorState.getSelection(),//SelectionState
pastedBlocks//BlockMap
);
this.setState(() => ({
suggestion: [],
editorState: EditorState.push(editorState, newState, 'insert-fragment')//Inserting values
}));
}//End of suggestionSelected
gettingSuggestion=(str,str2)=>{
让我们继续“”;
str=str.toUpperCase();//用于比较
对于(设i=0;i),我们正在搜索剩余的字符
reside=reside.concat(str2[i]);//reside+建议
}
}
返回(保留);
}
suggestionSelected(值){
const{editorState}=this.state;
const values=this.value.split(“”;//用于最新值
const remainer=this.gettingSuggestion(值[this.index],value);//剩余值
const pastedBlocks=ContentState.createFromText(remainer).blockMap;//获取块映射
const newState=Modifier.replaceWithFragment(//replaceWithFragment->set new state编辑器
editorState.getCurrentContent(),//ContentState
editorState.getSelection(),//SelectionState
粘贴块//块映射
);
此.setState(()=>({
建议:[],
editorState:editorState.push(editorState,newState,'insert fragment')//插入值
}));
}//建议结束当选
gettingSuggestion = (str, str2) =>{
let remain ="";
str = str.toUpperCase();//for compare
for( let i = 0 ; i<str2.length ; i++){
if(str2[i]!==str[i]){ //if not equal -> we are searching for remaining characters
remain= remain.concat(str2[i]);//remain + suggestion
}
}
return(remain);
}
suggestionSelected(value){
const { editorState } = this.state;
const values =this.value.split(" ");//for up to date values
const remainer = this.gettingSuggestion(values[this.index], value);//remaining values
const pastedBlocks = ContentState.createFromText(remainer).blockMap; //get blockMap
const newState = Modifier.replaceWithFragment( //replaceWithFragment -> set new state editor
editorState.getCurrentContent(),//ContentState
editorState.getSelection(),//SelectionState
pastedBlocks//BlockMap
);
this.setState(() => ({
suggestion: [],
editorState: EditorState.push(editorState, newState, 'insert-fragment')//Inserting values
}));
}//End of suggestionSelected