Skip to content

Commit 5970d6c

Browse files
committed
🔨 refactor(vue-html-to-paper.js): add support for window title and auto close
This commit adds support for two new options to the vue-html-to-paper.js file. The first option is `windowTitle` which allows the user to specify a custom title for the print window. The second option is `autoClose` which is a boolean value that determines whether or not the print window should automatically close after printing. If `autoClose` is set to false, the window will remain open after printing. 🐛 fix(index.js): add autoClose option to close window after printing ✨ feat(index.js): add windowTitle option to customize window title 🎨 style(index.html): update custom window title to be grammatically correct The autoClose option has been added to allow the window to be closed automatically after printing. The windowTitle option has been added to allow customization of the window title. The custom window title in index.html has been updated to be grammatically correct. 🔧 chore(index.js): add autoClose and windowTitle options to VueHtmlToPaper This commit adds two new options to the VueHtmlToPaper object: autoClose and windowTitle. The autoClose option allows the user to specify whether the print window should automatically close after printing. The windowTitle option allows the user to specify a custom title for the print window. These options can be passed in as localOptions or as part of the global options object.
1 parent 62d86a3 commit 5970d6c

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

build/vue-html-to-paper.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@
3030
let defaultName = '_blank',
3131
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
3232
defaultReplace = true,
33-
defaultStyles = [];
33+
defaultStyles = [],
34+
defaultWindowTitle = window.document.title;
3435
let {
3536
name = defaultName,
3637
specs = defaultSpecs,
3738
replace = defaultReplace,
3839
styles = defaultStyles,
40+
autoClose = true,
41+
windowTitle = defaultWindowTitle,
3942
} = options;
4043

4144
// If has localOptions
@@ -45,6 +48,8 @@
4548
if (localOptions.specs) specs = localOptions.specs;
4649
if (localOptions.replace) replace = localOptions.replace;
4750
if (localOptions.styles) styles = localOptions.styles;
51+
if (localOptions.autoClose === false) autoClose = localOptions.autoClose;
52+
if (localOptions.windowTitle) windowTitle = localOptions.windowTitle;
4853
}
4954

5055
specs = !!specs.length ? specs.join(',') : '';
@@ -62,7 +67,7 @@
6267
win.document.write(`
6368
<html>
6469
<head>
65-
<title>${window.document.title}</title>
70+
<title>${windowTitle || window.document.title}</title>
6671
</head>
6772
<body>
6873
${element.innerHTML}
@@ -73,10 +78,12 @@
7378
addStyles(win, styles);
7479

7580
setTimeout(() => {
76-
win.document.close();
7781
win.focus();
7882
win.print();
79-
setTimeout(function () {window.close();}, 1);
83+
console.warn('autoClose', autoClose);
84+
if (autoClose) {
85+
setTimeout(function () {win.close();}, 1);
86+
}
8087
cb();
8188
}, 1000);
8289

dist/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ const VueHtmlToPaper = {
2828
let defaultName = '_blank',
2929
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
3030
defaultReplace = true,
31-
defaultStyles = [];
31+
defaultStyles = [],
32+
defaultWindowTitle = window.document.title;
3233
let {
3334
name = defaultName,
3435
specs = defaultSpecs,
3536
replace = defaultReplace,
3637
styles = defaultStyles,
38+
autoClose = true,
39+
windowTitle = defaultWindowTitle,
3740
} = options;
3841

3942
// If has localOptions
@@ -43,6 +46,8 @@ const VueHtmlToPaper = {
4346
if (localOptions.specs) specs = localOptions.specs;
4447
if (localOptions.replace) replace = localOptions.replace;
4548
if (localOptions.styles) styles = localOptions.styles;
49+
if (localOptions.autoClose === false) autoClose = localOptions.autoClose;
50+
if (localOptions.windowTitle) windowTitle = localOptions.windowTitle;
4651
}
4752

4853
specs = !!specs.length ? specs.join(',') : '';
@@ -60,7 +65,7 @@ const VueHtmlToPaper = {
6065
win.document.write(`
6166
<html>
6267
<head>
63-
<title>${window.document.title}</title>
68+
<title>${windowTitle || window.document.title}</title>
6469
</head>
6570
<body>
6671
${element.innerHTML}
@@ -71,10 +76,12 @@ const VueHtmlToPaper = {
7176
addStyles(win, styles);
7277

7378
setTimeout(() => {
74-
win.document.close();
7579
win.focus();
7680
win.print();
77-
setTimeout(function () {window.close();}, 1);
81+
console.warn('autoClose', autoClose);
82+
if (autoClose) {
83+
setTimeout(function () {win.close();}, 1);
84+
}
7885
cb();
7986
}, 1000);
8087

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ <h4>Callback</h4>
179179
},
180180
async printCustomWindowTitle() {
181181
const localOptions = {
182-
windowTitle: 'This is custom window title',
182+
windowTitle: 'This is a custom window title',
183183
};
184184
await this.$htmlToPaper('printMe', localOptions);
185185
console.warn('done');

src/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ const VueHtmlToPaper = {
2828
let defaultName = '_blank',
2929
defaultSpecs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'],
3030
defaultReplace = true,
31-
defaultStyles = [];
31+
defaultStyles = [],
32+
defaultWindowTitle = window.document.title;
3233
let {
3334
name = defaultName,
3435
specs = defaultSpecs,
3536
replace = defaultReplace,
3637
styles = defaultStyles,
38+
autoClose = true,
39+
windowTitle = defaultWindowTitle,
3740
} = options;
3841

3942
// If has localOptions
@@ -43,6 +46,8 @@ const VueHtmlToPaper = {
4346
if (localOptions.specs) specs = localOptions.specs;
4447
if (localOptions.replace) replace = localOptions.replace;
4548
if (localOptions.styles) styles = localOptions.styles;
49+
if (localOptions.autoClose === false) autoClose = localOptions.autoClose;
50+
if (localOptions.windowTitle) windowTitle = localOptions.windowTitle;
4651
}
4752

4853
specs = !!specs.length ? specs.join(',') : '';
@@ -60,7 +65,7 @@ const VueHtmlToPaper = {
6065
win.document.write(`
6166
<html>
6267
<head>
63-
<title>${window.document.title}</title>
68+
<title>${windowTitle || window.document.title}</title>
6469
</head>
6570
<body>
6671
${element.innerHTML}
@@ -71,10 +76,12 @@ const VueHtmlToPaper = {
7176
addStyles(win, styles);
7277

7378
setTimeout(() => {
74-
win.document.close();
7579
win.focus();
7680
win.print();
77-
setTimeout(function () {window.close();}, 1);
81+
console.warn('autoClose', autoClose);
82+
if (autoClose) {
83+
setTimeout(function () {win.close();}, 1);
84+
}
7885
cb();
7986
}, 1000);
8087

0 commit comments

Comments
 (0)