-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathImageClipboard.min.js
1 lines (1 loc) · 1.72 KB
/
ImageClipboard.min.js
1
!function(e,t){"undefined"!=typeof module?module.exports=t:"function"==typeof define&&define.amd?define(e,t):this[e]=t}("ImageClipboard",function(e,t){"use strict";function n(){var e=document.createElement("div");return e.setAttribute("id","paste_catcher"),e.setAttribute("contenteditable",""),e.style.opacity=0,document.body.insertBefore(e,document.body.firstChild),e.focus(),o.el.addEventListener("click",function(){e.focus()}),e}function a(e){var t=new Image;o.el.innerHTML="",t.onload=function(){var e=document.createElement("img");e.src=t.src,e.style.maxHeight="100%",e.style.maxHeight="100%",o.el.appendChild(e),o.pasteCatcher&&(o.pasteCatcher.innerHTML=""),o.clipImage=t,"function"==typeof o.onpaste&&o.onpaste(t.src)},t.src=e,o.onpaste.call(o,e.split(",")[1])}function i(e){return"object"==typeof HTMLElement?e instanceof HTMLElement:e&&"object"==typeof e&&1===e.nodeType&&"string"==typeof e.nodeName}var o="object"==typeof this?this:{};return o.el=null,o.pasteCatcher=null,o.clipImage=null,o.onpaste=null,o.browserSupport=!0,o.init=function(e,t){if("string"==typeof e)o.el=document.querySelector(e);else{if(!i(e))return!1;o.el=e}return o.pasteCatcher=null,o.clipImage=null,o.onpaste="function"==typeof t?t:function(){},window.Clipboard||(o.pasteCatcher=n()),window.addEventListener("paste",o.pasteHandler),o},o.pasteHandler=function(e){var t;e.clipboardData&&e.clipboardData.items?(t=e.clipboardData.items,t&&(t=Array.prototype.filter.call(t,function(e){return e.type.indexOf("image")>=0}),Array.prototype.forEach.call(t,function(e){var t=e.getAsFile(),n=new FileReader;n.onloadend=function(){a(n.result)},n.readAsDataURL(t)}))):o.pasteCatcher&&setTimeout(function(){var e=o.pasteCatcher.firstElementChild;e&&"IMG"==e.tagName&&a(e.src)},5)},o.init(e,t)});