Draft-Js 未使用图像 URL 显示我的图像

问题描述

因此,我目前正在尝试能够按照本 [教程] [1] 粘贴图像。我遇到的问题是我的代码与本教程完全相同,每次粘贴图像时,它就像两次换行而没有文本一样。我的 handlePastedFiles 函数运行并返回 URL,然后我的 insertimage 运行并将链接传递给 insertimage 函数

此外,图片网址来自云存储,在浏览器中查看时该网址有效。

这是我的 handlePastedFiles 函数

const handlePastedFiles = (files) => {
console.log('handlePastedFiles is running')
const formData = new FormData();
formData.append('file',files[0]) 
console.log(files)
fetch(url,{method: 'POST',body: formData})
.then(res => {
  return res.json()
})
.then(data => {
  console.log(data)
  if (data.message) { 
      console.log(data.message)
      setEditorState(insertimage(data.message)) //created below
  } else {
    console.log('no file found')
  }
}).catch(err => {
    console.log(err) 
})

}

这是我的 insertimages 函数

const insertimage = ( url) => {
console.log('!!!!!',url)
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity(
    'IMAGE','IMMUTABLE',{ src: url },)
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const newEditorState = EditorState.set( editorState,{ currentContent: contentStateWithEntity });
return AtomicBlockUtils.insertAtomicBlock(newEditorState,entityKey,' ');

};

非常感谢任何帮助,如果我需要更多信息,请告诉我。先感谢您! [1]:https://daveteu.medium.com/draftjs-insert-paste-images-into-your-content-820159025258

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)