-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvue-clipboard.min.js
1 lines (1 loc) · 1.94 KB
/
vue-clipboard.min.js
1
!function(){function e(e){var t;if("INPUT"===e.nodeName||"TEXTAREA"===e.nodeName)e.focus(),e.setSelectionRange(0,e.value.length),t=e.value;else{e.hasAttribute("contenteditable")&&e.focus();var i=window.getSelection(),n=document.createRange();n.selectNodeContents(e),i.removeAllRanges(),i.addRange(n),t=i.toString()}return t}var t={};t.install=function(t){t.directive("clipboard",{params:["success","error"],acceptStatement:!0,bind:function(){this.arg="cut"===this.arg?"cut":"copy",t.util.on(this.el,"click",this.handler.bind(this))},update:function(e){this.text=e},unbind:function(){t.util.off(this.el,"click",this.handler),this.removeFake()},handler:function(){this.selectFake(this.text),"cut"===this.arg&&(this.vm[this.expression]="")},selectFake:function(t){var i="rtl"==document.documentElement.getAttribute("dir");this.removeFake(),this.fakeHandler=document.body.addEventListener("click",this.removeFake.bind(this)),this.fakeElem=document.createElement("textarea"),this.fakeElem.style.fontSize="12pt",this.fakeElem.style.border="0",this.fakeElem.style.padding="0",this.fakeElem.style.margin="0",this.fakeElem.style.position="fixed",this.fakeElem.style[i?"right":"left"]="-9999px",this.fakeElem.style.top=(window.pageYOffset||document.documentElement.scrollTop)+"px",this.fakeElem.setAttribute("readonly",""),this.fakeElem.value=t,document.body.appendChild(this.fakeElem),this.selectedText=e(this.fakeElem),this.copyText()},removeFake:function(){this.fakeHandler&&(document.body.removeEventListener("click"),this.fakeHandler=null),this.fakeElem&&(document.body.removeChild(this.fakeElem),this.fakeElem=null)},copyText:function(){var e;try{e=document.execCommand("copy")}catch(t){e=!1}this.handleResult(e)},handleResult:function(e){e?this.params.success&&this.params.success():this.params.error&&this.params.error()}})},"object"==typeof exports?module.exports=t:"function"==typeof define&&define.amd?define([],function(){return t}):window.Vue&&(window.vueClipboard=t,Vue.use(t))}();