微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。