@hgsj/vue-print-directive 1.0.1 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/print.js +1 -1
package/package.json
CHANGED
package/print.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
const Print=function(t,e){if(!(this instanceof Print))return new Print(t,e);this.options=this.extend({noPrint:".no-print",type:"html",text:""},e),"string"==typeof t?this.dom=document.querySelector(t):(this.isDOM(t),this.dom=this.isDOM(t)?t:t.$el),this.init()},MyPlugin=(Print.prototype={init:function(){var t=this.getStyle()+("text"===this.options.type?this.getTextHtml():this.getHtml());this.writeIframe(t)},extend:function(t,e){for(var n in e)t[n]=e[n];return t},getStyle:function(){for(var t="",e=document.querySelectorAll("style,link"),n=0;n<e.length;n++)t+=e[n].outerHTML;return t+="<style>"+(this.options.noPrint||".no-print")+"{display:none;}</style>"},getHtml:function(){for(var t=document.querySelectorAll("input"),e=document.querySelectorAll("textarea"),n=document.querySelectorAll("select"),o=0;o<t.length;o++)"checkbox"==t[o].type||"radio"==t[o].type?1==t[o].checked?t[o].setAttribute("checked","checked"):t[o].removeAttribute("checked"):(t[o].type,t[o].setAttribute("value",t[o].value));for(var i=0;i<e.length;i++)"textarea"==e[i].type&&(e[i].innerHTML=e[i].value);for(var r=0;r<n.length;r++)if("select-one"==n[r].type){var c,l=n[r].children;for(c in l)"OPTION"==l[c].tagName&&(1==l[c].selected?l[c].setAttribute("selected","selected"):l[c].removeAttribute("selected"))}return this.dom.outerHTML},getTextHtml:function(){var t="";return this.options&&"string"==typeof this.options.text&&this.options.text?t=this.options.text:this.dom&&(t=this.dom.innerText||this.dom.textContent||""),"<style>@media print{body{margin:0;}pre{font-family:Consolas, 'Courier New', monospace;font-size:12px;line-height:1.25;white-space:pre-wrap;word-break:break-word;}}</style><pre>"+(t=String(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">"))+"</pre>"},writeIframe:function(t){var e,n=document.createElement("iframe"),o=document.body.appendChild(n),i=(n.id="myIframe",n.setAttribute("style","position:absolute;width:0;height:0;top:-10px;left:-10px;"),e=o.contentWindow||o.contentDocument,(o=o.contentDocument||o.contentWindow.document).open(),o.write(t),o.close(),this);n.onload=function(){i.toPrint(e),setTimeout(function(){document.body.removeChild(n)},100)}},toPrint:function(e){try{setTimeout(function(){e.focus();try{e.document.execCommand("print",!1,null)||e.print()}catch(t){e.print()}e.close()},10)}catch(t){console.log("err",t)}},isDOM:"object"==typeof HTMLElement?function(t){return t instanceof HTMLElement}:function(t){return t&&"object"==typeof t&&1===t.nodeType&&"string"==typeof t.nodeName}},{});MyPlugin.install=function(t,e){t.prototype.$print=Print};export default MyPlugin;
|