Created
September 17, 2013 02:14
-
-
Save libo1106/6589293 to your computer and use it in GitHub Desktop.
黏贴图片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById("rte").focus(); | |
document.body.addEventListener("paste", function(e) { | |
for (var i = 0; i < e.clipboardData.items.length; i++) { | |
if (e.clipboardData.items[i].kind == "file" && e.clipboardData.items[i].type == "image/png") { | |
// get the blob | |
var imageFile = e.clipboardData.items[i].getAsFile(); | |
// read the blob as a data URL | |
var fileReader = new FileReader(); | |
fileReader.onloadend = function(e) { | |
// create an image | |
var image = document.createElement("IMG"); | |
image.src = this.result; | |
// insert the image | |
var range = window.getSelection().getRangeAt(0); | |
range.insertNode(image); | |
range.collapse(false); | |
// set the selection to after the image | |
var selection = window.getSelection(); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
}; | |
// TODO: Error Handling! | |
// fileReader.onerror = ... | |
fileReader.readAsDataURL(imageFile); | |
// prevent the default paste action | |
e.preventDefault(); | |
// only paste 1 image at a time | |
break; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment