Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Draft.js将代码块应用于粘贴的JSON数据_Javascript_Reactjs_Draftjs - Fatal编程技术网

Javascript 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

在使用draft.js时,我在编辑器中检查了粘贴的文本。如果是JSON消息,我希望它应用一个代码块。我目前有以下设置:

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';
})

但是,当我运行这个程序时,它只会使编辑器成为一个代码块,而不会插入任何文本