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.
- package/README.md +23 -0
- package/esm/OpenXML.d.ts +21 -0
- package/esm/OpenXML.js +60 -0
- package/esm/Word.d.ts +208 -0
- package/esm/Word.js +370 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +6 -0
- package/esm/node_modules/tslib/tslib.es6.js +120 -0
- package/esm/openxml/ContentType.d.ts +12 -0
- package/esm/openxml/ContentType.js +15 -0
- package/esm/openxml/Style.d.ts +41 -0
- package/esm/openxml/Style.js +156 -0
- package/esm/openxml/Theme.d.ts +26 -0
- package/esm/openxml/Theme.js +69 -0
- package/esm/openxml/Types.d.ts +2512 -0
- package/esm/openxml/Types.js +1003 -0
- package/esm/openxml/word/Background.d.ts +11 -0
- package/esm/openxml/word/Body.d.ts +18 -0
- package/esm/openxml/word/Body.js +67 -0
- package/esm/openxml/word/Bookmark.d.ts +6 -0
- package/esm/openxml/word/Bookmark.js +18 -0
- package/esm/openxml/word/Border.d.ts +10 -0
- package/esm/openxml/word/Break.d.ts +13 -0
- package/esm/openxml/word/Break.js +19 -0
- package/esm/openxml/word/CustomXml.d.ts +8 -0
- package/esm/openxml/word/FldSimple.d.ts +7 -0
- package/esm/openxml/word/FldSimple.js +15 -0
- package/esm/openxml/word/FrameProperties.d.ts +18 -0
- package/esm/openxml/word/Hyperlink.d.ts +13 -0
- package/esm/openxml/word/Hyperlink.js +53 -0
- package/esm/openxml/word/Indent.d.ts +8 -0
- package/esm/openxml/word/InlineText.d.ts +14 -0
- package/esm/openxml/word/InlineText.js +62 -0
- package/esm/openxml/word/InstrText.d.ts +4 -0
- package/esm/openxml/word/InstrText.js +8 -0
- package/esm/openxml/word/Paragraph.d.ts +34 -0
- package/esm/openxml/word/Paragraph.js +133 -0
- package/esm/openxml/word/Pict.d.ts +6 -0
- package/esm/openxml/word/Pict.js +19 -0
- package/esm/openxml/word/Ruby.d.ts +20 -0
- package/esm/openxml/word/Ruby.js +87 -0
- package/esm/openxml/word/Run.d.ts +31 -0
- package/esm/openxml/word/Run.js +98 -0
- package/esm/openxml/word/Section.d.ts +38 -0
- package/esm/openxml/word/Section.js +59 -0
- package/esm/openxml/word/Shading.d.ts +9 -0
- package/esm/openxml/word/SmartTag.d.ts +8 -0
- package/esm/openxml/word/SmartTag.js +18 -0
- package/esm/openxml/word/Spacing.d.ts +9 -0
- package/esm/openxml/word/Sym.d.ts +8 -0
- package/esm/openxml/word/Sym.js +16 -0
- package/esm/openxml/word/Tab.d.ts +8 -0
- package/esm/openxml/word/Tab.js +17 -0
- package/esm/openxml/word/Table.d.ts +49 -0
- package/esm/openxml/word/Table.js +248 -0
- package/esm/openxml/word/VerticalAlign.d.ts +6 -0
- package/esm/openxml/word/WDocument.d.ts +13 -0
- package/esm/openxml/word/WDocument.js +58 -0
- package/esm/openxml/word/drawing/Blip.d.ts +6 -0
- package/esm/openxml/word/drawing/Blip.js +17 -0
- package/esm/openxml/word/drawing/BlipFill.d.ts +9 -0
- package/esm/openxml/word/drawing/BlipFill.js +20 -0
- package/esm/openxml/word/drawing/Drawing.d.ts +6 -0
- package/esm/openxml/word/drawing/Drawing.js +15 -0
- package/esm/openxml/word/drawing/Pic.d.ts +8 -0
- package/esm/openxml/word/drawing/Pic.js +16 -0
- package/esm/openxml/word/drawing/ShapeProperties.d.ts +9 -0
- package/esm/openxml/word/drawing/ShapeProperties.js +20 -0
- package/esm/openxml/word/drawing/Transform.d.ts +17 -0
- package/esm/openxml/word/drawing/Transform.js +30 -0
- package/esm/openxml/word/numbering/AbstractNum.d.ts +13 -0
- package/esm/openxml/word/numbering/AbstractNum.js +42 -0
- package/esm/openxml/word/numbering/Lvl.d.ts +16 -0
- package/esm/openxml/word/numbering/Lvl.js +62 -0
- package/esm/openxml/word/numbering/Num.d.ts +10 -0
- package/esm/openxml/word/numbering/Num.js +46 -0
- package/esm/openxml/word/numbering/NumberProperties.d.ts +6 -0
- package/esm/openxml/word/numbering/NumberProperties.js +21 -0
- package/esm/openxml/word/numbering/Numbering.d.ts +9 -0
- package/esm/openxml/word/numbering/Numbering.js +50 -0
- package/esm/openxml/word/properties/Properties.d.ts +12 -0
- package/esm/openxml/word/table/Tc.d.ts +46 -0
- package/esm/openxml/word/table/Tc.js +234 -0
- package/esm/openxml/word/table/Tr.d.ts +18 -0
- package/esm/openxml/word/table/Tr.js +106 -0
- package/esm/package/PackageParser.d.ts +26 -0
- package/esm/package/XMLPackageParser.d.ts +31 -0
- package/esm/package/ZipPackageParser.d.ts +29 -0
- package/esm/package/ZipPackageParser.js +70 -0
- package/esm/parse/jcToTextAlign.d.ts +5 -0
- package/esm/parse/jcToTextAlign.js +21 -0
- package/esm/parse/parseBorder.d.ts +13 -0
- package/esm/parse/parseBorder.js +78 -0
- package/esm/parse/parseColor.d.ts +22 -0
- package/esm/parse/parseColor.js +131 -0
- package/esm/parse/parseFont.d.ts +7 -0
- package/esm/parse/parseFont.js +54 -0
- package/esm/parse/parseInd.d.ts +5 -0
- package/esm/parse/parseInd.js +27 -0
- package/esm/parse/parsePr.d.ts +8 -0
- package/esm/parse/parsePr.js +325 -0
- package/esm/parse/parseRelationship.d.ts +12 -0
- package/esm/parse/parseRelationship.js +40 -0
- package/esm/parse/parseSize.d.ts +27 -0
- package/esm/parse/parseSize.js +38 -0
- package/esm/parse/parseSpacing.d.ts +7 -0
- package/esm/parse/parseSpacing.js +43 -0
- package/esm/parse/parseTextDirection.d.ts +2 -0
- package/esm/parse/parseTextDirection.js +25 -0
- package/esm/parse/parseTrHeight.d.ts +2 -0
- package/esm/parse/parseTrHeight.js +17 -0
- package/esm/render/renderBody.d.ts +6 -0
- package/esm/render/renderBody.js +53 -0
- package/esm/render/renderBookmark.d.ts +6 -0
- package/esm/render/renderBookmark.js +17 -0
- package/esm/render/renderBr.d.ts +7 -0
- package/esm/render/renderBr.js +13 -0
- package/esm/render/renderDocument.d.ts +7 -0
- package/esm/render/renderDocument.js +14 -0
- package/esm/render/renderDrawing.d.ts +8 -0
- package/esm/render/renderDrawing.js +38 -0
- package/esm/render/renderHyperLink.d.ts +7 -0
- package/esm/render/renderHyperLink.js +41 -0
- package/esm/render/renderInlineText.d.ts +6 -0
- package/esm/render/renderInlineText.js +39 -0
- package/esm/render/renderInstrText.d.ts +8 -0
- package/esm/render/renderInstrText.js +40 -0
- package/esm/render/renderNumbering.d.ts +7 -0
- package/esm/render/renderNumbering.js +161 -0
- package/esm/render/renderParagraph.d.ts +7 -0
- package/esm/render/renderParagraph.js +78 -0
- package/esm/render/renderPict.d.ts +6 -0
- package/esm/render/renderPict.js +19 -0
- package/esm/render/renderRuby.d.ts +6 -0
- package/esm/render/renderRuby.js +53 -0
- package/esm/render/renderRun.d.ts +14 -0
- package/esm/render/renderRun.js +116 -0
- package/esm/render/renderSection.d.ts +6 -0
- package/esm/render/renderSection.js +36 -0
- package/esm/render/renderStyle.d.ts +13 -0
- package/esm/render/renderStyle.js +194 -0
- package/esm/render/renderSym.d.ts +3 -0
- package/esm/render/renderSym.js +10 -0
- package/esm/render/renderTab.d.ts +7 -0
- package/esm/render/renderTab.js +19 -0
- package/esm/render/renderTable.d.ts +6 -0
- package/esm/render/renderTable.js +186 -0
- package/esm/render/setElementStyle.d.ts +6 -0
- package/esm/render/setElementStyle.js +21 -0
- package/esm/util/autoSpace.d.ts +5 -0
- package/esm/util/autoSpace.js +33 -0
- package/esm/util/blob.d.ts +9 -0
- package/esm/util/blob.js +39 -0
- package/esm/util/dom.d.ts +36 -0
- package/esm/util/dom.js +63 -0
- package/esm/util/mergeRun.d.ts +17 -0
- package/esm/util/mergeRun.js +134 -0
- package/esm/util/xml.d.ts +8 -0
- package/esm/util/xml.js +12 -0
- package/lib/OpenXML.d.ts +21 -0
- package/lib/OpenXML.js +69 -0
- package/lib/Word.d.ts +208 -0
- package/lib/Word.js +374 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +10 -0
- package/lib/node_modules/tslib/tslib.es6.js +129 -0
- package/lib/openxml/ContentType.d.ts +12 -0
- package/lib/openxml/ContentType.js +19 -0
- package/lib/openxml/Style.d.ts +41 -0
- package/lib/openxml/Style.js +160 -0
- package/lib/openxml/Theme.d.ts +26 -0
- package/lib/openxml/Theme.js +73 -0
- package/lib/openxml/Types.d.ts +2512 -0
- package/lib/openxml/Types.js +1005 -0
- package/lib/openxml/word/Background.d.ts +11 -0
- package/lib/openxml/word/Body.d.ts +18 -0
- package/lib/openxml/word/Body.js +71 -0
- package/lib/openxml/word/Bookmark.d.ts +6 -0
- package/lib/openxml/word/Bookmark.js +22 -0
- package/lib/openxml/word/Border.d.ts +10 -0
- package/lib/openxml/word/Break.d.ts +13 -0
- package/lib/openxml/word/Break.js +23 -0
- package/lib/openxml/word/CustomXml.d.ts +8 -0
- package/lib/openxml/word/FldSimple.d.ts +7 -0
- package/lib/openxml/word/FldSimple.js +19 -0
- package/lib/openxml/word/FrameProperties.d.ts +18 -0
- package/lib/openxml/word/Hyperlink.d.ts +13 -0
- package/lib/openxml/word/Hyperlink.js +57 -0
- package/lib/openxml/word/Indent.d.ts +8 -0
- package/lib/openxml/word/InlineText.d.ts +14 -0
- package/lib/openxml/word/InlineText.js +66 -0
- package/lib/openxml/word/InstrText.d.ts +4 -0
- package/lib/openxml/word/InstrText.js +12 -0
- package/lib/openxml/word/Paragraph.d.ts +34 -0
- package/lib/openxml/word/Paragraph.js +137 -0
- package/lib/openxml/word/Pict.d.ts +6 -0
- package/lib/openxml/word/Pict.js +23 -0
- package/lib/openxml/word/Ruby.d.ts +20 -0
- package/lib/openxml/word/Ruby.js +91 -0
- package/lib/openxml/word/Run.d.ts +31 -0
- package/lib/openxml/word/Run.js +103 -0
- package/lib/openxml/word/Section.d.ts +38 -0
- package/lib/openxml/word/Section.js +63 -0
- package/lib/openxml/word/Shading.d.ts +9 -0
- package/lib/openxml/word/SmartTag.d.ts +8 -0
- package/lib/openxml/word/SmartTag.js +22 -0
- package/lib/openxml/word/Spacing.d.ts +9 -0
- package/lib/openxml/word/Sym.d.ts +8 -0
- package/lib/openxml/word/Sym.js +20 -0
- package/lib/openxml/word/Tab.d.ts +8 -0
- package/lib/openxml/word/Tab.js +21 -0
- package/lib/openxml/word/Table.d.ts +49 -0
- package/lib/openxml/word/Table.js +252 -0
- package/lib/openxml/word/VerticalAlign.d.ts +6 -0
- package/lib/openxml/word/WDocument.d.ts +13 -0
- package/lib/openxml/word/WDocument.js +62 -0
- package/lib/openxml/word/drawing/Blip.d.ts +6 -0
- package/lib/openxml/word/drawing/Blip.js +21 -0
- package/lib/openxml/word/drawing/BlipFill.d.ts +9 -0
- package/lib/openxml/word/drawing/BlipFill.js +24 -0
- package/lib/openxml/word/drawing/Drawing.d.ts +6 -0
- package/lib/openxml/word/drawing/Drawing.js +19 -0
- package/lib/openxml/word/drawing/Pic.d.ts +8 -0
- package/lib/openxml/word/drawing/Pic.js +20 -0
- package/lib/openxml/word/drawing/ShapeProperties.d.ts +9 -0
- package/lib/openxml/word/drawing/ShapeProperties.js +24 -0
- package/lib/openxml/word/drawing/Transform.d.ts +17 -0
- package/lib/openxml/word/drawing/Transform.js +34 -0
- package/lib/openxml/word/numbering/AbstractNum.d.ts +13 -0
- package/lib/openxml/word/numbering/AbstractNum.js +46 -0
- package/lib/openxml/word/numbering/Lvl.d.ts +16 -0
- package/lib/openxml/word/numbering/Lvl.js +66 -0
- package/lib/openxml/word/numbering/Num.d.ts +10 -0
- package/lib/openxml/word/numbering/Num.js +50 -0
- package/lib/openxml/word/numbering/NumberProperties.d.ts +6 -0
- package/lib/openxml/word/numbering/NumberProperties.js +25 -0
- package/lib/openxml/word/numbering/Numbering.d.ts +9 -0
- package/lib/openxml/word/numbering/Numbering.js +54 -0
- package/lib/openxml/word/properties/Properties.d.ts +12 -0
- package/lib/openxml/word/table/Tc.d.ts +46 -0
- package/lib/openxml/word/table/Tc.js +242 -0
- package/lib/openxml/word/table/Tr.d.ts +18 -0
- package/lib/openxml/word/table/Tr.js +110 -0
- package/lib/package/PackageParser.d.ts +26 -0
- package/lib/package/XMLPackageParser.d.ts +31 -0
- package/lib/package/ZipPackageParser.d.ts +29 -0
- package/lib/package/ZipPackageParser.js +74 -0
- package/lib/parse/jcToTextAlign.d.ts +5 -0
- package/lib/parse/jcToTextAlign.js +25 -0
- package/lib/parse/parseBorder.d.ts +13 -0
- package/lib/parse/parseBorder.js +83 -0
- package/lib/parse/parseColor.d.ts +22 -0
- package/lib/parse/parseColor.js +137 -0
- package/lib/parse/parseFont.d.ts +7 -0
- package/lib/parse/parseFont.js +58 -0
- package/lib/parse/parseInd.d.ts +5 -0
- package/lib/parse/parseInd.js +31 -0
- package/lib/parse/parsePr.d.ts +8 -0
- package/lib/parse/parsePr.js +329 -0
- package/lib/parse/parseRelationship.d.ts +12 -0
- package/lib/parse/parseRelationship.js +45 -0
- package/lib/parse/parseSize.d.ts +27 -0
- package/lib/parse/parseSize.js +44 -0
- package/lib/parse/parseSpacing.d.ts +7 -0
- package/lib/parse/parseSpacing.js +47 -0
- package/lib/parse/parseTextDirection.d.ts +2 -0
- package/lib/parse/parseTextDirection.js +29 -0
- package/lib/parse/parseTrHeight.d.ts +2 -0
- package/lib/parse/parseTrHeight.js +21 -0
- package/lib/render/renderBody.d.ts +6 -0
- package/lib/render/renderBody.js +57 -0
- package/lib/render/renderBookmark.d.ts +6 -0
- package/lib/render/renderBookmark.js +21 -0
- package/lib/render/renderBr.d.ts +7 -0
- package/lib/render/renderBr.js +17 -0
- package/lib/render/renderDocument.d.ts +7 -0
- package/lib/render/renderDocument.js +18 -0
- package/lib/render/renderDrawing.d.ts +8 -0
- package/lib/render/renderDrawing.js +42 -0
- package/lib/render/renderHyperLink.d.ts +7 -0
- package/lib/render/renderHyperLink.js +45 -0
- package/lib/render/renderInlineText.d.ts +6 -0
- package/lib/render/renderInlineText.js +43 -0
- package/lib/render/renderInstrText.d.ts +8 -0
- package/lib/render/renderInstrText.js +44 -0
- package/lib/render/renderNumbering.d.ts +7 -0
- package/lib/render/renderNumbering.js +165 -0
- package/lib/render/renderParagraph.d.ts +7 -0
- package/lib/render/renderParagraph.js +82 -0
- package/lib/render/renderPict.d.ts +6 -0
- package/lib/render/renderPict.js +23 -0
- package/lib/render/renderRuby.d.ts +6 -0
- package/lib/render/renderRuby.js +57 -0
- package/lib/render/renderRun.d.ts +14 -0
- package/lib/render/renderRun.js +121 -0
- package/lib/render/renderSection.d.ts +6 -0
- package/lib/render/renderSection.js +40 -0
- package/lib/render/renderStyle.d.ts +13 -0
- package/lib/render/renderStyle.js +199 -0
- package/lib/render/renderSym.d.ts +3 -0
- package/lib/render/renderSym.js +14 -0
- package/lib/render/renderTab.d.ts +7 -0
- package/lib/render/renderTab.js +23 -0
- package/lib/render/renderTable.d.ts +6 -0
- package/lib/render/renderTable.js +190 -0
- package/lib/render/setElementStyle.d.ts +6 -0
- package/lib/render/setElementStyle.js +25 -0
- package/lib/util/autoSpace.d.ts +5 -0
- package/lib/util/autoSpace.js +37 -0
- package/lib/util/blob.d.ts +9 -0
- package/lib/util/blob.js +44 -0
- package/lib/util/dom.d.ts +36 -0
- package/lib/util/dom.js +72 -0
- package/lib/util/mergeRun.d.ts +17 -0
- package/lib/util/mergeRun.js +140 -0
- package/lib/util/xml.d.ts +8 -0
- package/lib/util/xml.js +16 -0
- package/package.json +91 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# 说明
|
|
2
|
+
|
|
3
|
+
## 原理
|
|
4
|
+
|
|
5
|
+
docx 渲染器,原理是将 docx 里的 xml 格式转成 html
|
|
6
|
+
|
|
7
|
+
相对于 Canvas 渲染,这个实现方案比较简单,最终页面也可以很方便复制,但无法保证和原始 docx 文件展现一致,因为有部分功能难以在 HTML 中实现,比如图文环绕效果。
|
|
8
|
+
|
|
9
|
+
## 还不支持的功能
|
|
10
|
+
|
|
11
|
+
- wmf,需要使用 https://github.com/SheetJS/js-wmf
|
|
12
|
+
|
|
13
|
+
## 参考资料
|
|
14
|
+
|
|
15
|
+
- [官方规范](https://www.ecma-international.org/publications-and-standards/standards/ecma-376/)
|
|
16
|
+
- [标签的在线文档](http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/index.html)
|
|
17
|
+
|
|
18
|
+
日常开发可以使用 [OOXML viewer](https://marketplace.visualstudio.com/items?itemName=yuenm18.ooxml-viewer) 插件查看
|
|
19
|
+
|
|
20
|
+
开发过程啊参考了
|
|
21
|
+
|
|
22
|
+
- [docx](https://github.com/dolanmiu/docx) 的类型定义
|
|
23
|
+
- [docxjs](https://github.com/zVolodymyr/docxjs) 里格式的实现
|
package/esm/OpenXML.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* openxml 一些辅助函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 获取 w:val 的值
|
|
6
|
+
*/
|
|
7
|
+
export declare function getVal(element: Element): string;
|
|
8
|
+
/**
|
|
9
|
+
* 获取 w:val 的值,转成 number 类型
|
|
10
|
+
*/
|
|
11
|
+
export declare function getValNumber(element: Element): number;
|
|
12
|
+
/**
|
|
13
|
+
* 有可能是 on 或 off 之类的值,都归一化为 boolean
|
|
14
|
+
* @param value
|
|
15
|
+
* @param defaultValue 默认值
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare function normalizeBoolean(value: string | boolean | null, defaultValue?: boolean): boolean;
|
|
19
|
+
export declare function getValBoolean(element: Element, defaultValue?: boolean): boolean;
|
|
20
|
+
export declare function getAttrBoolean(element: Element, attr: string, defaultValue?: boolean): boolean;
|
|
21
|
+
export declare function getValHex(element: Element): number;
|
package/esm/OpenXML.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* openxml 一些辅助函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 获取 w:val 的值
|
|
6
|
+
*/
|
|
7
|
+
function getVal(element) {
|
|
8
|
+
return element.getAttribute('w:val') || '';
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 获取 w:val 的值,转成 number 类型
|
|
12
|
+
*/
|
|
13
|
+
function getValNumber(element) {
|
|
14
|
+
return parseInt(getVal(element), 10);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 有可能是 on 或 off 之类的值,都归一化为 boolean
|
|
18
|
+
* @param value
|
|
19
|
+
* @param defaultValue 默认值
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
function normalizeBoolean(value, defaultValue) {
|
|
23
|
+
if (defaultValue === void 0) { defaultValue = false; }
|
|
24
|
+
if (typeof value === 'boolean') {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
if (typeof value === 'string') {
|
|
28
|
+
switch (value) {
|
|
29
|
+
case '1':
|
|
30
|
+
return true;
|
|
31
|
+
case '0':
|
|
32
|
+
return false;
|
|
33
|
+
case 'on':
|
|
34
|
+
return true;
|
|
35
|
+
case 'off':
|
|
36
|
+
return false;
|
|
37
|
+
case 'true':
|
|
38
|
+
return true;
|
|
39
|
+
case 'false':
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === 'number') {
|
|
43
|
+
return value !== 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return defaultValue;
|
|
47
|
+
}
|
|
48
|
+
function getValBoolean(element, defaultValue) {
|
|
49
|
+
if (defaultValue === void 0) { defaultValue = true; }
|
|
50
|
+
return normalizeBoolean(getVal(element), defaultValue);
|
|
51
|
+
}
|
|
52
|
+
function getAttrBoolean(element, attr, defaultValue) {
|
|
53
|
+
if (defaultValue === void 0) { defaultValue = true; }
|
|
54
|
+
return normalizeBoolean(element.getAttribute(attr), defaultValue);
|
|
55
|
+
}
|
|
56
|
+
function getValHex(element) {
|
|
57
|
+
return parseInt(getVal(element) || '0', 16);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { getAttrBoolean, getVal, getValBoolean, getValHex, getValNumber, normalizeBoolean };
|
package/esm/Word.d.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 总入口,它将包括所有 word 文档信息,后续渲染的时候依赖它来获取关联信息
|
|
3
|
+
*/
|
|
4
|
+
import { Relationship } from './parse/parseRelationship';
|
|
5
|
+
import { ContentTypes } from './openxml/ContentType';
|
|
6
|
+
import { Styles } from './openxml/Style';
|
|
7
|
+
import { Theme } from './openxml/Theme';
|
|
8
|
+
import { Numbering } from './openxml/word/numbering/Numbering';
|
|
9
|
+
import { PackageParser } from './package/PackageParser';
|
|
10
|
+
import { Paragraph } from './openxml/word/Paragraph';
|
|
11
|
+
/**
|
|
12
|
+
* 渲染配置
|
|
13
|
+
*/
|
|
14
|
+
export interface WordRenderOptions {
|
|
15
|
+
/**
|
|
16
|
+
* css 类前缀
|
|
17
|
+
*/
|
|
18
|
+
classPrefix: string;
|
|
19
|
+
/** 图片是否使用 data url */
|
|
20
|
+
imageDataURL: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* 列表使用字体渲染,需要自行引入 Windings 字体
|
|
23
|
+
*/
|
|
24
|
+
bulletUseFont: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* 是否包裹出页面效果
|
|
27
|
+
*/
|
|
28
|
+
inWrap: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* 是否忽略文档宽度设置
|
|
31
|
+
*/
|
|
32
|
+
ignoreWidth?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* 是否忽略文档高度设置
|
|
35
|
+
*/
|
|
36
|
+
ignoreHeight?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* 强制文档内边距设置
|
|
39
|
+
*/
|
|
40
|
+
padding?: string;
|
|
41
|
+
/**
|
|
42
|
+
* 最小行高
|
|
43
|
+
*/
|
|
44
|
+
minLineHeight?: number;
|
|
45
|
+
/**
|
|
46
|
+
* 是否开启变量替换功能
|
|
47
|
+
*/
|
|
48
|
+
enableVar?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* 进行模板替换的函数
|
|
51
|
+
*/
|
|
52
|
+
replaceText?: (text: string) => string;
|
|
53
|
+
/**
|
|
54
|
+
* 是否开启调试模式
|
|
55
|
+
*/
|
|
56
|
+
debug?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 字体映射,用于替换文档中的字体
|
|
59
|
+
*/
|
|
60
|
+
fontMapping?: Record<string, string>;
|
|
61
|
+
/**
|
|
62
|
+
* 强制行高,设置之后所有文本都使用这个行高,可以优化排版效果
|
|
63
|
+
*/
|
|
64
|
+
forceLineHeight?: string;
|
|
65
|
+
}
|
|
66
|
+
export default class Word {
|
|
67
|
+
/**
|
|
68
|
+
* 全局 id,用于一个页面渲染多个 word 文档
|
|
69
|
+
*/
|
|
70
|
+
static globalId: number;
|
|
71
|
+
/**
|
|
72
|
+
* 当前渲染 id
|
|
73
|
+
*/
|
|
74
|
+
id: number;
|
|
75
|
+
/**
|
|
76
|
+
* openxml 包
|
|
77
|
+
*/
|
|
78
|
+
parser: PackageParser;
|
|
79
|
+
/**
|
|
80
|
+
* 解析 [Content_Types].xml 里的数据
|
|
81
|
+
*/
|
|
82
|
+
conentTypes: ContentTypes;
|
|
83
|
+
/**
|
|
84
|
+
* 解析 theme 目录里的数据
|
|
85
|
+
*/
|
|
86
|
+
themes: Theme[];
|
|
87
|
+
/**
|
|
88
|
+
* 解析 numbering.xml 里的数据
|
|
89
|
+
*/
|
|
90
|
+
numbering: Numbering;
|
|
91
|
+
/**
|
|
92
|
+
* 解析 styles.xml 里的数据
|
|
93
|
+
*/
|
|
94
|
+
styles: Styles;
|
|
95
|
+
renderOptions: WordRenderOptions;
|
|
96
|
+
relationships: Record<string, Relationship>;
|
|
97
|
+
/**
|
|
98
|
+
* 样式名映射,因为自定义样式名有可能不符合 css 命名规范,因此实际使用这个名字
|
|
99
|
+
*/
|
|
100
|
+
styleIdMap: Record<string, string>;
|
|
101
|
+
/**
|
|
102
|
+
* 用于自动生成样式名时的计数,保证每次都是唯一的
|
|
103
|
+
*/
|
|
104
|
+
styleIdNum: number;
|
|
105
|
+
/**
|
|
106
|
+
* 渲染根节点
|
|
107
|
+
*/
|
|
108
|
+
rootElement: HTMLElement;
|
|
109
|
+
wrapClassName: string;
|
|
110
|
+
/**
|
|
111
|
+
* 当前渲染的段落,主要用于获取 fldSimple
|
|
112
|
+
*/
|
|
113
|
+
currentParagraph: Paragraph;
|
|
114
|
+
/**
|
|
115
|
+
* 构建 word
|
|
116
|
+
*
|
|
117
|
+
* @param docxFile docx 文件
|
|
118
|
+
* @param options 渲染配置
|
|
119
|
+
* @param packaParser 包解析器
|
|
120
|
+
*/
|
|
121
|
+
constructor(docFile: ArrayBuffer | string, renderOptions?: Partial<WordRenderOptions>, parser?: PackageParser);
|
|
122
|
+
inited: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* 初始化一些公共资源,比如
|
|
125
|
+
*/
|
|
126
|
+
init(): void;
|
|
127
|
+
/**
|
|
128
|
+
* 解析全局主题配置
|
|
129
|
+
*/
|
|
130
|
+
initTheme(): void;
|
|
131
|
+
/**
|
|
132
|
+
* 解析全局样式
|
|
133
|
+
*/
|
|
134
|
+
initStyle(): void;
|
|
135
|
+
/**
|
|
136
|
+
* 解析关系
|
|
137
|
+
*/
|
|
138
|
+
initRelation(): void;
|
|
139
|
+
/**
|
|
140
|
+
* 解析全局配置
|
|
141
|
+
*/
|
|
142
|
+
initContentType(): void;
|
|
143
|
+
/**
|
|
144
|
+
* 解析 numbering
|
|
145
|
+
*/
|
|
146
|
+
initNumbering(): void;
|
|
147
|
+
/**
|
|
148
|
+
* 根据 id 获取关系
|
|
149
|
+
*/
|
|
150
|
+
getRelationship(id?: string): Relationship | null;
|
|
151
|
+
/**
|
|
152
|
+
* 进行文本替换
|
|
153
|
+
*/
|
|
154
|
+
replaceText(text: string): string;
|
|
155
|
+
/**
|
|
156
|
+
* 加载图片
|
|
157
|
+
*/
|
|
158
|
+
loadImage(relation: Relationship): Promise<string> | null;
|
|
159
|
+
/**
|
|
160
|
+
* 解析 html
|
|
161
|
+
*/
|
|
162
|
+
getXML(filePath: string): Document;
|
|
163
|
+
/**
|
|
164
|
+
* 获取 styleId 的显示名称,因为这里可以自定义,理论上会出现 css 不支持的语法
|
|
165
|
+
*/
|
|
166
|
+
getStyleIdDisplayName(styleId: string): string;
|
|
167
|
+
/**
|
|
168
|
+
* 生成个新的唯一的 class 名称
|
|
169
|
+
*/
|
|
170
|
+
genClassName(): string;
|
|
171
|
+
/**
|
|
172
|
+
* 添加新样式,主要用于表格的单元格样式
|
|
173
|
+
*/
|
|
174
|
+
appendStyle(style: string): void;
|
|
175
|
+
/**
|
|
176
|
+
* 返回样式表名及它的父级样式表名
|
|
177
|
+
* @param styleId 样式表里的 style 名称
|
|
178
|
+
* @returns 返回 className 数组
|
|
179
|
+
*/
|
|
180
|
+
getStyleClassName(stylId: string): string[];
|
|
181
|
+
/**
|
|
182
|
+
* 渲染时的 css 类前缀
|
|
183
|
+
*/
|
|
184
|
+
getClassPrefix(): string;
|
|
185
|
+
/**
|
|
186
|
+
* 获取主题色,目前基于 css 变量实现,方便动态修改
|
|
187
|
+
*/
|
|
188
|
+
getThemeColor(name: string): string;
|
|
189
|
+
addClass(element: HTMLElement, className: string): void;
|
|
190
|
+
/**
|
|
191
|
+
* 更新页面中的变量,这个要在 render 后运行
|
|
192
|
+
*/
|
|
193
|
+
updateVariable(): void;
|
|
194
|
+
/**
|
|
195
|
+
* 下载生成的文档,会对 word/document.xml 进行处理,替换文本
|
|
196
|
+
*/
|
|
197
|
+
download(fileName?: string): void;
|
|
198
|
+
/**
|
|
199
|
+
* 打印功能
|
|
200
|
+
*/
|
|
201
|
+
print(): Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* 渲染文档入口
|
|
204
|
+
*
|
|
205
|
+
* @param root 渲染的根节点
|
|
206
|
+
*/
|
|
207
|
+
render(root: HTMLElement): Promise<void>;
|
|
208
|
+
}
|
package/esm/Word.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { __values, __assign, __awaiter, __generator } from './node_modules/tslib/tslib.es6.js';
|
|
2
|
+
import { parseRelationships } from './parse/parseRelationship.js';
|
|
3
|
+
import { parseContentType } from './openxml/ContentType.js';
|
|
4
|
+
import { parseStyles } from './openxml/Style.js';
|
|
5
|
+
import { parseTheme } from './openxml/Theme.js';
|
|
6
|
+
import renderDocument from './render/renderDocument.js';
|
|
7
|
+
import { blobToDataURL, downloadBlob } from './util/blob.js';
|
|
8
|
+
import { Numbering } from './openxml/word/numbering/Numbering.js';
|
|
9
|
+
import { appendChild } from './util/dom.js';
|
|
10
|
+
import { renderStyle } from './render/renderStyle.js';
|
|
11
|
+
import { mergeRun } from './util/mergeRun.js';
|
|
12
|
+
import { WDocument } from './openxml/word/WDocument.js';
|
|
13
|
+
import { updateVariableText } from './render/renderRun.js';
|
|
14
|
+
import ZipPackageParser from './package/ZipPackageParser.js';
|
|
15
|
+
import { buildXML } from './util/xml.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 总入口,它将包括所有 word 文档信息,后续渲染的时候依赖它来获取关联信息
|
|
19
|
+
*/
|
|
20
|
+
var defaultRenderOptions = {
|
|
21
|
+
imageDataURL: false,
|
|
22
|
+
classPrefix: 'docx-viewer',
|
|
23
|
+
inWrap: true,
|
|
24
|
+
bulletUseFont: true,
|
|
25
|
+
ignoreHeight: true,
|
|
26
|
+
ignoreWidth: true,
|
|
27
|
+
minLineHeight: 1.0,
|
|
28
|
+
enableVar: false,
|
|
29
|
+
debug: false
|
|
30
|
+
};
|
|
31
|
+
var Word = /** @class */ (function () {
|
|
32
|
+
/**
|
|
33
|
+
* 构建 word
|
|
34
|
+
*
|
|
35
|
+
* @param docxFile docx 文件
|
|
36
|
+
* @param options 渲染配置
|
|
37
|
+
* @param packaParser 包解析器
|
|
38
|
+
*/
|
|
39
|
+
function Word(docFile, renderOptions, parser) {
|
|
40
|
+
if (parser === void 0) { parser = new ZipPackageParser(); }
|
|
41
|
+
/**
|
|
42
|
+
* 解析 theme 目录里的数据
|
|
43
|
+
*/
|
|
44
|
+
this.themes = [];
|
|
45
|
+
/**
|
|
46
|
+
* 样式名映射,因为自定义样式名有可能不符合 css 命名规范,因此实际使用这个名字
|
|
47
|
+
*/
|
|
48
|
+
this.styleIdMap = {};
|
|
49
|
+
/**
|
|
50
|
+
* 用于自动生成样式名时的计数,保证每次都是唯一的
|
|
51
|
+
*/
|
|
52
|
+
this.styleIdNum = 0;
|
|
53
|
+
this.wrapClassName = 'docx-viewer-wrapper';
|
|
54
|
+
this.inited = false;
|
|
55
|
+
parser.load(docFile);
|
|
56
|
+
this.id = Word.globalId++;
|
|
57
|
+
this.parser = parser;
|
|
58
|
+
this.renderOptions = __assign(__assign({}, defaultRenderOptions), renderOptions);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 初始化一些公共资源,比如
|
|
62
|
+
*/
|
|
63
|
+
Word.prototype.init = function () {
|
|
64
|
+
if (this.inited) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// 这个必须在最前面,因为后面很多依赖它来查找文件的
|
|
68
|
+
this.initContentType();
|
|
69
|
+
this.initTheme();
|
|
70
|
+
this.initStyle();
|
|
71
|
+
this.initRelation();
|
|
72
|
+
this.initNumbering();
|
|
73
|
+
this.inited = true;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* 解析全局主题配置
|
|
77
|
+
*/
|
|
78
|
+
Word.prototype.initTheme = function () {
|
|
79
|
+
var e_1, _a;
|
|
80
|
+
try {
|
|
81
|
+
for (var _b = __values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
82
|
+
var override = _c.value;
|
|
83
|
+
if (override.partName.startsWith('/word/theme')) {
|
|
84
|
+
var theme = this.parser.getXML(override.partName);
|
|
85
|
+
this.themes.push(parseTheme(theme));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
90
|
+
finally {
|
|
91
|
+
try {
|
|
92
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
93
|
+
}
|
|
94
|
+
finally { if (e_1) throw e_1.error; }
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* 解析全局样式
|
|
99
|
+
*/
|
|
100
|
+
Word.prototype.initStyle = function () {
|
|
101
|
+
var e_2, _a;
|
|
102
|
+
try {
|
|
103
|
+
for (var _b = __values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
104
|
+
var override = _c.value;
|
|
105
|
+
if (override.partName.startsWith('/word/styles.xml')) {
|
|
106
|
+
this.styles = parseStyles(this, this.parser.getXML('/word/styles.xml'));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
111
|
+
finally {
|
|
112
|
+
try {
|
|
113
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
114
|
+
}
|
|
115
|
+
finally { if (e_2) throw e_2.error; }
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* 解析关系
|
|
120
|
+
*/
|
|
121
|
+
Word.prototype.initRelation = function () {
|
|
122
|
+
var rels = {};
|
|
123
|
+
if (this.parser.fileExists('/_rels/.rels')) {
|
|
124
|
+
rels = parseRelationships(this.parser.getXML('/_rels/.rels'), 'root');
|
|
125
|
+
}
|
|
126
|
+
var documentRels = {};
|
|
127
|
+
if (this.parser.fileExists('/word/_rels/document.xml.rels')) {
|
|
128
|
+
documentRels = parseRelationships(this.parser.getXML('/word/_rels/document.xml.rels'), 'word');
|
|
129
|
+
}
|
|
130
|
+
this.relationships = __assign(__assign({}, rels), documentRels);
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* 解析全局配置
|
|
134
|
+
*/
|
|
135
|
+
Word.prototype.initContentType = function () {
|
|
136
|
+
var contentType = this.parser.getXML('[Content_Types].xml');
|
|
137
|
+
this.conentTypes = parseContentType(contentType);
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* 解析 numbering
|
|
141
|
+
*/
|
|
142
|
+
Word.prototype.initNumbering = function () {
|
|
143
|
+
var e_3, _a;
|
|
144
|
+
try {
|
|
145
|
+
for (var _b = __values(this.conentTypes.overrides), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
146
|
+
var override = _c.value;
|
|
147
|
+
if (override.partName.startsWith('/word/numbering')) {
|
|
148
|
+
var numberingData = this.parser.getXML(override.partName);
|
|
149
|
+
this.numbering = Numbering.fromXML(this, numberingData);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
154
|
+
finally {
|
|
155
|
+
try {
|
|
156
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
157
|
+
}
|
|
158
|
+
finally { if (e_3) throw e_3.error; }
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* 根据 id 获取关系
|
|
163
|
+
*/
|
|
164
|
+
Word.prototype.getRelationship = function (id) {
|
|
165
|
+
if (id) {
|
|
166
|
+
return this.relationships[id];
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* 进行文本替换
|
|
172
|
+
*/
|
|
173
|
+
Word.prototype.replaceText = function (text) {
|
|
174
|
+
if (this.renderOptions.enableVar === false ||
|
|
175
|
+
!this.renderOptions.replaceText) {
|
|
176
|
+
return text;
|
|
177
|
+
}
|
|
178
|
+
return this.renderOptions.replaceText(text);
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* 加载图片
|
|
182
|
+
*/
|
|
183
|
+
Word.prototype.loadImage = function (relation) {
|
|
184
|
+
var path = relation.target;
|
|
185
|
+
if (relation.part === 'word') {
|
|
186
|
+
path = 'word/' + path;
|
|
187
|
+
}
|
|
188
|
+
var data = this.parser.getFileByType(path, 'blob');
|
|
189
|
+
if (data) {
|
|
190
|
+
if (this.renderOptions.imageDataURL) {
|
|
191
|
+
return blobToDataURL(data);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
return new Promise(function (resolve, reject) {
|
|
195
|
+
resolve(URL.createObjectURL(data));
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* 解析 html
|
|
203
|
+
*/
|
|
204
|
+
Word.prototype.getXML = function (filePath) {
|
|
205
|
+
return this.parser.getXML(filePath);
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* 获取 styleId 的显示名称,因为这里可以自定义,理论上会出现 css 不支持的语法
|
|
209
|
+
*/
|
|
210
|
+
Word.prototype.getStyleIdDisplayName = function (styleId) {
|
|
211
|
+
/**
|
|
212
|
+
* 简单判断一下,如果是合法的 css 名称,就直接返回
|
|
213
|
+
* 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".
|
|
214
|
+
*/
|
|
215
|
+
if (styleId.match(/^[a-zA-Z]+[a-zA-Z0-9\-\_]*$/)) {
|
|
216
|
+
return this.getClassPrefix() + '-' + styleId;
|
|
217
|
+
}
|
|
218
|
+
if (styleId in this.styleIdMap) {
|
|
219
|
+
return this.styleIdMap[styleId];
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
this.styleIdMap[styleId] = this.genClassName();
|
|
223
|
+
return this.styleIdMap[styleId];
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* 生成个新的唯一的 class 名称
|
|
228
|
+
*/
|
|
229
|
+
Word.prototype.genClassName = function () {
|
|
230
|
+
return 'docx-classname-' + this.styleIdNum++;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* 添加新样式,主要用于表格的单元格样式
|
|
234
|
+
*/
|
|
235
|
+
Word.prototype.appendStyle = function (style) {
|
|
236
|
+
var styleElement = document.createElement('style');
|
|
237
|
+
styleElement.innerHTML = style;
|
|
238
|
+
this.rootElement.appendChild(styleElement);
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* 返回样式表名及它的父级样式表名
|
|
242
|
+
* @param styleId 样式表里的 style 名称
|
|
243
|
+
* @returns 返回 className 数组
|
|
244
|
+
*/
|
|
245
|
+
Word.prototype.getStyleClassName = function (stylId) {
|
|
246
|
+
var style = this.styles.styleMap[stylId];
|
|
247
|
+
if (!style) {
|
|
248
|
+
return [];
|
|
249
|
+
}
|
|
250
|
+
var classNames = [this.getStyleIdDisplayName(stylId)];
|
|
251
|
+
if (style.basedOn) {
|
|
252
|
+
classNames.unshift(this.getStyleIdDisplayName(style.basedOn));
|
|
253
|
+
}
|
|
254
|
+
return classNames;
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* 渲染时的 css 类前缀
|
|
258
|
+
*/
|
|
259
|
+
Word.prototype.getClassPrefix = function () {
|
|
260
|
+
return "".concat(this.renderOptions.classPrefix, "-").concat(this.id);
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* 获取主题色,目前基于 css 变量实现,方便动态修改
|
|
264
|
+
*/
|
|
265
|
+
Word.prototype.getThemeColor = function (name) {
|
|
266
|
+
return "var(--docx-".concat(this.id, "-theme-").concat(name, "-color)");
|
|
267
|
+
};
|
|
268
|
+
Word.prototype.addClass = function (element, className) {
|
|
269
|
+
element.classList.add("".concat(this.getClassPrefix(), "-").concat(className));
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* 更新页面中的变量,这个要在 render 后运行
|
|
273
|
+
*/
|
|
274
|
+
Word.prototype.updateVariable = function () {
|
|
275
|
+
if (!this.rootElement ||
|
|
276
|
+
this.renderOptions.enableVar === false ||
|
|
277
|
+
!this.renderOptions.replaceText) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
updateVariableText(this);
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* 下载生成的文档,会对 word/document.xml 进行处理,替换文本
|
|
284
|
+
*/
|
|
285
|
+
Word.prototype.download = function (fileName) {
|
|
286
|
+
if (fileName === void 0) { fileName = 'document.docx'; }
|
|
287
|
+
var documentData = this.getXML('word/document.xml');
|
|
288
|
+
if (this.renderOptions.enableVar && this.renderOptions.replaceText) {
|
|
289
|
+
mergeRun(this, documentData);
|
|
290
|
+
// 对文本进行替换
|
|
291
|
+
var ts = documentData.getElementsByTagName('w:t');
|
|
292
|
+
for (var i = 0; i < ts.length; i++) {
|
|
293
|
+
ts[i].textContent = this.replaceText(ts[i].textContent || '');
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
var blob = this.parser.generateZip(buildXML(documentData));
|
|
297
|
+
downloadBlob(blob, fileName);
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* 打印功能
|
|
301
|
+
*/
|
|
302
|
+
Word.prototype.print = function () {
|
|
303
|
+
var _a, _b;
|
|
304
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
305
|
+
var iframe;
|
|
306
|
+
return __generator(this, function (_c) {
|
|
307
|
+
switch (_c.label) {
|
|
308
|
+
case 0:
|
|
309
|
+
iframe = document.createElement('iframe');
|
|
310
|
+
iframe.style.position = 'absolute';
|
|
311
|
+
iframe.style.top = '-10000px';
|
|
312
|
+
document.body.appendChild(iframe);
|
|
313
|
+
(_a = iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.write('<div id="print"></div>');
|
|
314
|
+
return [4 /*yield*/, this.render((_b = iframe.contentDocument) === null || _b === void 0 ? void 0 : _b.getElementById('print'))];
|
|
315
|
+
case 1:
|
|
316
|
+
_c.sent();
|
|
317
|
+
setTimeout(function () {
|
|
318
|
+
var _a, _b;
|
|
319
|
+
iframe.focus();
|
|
320
|
+
(_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.print();
|
|
321
|
+
(_b = iframe.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(iframe);
|
|
322
|
+
}, 100);
|
|
323
|
+
window.focus();
|
|
324
|
+
return [2 /*return*/];
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
/**
|
|
330
|
+
* 渲染文档入口
|
|
331
|
+
*
|
|
332
|
+
* @param root 渲染的根节点
|
|
333
|
+
*/
|
|
334
|
+
Word.prototype.render = function (root) {
|
|
335
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
336
|
+
var renderOptions, isDebug, documentData, document, documentElement;
|
|
337
|
+
return __generator(this, function (_a) {
|
|
338
|
+
this.init();
|
|
339
|
+
renderOptions = this.renderOptions;
|
|
340
|
+
isDebug = renderOptions.debug;
|
|
341
|
+
isDebug && console.log('init', this);
|
|
342
|
+
this.rootElement = root;
|
|
343
|
+
root.innerHTML = '';
|
|
344
|
+
documentData = this.getXML('word/document.xml');
|
|
345
|
+
isDebug && console.log('documentData', documentData);
|
|
346
|
+
if (renderOptions.enableVar && renderOptions.replaceText) {
|
|
347
|
+
mergeRun(this, documentData);
|
|
348
|
+
// 这里不进行变量替换,方便后续进行局部替换来更新变量
|
|
349
|
+
}
|
|
350
|
+
document = WDocument.fromXML(this, documentData);
|
|
351
|
+
isDebug && console.log('document', document);
|
|
352
|
+
documentElement = renderDocument(this, document);
|
|
353
|
+
root.classList.add(this.getClassPrefix());
|
|
354
|
+
if (renderOptions.inWrap) {
|
|
355
|
+
root.classList.add(this.wrapClassName);
|
|
356
|
+
}
|
|
357
|
+
appendChild(root, renderStyle(this));
|
|
358
|
+
appendChild(root, documentElement);
|
|
359
|
+
return [2 /*return*/];
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
/**
|
|
364
|
+
* 全局 id,用于一个页面渲染多个 word 文档
|
|
365
|
+
*/
|
|
366
|
+
Word.globalId = 0;
|
|
367
|
+
return Word;
|
|
368
|
+
}());
|
|
369
|
+
|
|
370
|
+
export { Word as default };
|
package/esm/index.d.ts
ADDED