html-doc-js
Version:
js-export-word
2 lines (1 loc) • 13.8 kB
JavaScript
var exportWord=function(t,e){"use strict";function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=r(t),o=r(e);function i(t,e,r,n,o,i,a){try{var c=t[i](a),s=c.value}catch(t){return void r(t)}c.done?e(s):Promise.resolve(s).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function c(t){i(a,n,o,c,s,"next",t)}function s(t){i(a,n,o,c,s,"throw",t)}c(void 0)}))}}var c,s,l=(c=function(t){var e=function(t){var e,r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var o=e&&e.prototype instanceof d?e:d,i=Object.create(o.prototype),a=new _(n||[]);return i._invoke=function(t,e,r){var n=h;return function(o,i){if(n===p)throw new Error("Generator is already running");if(n===m){if("throw"===o)throw i;return j()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=E(a,r);if(c){if(c===y)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===h)throw n=m,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=p;var s=u(t,e,r);if("normal"===s.type){if(n=r.done?m:f,s.arg===y)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n=m,r.method="throw",r.arg=s.arg)}}}(t,r,a),i}function u(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var h="suspendedStart",f="suspendedYield",p="executing",m="completed",y={};function d(){}function v(){}function g(){}var w={};s(w,i,(function(){return this}));var x=Object.getPrototypeOf,b=x&&x(x(P([])));b&&b!==r&&n.call(b,i)&&(w=b);var T=g.prototype=d.prototype=Object.create(w);function L(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function O(t,e){function r(o,i,a,c){var s=u(t[o],t,i);if("throw"!==s.type){var l=s.arg,h=l.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){r("next",t,a,c)}),(function(t){r("throw",t,a,c)})):e.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return r("throw",t,a,c)}))}c(s.arg)}var o;this._invoke=function(t,n){function i(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(i,i):i()}}function E(t,r){var n=t.iterator[r.method];if(n===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=e,E(t,r),"throw"===r.method))return y;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return y}var o=u(n,t.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,y;var i=o.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function k(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function S(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(k,this),this.reset(!0)}function P(t){if(t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}return{next:j}}function j(){return{value:e,done:!0}}return v.prototype=g,s(T,"constructor",g),s(g,"constructor",v),v.displayName=s(g,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,s(t,c,"GeneratorFunction")),t.prototype=Object.create(T),t},t.awrap=function(t){return{__await:t}},L(O.prototype),s(O.prototype,a,(function(){return this})),t.AsyncIterator=O,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new O(l(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},L(T),s(T,c,"Generator"),s(T,i,(function(){return this})),s(T,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=P,_.prototype={constructor:_,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(S),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return c.type="throw",c.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var s=n.call(a,"catchLoc"),l=n.call(a,"finallyLoc");if(s&&l){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!l)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),S(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;S(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:P(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}},c(s={exports:{}},s.exports),s.exports);function u(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function h(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function f(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function p(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function m(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?p(Object(r),!0).forEach((function(e){u(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):p(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var y=function(){function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};h(this,t);var n={document:document,addStyle:!0,fileName:"",maxWidth:624,toImg:"",success:function(){}};this.config={},this.dom=e,this.c_dom=e.cloneNode(!0),this.config=m(m({},n),r)}var e,r,i,c,s,u;return e=t,r=[{key:"init",value:(u=a(l.mark((function t(){return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return this.config.addStyle&&this.sheetToSelf(this.c_dom),t.next=3,this.sheetToImg();case 3:return t.next=5,this.domToCanvas();case 5:case"end":return t.stop()}}),t,this)}))),function(){return u.apply(this,arguments)})},{key:"sheetToImg",value:(s=a(l.mark((function t(){var e,r,n;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:e=this.dom.querySelectorAll("img"),r=this.c_dom.querySelectorAll("img"),n=0;case 3:if(!(n<e.length)){t.next=11;break}return t.next=6,this.toBase64(e[n]);case 6:r[n].src=t.sent,this.imgStyleReset(r[n],e[n]);case 8:n++,t.next=3;break;case 11:case"end":return t.stop()}}),t,this)}))),function(){return s.apply(this,arguments)})},{key:"domToCanvas",value:(c=a(l.mark((function t(){var e,r,n,o,i,a;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((e=this.config.toImg).length){t.next=3;break}return t.abrupt("return");case 3:r=this.dom.querySelectorAll(e),n=this.c_dom.querySelectorAll(e),o=0;case 6:if(!(o<r.length)){t.next=18;break}return t.next=9,this.toBase64(r[o]);case 9:i=t.sent,(a=new Image).src=i,this.imgStyleReset(a,r[o]),n[o].innerHTML="",n[o].appendChild(a);case 15:o++,t.next=6;break;case 18:case"end":return t.stop()}}),t,this)}))),function(){return c.apply(this,arguments)})},{key:"exportWord",value:function(){var t=function(t){for(var e={mhtml:{top:"Mime-Version: 1.0\nContent-Base: "+location.href+'\nContent-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset="utf-8"\nContent-Location: '+location.href+'\n\n<!DOCTYPE html>\n<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">\n_html_</html>',head:'<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<style>\n_styles_\n</style>\n\x3c!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--\x3e</head>\n',body:"<body>_body_</body>"}},r=[],n=t.querySelectorAll("img"),o=0;o<n.length;o++){var i=n[o].src;r[o]={type:i.substring(i.indexOf(":")+1,i.indexOf(";")),encoding:i.substring(i.indexOf(";")+1,i.indexOf(",")),location:n[o].src,data:i.substring(i.indexOf(",")+1)}}for(var a="\n",c=0;c<r.length;c++)a+="--NEXT.ITEM-BOUNDARY\n",a+="Content-Location: "+r[c].location+"\n",a+="Content-Type: "+r[c].type+"\n",a+="Content-Transfer-Encoding: "+r[c].encoding+"\n\n",a+=r[c].data+"\n\n";a+="--NEXT.ITEM-BOUNDARY--";var s=e.mhtml.top.replace("_html_",e.mhtml.head.replace("_styles_","")+e.mhtml.body.replace("_body_",t.innerHTML))+a;return new Blob([s],{type:"application/msword;charset=utf-8"})}(this.c_dom);this.config.fileName&&n.default(t,this.config.fileName+".doc"),this.config.success(t,this.c_dom)}},{key:"sheetToSelf",value:function(t){var e=this.config.document.styleSheets,r=t;function n(t){var e=t.split(";");e.splice(e.length-1,1);var r={};return e.forEach((function(t){var e=t.split(":")[0];r[e.replace(/ /g,"")]=t.split(":").map((function(t,e){return e?t:""})).join("")})),r}for(var o=0,i=e.length;o<i;o++){try{e[o].rules||e[o].cssRules}catch(t){console.warn("Can't read the css rules of: "+e[o].href,t);continue}var a=e[o],c=a.rules,s=a.cssRules;Array.from(c||s||[]).forEach((function(t){var e=t.selectorText,o=t.style;if("*"!==e)try{r.querySelectorAll(e).forEach((function(t){if(t.style.cssText){var e=n(t.style.cssText),r=n(o.cssText);for(var i in r)e[i]=r[i];for(var a in e)t.style[a]=e[a]}else t.style.cssText=o.cssText}))}catch(t){console.log("转换成行内样式失败",t)}}))}}},{key:"imgStyleReset",value:function(t,e){var r=this.config.maxWidth,n=Math.min(e.clientWidth,r),o=e.clientHeight*n/e.clientWidth;return t.width=n,t.height=o,t}},{key:"toBase64",value:function(t){return new Promise((function(e){o.default(t).then((function(t){var r=t.toDataURL("image/jpeg",1);e(r)}))}))}}],r&&f(e.prototype,r),i&&f(e,i),Object.defineProperty(e,"prototype",{writable:!1}),t}();function d(){return(d=a(l.mark((function t(e,r){var n;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new y(e,r),t.next=3,n.init();case 3:n.exportWord();case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}return function(t,e){return d.apply(this,arguments)}}(saveAs,html2canvas);