-
Notifications
You must be signed in to change notification settings - Fork 5
/
mhtmlToWord.js
57 lines (49 loc) · 1.6 KB
/
mhtmlToWord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
;(function(window){
//符合commonjs规范引入依赖
if(typeof module !== 'undefined'){
window.saveAs = require('file-saver')
window.baidu = require('baidu-template-pro')
}
function getModelHtml(mhtml,style=''){
return`
Content-Type: text/html; charset="utf-8"
<!DOCTYPE html>
<html>
<head>
<style>
${style}
</style>
</head>
<body>
${mhtml}
</body>
</html>
`
}
//主函数
let exportWord = ({mhtml,style,filename,data,selector})=>{
if(selector){
let nodes = window.document.querySelectorAll(selector)
mhtml = nodes.length>0?Array.from(nodes).reduce((a,b)=>a+b.innerHTML,''):''
}
//没有baiduTemplatePro.js依赖时必须传入selector
if (!selector && typeof baidu === 'undefined') {
console.error("wordExport : missing (selector) for params without depandency (baiduTemplatePro.js)");
return;
}
if (typeof saveAs === "undefined") {
console.error("wordExport : missing dependency (FileSaver.js)");
return;
}
//没有模板引擎时,将获取节点的html字符串生成模板
let html = typeof baidu !== 'undefined'?baidu.template(getModelHtml(mhtml,style),data):getModelHtml(mhtml)
let blob = new Blob([html],{type:'application/msword;charset=utf-8'})
saveAs(blob,filename+'.doc')
}
//添加exportWord到全局对象
window.exportWord = window.exportWord||exportWord
//如果符合commonjs规范,exports出去
if(typeof module==='object'&&typeof module.exports==='object'){
module.exports = {exportWord}
}
})(window)