From 1f68e955ec216f53e6215ef9562feec13d5c3e5f Mon Sep 17 00:00:00 2001 From: Celeste Date: Tue, 19 Nov 2019 20:10:18 +0800 Subject: [PATCH 1/2] feat: export htmlToPaper and setOptions --- src/index.js | 101 +++++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/src/index.js b/src/index.js index 89414b0..4ed4729 100644 --- a/src/index.js +++ b/src/index.js @@ -8,54 +8,67 @@ function addStyles (win, styles) { }); } +const options = { + name: '_blank', + specs: ['fullscreen=yes', 'titlebar=yes', 'scrollbars=yes'].join(','), + replace: true, + styles: [] +}; + +const setOptions = ({ name, specs, replace, styles }) => { + specs = !!specs.length ? specs.join(',') : ''; + + options.name = name || options.name; + options.specs = specs || options.specs; + options.replace = replace || options.replace; + options.styles = styles || options.styles; +}; + +const htmlToPaper = (el, cb = () => true) => { + const element = document.getElementById(el); + + if(!element) { + alert(`Element to print #${el} not found!`); + return; + } + + const url = ''; + const win = window.open(url, options.name, options.specs, options.replace); + + win.document.write(` + + + ${document.title} + + + ${element.innerHTML} + + + `); + + addStyles(win, options.styles); + + setTimeout(() => { + win.document.close(); + win.focus(); + win.print(); + win.close(); + cb(); + }, 1000); + return true; +}; + const VueHtmlToPaper = { - install (Vue, options = {}) { + install(Vue, options = {}) { + setOptions(options); + Vue.mixin({ methods: { - $htmlToPaper (el, cb = () => true) { - let { - name = '_blank', - specs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'], - replace = true, - styles = [] - } = options; - specs = !!specs.length ? specs.join(',') : ''; - - const element = document.getElementById(el); - - if(!element) { - alert(`Element to print #${el} not found!`); - return; - } - - const url = ''; - const win = window.open(url, name, specs, replace); - - win.document.write(` - - - ${document.title} - - - ${element.innerHTML} - - - `); - - addStyles(win, styles); - - setTimeout(() => { - win.document.close(); - win.focus(); - win.print(); - win.close(); - cb(); - }, 1000); - return true; - } + $htmlToPaper: htmlToPaper } }); } -} +}; -export default VueHtmlToPaper; \ No newline at end of file +export default VueHtmlToPaper; +export { htmlToPaper, setOptions }; From a205c71062c9d38ee7fde69952f5ca005da91561 Mon Sep 17 00:00:00 2001 From: Celeste Date: Tue, 19 Nov 2019 20:18:46 +0800 Subject: [PATCH 2/2] chore: create new build --- build/vue-html-to-paper.js | 108 +++++++++++++++++++++---------------- dist/index.js | 2 +- 2 files changed, 63 insertions(+), 47 deletions(-) diff --git a/build/vue-html-to-paper.js b/build/vue-html-to-paper.js index 644f2c9..fdadf29 100644 --- a/build/vue-html-to-paper.js +++ b/build/vue-html-to-paper.js @@ -1,8 +1,8 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = global || self, global.VueHtmlToPaper = factory()); -}(this, function () { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = global || self, factory(global.VueHtmlToPaper = {})); +}(this, function (exports) { 'use strict'; function addStyles (win, styles) { styles.forEach(style => { @@ -14,56 +14,72 @@ }); } - const VueHtmlToPaper = { - install (Vue, options = {}) { - Vue.mixin({ - methods: { - $htmlToPaper (el, cb = () => true) { - let { - name = '_blank', - specs = ['fullscreen=yes','titlebar=yes', 'scrollbars=yes'], - replace = true, - styles = [] - } = options; - specs = !!specs.length ? specs.join(',') : ''; + const options = { + name: '_blank', + specs: ['fullscreen=yes', 'titlebar=yes', 'scrollbars=yes'].join(','), + replace: true, + styles: [] + }; + + const setOptions = ({ name, specs, replace, styles }) => { + specs = !!specs.length ? specs.join(',') : ''; - const element = document.getElementById(el); + options.name = name || options.name; + options.specs = specs || options.specs; + options.replace = replace || options.replace; + options.styles = styles || options.styles; + }; + + const htmlToPaper = (el, cb = () => true) => { + const element = document.getElementById(el); + + if(!element) { + alert(`Element to print #${el} not found!`); + return; + } - if(!element) { - alert(`Element to print #${el} not found!`); - return; - } - - const url = ''; - const win = window.open(url, name, specs, replace); + const url = ''; + const win = window.open(url, options.name, options.specs, options.replace); - win.document.write(` - - - ${document.title} - - - ${element.innerHTML} - - - `); + win.document.write(` + + + ${document.title} + + + ${element.innerHTML} + + + `); - addStyles(win, styles); - - setTimeout(() => { - win.document.close(); - win.focus(); - win.print(); - win.close(); - cb(); - }, 1000); - return true; - } + addStyles(win, options.styles); + + setTimeout(() => { + win.document.close(); + win.focus(); + win.print(); + win.close(); + cb(); + }, 1000); + return true; + }; + + const VueHtmlToPaper = { + install(Vue, options = {}) { + setOptions(options); + + Vue.mixin({ + methods: { + $htmlToPaper: htmlToPaper } }); } }; - return VueHtmlToPaper; + exports.default = VueHtmlToPaper; + exports.htmlToPaper = htmlToPaper; + exports.setOptions = setOptions; + + Object.defineProperty(exports, '__esModule', { value: true }); })); diff --git a/dist/index.js b/dist/index.js index 6382723..4696b15 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -module.exports=function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(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)})}Object.defineProperty(t,"__esModule",{value:!0}),t.default={install:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};e.mixin({methods:{$htmlToPaper:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:function(){return!0},o=t.name,i=void 0===o?"_blank":o,u=t.specs,l=void 0===u?["fullscreen=yes","titlebar=yes","scrollbars=yes"]:u,c=t.replace,s=void 0===c||c,a=t.styles,d=void 0===a?[]:a;l=l.length?l.join(","):"";var f=document.getElementById(e);if(!f)return void alert("Element to print #"+e+" not found!");var m=window.open("",i,l,s);return m.document.write("\n \n \n "+document.title+"\n \n \n "+f.innerHTML+"\n \n \n "),r(m,d),setTimeout(function(){m.document.close(),m.focus(),m.print(),m.close(),n()},1e3),!0}}})}}}]); \ No newline at end of file +module.exports=function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,n){"use strict";function r(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)})}Object.defineProperty(t,"__esModule",{value:!0});var o={name:"_blank",specs:"fullscreen=yes,titlebar=yes,scrollbars=yes",replace:!0,styles:[]},s=function(e){var t=e.name,n=e.specs,r=e.replace,s=e.styles;n=n.length?n.join(","):"",o.name=t||o.name,o.specs=n||o.specs,o.replace=r||o.replace,o.styles=s||o.styles},l=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:function(){return!0},n=document.getElementById(e);if(!n)return void alert("Element to print #"+e+" not found!");var s=window.open("",o.name,o.specs,o.replace);return s.document.write("\n \n \n "+document.title+"\n \n \n "+n.innerHTML+"\n \n \n "),r(s,o.styles),setTimeout(function(){s.document.close(),s.focus(),s.print(),s.close(),t()},1e3),!0};t.default={install:function(e){s(arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}),e.mixin({methods:{$htmlToPaper:l}})}},t.htmlToPaper=l,t.setOptions=s}]); \ No newline at end of file