office-viewer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/README.md +23 -0
  2. package/esm/OpenXML.d.ts +21 -0
  3. package/esm/OpenXML.js +60 -0
  4. package/esm/Word.d.ts +208 -0
  5. package/esm/Word.js +370 -0
  6. package/esm/index.d.ts +6 -0
  7. package/esm/index.js +6 -0
  8. package/esm/node_modules/tslib/tslib.es6.js +120 -0
  9. package/esm/openxml/ContentType.d.ts +12 -0
  10. package/esm/openxml/ContentType.js +15 -0
  11. package/esm/openxml/Style.d.ts +41 -0
  12. package/esm/openxml/Style.js +156 -0
  13. package/esm/openxml/Theme.d.ts +26 -0
  14. package/esm/openxml/Theme.js +69 -0
  15. package/esm/openxml/Types.d.ts +2512 -0
  16. package/esm/openxml/Types.js +1003 -0
  17. package/esm/openxml/word/Background.d.ts +11 -0
  18. package/esm/openxml/word/Body.d.ts +18 -0
  19. package/esm/openxml/word/Body.js +67 -0
  20. package/esm/openxml/word/Bookmark.d.ts +6 -0
  21. package/esm/openxml/word/Bookmark.js +18 -0
  22. package/esm/openxml/word/Border.d.ts +10 -0
  23. package/esm/openxml/word/Break.d.ts +13 -0
  24. package/esm/openxml/word/Break.js +19 -0
  25. package/esm/openxml/word/CustomXml.d.ts +8 -0
  26. package/esm/openxml/word/FldSimple.d.ts +7 -0
  27. package/esm/openxml/word/FldSimple.js +15 -0
  28. package/esm/openxml/word/FrameProperties.d.ts +18 -0
  29. package/esm/openxml/word/Hyperlink.d.ts +13 -0
  30. package/esm/openxml/word/Hyperlink.js +53 -0
  31. package/esm/openxml/word/Indent.d.ts +8 -0
  32. package/esm/openxml/word/InlineText.d.ts +14 -0
  33. package/esm/openxml/word/InlineText.js +62 -0
  34. package/esm/openxml/word/InstrText.d.ts +4 -0
  35. package/esm/openxml/word/InstrText.js +8 -0
  36. package/esm/openxml/word/Paragraph.d.ts +34 -0
  37. package/esm/openxml/word/Paragraph.js +133 -0
  38. package/esm/openxml/word/Pict.d.ts +6 -0
  39. package/esm/openxml/word/Pict.js +19 -0
  40. package/esm/openxml/word/Ruby.d.ts +20 -0
  41. package/esm/openxml/word/Ruby.js +87 -0
  42. package/esm/openxml/word/Run.d.ts +31 -0
  43. package/esm/openxml/word/Run.js +98 -0
  44. package/esm/openxml/word/Section.d.ts +38 -0
  45. package/esm/openxml/word/Section.js +59 -0
  46. package/esm/openxml/word/Shading.d.ts +9 -0
  47. package/esm/openxml/word/SmartTag.d.ts +8 -0
  48. package/esm/openxml/word/SmartTag.js +18 -0
  49. package/esm/openxml/word/Spacing.d.ts +9 -0
  50. package/esm/openxml/word/Sym.d.ts +8 -0
  51. package/esm/openxml/word/Sym.js +16 -0
  52. package/esm/openxml/word/Tab.d.ts +8 -0
  53. package/esm/openxml/word/Tab.js +17 -0
  54. package/esm/openxml/word/Table.d.ts +49 -0
  55. package/esm/openxml/word/Table.js +248 -0
  56. package/esm/openxml/word/VerticalAlign.d.ts +6 -0
  57. package/esm/openxml/word/WDocument.d.ts +13 -0
  58. package/esm/openxml/word/WDocument.js +58 -0
  59. package/esm/openxml/word/drawing/Blip.d.ts +6 -0
  60. package/esm/openxml/word/drawing/Blip.js +17 -0
  61. package/esm/openxml/word/drawing/BlipFill.d.ts +9 -0
  62. package/esm/openxml/word/drawing/BlipFill.js +20 -0
  63. package/esm/openxml/word/drawing/Drawing.d.ts +6 -0
  64. package/esm/openxml/word/drawing/Drawing.js +15 -0
  65. package/esm/openxml/word/drawing/Pic.d.ts +8 -0
  66. package/esm/openxml/word/drawing/Pic.js +16 -0
  67. package/esm/openxml/word/drawing/ShapeProperties.d.ts +9 -0
  68. package/esm/openxml/word/drawing/ShapeProperties.js +20 -0
  69. package/esm/openxml/word/drawing/Transform.d.ts +17 -0
  70. package/esm/openxml/word/drawing/Transform.js +30 -0
  71. package/esm/openxml/word/numbering/AbstractNum.d.ts +13 -0
  72. package/esm/openxml/word/numbering/AbstractNum.js +42 -0
  73. package/esm/openxml/word/numbering/Lvl.d.ts +16 -0
  74. package/esm/openxml/word/numbering/Lvl.js +62 -0
  75. package/esm/openxml/word/numbering/Num.d.ts +10 -0
  76. package/esm/openxml/word/numbering/Num.js +46 -0
  77. package/esm/openxml/word/numbering/NumberProperties.d.ts +6 -0
  78. package/esm/openxml/word/numbering/NumberProperties.js +21 -0
  79. package/esm/openxml/word/numbering/Numbering.d.ts +9 -0
  80. package/esm/openxml/word/numbering/Numbering.js +50 -0
  81. package/esm/openxml/word/properties/Properties.d.ts +12 -0
  82. package/esm/openxml/word/table/Tc.d.ts +46 -0
  83. package/esm/openxml/word/table/Tc.js +234 -0
  84. package/esm/openxml/word/table/Tr.d.ts +18 -0
  85. package/esm/openxml/word/table/Tr.js +106 -0
  86. package/esm/package/PackageParser.d.ts +26 -0
  87. package/esm/package/XMLPackageParser.d.ts +31 -0
  88. package/esm/package/ZipPackageParser.d.ts +29 -0
  89. package/esm/package/ZipPackageParser.js +70 -0
  90. package/esm/parse/jcToTextAlign.d.ts +5 -0
  91. package/esm/parse/jcToTextAlign.js +21 -0
  92. package/esm/parse/parseBorder.d.ts +13 -0
  93. package/esm/parse/parseBorder.js +78 -0
  94. package/esm/parse/parseColor.d.ts +22 -0
  95. package/esm/parse/parseColor.js +131 -0
  96. package/esm/parse/parseFont.d.ts +7 -0
  97. package/esm/parse/parseFont.js +54 -0
  98. package/esm/parse/parseInd.d.ts +5 -0
  99. package/esm/parse/parseInd.js +27 -0
  100. package/esm/parse/parsePr.d.ts +8 -0
  101. package/esm/parse/parsePr.js +325 -0
  102. package/esm/parse/parseRelationship.d.ts +12 -0
  103. package/esm/parse/parseRelationship.js +40 -0
  104. package/esm/parse/parseSize.d.ts +27 -0
  105. package/esm/parse/parseSize.js +38 -0
  106. package/esm/parse/parseSpacing.d.ts +7 -0
  107. package/esm/parse/parseSpacing.js +43 -0
  108. package/esm/parse/parseTextDirection.d.ts +2 -0
  109. package/esm/parse/parseTextDirection.js +25 -0
  110. package/esm/parse/parseTrHeight.d.ts +2 -0
  111. package/esm/parse/parseTrHeight.js +17 -0
  112. package/esm/render/renderBody.d.ts +6 -0
  113. package/esm/render/renderBody.js +53 -0
  114. package/esm/render/renderBookmark.d.ts +6 -0
  115. package/esm/render/renderBookmark.js +17 -0
  116. package/esm/render/renderBr.d.ts +7 -0
  117. package/esm/render/renderBr.js +13 -0
  118. package/esm/render/renderDocument.d.ts +7 -0
  119. package/esm/render/renderDocument.js +14 -0
  120. package/esm/render/renderDrawing.d.ts +8 -0
  121. package/esm/render/renderDrawing.js +38 -0
  122. package/esm/render/renderHyperLink.d.ts +7 -0
  123. package/esm/render/renderHyperLink.js +41 -0
  124. package/esm/render/renderInlineText.d.ts +6 -0
  125. package/esm/render/renderInlineText.js +39 -0
  126. package/esm/render/renderInstrText.d.ts +8 -0
  127. package/esm/render/renderInstrText.js +40 -0
  128. package/esm/render/renderNumbering.d.ts +7 -0
  129. package/esm/render/renderNumbering.js +161 -0
  130. package/esm/render/renderParagraph.d.ts +7 -0
  131. package/esm/render/renderParagraph.js +78 -0
  132. package/esm/render/renderPict.d.ts +6 -0
  133. package/esm/render/renderPict.js +19 -0
  134. package/esm/render/renderRuby.d.ts +6 -0
  135. package/esm/render/renderRuby.js +53 -0
  136. package/esm/render/renderRun.d.ts +14 -0
  137. package/esm/render/renderRun.js +116 -0
  138. package/esm/render/renderSection.d.ts +6 -0
  139. package/esm/render/renderSection.js +36 -0
  140. package/esm/render/renderStyle.d.ts +13 -0
  141. package/esm/render/renderStyle.js +194 -0
  142. package/esm/render/renderSym.d.ts +3 -0
  143. package/esm/render/renderSym.js +10 -0
  144. package/esm/render/renderTab.d.ts +7 -0
  145. package/esm/render/renderTab.js +19 -0
  146. package/esm/render/renderTable.d.ts +6 -0
  147. package/esm/render/renderTable.js +186 -0
  148. package/esm/render/setElementStyle.d.ts +6 -0
  149. package/esm/render/setElementStyle.js +21 -0
  150. package/esm/util/autoSpace.d.ts +5 -0
  151. package/esm/util/autoSpace.js +33 -0
  152. package/esm/util/blob.d.ts +9 -0
  153. package/esm/util/blob.js +39 -0
  154. package/esm/util/dom.d.ts +36 -0
  155. package/esm/util/dom.js +63 -0
  156. package/esm/util/mergeRun.d.ts +17 -0
  157. package/esm/util/mergeRun.js +134 -0
  158. package/esm/util/xml.d.ts +8 -0
  159. package/esm/util/xml.js +12 -0
  160. package/lib/OpenXML.d.ts +21 -0
  161. package/lib/OpenXML.js +69 -0
  162. package/lib/Word.d.ts +208 -0
  163. package/lib/Word.js +374 -0
  164. package/lib/index.d.ts +6 -0
  165. package/lib/index.js +10 -0
  166. package/lib/node_modules/tslib/tslib.es6.js +129 -0
  167. package/lib/openxml/ContentType.d.ts +12 -0
  168. package/lib/openxml/ContentType.js +19 -0
  169. package/lib/openxml/Style.d.ts +41 -0
  170. package/lib/openxml/Style.js +160 -0
  171. package/lib/openxml/Theme.d.ts +26 -0
  172. package/lib/openxml/Theme.js +73 -0
  173. package/lib/openxml/Types.d.ts +2512 -0
  174. package/lib/openxml/Types.js +1005 -0
  175. package/lib/openxml/word/Background.d.ts +11 -0
  176. package/lib/openxml/word/Body.d.ts +18 -0
  177. package/lib/openxml/word/Body.js +71 -0
  178. package/lib/openxml/word/Bookmark.d.ts +6 -0
  179. package/lib/openxml/word/Bookmark.js +22 -0
  180. package/lib/openxml/word/Border.d.ts +10 -0
  181. package/lib/openxml/word/Break.d.ts +13 -0
  182. package/lib/openxml/word/Break.js +23 -0
  183. package/lib/openxml/word/CustomXml.d.ts +8 -0
  184. package/lib/openxml/word/FldSimple.d.ts +7 -0
  185. package/lib/openxml/word/FldSimple.js +19 -0
  186. package/lib/openxml/word/FrameProperties.d.ts +18 -0
  187. package/lib/openxml/word/Hyperlink.d.ts +13 -0
  188. package/lib/openxml/word/Hyperlink.js +57 -0
  189. package/lib/openxml/word/Indent.d.ts +8 -0
  190. package/lib/openxml/word/InlineText.d.ts +14 -0
  191. package/lib/openxml/word/InlineText.js +66 -0
  192. package/lib/openxml/word/InstrText.d.ts +4 -0
  193. package/lib/openxml/word/InstrText.js +12 -0
  194. package/lib/openxml/word/Paragraph.d.ts +34 -0
  195. package/lib/openxml/word/Paragraph.js +137 -0
  196. package/lib/openxml/word/Pict.d.ts +6 -0
  197. package/lib/openxml/word/Pict.js +23 -0
  198. package/lib/openxml/word/Ruby.d.ts +20 -0
  199. package/lib/openxml/word/Ruby.js +91 -0
  200. package/lib/openxml/word/Run.d.ts +31 -0
  201. package/lib/openxml/word/Run.js +103 -0
  202. package/lib/openxml/word/Section.d.ts +38 -0
  203. package/lib/openxml/word/Section.js +63 -0
  204. package/lib/openxml/word/Shading.d.ts +9 -0
  205. package/lib/openxml/word/SmartTag.d.ts +8 -0
  206. package/lib/openxml/word/SmartTag.js +22 -0
  207. package/lib/openxml/word/Spacing.d.ts +9 -0
  208. package/lib/openxml/word/Sym.d.ts +8 -0
  209. package/lib/openxml/word/Sym.js +20 -0
  210. package/lib/openxml/word/Tab.d.ts +8 -0
  211. package/lib/openxml/word/Tab.js +21 -0
  212. package/lib/openxml/word/Table.d.ts +49 -0
  213. package/lib/openxml/word/Table.js +252 -0
  214. package/lib/openxml/word/VerticalAlign.d.ts +6 -0
  215. package/lib/openxml/word/WDocument.d.ts +13 -0
  216. package/lib/openxml/word/WDocument.js +62 -0
  217. package/lib/openxml/word/drawing/Blip.d.ts +6 -0
  218. package/lib/openxml/word/drawing/Blip.js +21 -0
  219. package/lib/openxml/word/drawing/BlipFill.d.ts +9 -0
  220. package/lib/openxml/word/drawing/BlipFill.js +24 -0
  221. package/lib/openxml/word/drawing/Drawing.d.ts +6 -0
  222. package/lib/openxml/word/drawing/Drawing.js +19 -0
  223. package/lib/openxml/word/drawing/Pic.d.ts +8 -0
  224. package/lib/openxml/word/drawing/Pic.js +20 -0
  225. package/lib/openxml/word/drawing/ShapeProperties.d.ts +9 -0
  226. package/lib/openxml/word/drawing/ShapeProperties.js +24 -0
  227. package/lib/openxml/word/drawing/Transform.d.ts +17 -0
  228. package/lib/openxml/word/drawing/Transform.js +34 -0
  229. package/lib/openxml/word/numbering/AbstractNum.d.ts +13 -0
  230. package/lib/openxml/word/numbering/AbstractNum.js +46 -0
  231. package/lib/openxml/word/numbering/Lvl.d.ts +16 -0
  232. package/lib/openxml/word/numbering/Lvl.js +66 -0
  233. package/lib/openxml/word/numbering/Num.d.ts +10 -0
  234. package/lib/openxml/word/numbering/Num.js +50 -0
  235. package/lib/openxml/word/numbering/NumberProperties.d.ts +6 -0
  236. package/lib/openxml/word/numbering/NumberProperties.js +25 -0
  237. package/lib/openxml/word/numbering/Numbering.d.ts +9 -0
  238. package/lib/openxml/word/numbering/Numbering.js +54 -0
  239. package/lib/openxml/word/properties/Properties.d.ts +12 -0
  240. package/lib/openxml/word/table/Tc.d.ts +46 -0
  241. package/lib/openxml/word/table/Tc.js +242 -0
  242. package/lib/openxml/word/table/Tr.d.ts +18 -0
  243. package/lib/openxml/word/table/Tr.js +110 -0
  244. package/lib/package/PackageParser.d.ts +26 -0
  245. package/lib/package/XMLPackageParser.d.ts +31 -0
  246. package/lib/package/ZipPackageParser.d.ts +29 -0
  247. package/lib/package/ZipPackageParser.js +74 -0
  248. package/lib/parse/jcToTextAlign.d.ts +5 -0
  249. package/lib/parse/jcToTextAlign.js +25 -0
  250. package/lib/parse/parseBorder.d.ts +13 -0
  251. package/lib/parse/parseBorder.js +83 -0
  252. package/lib/parse/parseColor.d.ts +22 -0
  253. package/lib/parse/parseColor.js +137 -0
  254. package/lib/parse/parseFont.d.ts +7 -0
  255. package/lib/parse/parseFont.js +58 -0
  256. package/lib/parse/parseInd.d.ts +5 -0
  257. package/lib/parse/parseInd.js +31 -0
  258. package/lib/parse/parsePr.d.ts +8 -0
  259. package/lib/parse/parsePr.js +329 -0
  260. package/lib/parse/parseRelationship.d.ts +12 -0
  261. package/lib/parse/parseRelationship.js +45 -0
  262. package/lib/parse/parseSize.d.ts +27 -0
  263. package/lib/parse/parseSize.js +44 -0
  264. package/lib/parse/parseSpacing.d.ts +7 -0
  265. package/lib/parse/parseSpacing.js +47 -0
  266. package/lib/parse/parseTextDirection.d.ts +2 -0
  267. package/lib/parse/parseTextDirection.js +29 -0
  268. package/lib/parse/parseTrHeight.d.ts +2 -0
  269. package/lib/parse/parseTrHeight.js +21 -0
  270. package/lib/render/renderBody.d.ts +6 -0
  271. package/lib/render/renderBody.js +57 -0
  272. package/lib/render/renderBookmark.d.ts +6 -0
  273. package/lib/render/renderBookmark.js +21 -0
  274. package/lib/render/renderBr.d.ts +7 -0
  275. package/lib/render/renderBr.js +17 -0
  276. package/lib/render/renderDocument.d.ts +7 -0
  277. package/lib/render/renderDocument.js +18 -0
  278. package/lib/render/renderDrawing.d.ts +8 -0
  279. package/lib/render/renderDrawing.js +42 -0
  280. package/lib/render/renderHyperLink.d.ts +7 -0
  281. package/lib/render/renderHyperLink.js +45 -0
  282. package/lib/render/renderInlineText.d.ts +6 -0
  283. package/lib/render/renderInlineText.js +43 -0
  284. package/lib/render/renderInstrText.d.ts +8 -0
  285. package/lib/render/renderInstrText.js +44 -0
  286. package/lib/render/renderNumbering.d.ts +7 -0
  287. package/lib/render/renderNumbering.js +165 -0
  288. package/lib/render/renderParagraph.d.ts +7 -0
  289. package/lib/render/renderParagraph.js +82 -0
  290. package/lib/render/renderPict.d.ts +6 -0
  291. package/lib/render/renderPict.js +23 -0
  292. package/lib/render/renderRuby.d.ts +6 -0
  293. package/lib/render/renderRuby.js +57 -0
  294. package/lib/render/renderRun.d.ts +14 -0
  295. package/lib/render/renderRun.js +121 -0
  296. package/lib/render/renderSection.d.ts +6 -0
  297. package/lib/render/renderSection.js +40 -0
  298. package/lib/render/renderStyle.d.ts +13 -0
  299. package/lib/render/renderStyle.js +199 -0
  300. package/lib/render/renderSym.d.ts +3 -0
  301. package/lib/render/renderSym.js +14 -0
  302. package/lib/render/renderTab.d.ts +7 -0
  303. package/lib/render/renderTab.js +23 -0
  304. package/lib/render/renderTable.d.ts +6 -0
  305. package/lib/render/renderTable.js +190 -0
  306. package/lib/render/setElementStyle.d.ts +6 -0
  307. package/lib/render/setElementStyle.js +25 -0
  308. package/lib/util/autoSpace.d.ts +5 -0
  309. package/lib/util/autoSpace.js +37 -0
  310. package/lib/util/blob.d.ts +9 -0
  311. package/lib/util/blob.js +44 -0
  312. package/lib/util/dom.d.ts +36 -0
  313. package/lib/util/dom.js +72 -0
  314. package/lib/util/mergeRun.d.ts +17 -0
  315. package/lib/util/mergeRun.js +140 -0
  316. package/lib/util/xml.d.ts +8 -0
  317. package/lib/util/xml.js +16 -0
  318. package/package.json +91 -0
