-
Notifications
You must be signed in to change notification settings - Fork 66
/
document.js
234 lines (206 loc) · 5.21 KB
/
document.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import JSZip from 'jszip'
import cheer from "cheerio"
import "./cheerio-fn"
import {Parser, DomHandler} from "htmlparser2"
const normalize=path=>path.split("/").filter(a=>a!=".")
.reduceRight((n,a)=>{
if(a==".."){
n.r++
}else if(n.r){
n.r--
}else{
n.trimed.unshift(a)
}
return n
},{trimed:[],r:0}).trimed.join("/")
/**
* document parser
*
* @example
* Document.load(file)
* .then(doc=>doc.parse())
*/
export default class ZipDocument{
static ext="unknown"
static mime="application/zip"
constructor(parts,raw,props){
this.parts=parts
this.raw=raw
this.props=props
this._shouldReleased=new Map()
}
normalizePath(){
return normalize(...arguments)
}
getPart(name){
name=normalize(name)
return this.parts[name]
}
getDataPart(name){
name=normalize(name)
let part=this.parts[name]
let crc32=part._data.crc32
let data=part.asUint8Array()//unsafe call, part._data is changed
data.crc32=part._data.crc32=crc32//so keep crc32 on part._data for future
return data
}
getDataPartAsUrl(name,type="*/*"){
name=normalize(name)
let part=this.parts[name]
let crc32=part._data.crc32
if(!this._shouldReleased.has(crc32)){
this._shouldReleased.set(crc32,URL.createObjectURL(new Blob([this.getDataPart(name)],{type})))
}
return this._shouldReleased.get(crc32)
}
getPartCrc32(name){
name=normalize(name)
let part=this.parts[name]
let crc32=part._data.crc32
return crc32
}
release(){
for(let [, url] of this._shouldReleased){
URL.revokeObjectURL(url)
}
}
getObjectPart(name){
name=normalize(name)
const part=this.parts[name]
if(!part)
return null
else if(part.cheerio)
return part
else{
const $=Object.assign(this.parts[name]=this.constructor.parseXml(part.asText()),{part:name})
Object.assign($.root()[0].attribs,{part:name})
$.prototype.part=()=>name
return $
}
}
$(node){
const root=a=>a.root || (a.parent && root(a.parent))
return this.getObjectPart(root(node).attribs.part)(node)
}
parse(domHandler){
}
render(){
}
serialize(){
let newDoc=new JSZip()
Object.keys(this.parts).forEach(path=>{
let part=this.parts[path]
if(part.cheerio){
newDoc.file(path,part.xml())
}else{
newDoc.file(path,part._data, part.options)
}
})
return newDoc
}
save(file,options){
file=file||this.props.name||`${Date.now()}.docx`
let newDoc=this.serialize()
if(typeof(document)!="undefined" && window.URL && window.URL.createObjectURL){
let data=newDoc.generate({...options,type:"blob",mimeType:this.constructor.mime})
let url = window.URL.createObjectURL(data)
let link = document.createElement("a");
document.body.appendChild(link)
link.download = file
link.href = url;
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
}else{
let data=newDoc.generate({...options,type:"nodebuffer"})
return new Promise((resolve,reject)=>
require("f"+"s").writeFile(file,data,error=>{
error ? reject(error) : resolve(data)
})
)
}
}
clone(){
let zip=new JSZip()
let props= props ? JSON.parse(JSON.stringify(this.props)) : props
let parts=Object.keys(this.parts).reduce((state, k)=>{
let v=this.parts[k]
if(v.cheerio){
zip.file(v.name,v.xml(),v.options)
state[k]=zip.file(v.name)
}else{
zip.file(v.name,v._data,v.options)
state[k]=zip.file(v.name)
}
return state
},{})
return new this.constructor(parts,zip, props)
}
/**
* a helper to load document file
* @param inputFile {File} - a html input file, or nodejs file
* @return {Promise}
*/
static load(inputFile){
const DocumentSelf=this
if(inputFile instanceof ZipDocument)
return Promise.resolve(inputFile)
return new Promise((resolve, reject)=>{
function parse(data, props={}){
try{
let raw=new JSZip(data),parts={}
raw.filter((path,file)=>parts[path]=file)
resolve(new DocumentSelf(parts,raw,props))
}catch(error){
reject(error)
}
}
if(typeof inputFile=='string'){//file name
require('fs').readFile(inputFile,function(error, data){
if(error)
reject(error);
else if(data){
parse(data, {name:inputFile.split(/[\/\\]/).pop().replace(/\.docx$/i,'')})
}
})
}else if(inputFile instanceof Blob){
var reader=new FileReader();
reader.onload=function(e){
parse(e.target.result, (inputFile.name ? {
name:inputFile.name.replace(/\.docx$/i,''),
lastModified:inputFile.lastModified,
size:inputFile.size
} : {size:inputFile.size}))
}
reader.readAsArrayBuffer(inputFile);
}else {
parse(inputFile)
}
})
}
static create(){
return this.load(`${__dirname}/../templates/blank.${this.ext}`)
}
static parseXml(data){
try{
let opt={xmlMode:true,decodeEntities: false}
let handler=new ContentDomHandler(opt)
new Parser(handler,opt).end(data)
let parsed=cheer.load(handler.dom,opt)
if(typeof(parsed.cheerio)=="undefined")
parsed.cheerio="customized"
return parsed
}catch(error){
console.error(error)
return null
}
}
}
class ContentDomHandler extends DomHandler{
_addDomElement(el){
if(el.type=="text" && (el.data[0]=='\r' || el.data[0]=='\n'))
;//remove format whitespaces
else
return super._addDomElement(el)
}
}