Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从文件拖拽图片到富文本框以及复制图片到文本框,都会跳过上传服务器,仍然显示的是base64格式 #455

Open
zhangzor opened this issue Feb 24, 2023 · 5 comments
Labels

Comments

@zhangzor
Copy link

直接从文件拖拽图片到富文本输入框内,会跳过上传服务器操作。
image

@zhangzor zhangzor added the bug label Feb 24, 2023
@Zou-Wen
Copy link

Zou-Wen commented Apr 10, 2023

https://www.codemzy.com/blog/tiptap-pasting-images

可以参考这篇文章关于粘贴图片的自定义

@zhangzor
Copy link
Author

zhangzor commented Apr 11, 2023 via email

@Willy-Woncat
Copy link

pasteImg(s) {
      let pattern = /<img src="data:image\/.*?;base64,([^"]+)"[^>]*>/g
      const base64TextArr = []
      let matches = pattern.exec(s)
      if (matches) {
        while (matches) {
          const base64Text = matches[1]
          base64TextArr.push(base64Text)
          matches = pattern.exec(s)
        }
        const data = {}
        data.base64String = base64TextArr[0]
        // 上传操作
        upload(data).then(resp => {
          this.$nextTick(() => {
            pattern = /<img src="data:image\/.*?;base64,([^"]+)"/g
            this.content = s.replace(pattern, '<img src="api/' + 上传之后的url + '"')
          })
        })
      }
    }

el-tiptap节点的onUpdate加上这个,就能将base64文本变成你所需要的

@Bloodcapillary
Copy link

pasteImg(s) {
      let pattern = /<img src="data:image\/.*?;base64,([^"]+)"[^>]*>/g
      const base64TextArr = []
      let matches = pattern.exec(s)
      if (matches) {
        while (matches) {
          const base64Text = matches[1]
          base64TextArr.push(base64Text)
          matches = pattern.exec(s)
        }
        const data = {}
        data.base64String = base64TextArr[0]
        // 上传操作
        upload(data).then(resp => {
          this.$nextTick(() => {
            pattern = /<img src="data:image\/.*?;base64,([^"]+)"/g
            this.content = s.replace(pattern, '<img src="api/' + 上传之后的url + '"')
          })
        })
      }
    }

el-tiptap节点的onUpdate加上这个,就能将base64文本变成你所需要的

图片上面的气泡菜单应该怎么才能禁用呢,怎么直接{bubble: false}无效啊

@yun77op
Copy link

yun77op commented Dec 16, 2023

https://www.codemzy.com/blog/tiptap-pasting-images

可以参考这篇文章关于粘贴图片的自定义

onPaste可以使用这种方式绑定事件,但绑定onDrop后,如果从电脑里拖入图片到编辑器,却不会触发onDrop。可以参看 https://discuss.prosemirror.net/t/handle-image-drop-from-file-system/1195

扩展element-tiptap的Image的插件,如果通过handleDOMEvents下的drop可以监听原生drop事件

import { Image } from 'element-tiptap'
import { Plugin } from 'tiptap'

async function upload(file) {
  // upload file to server
}
export default class CustomImage extends Image {
  get plugins() {
    return [
      new Plugin({
        props: {
          handleDOMEvents: {
            drop(view, event) {
              event.preventDefault()

              const hasFiles = event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files.length

              if (!hasFiles) {
                return
              }

              const images = Array.from(event.dataTransfer.files).filter(file => /image/i.test(file.type))

              if (images.length === 0) {
                return
              }

              event.preventDefault()

              const { schema } = view.state
              const coordinates = view.posAtCoords({ left: event.clientX, top: event.clientY })

              images.forEach(image => {
                upload(image).then(url => {
                  const node = schema.nodes.image.create({
                    src: url
                  })
                  const transaction = view.state.tr.insert(coordinates.pos, node)
                  view.dispatch(transaction)
                })

              })
            }
          }
        }
      })
    ]
  }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants