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

粘贴时图片如何处理成url #260

Closed
liwenjingf opened this issue Aug 23, 2023 · 5 comments
Closed

粘贴时图片如何处理成url #260

liwenjingf opened this issue Aug 23, 2023 · 5 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@liwenjingf
Copy link

What problem does this feature solve?

你好,麻烦问下
1.菜单栏的上传图片在onchange已处理,ctrl+v的时候,怎么处理图片为url呢。;
2.只读模式的时候,还能ctrl+v内容,在哪里禁用呢

What does the proposed API look like?

ctrl+v的时候,我的业务需要先调用上传接口,再用接口返回的url回显

@douglasmatheus
Copy link
Contributor

Hi, I use the following code to work with pasting of images and files. The original code is pure js, so I just adapted the following code for the editor, putting it inside main.ts.

The code needs to be revised for ts, but as it is it works. It is also already handling drag and drop events.

Hope it helps you.

  interface Event {
    clipboardData: any
  }

  container.onpaste = function (event: Event) {
    const items = event.clipboardData.items
    for (const index in items) {
      const item = items[index]
      if (item.kind === 'file') {
        const file = item.getAsFile()
        const fileReader = new FileReader()
        fileReader.readAsDataURL(file)
        fileReader.onload = function () {
          // 计算宽高
          const image = new Image()
          const value = fileReader.result as string
          image.src = value
          image.onload = function () {
            instance.command.executeImage({
              value,
              width: image.width,
              height: image.height
            })
            imageFileDom.value = ''
          }
        }
      }
    }
}
// Optional.   Show the copy icon when dragging over. Seems to only work for chrome.
container.addEventListener('dragover', function (e) {
  if (e) {
      e.stopPropagation()
      e.preventDefault()
      if (e.dataTransfer) {
        e.dataTransfer.dropEffect = 'copy'
      }
  }
})

// Get file data on drop
container.addEventListener('drop', function (e) {
    e.stopPropagation()
    e.preventDefault()
    const files = e?.dataTransfer?.files // Array of all files

    if (files) {
      for (let i = 0; i < files.length; i++) {
        const file = files[i]
        const fileReader = new FileReader()
        fileReader.readAsDataURL(file)
        fileReader.onload = function () {
          // 计算宽高
          const image = new Image()
          const value = fileReader.result as string
          image.src = value
          image.onload = function () {
            instance.command.executeImage({
              value,
              width: image.width,
              height: image.height
            })
            imageFileDom.value = ''
          }
        }
      }
    }
})

@douglasmatheus
Copy link
Contributor

I hadn't noticed, but the editor already handles the paste event with images. I don't know if that's exactly what you wanted, anyway I leave the code in case someone wants to use it.

@Hufe921 Hufe921 added bug Something isn't working enhancement New feature or request labels Aug 23, 2023
@liwenjingf
Copy link
Author

在main.ts重写onpaste也覆盖不了插件里的_paste事件,会同时出现两个图片。我貌似找不到改变_paste的入口

@douglasmatheus
Copy link
Contributor

Yes I realized that later. It would be interesting if @Hufe921 added the paste event inside listener/eventBus.

Anyway @Hufe921 , I realized that drag and drop of images is not working.

@Hufe921 Hufe921 closed this as completed Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants