Skip to content

Instantly share code, notes, and snippets.

@boboidream
Last active October 13, 2017 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boboidream/6cbcd8f6e65a6242006cab1a029f8cb8 to your computer and use it in GitHub Desktop.
Save boboidream/6cbcd8f6e65a6242006cab1a029f8cb8 to your computer and use it in GitHub Desktop.
从剪切板粘贴图片
// 从剪切板粘贴图片
$document.on('paste', '.my_editor pre', function(ev) {
var clipboardData, file, item, j, k, ref1, len
var clipboardData = (ev.originalEvent || ev).clipboardData
var $my_editor = $(this).closest('.my_editor')
var text = (ev.originalEvent || ev).clipboardData.getData('Text') // 获取到纯文本
if (text) {
ev.preventDefault();
text.replace(/\n/g, '<br/>')
document.execCommand("insertHTML", false, text.trim());
}
if (clipboardData.items) {
ref1 = clipboardData.items;
for (j = 0, len = ref1.length; j < len; j++) {
item = ref1[j];
if (item.type.match(/^image\//)) {
file = item.getAsFile()
uploadImg(file, $my_editor)
ev.preventDefault();
break;
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment