Javascript Draft.js将代码块应用于粘贴的JSON数据
在使用draft.js时,我在编辑器中检查了粘贴的文本。如果是JSON消息,我希望它应用一个代码块。我目前有以下设置:Javascript Draft.js将代码块应用于粘贴的JSON数据,javascript,reactjs,draftjs,Javascript,Reactjs,Draftjs,在使用draft.js时,我在编辑器中检查了粘贴的文本。如果是JSON消息,我希望它应用一个代码块。我目前有以下设置: handlePastedText = (text, html, editorState) => { const newContent = ContentState.createFromText(text); const blockMap = newContent.getBlockMap(); const newContentState = Modifier.repl
handlePastedText = (text, html, editorState) => {
const newContent = ContentState.createFromText(text);
const blockMap = newContent.getBlockMap();
const newContentState = Modifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
blockMap
);
try {
JSON.parse(text)
} catch (error) {
this.setState({
editorState: EditorState.push(
editorState,
newContentState,
'insert-fragment'
)
});
return 'handled';
}
this.setState({
editorState: EditorState.push(
editorState,
newContentState,
'insert-fragment'
)
});
this.onChange(RichUtils.toggleBlockType(editorState, 'code-block'));
return 'handled';
})
但是,当我运行这个程序时,它只会使编辑器成为一个代码块,而不会插入任何文本