office-viewer 0.1.3 → 0.1.4
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 +1 -1
- package/esm/Word.js +16 -10
- package/esm/openxml/drawing/Drawing.d.ts +2 -0
- package/esm/openxml/drawing/Drawing.js +4 -0
- package/esm/openxml/drawing/Transform.d.ts +2 -0
- package/esm/openxml/drawing/Transform.js +41 -12
- package/esm/openxml/word/Tab.d.ts +1 -1
- package/esm/openxml/word/wps/WPG.d.ts +10 -1
- package/esm/openxml/word/wps/WPG.js +56 -0
- package/esm/openxml/word/wps/WPS.js +1 -0
- package/esm/render/renderDrawing.js +183 -71
- package/esm/render/renderParagraph.js +49 -6
- package/esm/render/renderRun.js +7 -2
- package/esm/render/renderStyle.js +1 -1
- package/esm/render/renderTab.d.ts +1 -1
- package/esm/render/renderTab.js +7 -1
- package/esm/util/get.d.ts +4 -0
- package/esm/util/get.js +18 -0
- package/esm/util/replaceVar.js +2 -1
- package/lib/Word.js +16 -10
- package/lib/openxml/drawing/Drawing.d.ts +2 -0
- package/lib/openxml/drawing/Drawing.js +4 -0
- package/lib/openxml/drawing/Transform.d.ts +2 -0
- package/lib/openxml/drawing/Transform.js +41 -12
- package/lib/openxml/word/Tab.d.ts +1 -1
- package/lib/openxml/word/wps/WPG.d.ts +10 -1
- package/lib/openxml/word/wps/WPG.js +60 -0
- package/lib/openxml/word/wps/WPS.js +1 -0
- package/lib/render/renderDrawing.js +183 -71
- package/lib/render/renderParagraph.js +49 -6
- package/lib/render/renderRun.js +7 -2
- package/lib/render/renderStyle.js +1 -1
- package/lib/render/renderTab.d.ts +1 -1
- package/lib/render/renderTab.js +7 -1
- package/lib/util/get.d.ts +4 -0
- package/lib/util/get.js +22 -0
- package/lib/util/replaceVar.js +2 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ docx 渲染器,原理是将 docx 里的 xml 格式转成 html
|
|
|
20
20
|
- [官方规范](https://www.ecma-international.org/publications-and-standards/standards/ecma-376/)
|
|
21
21
|
- [标签的在线文档](http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/index.html)
|
|
22
22
|
|
|
23
|
-
日常开发可以使用 [OOXML viewer](https://marketplace.visualstudio.com/items?itemName=yuenm18.
|
|
23
|
+
日常开发可以使用 [OOXML viewer](https://marketplace.visualstudio.com/items?itemName=yuenm18.ooxml-viewer) 插件查看
|
|
24
24
|
|
|
25
25
|
开发过程啊参考了
|
|
26
26
|
|
package/esm/Word.js
CHANGED
|
@@ -22,6 +22,7 @@ import { parseEndnotes } from './parse/parseEndnotes.js';
|
|
|
22
22
|
import { renderNotes } from './render/renderNotes.js';
|
|
23
23
|
import { printIframe } from './util/print.js';
|
|
24
24
|
import { Settings } from './openxml/Settings.js';
|
|
25
|
+
import { get } from './util/get.js';
|
|
25
26
|
|
|
26
27
|
var defaultRenderOptions = {
|
|
27
28
|
classPrefix: 'docx-viewer',
|
|
@@ -43,8 +44,8 @@ var defaultRenderOptions = {
|
|
|
43
44
|
renderHeader: true,
|
|
44
45
|
renderFooter: true,
|
|
45
46
|
data: {},
|
|
46
|
-
evalVar: function (
|
|
47
|
-
return
|
|
47
|
+
evalVar: function (path, data) {
|
|
48
|
+
return get(data, path);
|
|
48
49
|
}
|
|
49
50
|
};
|
|
50
51
|
var Word = /** @class */ (function () {
|
|
@@ -293,17 +294,19 @@ var Word = /** @class */ (function () {
|
|
|
293
294
|
* 进行单个文本替换
|
|
294
295
|
*/
|
|
295
296
|
Word.prototype.replaceText = function (text) {
|
|
297
|
+
var _this = this;
|
|
296
298
|
if (this.renderOptions.enableVar === false) {
|
|
297
299
|
return text;
|
|
298
300
|
}
|
|
299
301
|
var data = this.renderOptions.data;
|
|
300
302
|
if (text.indexOf('{{') !== -1) {
|
|
301
|
-
text = text.replace(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
303
|
+
text = text.replace(/{{([^}]+)}}/g, function (all, group) {
|
|
304
|
+
var result = _this.renderOptions.evalVar(group, data);
|
|
305
|
+
if (typeof result === 'undefined') {
|
|
306
|
+
return '';
|
|
307
|
+
}
|
|
308
|
+
return String(result);
|
|
309
|
+
});
|
|
307
310
|
}
|
|
308
311
|
return text;
|
|
309
312
|
};
|
|
@@ -416,18 +419,21 @@ var Word = /** @class */ (function () {
|
|
|
416
419
|
* 获取主题色
|
|
417
420
|
*/
|
|
418
421
|
Word.prototype.getThemeColor = function (name) {
|
|
419
|
-
var _a, _b
|
|
422
|
+
var _a, _b;
|
|
420
423
|
if (this.settings.clrSchemeMapping) {
|
|
421
424
|
name = this.settings.clrSchemeMapping[name] || name;
|
|
422
425
|
}
|
|
423
426
|
if (this.themes && this.themes.length > 0) {
|
|
424
427
|
var theme = this.themes[0];
|
|
425
|
-
var
|
|
428
|
+
var colors = (_b = (_a = theme.themeElements) === null || _a === void 0 ? void 0 : _a.clrScheme) === null || _b === void 0 ? void 0 : _b.colors;
|
|
429
|
+
var color = colors === null || colors === void 0 ? void 0 : colors[name];
|
|
426
430
|
if (color) {
|
|
427
431
|
return color;
|
|
428
432
|
}
|
|
429
433
|
else {
|
|
434
|
+
// 找不到可能是从其它地方拷贝过来的,这时取 accent1
|
|
430
435
|
console.warn('unknown theme color: ' + name);
|
|
436
|
+
return (colors === null || colors === void 0 ? void 0 : colors['accent1']) || '';
|
|
431
437
|
}
|
|
432
438
|
}
|
|
433
439
|
return '';
|
|
@@ -5,6 +5,7 @@ import { CSSStyle } from './../Style';
|
|
|
5
5
|
import Word from '../../Word';
|
|
6
6
|
import { Pic } from './Pic';
|
|
7
7
|
import { WPS } from '../word/wps/WPS';
|
|
8
|
+
import { WPG } from '../word/wps/WPG';
|
|
8
9
|
/**
|
|
9
10
|
* drawing 在文档中的位置,目前有两种情况,child 和 anchor
|
|
10
11
|
*/
|
|
@@ -24,6 +25,7 @@ export interface Anchor {
|
|
|
24
25
|
export declare class Drawing {
|
|
25
26
|
pic?: Pic;
|
|
26
27
|
wps?: WPS;
|
|
28
|
+
wpg?: WPG;
|
|
27
29
|
diagram?: ConstrainDOMStringParameters;
|
|
28
30
|
position: Position;
|
|
29
31
|
anchor?: Anchor;
|
|
@@ -4,6 +4,7 @@ import { getAttrNumber, getAttrBoolean } from '../../OpenXML.js';
|
|
|
4
4
|
import { Pic } from './Pic.js';
|
|
5
5
|
import { WPS } from '../word/wps/WPS.js';
|
|
6
6
|
import { Diagram } from './diagram/Diagram.js';
|
|
7
|
+
import { WPG } from '../word/wps/WPG.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* 目前图片和 textbox 都会依赖这个
|
|
@@ -123,6 +124,9 @@ var Drawing = /** @class */ (function () {
|
|
|
123
124
|
case 'wps:wsp':
|
|
124
125
|
drawing.wps = WPS.fromXML(word, graphicDataChild);
|
|
125
126
|
break;
|
|
127
|
+
case 'wpg:wgp':
|
|
128
|
+
drawing.wpg = WPG.fromXML(word, graphicDataChild);
|
|
129
|
+
break;
|
|
126
130
|
case 'dgm:relIds':
|
|
127
131
|
// 这个是 diagram 的关系
|
|
128
132
|
// http://webapp.docx4java.org/OnlineDemo/ecma376/DrawingML/relIds.html
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __values } from 'tslib';
|
|
1
2
|
import { parseSize, LengthUsage, convertAngle } from '../../parse/parseSize.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -7,20 +8,48 @@ var Transform = /** @class */ (function () {
|
|
|
7
8
|
function Transform() {
|
|
8
9
|
}
|
|
9
10
|
Transform.fromXML = function (word, element) {
|
|
11
|
+
var e_1, _a;
|
|
10
12
|
var transform = new Transform();
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
try {
|
|
14
|
+
for (var _b = __values(element.children), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
15
|
+
var child = _c.value;
|
|
16
|
+
var tagName = child.tagName;
|
|
17
|
+
switch (tagName) {
|
|
18
|
+
case 'a:off':
|
|
19
|
+
transform.off = {
|
|
20
|
+
x: parseSize(child, 'x', LengthUsage.Emu),
|
|
21
|
+
y: parseSize(child, 'y', LengthUsage.Emu)
|
|
22
|
+
};
|
|
23
|
+
break;
|
|
24
|
+
case 'a:ext':
|
|
25
|
+
transform.ext = {
|
|
26
|
+
cx: parseSize(child, 'cx', LengthUsage.Emu),
|
|
27
|
+
cy: parseSize(child, 'cy', LengthUsage.Emu)
|
|
28
|
+
};
|
|
29
|
+
break;
|
|
30
|
+
case 'a:chOff':
|
|
31
|
+
transform.chOff = {
|
|
32
|
+
x: parseSize(child, 'x', LengthUsage.Emu),
|
|
33
|
+
y: parseSize(child, 'y', LengthUsage.Emu)
|
|
34
|
+
};
|
|
35
|
+
break;
|
|
36
|
+
case 'a:chExt':
|
|
37
|
+
transform.chExt = {
|
|
38
|
+
cx: parseSize(child, 'cx', LengthUsage.Emu),
|
|
39
|
+
cy: parseSize(child, 'cy', LengthUsage.Emu)
|
|
40
|
+
};
|
|
41
|
+
break;
|
|
42
|
+
default:
|
|
43
|
+
console.warn('Transform: Unknown tag ', tagName, child);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
17
46
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
47
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
48
|
+
finally {
|
|
49
|
+
try {
|
|
50
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
51
|
+
}
|
|
52
|
+
finally { if (e_1) throw e_1.error; }
|
|
24
53
|
}
|
|
25
54
|
var rot = element.getAttribute('rot');
|
|
26
55
|
if (rot) {
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 图形组,目前是当成 wps 用了
|
|
3
3
|
*/
|
|
4
|
+
import { WPS } from './WPS';
|
|
5
|
+
import Word from '../../../Word';
|
|
6
|
+
import { ShapePr } from '../../drawing/ShapeProperties';
|
|
7
|
+
import { Pic } from '../../drawing/Pic';
|
|
4
8
|
export declare class WPG {
|
|
9
|
+
wps: WPS[];
|
|
10
|
+
wpg: WPG[];
|
|
11
|
+
spPr?: ShapePr;
|
|
12
|
+
pic?: Pic;
|
|
13
|
+
static fromXML(word: Word, element: Element): WPG;
|
|
5
14
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { __values } from 'tslib';
|
|
2
|
+
import { WPS } from './WPS.js';
|
|
3
|
+
import { ShapePr } from '../../drawing/ShapeProperties.js';
|
|
4
|
+
import { Pic } from '../../drawing/Pic.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 图形组,目前是当成 wps 用了
|
|
8
|
+
*/
|
|
9
|
+
var WPG = /** @class */ (function () {
|
|
10
|
+
function WPG() {
|
|
11
|
+
}
|
|
12
|
+
WPG.fromXML = function (word, element) {
|
|
13
|
+
var e_1, _a;
|
|
14
|
+
var wpg = new WPG();
|
|
15
|
+
var wps = [];
|
|
16
|
+
wpg.wps = wps;
|
|
17
|
+
wpg.wpg = [];
|
|
18
|
+
try {
|
|
19
|
+
for (var _b = __values(element.children), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
20
|
+
var child = _c.value;
|
|
21
|
+
var tagName = child.tagName;
|
|
22
|
+
switch (tagName) {
|
|
23
|
+
case 'wpg:cNvGrpSpPr':
|
|
24
|
+
// 和展现无关
|
|
25
|
+
break;
|
|
26
|
+
case 'wpg:grpSpPr':
|
|
27
|
+
wpg.spPr = ShapePr.fromXML(word, child);
|
|
28
|
+
break;
|
|
29
|
+
case 'wps:wsp':
|
|
30
|
+
wps.push(WPS.fromXML(word, child));
|
|
31
|
+
break;
|
|
32
|
+
case 'pic:pic':
|
|
33
|
+
wpg.pic = Pic.fromXML(word, child);
|
|
34
|
+
break;
|
|
35
|
+
case 'wpg:grpSp':
|
|
36
|
+
// 组合中的组合
|
|
37
|
+
wpg.wpg.push(WPG.fromXML(word, child));
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
console.warn('WPS: Unknown tag ', tagName, child);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
45
|
+
finally {
|
|
46
|
+
try {
|
|
47
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
48
|
+
}
|
|
49
|
+
finally { if (e_1) throw e_1.error; }
|
|
50
|
+
}
|
|
51
|
+
return wpg;
|
|
52
|
+
};
|
|
53
|
+
return WPG;
|
|
54
|
+
}());
|
|
55
|
+
|
|
56
|
+
export { WPG };
|
|
@@ -10,8 +10,9 @@ import { renderCustGeom } from './renderCustGeom.js';
|
|
|
10
10
|
/**
|
|
11
11
|
* 渲染图片
|
|
12
12
|
*/
|
|
13
|
-
function renderPic(pic, word,
|
|
14
|
-
var _a, _b;
|
|
13
|
+
function renderPic(pic, word, wpg) {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
if (wpg === void 0) { wpg = null; }
|
|
15
16
|
var blip = (_a = pic.blipFill) === null || _a === void 0 ? void 0 : _a.blip;
|
|
16
17
|
if (blip && blip.src) {
|
|
17
18
|
var img = document.createElement('img');
|
|
@@ -31,15 +32,27 @@ function renderPic(pic, word, drawing) {
|
|
|
31
32
|
}
|
|
32
33
|
var xfrm = (_b = pic.spPr) === null || _b === void 0 ? void 0 : _b.xfrm;
|
|
33
34
|
if (xfrm) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
if (wpg) {
|
|
36
|
+
var rect = getRectInGroup(xfrm, (_c = wpg.spPr) === null || _c === void 0 ? void 0 : _c.xfrm);
|
|
37
|
+
if (rect) {
|
|
38
|
+
img.style.position = 'absolute';
|
|
39
|
+
img.style.left = rect.left + 'px';
|
|
40
|
+
img.style.top = rect.top + 'px';
|
|
41
|
+
img.style.width = rect.width + 'px';
|
|
42
|
+
img.style.height = rect.height + 'px';
|
|
43
|
+
}
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
else {
|
|
46
|
+
var off = xfrm.off;
|
|
47
|
+
if (off) {
|
|
48
|
+
img.style.left = off.x;
|
|
49
|
+
img.style.top = off.y;
|
|
50
|
+
}
|
|
51
|
+
var ext = xfrm.ext;
|
|
52
|
+
if (ext) {
|
|
53
|
+
img.style.width = ext.cx;
|
|
54
|
+
img.style.height = ext.cy;
|
|
55
|
+
}
|
|
43
56
|
}
|
|
44
57
|
if (xfrm.rot) {
|
|
45
58
|
img.style.transform = "rotate(".concat(xfrm.rot, "deg)");
|
|
@@ -49,6 +62,162 @@ function renderPic(pic, word, drawing) {
|
|
|
49
62
|
}
|
|
50
63
|
return null;
|
|
51
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* 获取在 group 中的位置
|
|
67
|
+
* @param xfrm 元素的位置定义
|
|
68
|
+
* @param groupXfrm 分组中的位置定义
|
|
69
|
+
*/
|
|
70
|
+
function getRectInGroup(xfrm, groupXfrm) {
|
|
71
|
+
var off = xfrm.off;
|
|
72
|
+
var width = parseFloat(xfrm.ext.cx.replace('px', ''));
|
|
73
|
+
var height = parseFloat(xfrm.ext.cy.replace('px', ''));
|
|
74
|
+
if (off && groupXfrm.chOff && groupXfrm.ext && groupXfrm.chExt) {
|
|
75
|
+
// 先算缩放比
|
|
76
|
+
var scaleX = parseFloat(groupXfrm.ext.cx.replace('px', '')) /
|
|
77
|
+
parseFloat(groupXfrm.chExt.cx.replace('px', ''));
|
|
78
|
+
var scaleY = parseFloat(groupXfrm.ext.cy.replace('px', '')) /
|
|
79
|
+
parseFloat(groupXfrm.chExt.cy.replace('px', ''));
|
|
80
|
+
var groupOffX = parseFloat(groupXfrm.chOff.x.replace('px', ''));
|
|
81
|
+
var groupOffY = parseFloat(groupXfrm.chOff.y.replace('px', ''));
|
|
82
|
+
var x = parseFloat(off.x.replace('px', ''));
|
|
83
|
+
var y = parseFloat(off.y.replace('px', ''));
|
|
84
|
+
return {
|
|
85
|
+
left: scaleX * (x - groupOffX),
|
|
86
|
+
top: scaleY * (y - groupOffY),
|
|
87
|
+
width: scaleX * width,
|
|
88
|
+
height: scaleY * height
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 渲染文本框
|
|
95
|
+
*/
|
|
96
|
+
function renderWps(word, container, wps, wpg) {
|
|
97
|
+
var e_1, _a;
|
|
98
|
+
var _b;
|
|
99
|
+
if (wpg === void 0) { wpg = null; }
|
|
100
|
+
var wpsStyle = wps.wpsStyle;
|
|
101
|
+
var spPr = wps.spPr;
|
|
102
|
+
applyStyle(container, wps.style);
|
|
103
|
+
if (wpsStyle === null || wpsStyle === void 0 ? void 0 : wpsStyle.fontColor) {
|
|
104
|
+
container.style.color = wpsStyle.fontColor;
|
|
105
|
+
}
|
|
106
|
+
if (spPr === null || spPr === void 0 ? void 0 : spPr.xfrm) {
|
|
107
|
+
var ext = spPr.xfrm.ext;
|
|
108
|
+
if (ext) {
|
|
109
|
+
var width = parseFloat(ext.cx.replace('px', ''));
|
|
110
|
+
var height = parseFloat(ext.cy.replace('px', ''));
|
|
111
|
+
// 在分组中的计算方式不一样,另外在分组中还需要绝对定位
|
|
112
|
+
// 这个计算方法在官方文档里没找到
|
|
113
|
+
if (wpg) {
|
|
114
|
+
container.style.position = 'absolute';
|
|
115
|
+
var rect = getRectInGroup(spPr.xfrm, (_b = wpg.spPr) === null || _b === void 0 ? void 0 : _b.xfrm);
|
|
116
|
+
if (rect) {
|
|
117
|
+
container.style.left = rect.left + 'px';
|
|
118
|
+
container.style.top = rect.top + 'px';
|
|
119
|
+
width = rect.width;
|
|
120
|
+
height = rect.height;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
container.style.width = width + 'px';
|
|
124
|
+
container.style.height = height + 'px';
|
|
125
|
+
if (spPr.geom) {
|
|
126
|
+
appendChild(container, renderGeom(spPr.geom, spPr, width, height, wps.wpsStyle));
|
|
127
|
+
}
|
|
128
|
+
if (spPr.custGeom) {
|
|
129
|
+
appendChild(container, renderCustGeom(spPr.custGeom, spPr, width, height, wps.wpsStyle));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (spPr.xfrm.rot) {
|
|
133
|
+
container.style.transform = "rotate(".concat(spPr.xfrm.rot, "deg)");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
var txbxContent = wps.txbxContent;
|
|
137
|
+
if (txbxContent.length) {
|
|
138
|
+
// 为了实现垂直居中,将父容器改成 table 布局
|
|
139
|
+
var textContainer = document.createElement('div');
|
|
140
|
+
textContainer.dataset.name = 'textContainer';
|
|
141
|
+
container.style.display = 'table';
|
|
142
|
+
textContainer.style.display = 'table-cell';
|
|
143
|
+
textContainer.style.verticalAlign = 'middle';
|
|
144
|
+
if (wps.style && wps.style['vertical-align']) {
|
|
145
|
+
textContainer.style.verticalAlign = wps.style['vertical-align'];
|
|
146
|
+
// 容器的 vertical-align 需要去掉,虽然也不影响
|
|
147
|
+
container.style.verticalAlign = '';
|
|
148
|
+
}
|
|
149
|
+
appendChild(container, textContainer);
|
|
150
|
+
try {
|
|
151
|
+
for (var txbxContent_1 = __values(txbxContent), txbxContent_1_1 = txbxContent_1.next(); !txbxContent_1_1.done; txbxContent_1_1 = txbxContent_1.next()) {
|
|
152
|
+
var txbxContentChild = txbxContent_1_1.value;
|
|
153
|
+
if (txbxContentChild instanceof Paragraph) {
|
|
154
|
+
appendChild(textContainer, renderParagraph(word, txbxContentChild));
|
|
155
|
+
}
|
|
156
|
+
else if (txbxContentChild instanceof Table) {
|
|
157
|
+
appendChild(textContainer, renderTable(word, txbxContentChild));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
162
|
+
finally {
|
|
163
|
+
try {
|
|
164
|
+
if (txbxContent_1_1 && !txbxContent_1_1.done && (_a = txbxContent_1.return)) _a.call(txbxContent_1);
|
|
165
|
+
}
|
|
166
|
+
finally { if (e_1) throw e_1.error; }
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function renderWpg(word, wpg) {
|
|
171
|
+
var e_2, _a, e_3, _b;
|
|
172
|
+
var _c, _d;
|
|
173
|
+
var container = document.createElement('div');
|
|
174
|
+
var wpgContainer = document.createElement('div');
|
|
175
|
+
var spPr = wpg.spPr;
|
|
176
|
+
if (spPr === null || spPr === void 0 ? void 0 : spPr.xfrm) {
|
|
177
|
+
var ext = (_c = spPr === null || spPr === void 0 ? void 0 : spPr.xfrm) === null || _c === void 0 ? void 0 : _c.ext;
|
|
178
|
+
if (ext) {
|
|
179
|
+
wpgContainer.style.width = ext.cx;
|
|
180
|
+
wpgContainer.style.height = ext.cy;
|
|
181
|
+
}
|
|
182
|
+
var rot = (_d = spPr === null || spPr === void 0 ? void 0 : spPr.xfrm) === null || _d === void 0 ? void 0 : _d.rot;
|
|
183
|
+
if (rot) {
|
|
184
|
+
wpgContainer.style.transform = "rotate(".concat(rot, "deg)");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
for (var _e = __values(wpg.wps), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
189
|
+
var wps = _f.value;
|
|
190
|
+
var wpsContainer = document.createElement('div');
|
|
191
|
+
renderWps(word, wpsContainer, wps, wpg);
|
|
192
|
+
appendChild(wpgContainer, wpsContainer);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
196
|
+
finally {
|
|
197
|
+
try {
|
|
198
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
199
|
+
}
|
|
200
|
+
finally { if (e_2) throw e_2.error; }
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
for (var _g = __values(wpg.wpg), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
204
|
+
var childWpg = _h.value;
|
|
205
|
+
appendChild(container, renderWpg(word, childWpg));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
209
|
+
finally {
|
|
210
|
+
try {
|
|
211
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
212
|
+
}
|
|
213
|
+
finally { if (e_3) throw e_3.error; }
|
|
214
|
+
}
|
|
215
|
+
if (wpg.pic) {
|
|
216
|
+
appendChild(wpgContainer, renderPic(wpg.pic, word, wpg));
|
|
217
|
+
}
|
|
218
|
+
appendChild(container, wpgContainer);
|
|
219
|
+
return container;
|
|
220
|
+
}
|
|
52
221
|
/**
|
|
53
222
|
* 渲染图片,目前只支持 picture
|
|
54
223
|
* http://officeopenxml.com/drwOverview.php
|
|
@@ -56,7 +225,6 @@ function renderPic(pic, word, drawing) {
|
|
|
56
225
|
*
|
|
57
226
|
*/
|
|
58
227
|
function renderDrawing(word, drawing, inHeader) {
|
|
59
|
-
var e_1, _a;
|
|
60
228
|
var container = document.createElement('div');
|
|
61
229
|
if (drawing.position === 'inline') {
|
|
62
230
|
container.style.display = 'inline-block';
|
|
@@ -72,66 +240,10 @@ function renderDrawing(word, drawing, inHeader) {
|
|
|
72
240
|
container.dataset.id = drawing.id || '';
|
|
73
241
|
container.dataset.name = drawing.name || '';
|
|
74
242
|
if (drawing.wps) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (wpsStyle === null || wpsStyle === void 0 ? void 0 : wpsStyle.fontColor) {
|
|
80
|
-
container.style.color = wpsStyle.fontColor;
|
|
81
|
-
}
|
|
82
|
-
if (spPr === null || spPr === void 0 ? void 0 : spPr.xfrm) {
|
|
83
|
-
var ext = spPr.xfrm.ext;
|
|
84
|
-
if (ext) {
|
|
85
|
-
container.style.width = ext.cx;
|
|
86
|
-
container.style.height = ext.cy;
|
|
87
|
-
if (spPr.geom) {
|
|
88
|
-
var width = parseFloat(ext.cx.replace('px', ''));
|
|
89
|
-
var height = parseFloat(ext.cy.replace('px', ''));
|
|
90
|
-
appendChild(container, renderGeom(spPr.geom, spPr, width, height, wps.wpsStyle));
|
|
91
|
-
}
|
|
92
|
-
if (spPr.custGeom) {
|
|
93
|
-
var width = parseFloat(ext.cx.replace('px', ''));
|
|
94
|
-
var height = parseFloat(ext.cy.replace('px', ''));
|
|
95
|
-
appendChild(container, renderCustGeom(spPr.custGeom, spPr, width, height, wps.wpsStyle));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (spPr.xfrm.rot) {
|
|
99
|
-
container.style.transform = "rotate(".concat(spPr.xfrm.rot, "deg)");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
var txbxContent = wps.txbxContent;
|
|
103
|
-
if (txbxContent.length) {
|
|
104
|
-
// 为了实现垂直居中,将父容器改成 table 布局
|
|
105
|
-
var textContainer = document.createElement('div');
|
|
106
|
-
textContainer.dataset.name = 'textContainer';
|
|
107
|
-
container.style.display = 'table';
|
|
108
|
-
textContainer.style.display = 'table-cell';
|
|
109
|
-
textContainer.style.verticalAlign = 'middle';
|
|
110
|
-
if (wps.style && wps.style['vertical-align']) {
|
|
111
|
-
textContainer.style.verticalAlign = wps.style['vertical-align'];
|
|
112
|
-
// 容器的 vertical-align 需要去掉,虽然也不影响
|
|
113
|
-
container.style.verticalAlign = '';
|
|
114
|
-
}
|
|
115
|
-
appendChild(container, textContainer);
|
|
116
|
-
try {
|
|
117
|
-
for (var txbxContent_1 = __values(txbxContent), txbxContent_1_1 = txbxContent_1.next(); !txbxContent_1_1.done; txbxContent_1_1 = txbxContent_1.next()) {
|
|
118
|
-
var txbxContentChild = txbxContent_1_1.value;
|
|
119
|
-
if (txbxContentChild instanceof Paragraph) {
|
|
120
|
-
appendChild(textContainer, renderParagraph(word, txbxContentChild));
|
|
121
|
-
}
|
|
122
|
-
else if (txbxContentChild instanceof Table) {
|
|
123
|
-
appendChild(textContainer, renderTable(word, txbxContentChild));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
128
|
-
finally {
|
|
129
|
-
try {
|
|
130
|
-
if (txbxContent_1_1 && !txbxContent_1_1.done && (_a = txbxContent_1.return)) _a.call(txbxContent_1);
|
|
131
|
-
}
|
|
132
|
-
finally { if (e_1) throw e_1.error; }
|
|
133
|
-
}
|
|
134
|
-
}
|
|
243
|
+
renderWps(word, container, drawing.wps);
|
|
244
|
+
}
|
|
245
|
+
if (drawing.wpg) {
|
|
246
|
+
appendChild(container, renderWpg(word, drawing.wpg));
|
|
135
247
|
}
|
|
136
248
|
// 如果没内容就不渲染了,避免高度导致撑开父节点
|
|
137
249
|
if (container.children.length === 0) {
|
|
@@ -8,15 +8,17 @@ import { renderHyperLink } from './renderHyperLink.js';
|
|
|
8
8
|
import { renderBookmarkStart } from './renderBookmark.js';
|
|
9
9
|
import { renderNumbering } from './renderNumbering.js';
|
|
10
10
|
import { setElementStyle } from './setElementStyle.js';
|
|
11
|
+
import { renderTab } from './renderTab.js';
|
|
11
12
|
import { OMath } from '../openxml/math/OMath.js';
|
|
12
13
|
import { renderOMath } from './renderMath.js';
|
|
14
|
+
import { Tab } from '../openxml/word/Tab.js';
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* 渲染段落
|
|
16
18
|
* @param renderEmptySpace 如果是 true 的话,当内容为空时会自动加上
|
|
17
19
|
*/
|
|
18
20
|
function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
19
|
-
var e_1, _a;
|
|
21
|
+
var e_1, _a, e_2, _b, e_3, _c;
|
|
20
22
|
if (renderEmptySpace === void 0) { renderEmptySpace = true; }
|
|
21
23
|
if (inHeader === void 0) { inHeader = false; }
|
|
22
24
|
word.currentParagraph = paragraph;
|
|
@@ -31,9 +33,50 @@ function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
|
31
33
|
appendChild(p, renderNumbering(p, word, properties.numPr));
|
|
32
34
|
}
|
|
33
35
|
var inFldChar = false;
|
|
36
|
+
if (properties.tabs && properties.tabs.length) {
|
|
37
|
+
// 目前只支持渲染第一个,因为第二个位置取决于前面内容位置,挺麻烦
|
|
38
|
+
// 虽然目前这个实现很 hack,但可以支持常见情况
|
|
39
|
+
appendChild(p, renderTab(word, properties.tabs[0], true));
|
|
40
|
+
// 同时删掉第一个 run 中的 tab
|
|
41
|
+
var done = false;
|
|
42
|
+
try {
|
|
43
|
+
for (var _d = __values(paragraph.children), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
44
|
+
var child = _e.value;
|
|
45
|
+
if (done) {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
if (child instanceof Run) {
|
|
49
|
+
try {
|
|
50
|
+
for (var _f = (e_2 = void 0, __values(child.children)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
51
|
+
var runChild = _g.value;
|
|
52
|
+
if (runChild instanceof Tab) {
|
|
53
|
+
child.children.splice(child.children.indexOf(runChild), 1);
|
|
54
|
+
done = true;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
60
|
+
finally {
|
|
61
|
+
try {
|
|
62
|
+
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
63
|
+
}
|
|
64
|
+
finally { if (e_2) throw e_2.error; }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
70
|
+
finally {
|
|
71
|
+
try {
|
|
72
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
73
|
+
}
|
|
74
|
+
finally { if (e_1) throw e_1.error; }
|
|
75
|
+
}
|
|
76
|
+
}
|
|
34
77
|
try {
|
|
35
|
-
for (var
|
|
36
|
-
var child =
|
|
78
|
+
for (var _h = __values(paragraph.children), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
79
|
+
var child = _j.value;
|
|
37
80
|
if (child instanceof Run) {
|
|
38
81
|
if (child.fldChar === 'begin') {
|
|
39
82
|
inFldChar = true;
|
|
@@ -58,12 +101,12 @@ function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
|
58
101
|
}
|
|
59
102
|
}
|
|
60
103
|
}
|
|
61
|
-
catch (
|
|
104
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
62
105
|
finally {
|
|
63
106
|
try {
|
|
64
|
-
if (
|
|
107
|
+
if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
|
|
65
108
|
}
|
|
66
|
-
finally { if (
|
|
109
|
+
finally { if (e_3) throw e_3.error; }
|
|
67
110
|
}
|
|
68
111
|
// 空行自动加个空格,不然会没高度
|
|
69
112
|
if (p.innerHTML === '' && renderEmptySpace) {
|