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

Paste images #137

Open
ncammarata opened this issue Jun 5, 2014 · 11 comments
Open

Paste images #137

ncammarata opened this issue Jun 5, 2014 · 11 comments
Labels

Comments

@ncammarata
Copy link

It would be nice if we could paste an image on the clipboard instead of having to save it to a file and upload from there.

@jhchen jhchen added the feature label Jun 6, 2014
@zD12
Copy link

zD12 commented Jun 9, 2014

You should view issue #122. They plan on adding 'drag-n-drop' features to Quill, but it was posted a while back on May 25, 2014.

@dandv
Copy link

dandv commented May 25, 2015

👍 super handy. Became addicted to pasting images from clipboard after using it in Slack, GitHub, StackOverflow.

I can paste images from the same page, but not from the clipboard.

I've listed other WYSIWYG editors that support pasting images in this comparison.

@jhchen jhchen added this to the Quill 1.0 milestone Dec 15, 2015
@jhchen jhchen removed this from the Quill 1.0 milestone Aug 29, 2016
@kensnyder
Copy link

Here is a demo of my plugin for image drag/drop and paste for Quill. If there is interest I can publish to npm or submit a pull request to add to Quill core. Also demoed there is a plugin for image resizing.

@htsh
Copy link

htsh commented Mar 17, 2017

@kensnyder would love it if the drag/drop and resize plugins were made available. thanks!

@kensnyder
Copy link

These plugins are now available on npm: quill-image-resize-module and quill-image-drop-module. Contributions are welcome.

@idoromm
Copy link

idoromm commented Feb 21, 2018

@kensnyder any plans on making image pasting module as well?
I think allowing image pasting is as useful as drag-n-drop to some people. Thanks!

@johnozbay
Copy link

johnozbay commented May 5, 2018

Here's what I'm using to add very basic support for pasting images from clipboard. It uses the Clipboard API. This will return an image element, which quill parses and inserts inline into the editor.

It can be improved, but for my use case this works.
You can simply add the following into your js file and it should work.
Feel free to convert to a module if you've got time 🍻

Tested in Chrome and Firefox latest (but you should still keep an eye out for browser support as usual)

var IMAGE_MIME_REGEX = /^image\/(p?jpeg|gif|png)$/i;
var loadImage = function (file) {
    var reader = new FileReader();
    reader.onload = function(e){
        var img = document.createElement('img');
        img.src = e.target.result;
        var range = window.getSelection().getRangeAt(0);
        range.deleteContents();
        range.insertNode(img);
    };
    reader.readAsDataURL(file);
};

document.onpaste = function(e){
    var items = e.clipboardData.items;

    for (var i = 0; i < items.length; i++) {
        if (IMAGE_MIME_REGEX.test(items[i].type)) {
            loadImage(items[i].getAsFile());
            return;
        }
    }

    // Normal paste handling here
}

Source : https://gist.github.com/dusanmarsa/2ca9f1df36e14864328a2bb0b353332e

@hardythomas
Copy link

hardythomas commented Jun 24, 2018

@johnozbay, please attribute code if you pulled it from somewhere.

Also, edit your post to include an important line you missed in your snippet:
var IMAGE_MIME_REGEX = /^image\/(p?jpeg|gif|png)$/i;

Thank you for getting this out here!

@johnozbay
Copy link

@hardythomas at the time of posting this I copied the code from the project I was using it in. The lack of attribution was without any bad intention, but merely an oversight in a busy day. I apologize.
Added the source & updated snippet with an edit now. Thanks for the heads up, and keeping me warned 🙏🏻

@hardythomas
Copy link

hardythomas commented Jul 13, 2018 via email

@edeych
Copy link

edeych commented Mar 13, 2020

@johnozbay this code produces double paste in most recent Firefox. Without the polyfill, pasting images directly into quill works. But Chrome still requires your polyfill.

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

10 participants