Skip to content

Commit 29df424

Browse files
committed
refactor: new build
1 parent dabff54 commit 29df424

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

dist/index.js

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
1-
(()=>{"use strict";var e={d:(t,n)=>{for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{default:()=>o});var n=function(e,t){t.forEach((function(t){var n=e.document.createElement("link");n.setAttribute("rel","stylesheet"),n.setAttribute("type","text/css"),n.setAttribute("href",t),e.document.getElementsByTagName("head")[0].appendChild(n)}))};const o={install:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e.prototype.$htmlToPaper=function(e,o){var l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){return!0},i="_blank",r=["fullscreen=yes","titlebar=yes","scrollbars=yes"],u=[],s=1e3,a=!0,d=window.document.title,c=t.name,m=void 0===c?i:c,y=t.specs,f=void 0===y?r:y,p=t.styles,v=void 0===p?u:p,b=t.timeout,w=void 0===b?s:b,h=t.autoClose,g=void 0===h?a:h,T=t.windowTitle,P=void 0===T?d:T;null!=o&&o.name&&(m=o.name),null!=o&&o.specs&&(f=o.specs),null!=o&&o.styles&&(v=o.styles),null!=o&&o.timeout&&(w=o.timeout),null!=o&&o.autoClose&&(g=o.autoClose),null!=o&&o.windowTitle&&(P=o.windowTitle),f=f.length?f.join(","):"";var S=e;if(("string"==typeof e||e instanceof String)&&(S=window.document.getElementById(e)),S){var j="",E=window.open(j,m,f);E.document.write("\n <html>\n <head>\n <title>".concat(P,"</title>\n </head>\n <body>\n ").concat(S.innerHTML,"\n </body>\n </html>\n ")),n(E,v);var O=new Promise((function(e){setTimeout((function(){E.focus(),E.print(),g&&E.document.close(),g&&E.close(),l&&l(),e()}),w)}));return!!l||O}alert("Element to print #".concat(e," not found!"))}}};module.exports.VueHtmlToPaper=t})();
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
const addStyles = (win, styles) => {
6+
styles.forEach((style) => {
7+
let link = win.document.createElement('link');
8+
link.setAttribute('rel', 'stylesheet');
9+
link.setAttribute('type', 'text/css');
10+
link.setAttribute('href', style);
11+
win.document.getElementsByTagName('head')[0].appendChild(link);
12+
});
13+
};
14+
15+
const VueHtmlToPaper = {
16+
install (Vue, options = {}) {
17+
Vue.prototype.$htmlToPaper = (el, localOptions, cb = () => true) => {
18+
let defaultName = '_blank',
19+
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
20+
defaultStyles = [],
21+
defaultTimeout = 1000,
22+
defaultAutoClose = true,
23+
defaultWindowTitle = window.document.title;
24+
let {
25+
name = defaultName,
26+
specs = defaultSpecs,
27+
styles = defaultStyles,
28+
timeout = defaultTimeout,
29+
autoClose = defaultAutoClose,
30+
windowTitle = defaultWindowTitle,
31+
} = options;
32+
33+
if (localOptions?.name) name = localOptions.name;
34+
if (localOptions?.specs) specs = localOptions.specs;
35+
if (localOptions?.styles) styles = localOptions.styles;
36+
if (localOptions?.timeout) timeout = localOptions.timeout;
37+
if (localOptions?.autoClose) autoClose = localOptions.autoClose;
38+
if (localOptions?.windowTitle) windowTitle = localOptions.windowTitle;
39+
40+
specs = !!specs.length ? specs.join(',') : '';
41+
42+
let element = el;
43+
if (typeof el === 'string' || el instanceof String) {
44+
element = window.document.getElementById(el);
45+
}
46+
if (!element) {
47+
alert(`Element to print #${el} not found!`);
48+
return;
49+
}
50+
51+
const url = '';
52+
const win = window.open(url, name, specs);
53+
54+
win.document.write(`
55+
<html>
56+
<head>
57+
<title>${windowTitle}</title>
58+
</head>
59+
<body>
60+
${element.innerHTML}
61+
</body>
62+
</html>
63+
`);
64+
65+
addStyles(win, styles);
66+
67+
const promise = new Promise((resolve) => {
68+
setTimeout(() => {
69+
win.focus();
70+
win.print();
71+
autoClose && win.document.close();
72+
autoClose && win.close();
73+
if (cb) cb();
74+
resolve();
75+
}, timeout);
76+
});
77+
78+
return cb ? true : promise;
79+
};
80+
},
81+
};
82+
83+
exports.default = VueHtmlToPaper;

0 commit comments

Comments
 (0)