npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

quill-image-drop-and-paste-zoi

v1.0.4

Published

A quill editor module for drop and paste image, with a callback hook before insert image into the editor

Downloads

7

Readme

QuillImageDropAndPaste

A quill editor module for drop and paste image, with a callback hook before inserting image into the editor.

This module was forked from [quill-image-drop-module]: https://www.npmjs.com/package/quill-image-drop-module The only difference was that we could choose how to handle the image we just dropped or pasted, without inserting a base64 url image into the editor directly. For example, a base64 string was too large, if we saved it into the database, it could easilly out of the size of the column, the best practice was to save the image on our server and returned the image's url, and finally we inserted the image with the returned url into the editor.

Examples

React Demo

Vue Demo

Simple Web Demo

Install

npm install quill-image-drop-and-paste --save

Usage

ES6

import Quill from 'quill'
import QuillImageDropAndPaste from 'quill-image-drop-and-paste'
import { base64StringToBlob } from 'blob-util'

Quill.register('modules/imageDropAndPaste', QuillImageDropAndPaste)

const quill = new Quill('#editor-container', {
  modules: {
    imageDropAndPaste: {
      // add an custom image handler
      handler: imageHandler
    }
  }
})

/**
* Do something to our dropped or pasted image
* @param.imageDataUrl - image's base64 url
* @param.type - image's mime type
* @param.file - File Object
*/
function imageHandler(imageDataUrl, type, file) {
  // give a default mime type if the type was null
  if (!type) type = 'image/png'

  // base64 to blob
  var blob = base64StringToBlob(base64URL.replace(/^data:image\/\w+;base64,/, ''), type)

  var filename = ['my', 'cool', 'image', '-', Math.floor(Math.random() * 1e12), '-', new Date().getTime(), '.', type.match(/^image\/(\w+)$/i)[1]].join('')

  // generate a form data
  var formData = new FormData()
  formData.append('filename', filename)
  formData.append('file', blob)

  // upload image to your server
  callUploadAPI(your_upload_url, formData, (err, res) => {
    if (err) return
    // success? you should return the uploaded image's url
    // then insert into the quill editor
    const index = (quill.getSelection() || {}).index || quill.getLength()
    if (index) quill.insertEmbed(index, 'image', res.data.image_url, 'user')
  })
}

Additional, you could rewrite the toolbar's insert image button with our image handler.

quill.getModule('toolbar').addHandler('image', (clicked) => {
  if (clicked) {
    let fileInput = this.container.querySelector('input.ql-image[type=file]')
    if (fileInput == null) {
      fileInput = document.createElement('input')
      fileInput.setAttribute('type', 'file')
      fileInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon')
      fileInput.classList.add('ql-image')
      fileInput.addEventListener('change', (e) => {
        var files = e.target.files, file
        if (files.length > 0) {
          file = files[0]
          var type = file.type
          var reader = new FileReader()
          reader.onload = (e) => {
            // handle the inserted image
            imageHandler(e.target.result, type)
            fileInput.value = ''
          }
          reader.readAsDataURL(file)
        }
      })
    }
    fileInput.click()
  }
})

Script Tag

Copy quill-image-drop-and-paste.min.js into your web root or include from node_modules

<script src="/node_modules/quill-image-drop-and-paste/quill-image-drop-and-paste.min.js"></script>
var quill = new Quill(editorSelector, {
  // ...
  modules: {
    imageDropAndPaste: {
      // add an custom image handler
      handler: imageHandler
    }
  }
});

Finally

If you didnot config a image handler, it will insert the image into the quill editor directory after you drop/paste a image. Just like the module [quill-image-drop-module]: https://www.npmjs.com/package/quill-image-drop-module did.