package/lib/Word.js ADDED
@@ -0,0 +1,374 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib_es6 = require('./node_modules/tslib/tslib.es6.js');
6
+ var parseRelationship = require('./parse/parseRelationship.js');
7
+ var ContentType = require('./openxml/ContentType.js');
8
+ var Style = require('./openxml/Style.js');
9
+ var Theme = require('./openxml/Theme.js');
10
+ var renderDocument = require('./render/renderDocument.js');
11
+ var blob = require('./util/blob.js');
12
+ var Numbering = require('./openxml/word/numbering/Numbering.js');
13
+ var dom = require('./util/dom.js');
14
+ var renderStyle = require('./render/renderStyle.js');
15
+ var mergeRun = require('./util/mergeRun.js');
16
+ var WDocument = require('./openxml/word/WDocument.js');
17
+ var renderRun = require('./render/renderRun.js');
18
+ var ZipPackageParser = require('./package/ZipPackageParser.js');
19
+ var xml = require('./util/xml.js');
20
+
21
+ /**
22
+ * 总入口,它将包括所有 word 文档信息,后续渲染的时候依赖它来获取关联信息
23
+ */
24
+ var defaultRenderOptions = {
25
+ imageDataURL: false,
26
+ classPrefix: 'docx-viewer',
27
+ inWrap: true,
28
+ bulletUseFont: true,
29
+ ignoreHeight: true,
30
+ ignoreWidth: true,
31
+ minLineHeight: 1.0,
32
+ enableVar: false,
33
+ debug: false
34
+ };
35
+ var Word = /** @class */ (function () {
36
+ /**
37
+ * 构建 word
38
+ *
39
+ * @param docxFile docx 文件
40
+ * @param options 渲染配置
41
+ * @param packaParser 包解析器
42
+ */
43
+ function Word(docFile, renderOptions, parser) {
44
+ if (parser === void 0) { parser = new ZipPackageParser["default"](); }
45
+ /**
46
+ * 解析 theme 目录里的数据
47
+ */
48
+ this.themes = [];
49
+ /**
50
+ * 样式名映射,因为自定义样式名有可能不符合 css 命名规范,因此实际使用这个名字
51
+ */
52
+ this.styleIdMap = {};
53
+ /**
54
+ * 用于自动生成样式名时的计数,保证每次都是唯一的
55
+ */
56
+ this.styleIdNum = 0;
57
+ this.wrapClassName = 'docx-viewer-wrapper';
58
+ this.inited = false;
59
+ parser.load(docFile);
60
+ this.id = Word.globalId++;
61
+ this.parser = parser;
62
+ this.renderOptions = tslib_es6.__assign(tslib_es6.__assign({}, defaultRenderOptions), renderOptions);
63
+ }
64
+ /**
65
+ * 初始化一些公共资源,比如
66
+ */
67
+ Word.prototype.init = function () {
68
+ if (this.inited) {
69
+ return;
70
+ }
71
+ // 这个必须在最前面,因为后面很多依赖它来查找文件的
72
+ this.initContentType();
73
+ this.initTheme();
74
+ this.initStyle();
75
+ this.initRelation();
76
+ this.initNumbering();
77
+ this.inited = true;
78
+ };
79
+ /**
80
+ * 解析全局主题配置
81
+ */
82
+ Word.prototype.initTheme = function () {
83
+ var e_1, _a;
84
+ try {
85
+ for (var _b = tslib_es6.__values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
86
+ var override = _c.value;
87
+ if (override.partName.startsWith('/word/theme')) {
88
+ var theme = this.parser.getXML(override.partName);
89
+ this.themes.push(Theme.parseTheme(theme));
90
+ }
91
+ }
92
+ }
93
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
94
+ finally {
95
+ try {
96
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
97
+ }
98
+ finally { if (e_1) throw e_1.error; }
99
+ }
100
+ };
101
+ /**
102
+ * 解析全局样式
103
+ */
104
+ Word.prototype.initStyle = function () {
105
+ var e_2, _a;
106
+ try {
107
+ for (var _b = tslib_es6.__values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
108
+ var override = _c.value;
109
+ if (override.partName.startsWith('/word/styles.xml')) {
110
+ this.styles = Style.parseStyles(this, this.parser.getXML('/word/styles.xml'));
111
+ }
112
+ }
113
+ }
114
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
115
+ finally {
116
+ try {
117
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
118
+ }
119
+ finally { if (e_2) throw e_2.error; }
120
+ }
121
+ };
122
+ /**
123
+ * 解析关系
124
+ */
125
+ Word.prototype.initRelation = function () {
126
+ var rels = {};
127
+ if (this.parser.fileExists('/_rels/.rels')) {
128
+ rels = parseRelationship.parseRelationships(this.parser.getXML('/_rels/.rels'), 'root');
129
+ }
130
+ var documentRels = {};
131
+ if (this.parser.fileExists('/word/_rels/document.xml.rels')) {
132
+ documentRels = parseRelationship.parseRelationships(this.parser.getXML('/word/_rels/document.xml.rels'), 'word');
133
+ }
134
+ this.relationships = tslib_es6.__assign(tslib_es6.__assign({}, rels), documentRels);
135
+ };
136
+ /**
137
+ * 解析全局配置
138
+ */
139
+ Word.prototype.initContentType = function () {
140
+ var contentType = this.parser.getXML('[Content_Types].xml');
141
+ this.conentTypes = ContentType.parseContentType(contentType);
142
+ };
143
+ /**
144
+ * 解析 numbering
145
+ */
146
+ Word.prototype.initNumbering = function () {
147
+ var e_3, _a;
148
+ try {
149
+ for (var _b = tslib_es6.__values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
150
+ var override = _c.value;
151
+ if (override.partName.startsWith('/word/numbering')) {
152
+ var numberingData = this.parser.getXML(override.partName);
153
+ this.numbering = Numbering.Numbering.fromXML(this, numberingData);
154
+ }
155
+ }
156
+ }
157
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
158
+ finally {
159
+ try {
160
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
161
+ }
162
+ finally { if (e_3) throw e_3.error; }
163
+ }
164
+ };
165
+ /**
166
+ * 根据 id 获取关系
167
+ */
168
+ Word.prototype.getRelationship = function (id) {
169
+ if (id) {
170
+ return this.relationships[id];
171
+ }
172
+ return null;
173
+ };
174
+ /**
175
+ * 进行文本替换
176
+ */
177
+ Word.prototype.replaceText = function (text) {
178
+ if (this.renderOptions.enableVar === false ||
179
+ !this.renderOptions.replaceText) {
180
+ return text;
181
+ }
182
+ return this.renderOptions.replaceText(text);
183
+ };
184
+ /**
185
+ * 加载图片
186
+ */
187
+ Word.prototype.loadImage = function (relation) {
188
+ var path = relation.target;
189
+ if (relation.part === 'word') {
190
+ path = 'word/' + path;
191
+ }
192
+ var data = this.parser.getFileByType(path, 'blob');
193
+ if (data) {
194
+ if (this.renderOptions.imageDataURL) {
195
+ return blob.blobToDataURL(data);
196
+ }
197
+ else {
198
+ return new Promise(function (resolve, reject) {
199
+ resolve(URL.createObjectURL(data));
200
+ });
201
+ }
202
+ }
203
+ return null;
204
+ };
205
+ /**
206
+ * 解析 html
207
+ */
208
+ Word.prototype.getXML = function (filePath) {
209
+ return this.parser.getXML(filePath);
210
+ };
211
+ /**
212
+ * 获取 styleId 的显示名称,因为这里可以自定义,理论上会出现 css 不支持的语法
213
+ */
214
+ Word.prototype.getStyleIdDisplayName = function (styleId) {
215
+ /**
216
+ * 简单判断一下,如果是合法的 css 名称,就直接返回
217
+ * In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".
218
+ */
219
+ if (styleId.match(/^[a-zA-Z]+[a-zA-Z0-9\-\_]*$/)) {
220
+ return this.getClassPrefix() + '-' + styleId;
221
+ }
222
+ if (styleId in this.styleIdMap) {
223
+ return this.styleIdMap[styleId];
224
+ }
225
+ else {
226
+ this.styleIdMap[styleId] = this.genClassName();
227
+ return this.styleIdMap[styleId];
228
+ }
229
+ };
230
+ /**
231
+ * 生成个新的唯一的 class 名称
232
+ */
233
+ Word.prototype.genClassName = function () {
234
+ return 'docx-classname-' + this.styleIdNum++;
235
+ };
236
+ /**
237
+ * 添加新样式,主要用于表格的单元格样式
238
+ */
239
+ Word.prototype.appendStyle = function (style) {
240
+ var styleElement = document.createElement('style');
241
+ styleElement.innerHTML = style;
242
+ this.rootElement.appendChild(styleElement);
243
+ };
244
+ /**
245
+ * 返回样式表名及它的父级样式表名
246
+ * @param styleId 样式表里的 style 名称
247
+ * @returns 返回 className 数组
248
+ */
249
+ Word.prototype.getStyleClassName = function (stylId) {
250
+ var style = this.styles.styleMap[stylId];
251
+ if (!style) {
252
+ return [];
253
+ }
254
+ var classNames = [this.getStyleIdDisplayName(stylId)];
255
+ if (style.basedOn) {
256
+ classNames.unshift(this.getStyleIdDisplayName(style.basedOn));
257
+ }
258
+ return classNames;
259
+ };
260
+ /**
261
+ * 渲染时的 css 类前缀
262
+ */
263
+ Word.prototype.getClassPrefix = function () {
264
+ return "".concat(this.renderOptions.classPrefix, "-").concat(this.id);
265
+ };
266
+ /**
267
+ * 获取主题色,目前基于 css 变量实现,方便动态修改
268
+ */
269
+ Word.prototype.getThemeColor = function (name) {
270
+ return "var(--docx-".concat(this.id, "-theme-").concat(name, "-color)");
271
+ };
272
+ Word.prototype.addClass = function (element, className) {
273
+ element.classList.add("".concat(this.getClassPrefix(), "-").concat(className));
274
+ };
275
+ /**
276
+ * 更新页面中的变量,这个要在 render 后运行
277
+ */
278
+ Word.prototype.updateVariable = function () {
279
+ if (!this.rootElement ||
280
+ this.renderOptions.enableVar === false ||
281
+ !this.renderOptions.replaceText) {
282
+ return;
283
+ }
284
+ renderRun.updateVariableText(this);
285
+ };
286
+ /**
287
+ * 下载生成的文档,会对 word/document.xml 进行处理,替换文本
288
+ */
289
+ Word.prototype.download = function (fileName) {
290
+ if (fileName === void 0) { fileName = 'document.docx'; }
291
+ var documentData = this.getXML('word/document.xml');
292
+ if (this.renderOptions.enableVar && this.renderOptions.replaceText) {
293
+ mergeRun.mergeRun(this, documentData);
294
+ // 对文本进行替换
295
+ var ts = documentData.getElementsByTagName('w:t');
296
+ for (var i = 0; i < ts.length; i++) {
297
+ ts[i].textContent = this.replaceText(ts[i].textContent || '');
298
+ }
299
+ }
300
+ var blob$1 = this.parser.generateZip(xml.buildXML(documentData));
301
+ blob.downloadBlob(blob$1, fileName);
302
+ };
303
+ /**
304
+ * 打印功能
305
+ */
306
+ Word.prototype.print = function () {
307
+ var _a, _b;
308
+ return tslib_es6.__awaiter(this, void 0, void 0, function () {
309
+ var iframe;
310
+ return tslib_es6.__generator(this, function (_c) {
311
+ switch (_c.label) {
312
+ case 0:
313
+ iframe = document.createElement('iframe');
314
+ iframe.style.position = 'absolute';
315
+ iframe.style.top = '-10000px';
316
+ document.body.appendChild(iframe);
317
+ (_a = iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.write('<div id="print"></div>');
318
+ return [4 /*yield*/, this.render((_b = iframe.contentDocument) === null || _b === void 0 ? void 0 : _b.getElementById('print'))];
319
+ case 1:
320
+ _c.sent();
321
+ setTimeout(function () {
322
+ var _a, _b;
323
+ iframe.focus();
324
+ (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.print();
325
+ (_b = iframe.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(iframe);
326
+ }, 100);
327
+ window.focus();
328
+ return [2 /*return*/];
329
+ }
330
+ });
331
+ });
332
+ };
333
+ /**
334
+ * 渲染文档入口
335
+ *
336
+ * @param root 渲染的根节点
337
+ */
338
+ Word.prototype.render = function (root) {
339
+ return tslib_es6.__awaiter(this, void 0, void 0, function () {
340
+ var renderOptions, isDebug, documentData, document, documentElement;
341
+ return tslib_es6.__generator(this, function (_a) {
342
+ this.init();
343
+ renderOptions = this.renderOptions;
344
+ isDebug = renderOptions.debug;
345
+ isDebug && console.log('init', this);
346
+ this.rootElement = root;
347
+ root.innerHTML = '';
348
+ documentData = this.getXML('word/document.xml');
349
+ isDebug && console.log('documentData', documentData);
350
+ if (renderOptions.enableVar && renderOptions.replaceText) {
351
+ mergeRun.mergeRun(this, documentData);
352
+ // 这里不进行变量替换,方便后续进行局部替换来更新变量
353
+ }
354
+ document = WDocument.WDocument.fromXML(this, documentData);
355
+ isDebug && console.log('document', document);
356
+ documentElement = renderDocument["default"](this, document);
357
+ root.classList.add(this.getClassPrefix());
358
+ if (renderOptions.inWrap) {
359
+ root.classList.add(this.wrapClassName);
360
+ }
361
+ dom.appendChild(root, renderStyle.renderStyle(this));
362
+ dom.appendChild(root, documentElement);
363
+ return [2 /*return*/];
364
+ });
365
+ });
366
+ };
367
+ /**
368
+ * 全局 id,用于一个页面渲染多个 word 文档
369
+ */
370
+ Word.globalId = 0;
371
+ return Word;
372
+ }());
373
+
374
+ exports["default"] = Word;
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import Word from './Word';
2
+ export { Word };
3
+ declare const _default: {
4
+ Word: typeof Word;
5
+ };
6
+ export default _default;
package/lib/index.js ADDED
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var Word = require('./Word.js');
6
+
7
+ var index = { Word: Word["default"] };
8
+
9
+ exports.Word = Word["default"];
10
+ exports["default"] = index;
@@ -0,0 +1,129 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise */
20
+
21
+ var extendStatics = function(d, b) {
22
+ extendStatics = Object.setPrototypeOf ||
23
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
24
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
25
+ return extendStatics(d, b);
26
+ };
27
+
28
+ function __extends(d, b) {
29
+ if (typeof b !== "function" && b !== null)
30
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
31
+ extendStatics(d, b);
32
+ function __() { this.constructor = d; }
33
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34
+ }
35
+
36
+ exports.__assign = function() {
37
+ exports.__assign = Object.assign || function __assign(t) {
38
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
39
+ s = arguments[i];
40
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
41
+ }
42
+ return t;
43
+ };
44
+ return exports.__assign.apply(this, arguments);
45
+ };
46
+
47
+ function __awaiter(thisArg, _arguments, P, generator) {
48
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
49
+ return new (P || (P = Promise))(function (resolve, reject) {
50
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
51
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
52
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
53
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
54
+ });
55
+ }
56
+
57
+ function __generator(thisArg, body) {
58
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
59
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
60
+ function verb(n) { return function (v) { return step([n, v]); }; }
61
+ function step(op) {
62
+ if (f) throw new TypeError("Generator is already executing.");
63
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
64
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
65
+ if (y = 0, t) op = [op[0] & 2, t.value];
66
+ switch (op[0]) {
67
+ case 0: case 1: t = op; break;
68
+ case 4: _.label++; return { value: op[1], done: false };
69
+ case 5: _.label++; y = op[1]; op = [0]; continue;
70
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
71
+ default:
72
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
73
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
74
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
75
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
76
+ if (t[2]) _.ops.pop();
77
+ _.trys.pop(); continue;
78
+ }
79
+ op = body.call(thisArg, _);
80
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
81
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
82
+ }
83
+ }
84
+
85
+ function __values(o) {
86
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
87
+ if (m) return m.call(o);
88
+ if (o && typeof o.length === "number") return {
89
+ next: function () {
90
+ if (o && i >= o.length) o = void 0;
91
+ return { value: o && o[i++], done: !o };
92
+ }
93
+ };
94
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
95
+ }
96
+
97
+ function __read(o, n) {
98
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
99
+ if (!m) return o;
100
+ var i = m.call(o), r, ar = [], e;
101
+ try {
102
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
103
+ }
104
+ catch (error) { e = { error: error }; }
105
+ finally {
106
+ try {
107
+ if (r && !r.done && (m = i["return"])) m.call(i);
108
+ }
109
+ finally { if (e) throw e.error; }
110
+ }
111
+ return ar;
112
+ }
113
+
114
+ function __spreadArray(to, from, pack) {
115
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
116
+ if (ar || !(i in from)) {
117
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
118
+ ar[i] = from[i];
119
+ }
120
+ }
121
+ return to.concat(ar || Array.prototype.slice.call(from));
122
+ }
123
+
124
+ exports.__awaiter = __awaiter;
125
+ exports.__extends = __extends;
126
+ exports.__generator = __generator;
127
+ exports.__read = __read;
128
+ exports.__spreadArray = __spreadArray;
129
+ exports.__values = __values;
@@ -0,0 +1,12 @@
1
+ interface Override {
2
+ partName: string;
3
+ contentType: string;
4
+ }
5
+ export interface ContentTypes {
6
+ overrides: Override[];
7
+ }
8
+ /**
9
+ * 解析 [Content_Types].xml
10
+ */
11
+ export declare function parseContentType(doc: Document): ContentTypes;
12
+ export {};
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * 解析 [Content_Types].xml
7
+ */
8
+ function parseContentType(doc) {
9
+ var types = { overrides: [] };
10
+ doc.querySelectorAll('Override').forEach(function (item) {
11
+ types.overrides.push({
12
+ partName: item.getAttribute('PartName'),
13
+ contentType: item.getAttribute('ContentType')
14
+ });
15
+ });
16
+ return types;
17
+ }
18
+
19
+ exports.parseContentType = parseContentType;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 解析共享样式 Style
3
+ */
4
+ import Word from '../Word';
5
+ import { ST_StyleType, ST_TblStyleOverrideType } from './Types';
6
+ import { ParagraphPr } from './word/Paragraph';
7
+ import { RunPr } from './word/Run';
8
+ import { TablePr } from './word/Table';
9
+ import { TcPr } from './word/table/Tc';
10
+ import { TrPr } from './word/table/Tr';
11
+ export interface CSSStyle {
12
+ [key: string]: string;
13
+ }
14
+ export interface TblStylePrStyle {
15
+ rPr?: RunPr;
16
+ pPr?: ParagraphPr;
17
+ tblPr?: TablePr;
18
+ trPr?: TrPr;
19
+ tcPr?: TcPr;
20
+ }
21
+ /**
22
+ * 单个样式定义
23
+ */
24
+ export interface Style extends TblStylePrStyle {
25
+ id?: string;
26
+ type?: ST_StyleType;
27
+ name?: string;
28
+ basedOn?: string;
29
+ tblStylePr?: Record<ST_TblStyleOverrideType, TblStylePrStyle>;
30
+ }
31
+ /**
32
+ * 所有样式定义及默认样式
33
+ */
34
+ export interface Styles {
35
+ styleMap: Record<string, Style>;
36
+ defaultStyle?: Style;
37
+ }
38
+ /**
39
+ * 解析 styles.xml
40
+ */
41
+ export declare function parseStyles(word: Word, doc: Document): Styles;