office-viewer 0.1.5 → 0.2.1
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 +0 -2
- package/esm/Word.d.ts +9 -1
- package/esm/Word.js +93 -44
- package/esm/package/PackageParser.d.ts +4 -0
- package/esm/package/XMLPackageParser.d.ts +1 -0
- package/esm/package/ZipPackageParser.d.ts +4 -0
- package/esm/package/ZipPackageParser.js +9 -0
- package/esm/render/renderStyle.js +1 -1
- package/esm/render/setElementStyle.js +2 -2
- package/esm/util/fileType.d.ts +9 -0
- package/esm/util/fileType.js +66 -0
- package/esm/util/replaceVar.d.ts +7 -1
- package/esm/util/replaceVar.js +277 -114
- package/lib/Word.d.ts +9 -1
- package/lib/Word.js +92 -43
- package/lib/package/PackageParser.d.ts +4 -0
- package/lib/package/XMLPackageParser.d.ts +1 -0
- package/lib/package/ZipPackageParser.d.ts +4 -0
- package/lib/package/ZipPackageParser.js +9 -0
- package/lib/render/renderStyle.js +1 -1
- package/lib/render/setElementStyle.js +2 -2
- package/lib/util/fileType.d.ts +9 -0
- package/lib/util/fileType.js +70 -0
- package/lib/util/replaceVar.d.ts +7 -1
- package/lib/util/replaceVar.js +276 -113
- package/package.json +5 -2
|
@@ -13,6 +13,10 @@ export interface PackageParser {
|
|
|
13
13
|
* 根据类型读取文件
|
|
14
14
|
*/
|
|
15
15
|
getFileByType(filePath: string, type: 'string' | 'blob' | 'uint8array'): string | Blob | Uint8Array | null;
|
|
16
|
+
/**
|
|
17
|
+
* 写入文件,主要用于图片
|
|
18
|
+
*/
|
|
19
|
+
saveFile(filePath: string, content: Uint8Array | string): void;
|
|
16
20
|
/**
|
|
17
21
|
* 文件是否存在
|
|
18
22
|
*/
|
|
@@ -20,6 +20,7 @@ export default class XMLPackageParser implements PackageParser {
|
|
|
20
20
|
* 在 xml 下基本不用这个
|
|
21
21
|
*/
|
|
22
22
|
getFileByType(filePath: string, type: 'string' | 'blob'): string | Uint8Array | Blob | null;
|
|
23
|
+
saveFile(filePath: string, content: Uint8Array | string): void;
|
|
23
24
|
/**
|
|
24
25
|
* 判断文件是否存在
|
|
25
26
|
*/
|
|
@@ -18,6 +18,10 @@ export default class ZipPackageParser implements PackageParser {
|
|
|
18
18
|
* 根据类型读取文件
|
|
19
19
|
*/
|
|
20
20
|
getFileByType(filePath: string, type: 'string' | 'blob' | 'uint8array'): string | Uint8Array | Blob | null;
|
|
21
|
+
/**
|
|
22
|
+
* xml 下没这功能
|
|
23
|
+
*/
|
|
24
|
+
saveFile(filePath: string, content: Uint8Array | string): void;
|
|
21
25
|
/**
|
|
22
26
|
* 判断文件是否存在
|
|
23
27
|
*/
|
|
@@ -52,6 +52,15 @@ var ZipPackageParser = /** @class */ (function () {
|
|
|
52
52
|
console.warn('getFileByType', filePath, 'not found');
|
|
53
53
|
return null;
|
|
54
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* xml 下没这功能
|
|
57
|
+
*/
|
|
58
|
+
ZipPackageParser.prototype.saveFile = function (filePath, content) {
|
|
59
|
+
if (typeof content === 'string') {
|
|
60
|
+
content = fflate.strToU8(content);
|
|
61
|
+
}
|
|
62
|
+
this.zip[filePath] = content;
|
|
63
|
+
};
|
|
55
64
|
/**
|
|
56
65
|
* 判断文件是否存在
|
|
57
66
|
*/
|
|
@@ -25,7 +25,7 @@ function generateDefaultStyle(word) {
|
|
|
25
25
|
}
|
|
26
26
|
var hyphens = ((_a = word.settings) === null || _a === void 0 ? void 0 : _a.autoHyphenation) ? 'hyphens: auto;' : '';
|
|
27
27
|
var classPrefix = word.getClassPrefix();
|
|
28
|
-
return "\n\n\n /** docDefaults **/\n .".concat(classPrefix, " {\n --docx-theme-font-minorHAnsi: Calibri, Helvetica, Arial, 'Helvetica Neue';\n --docx-theme-font-minorEastAsia: 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', 'STHeiti',\n 'Microsoft YaHei';\n }\n\n .").concat(classPrefix, " p {\n margin: 0;\n padding: 0;\n line-height: 1.5;\n ").concat(hyphens, "\n }\n\n .").concat(classPrefix, " .justify
|
|
28
|
+
return "\n\n\n /** docDefaults **/\n .".concat(classPrefix, " {\n --docx-theme-font-minorHAnsi: Calibri, Helvetica, Arial, 'Helvetica Neue';\n --docx-theme-font-minorEastAsia: 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', 'STHeiti',\n 'Microsoft YaHei';\n }\n\n .").concat(classPrefix, " p {\n margin: 0;\n padding: 0;\n line-height: 1.5;\n ").concat(hyphens, "\n }\n\n .").concat(classPrefix, " .justify {\n text-align-last: left;\n }\n\n .").concat(classPrefix, " table {\n border-spacing: 0;\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-p {\n ").concat(defaultPStyle, "\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-r {\n overflow-wrap: break-word;\n ").concat(defaultRStyle, "\n }\n ");
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* 生成表格级别样式
|
|
@@ -13,8 +13,8 @@ function setElementStyle(word, element, properties) {
|
|
|
13
13
|
}
|
|
14
14
|
if (properties.cssStyle) {
|
|
15
15
|
dom.applyStyle(element, properties.cssStyle);
|
|
16
|
-
//
|
|
17
|
-
//
|
|
16
|
+
// 目前默认最后一行用左对齐
|
|
17
|
+
// 这里用 class 的主要原因是方便用户自己覆盖,比如可能有语言需要右对齐
|
|
18
18
|
if (properties.cssStyle['text-align'] === 'justify') {
|
|
19
19
|
dom.addClassName(element, 'justify');
|
|
20
20
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 自动识别文件类型,只支持少数几种,参考了 file-type 项目里的实现
|
|
9
|
+
*/
|
|
10
|
+
function check(buffer, headers, options) {
|
|
11
|
+
var e_1, _a;
|
|
12
|
+
if (options === void 0) { options = {}; }
|
|
13
|
+
var offset = options.offset || 0;
|
|
14
|
+
try {
|
|
15
|
+
for (var _b = tslib.__values(headers.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
16
|
+
var _d = tslib.__read(_c.value, 2), index = _d[0], header = _d[1];
|
|
17
|
+
// If a bitmask is set
|
|
18
|
+
if (options.mask) {
|
|
19
|
+
// If header doesn't equal `buf` with bits masked off
|
|
20
|
+
if (header !== (options.mask[index] & buffer[index + offset])) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else if (header !== buffer[index + offset]) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
30
|
+
finally {
|
|
31
|
+
try {
|
|
32
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
33
|
+
}
|
|
34
|
+
finally { if (e_1) throw e_1.error; }
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
function stringToBytes(string) {
|
|
39
|
+
return tslib.__spreadArray([], tslib.__read(string), false).map(function (character) { return character.charCodeAt(0); });
|
|
40
|
+
}
|
|
41
|
+
function checkString(buffer, string, options) {
|
|
42
|
+
if (options === void 0) { options = {}; }
|
|
43
|
+
return check(buffer, stringToBytes(string), options);
|
|
44
|
+
}
|
|
45
|
+
function fileTypeFromBuffer(buffer) {
|
|
46
|
+
if (check(buffer, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
|
|
47
|
+
return { ext: 'png', mime: 'image/png' };
|
|
48
|
+
}
|
|
49
|
+
if (check(buffer, [0xff, 0xd8, 0xff])) {
|
|
50
|
+
return { ext: 'jpg', mime: 'image/jpeg' };
|
|
51
|
+
}
|
|
52
|
+
if (check(buffer, [0x47, 0x49, 0x46])) {
|
|
53
|
+
return { ext: 'gif', mime: 'image/gif' };
|
|
54
|
+
}
|
|
55
|
+
if (check(buffer, [0x42, 0x4d])) {
|
|
56
|
+
return { ext: 'bmp', mime: 'image/bmp' };
|
|
57
|
+
}
|
|
58
|
+
if (check(buffer, [0xc5, 0xd0, 0xd3, 0xc6])) {
|
|
59
|
+
return { ext: 'eps', mime: 'application/eps' };
|
|
60
|
+
}
|
|
61
|
+
if (checkString(buffer, '8BPS')) {
|
|
62
|
+
return { ext: 'psd', mime: 'image/vnd.adobe.photoshop' };
|
|
63
|
+
}
|
|
64
|
+
if (checkString(buffer, '%PDF')) {
|
|
65
|
+
return { ext: 'pdf', mime: 'application/pdf' };
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
exports.fileTypeFromBuffer = fileTypeFromBuffer;
|
package/lib/util/replaceVar.d.ts
CHANGED
|
@@ -7,4 +7,10 @@ import Word from '../Word';
|
|
|
7
7
|
* 替换单个文本变量
|
|
8
8
|
*/
|
|
9
9
|
export declare function replaceT(word: Word, t: Element, data: any): void;
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* 变量替换主入口
|
|
12
|
+
* @param word
|
|
13
|
+
* @param documentData
|
|
14
|
+
* @param replaceImage 是否替换掉图片,只有下载时才替换,避免性能问题
|
|
15
|
+
*/
|
|
16
|
+
export declare function replaceVar(word: Word, documentData: Document, replaceImage?: boolean): Promise<void>;
|
package/lib/util/replaceVar.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var tslib = require('tslib');
|
|
6
|
+
var Pic = require('../openxml/drawing/Pic.js');
|
|
6
7
|
var createObject = require('./createObject.js');
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -34,115 +35,192 @@ function replaceText(word, text, data) {
|
|
|
34
35
|
}
|
|
35
36
|
return text;
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// 生成的新 id
|
|
39
|
+
var newRelId = 1;
|
|
40
|
+
/**
|
|
41
|
+
* 替换图片里的变量
|
|
42
|
+
* @param replaceImage 如果为 true,则会实际替换掉 zip 里的图片,但可能影响性能所以默认不开启,只有下载的时候才开启
|
|
43
|
+
*/
|
|
44
|
+
function replaceAlt(word, cNvPr, data, replaceImage) {
|
|
45
|
+
if (replaceImage === void 0) { replaceImage = false; }
|
|
46
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
47
|
+
var alt, imageURL, parentElement, blip, newId, imageResponse, imageData, pic, blip_1;
|
|
48
|
+
return tslib.__generator(this, function (_a) {
|
|
49
|
+
switch (_a.label) {
|
|
50
|
+
case 0:
|
|
51
|
+
if (cNvPr.getAttribute('downloaded')) {
|
|
52
|
+
// 已经替换过了
|
|
53
|
+
return [2 /*return*/];
|
|
54
|
+
}
|
|
55
|
+
alt = cNvPr.getAttribute('descr') || '';
|
|
56
|
+
imageURL = replaceText(word, alt, data);
|
|
57
|
+
cNvPr.setAttribute('descrVar', imageURL);
|
|
58
|
+
if (!(replaceImage && imageURL)) return [3 /*break*/, 4];
|
|
59
|
+
parentElement = cNvPr.parentElement.parentElement;
|
|
60
|
+
blip = parentElement.getElementsByTagName('a:blip').item(0);
|
|
61
|
+
if (!blip) return [3 /*break*/, 3];
|
|
62
|
+
newId = "rIdn".concat(newRelId);
|
|
63
|
+
blip.setAttribute('r:embed', newId);
|
|
64
|
+
return [4 /*yield*/, fetch(imageURL)];
|
|
65
|
+
case 1:
|
|
66
|
+
imageResponse = _a.sent();
|
|
67
|
+
return [4 /*yield*/, imageResponse.arrayBuffer()];
|
|
68
|
+
case 2:
|
|
69
|
+
imageData = _a.sent();
|
|
70
|
+
word.saveNewImage(newId, new Uint8Array(imageData));
|
|
71
|
+
cNvPr.setAttribute('downloaded', 'true');
|
|
72
|
+
newRelId++;
|
|
73
|
+
_a.label = 3;
|
|
74
|
+
case 3:
|
|
75
|
+
pic = Pic.Pic.fromXML(word, parentElement);
|
|
76
|
+
if (pic && pic.blipFill && pic.blipFill.blip) {
|
|
77
|
+
blip_1 = pic.blipFill.blip;
|
|
78
|
+
if (blip_1.embled) ;
|
|
79
|
+
}
|
|
80
|
+
_a.label = 4;
|
|
81
|
+
case 4: return [2 /*return*/];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
40
85
|
}
|
|
41
86
|
/**
|
|
42
87
|
* 替换表格行
|
|
43
88
|
*/
|
|
44
|
-
function replaceTableRow(word, tr) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
89
|
+
function replaceTableRow(word, tr, replaceImage) {
|
|
90
|
+
if (replaceImage === void 0) { replaceImage = false; }
|
|
91
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
92
|
+
var evalVar, data, table, tcs, hasLoop, loopArray, tcs_1, tcs_1_1, tc, ts, ts_1, ts_1_1, t, text, arrayNameMatch, arrayMatchResult, arrayName, array, loopArray_1, loopArray_1_1, item, newTr, ts, rowData, ts_2, ts_2_1, t, _a, _b, cNvPr, e_1_1, e_2_1;
|
|
93
|
+
var e_3, _c, e_4, _d, e_2, _e, e_5, _f, e_1, _g;
|
|
94
|
+
return tslib.__generator(this, function (_h) {
|
|
95
|
+
switch (_h.label) {
|
|
96
|
+
case 0:
|
|
97
|
+
evalVar = word.renderOptions.evalVar;
|
|
98
|
+
data = word.renderOptions.data;
|
|
99
|
+
table = tr.parentNode;
|
|
100
|
+
tcs = tr.getElementsByTagName('w:tc');
|
|
101
|
+
hasLoop = false;
|
|
102
|
+
loopArray = [];
|
|
103
|
+
try {
|
|
104
|
+
// 查找对应的循环
|
|
105
|
+
for (tcs_1 = tslib.__values(tcs), tcs_1_1 = tcs_1.next(); !tcs_1_1.done; tcs_1_1 = tcs_1.next()) {
|
|
106
|
+
tc = tcs_1_1.value;
|
|
107
|
+
ts = tc.getElementsByTagName('w:t');
|
|
108
|
+
try {
|
|
109
|
+
for (ts_1 = (e_4 = void 0, tslib.__values(ts)), ts_1_1 = ts_1.next(); !ts_1_1.done; ts_1_1 = ts_1.next()) {
|
|
110
|
+
t = ts_1_1.value;
|
|
111
|
+
text = t.textContent || '';
|
|
112
|
+
if (text.startsWith('{{#')) {
|
|
113
|
+
arrayNameMatch = /{{#([^\}]+)}}/;
|
|
114
|
+
arrayMatchResult = arrayNameMatch.exec(text);
|
|
115
|
+
if (arrayMatchResult && arrayMatchResult.length > 0) {
|
|
116
|
+
hasLoop = true;
|
|
117
|
+
arrayName = arrayMatchResult[1];
|
|
118
|
+
array = evalVar(arrayName, data);
|
|
119
|
+
if (Array.isArray(array)) {
|
|
120
|
+
loopArray = array;
|
|
121
|
+
}
|
|
122
|
+
// 去掉这个循环变量
|
|
123
|
+
t.textContent = t.textContent.replace("{{#".concat(arrayName, "}}"), '');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (text.indexOf('{{/}}') !== -1) {
|
|
127
|
+
// 去掉结束变量
|
|
128
|
+
t.textContent = t.textContent.replace('{{/}}', '');
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
133
|
+
finally {
|
|
134
|
+
try {
|
|
135
|
+
if (ts_1_1 && !ts_1_1.done && (_d = ts_1.return)) _d.call(ts_1);
|
|
136
|
+
}
|
|
137
|
+
finally { if (e_4) throw e_4.error; }
|
|
70
138
|
}
|
|
71
|
-
// 去掉这个循环变量
|
|
72
|
-
t.textContent = t.textContent.replace("{{#".concat(arrayName, "}}"), '');
|
|
73
139
|
}
|
|
74
140
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
82
|
-
finally {
|
|
83
|
-
try {
|
|
84
|
-
if (ts_1_1 && !ts_1_1.done && (_b = ts_1.return)) _b.call(ts_1);
|
|
85
|
-
}
|
|
86
|
-
finally { if (e_2) throw e_2.error; }
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
91
|
-
finally {
|
|
92
|
-
try {
|
|
93
|
-
if (tcs_1_1 && !tcs_1_1.done && (_a = tcs_1.return)) _a.call(tcs_1);
|
|
94
|
-
}
|
|
95
|
-
finally { if (e_1) throw e_1.error; }
|
|
96
|
-
}
|
|
97
|
-
if (hasLoop) {
|
|
98
|
-
try {
|
|
99
|
-
// 有循环,复制多行
|
|
100
|
-
for (var loopArray_1 = tslib.__values(loopArray), loopArray_1_1 = loopArray_1.next(); !loopArray_1_1.done; loopArray_1_1 = loopArray_1.next()) {
|
|
101
|
-
var item = loopArray_1_1.value;
|
|
102
|
-
var newTr = cloneTr(tr);
|
|
103
|
-
var ts = newTr.getElementsByTagName('w:t');
|
|
104
|
-
// 将 item 加入上下文
|
|
105
|
-
var rowData = createObject.createObject(data, item);
|
|
106
|
-
try {
|
|
107
|
-
for (var ts_2 = (e_4 = void 0, tslib.__values(ts)), ts_2_1 = ts_2.next(); !ts_2_1.done; ts_2_1 = ts_2.next()) {
|
|
108
|
-
var t = ts_2_1.value;
|
|
109
|
-
replaceT(word, t, rowData);
|
|
141
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
142
|
+
finally {
|
|
143
|
+
try {
|
|
144
|
+
if (tcs_1_1 && !tcs_1_1.done && (_c = tcs_1.return)) _c.call(tcs_1);
|
|
145
|
+
}
|
|
146
|
+
finally { if (e_3) throw e_3.error; }
|
|
110
147
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
148
|
+
if (!hasLoop) return [3 /*break*/, 16];
|
|
149
|
+
_h.label = 1;
|
|
150
|
+
case 1:
|
|
151
|
+
_h.trys.push([1, 13, 14, 15]);
|
|
152
|
+
loopArray_1 = tslib.__values(loopArray), loopArray_1_1 = loopArray_1.next();
|
|
153
|
+
_h.label = 2;
|
|
154
|
+
case 2:
|
|
155
|
+
if (!!loopArray_1_1.done) return [3 /*break*/, 12];
|
|
156
|
+
item = loopArray_1_1.value;
|
|
157
|
+
newTr = cloneTr(tr);
|
|
158
|
+
ts = newTr.getElementsByTagName('w:t');
|
|
159
|
+
rowData = createObject.createObject(data, item);
|
|
114
160
|
try {
|
|
115
|
-
|
|
161
|
+
for (ts_2 = (e_5 = void 0, tslib.__values(ts)), ts_2_1 = ts_2.next(); !ts_2_1.done; ts_2_1 = ts_2.next()) {
|
|
162
|
+
t = ts_2_1.value;
|
|
163
|
+
replaceT(word, t, rowData);
|
|
164
|
+
}
|
|
116
165
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
replaceAlt(word, cNvPr, rowData);
|
|
166
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
167
|
+
finally {
|
|
168
|
+
try {
|
|
169
|
+
if (ts_2_1 && !ts_2_1.done && (_f = ts_2.return)) _f.call(ts_2);
|
|
170
|
+
}
|
|
171
|
+
finally { if (e_5) throw e_5.error; }
|
|
124
172
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
173
|
+
_h.label = 3;
|
|
174
|
+
case 3:
|
|
175
|
+
_h.trys.push([3, 8, 9, 10]);
|
|
176
|
+
_a = (e_1 = void 0, tslib.__values(newTr.getElementsByTagName('pic:cNvPr'))), _b = _a.next();
|
|
177
|
+
_h.label = 4;
|
|
178
|
+
case 4:
|
|
179
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
180
|
+
cNvPr = _b.value;
|
|
181
|
+
return [4 /*yield*/, replaceAlt(word, cNvPr, rowData, replaceImage)];
|
|
182
|
+
case 5:
|
|
183
|
+
_h.sent();
|
|
184
|
+
_h.label = 6;
|
|
185
|
+
case 6:
|
|
186
|
+
_b = _a.next();
|
|
187
|
+
return [3 /*break*/, 4];
|
|
188
|
+
case 7: return [3 /*break*/, 10];
|
|
189
|
+
case 8:
|
|
190
|
+
e_1_1 = _h.sent();
|
|
191
|
+
e_1 = { error: e_1_1 };
|
|
192
|
+
return [3 /*break*/, 10];
|
|
193
|
+
case 9:
|
|
128
194
|
try {
|
|
129
|
-
if (
|
|
195
|
+
if (_b && !_b.done && (_g = _a.return)) _g.call(_a);
|
|
130
196
|
}
|
|
131
|
-
finally { if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
197
|
+
finally { if (e_1) throw e_1.error; }
|
|
198
|
+
return [7 /*endfinally*/];
|
|
199
|
+
case 10:
|
|
200
|
+
table.insertBefore(newTr, tr);
|
|
201
|
+
_h.label = 11;
|
|
202
|
+
case 11:
|
|
203
|
+
loopArray_1_1 = loopArray_1.next();
|
|
204
|
+
return [3 /*break*/, 2];
|
|
205
|
+
case 12: return [3 /*break*/, 15];
|
|
206
|
+
case 13:
|
|
207
|
+
e_2_1 = _h.sent();
|
|
208
|
+
e_2 = { error: e_2_1 };
|
|
209
|
+
return [3 /*break*/, 15];
|
|
210
|
+
case 14:
|
|
211
|
+
try {
|
|
212
|
+
if (loopArray_1_1 && !loopArray_1_1.done && (_e = loopArray_1.return)) _e.call(loopArray_1);
|
|
213
|
+
}
|
|
214
|
+
finally { if (e_2) throw e_2.error; }
|
|
215
|
+
return [7 /*endfinally*/];
|
|
216
|
+
case 15:
|
|
217
|
+
// 删除原来的行
|
|
218
|
+
table.removeChild(tr);
|
|
219
|
+
_h.label = 16;
|
|
220
|
+
case 16: return [2 /*return*/];
|
|
140
221
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// 删除原来的行
|
|
144
|
-
table.removeChild(tr);
|
|
145
|
-
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
146
224
|
}
|
|
147
225
|
/**
|
|
148
226
|
* 克隆行,并进行一些清理
|
|
@@ -195,25 +273,110 @@ function removeAllAttr(node) {
|
|
|
195
273
|
/**
|
|
196
274
|
* 替换表格,目前只支持行
|
|
197
275
|
*/
|
|
198
|
-
function replaceTable(word, documentData) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
276
|
+
function replaceTable(word, documentData, replaceImage) {
|
|
277
|
+
if (replaceImage === void 0) { replaceImage = false; }
|
|
278
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
279
|
+
var trs, trs_1, trs_1_1, tr, e_8_1;
|
|
280
|
+
var e_8, _a;
|
|
281
|
+
return tslib.__generator(this, function (_b) {
|
|
282
|
+
switch (_b.label) {
|
|
283
|
+
case 0:
|
|
284
|
+
trs = [].slice.call(documentData.getElementsByTagName('w:tr'));
|
|
285
|
+
_b.label = 1;
|
|
286
|
+
case 1:
|
|
287
|
+
_b.trys.push([1, 6, 7, 8]);
|
|
288
|
+
trs_1 = tslib.__values(trs), trs_1_1 = trs_1.next();
|
|
289
|
+
_b.label = 2;
|
|
290
|
+
case 2:
|
|
291
|
+
if (!!trs_1_1.done) return [3 /*break*/, 5];
|
|
292
|
+
tr = trs_1_1.value;
|
|
293
|
+
return [4 /*yield*/, replaceTableRow(word, tr, replaceImage)];
|
|
294
|
+
case 3:
|
|
295
|
+
_b.sent();
|
|
296
|
+
_b.label = 4;
|
|
297
|
+
case 4:
|
|
298
|
+
trs_1_1 = trs_1.next();
|
|
299
|
+
return [3 /*break*/, 2];
|
|
300
|
+
case 5: return [3 /*break*/, 8];
|
|
301
|
+
case 6:
|
|
302
|
+
e_8_1 = _b.sent();
|
|
303
|
+
e_8 = { error: e_8_1 };
|
|
304
|
+
return [3 /*break*/, 8];
|
|
305
|
+
case 7:
|
|
306
|
+
try {
|
|
307
|
+
if (trs_1_1 && !trs_1_1.done && (_a = trs_1.return)) _a.call(trs_1);
|
|
308
|
+
}
|
|
309
|
+
finally { if (e_8) throw e_8.error; }
|
|
310
|
+
return [7 /*endfinally*/];
|
|
311
|
+
case 8: return [2 /*return*/];
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
});
|
|
214
315
|
}
|
|
215
|
-
|
|
216
|
-
|
|
316
|
+
/**
|
|
317
|
+
* 替换单个图片,必须是不在表格里的
|
|
318
|
+
* @param word
|
|
319
|
+
* @param documentData
|
|
320
|
+
*/
|
|
321
|
+
function replaceSingleImage(word, documentData) {
|
|
322
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
323
|
+
var _a, _b, cNvPr, e_9_1;
|
|
324
|
+
var e_9, _c;
|
|
325
|
+
return tslib.__generator(this, function (_d) {
|
|
326
|
+
switch (_d.label) {
|
|
327
|
+
case 0:
|
|
328
|
+
_d.trys.push([0, 5, 6, 7]);
|
|
329
|
+
_a = tslib.__values(documentData.getElementsByTagName('pic:cNvPr')), _b = _a.next();
|
|
330
|
+
_d.label = 1;
|
|
331
|
+
case 1:
|
|
332
|
+
if (!!_b.done) return [3 /*break*/, 4];
|
|
333
|
+
cNvPr = _b.value;
|
|
334
|
+
return [4 /*yield*/, replaceAlt(word, cNvPr, word.renderOptions.data, true)];
|
|
335
|
+
case 2:
|
|
336
|
+
_d.sent();
|
|
337
|
+
_d.label = 3;
|
|
338
|
+
case 3:
|
|
339
|
+
_b = _a.next();
|
|
340
|
+
return [3 /*break*/, 1];
|
|
341
|
+
case 4: return [3 /*break*/, 7];
|
|
342
|
+
case 5:
|
|
343
|
+
e_9_1 = _d.sent();
|
|
344
|
+
e_9 = { error: e_9_1 };
|
|
345
|
+
return [3 /*break*/, 7];
|
|
346
|
+
case 6:
|
|
347
|
+
try {
|
|
348
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
349
|
+
}
|
|
350
|
+
finally { if (e_9) throw e_9.error; }
|
|
351
|
+
return [7 /*endfinally*/];
|
|
352
|
+
case 7: return [2 /*return*/];
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* 变量替换主入口
|
|
359
|
+
* @param word
|
|
360
|
+
* @param documentData
|
|
361
|
+
* @param replaceImage 是否替换掉图片,只有下载时才替换,避免性能问题
|
|
362
|
+
*/
|
|
363
|
+
function replaceVar(word, documentData, replaceImage) {
|
|
364
|
+
if (replaceImage === void 0) { replaceImage = false; }
|
|
365
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
366
|
+
return tslib.__generator(this, function (_a) {
|
|
367
|
+
switch (_a.label) {
|
|
368
|
+
case 0: return [4 /*yield*/, replaceTable(word, documentData, replaceImage)];
|
|
369
|
+
case 1:
|
|
370
|
+
_a.sent();
|
|
371
|
+
if (!replaceImage) return [3 /*break*/, 3];
|
|
372
|
+
return [4 /*yield*/, replaceSingleImage(word, documentData)];
|
|
373
|
+
case 2:
|
|
374
|
+
_a.sent();
|
|
375
|
+
_a.label = 3;
|
|
376
|
+
case 3: return [2 /*return*/];
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
});
|
|
217
380
|
}
|
|
218
381
|
|
|
219
382
|
exports.replaceT = replaceT;
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "office-viewer",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "office 文档在线预览",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"dev": "vite",
|
|
10
|
+
"start": "vite",
|
|
10
11
|
"build": "npm run clean-dist && cross-env NODE_ENV=production rollup -c ",
|
|
12
|
+
"build-esm": "npm run clean-dist && cross-env NODE_ENV=production rollup -c rollup.esm.config.js",
|
|
11
13
|
"test": "jest",
|
|
12
14
|
"lib": "npm run clean-dist && cross-env NODE_ENV=production IS_LIB=1 rollup -c",
|
|
13
15
|
"update-snapshot": "jest --updateSnapshot",
|
|
@@ -49,8 +51,9 @@
|
|
|
49
51
|
"@rollup/plugin-json": "^4.1.0",
|
|
50
52
|
"@rollup/plugin-node-resolve": "^14.1.0",
|
|
51
53
|
"@rollup/plugin-typescript": "^8.3.4",
|
|
54
|
+
"@testing-library/jest-dom": "^5.17.0",
|
|
52
55
|
"@types/jest": "^28.1.0",
|
|
53
|
-
"@
|
|
56
|
+
"@types/prettier": "^2.7.3",
|
|
54
57
|
"amis-formula": "^2.7.2",
|
|
55
58
|
"fast-xml-parser": "4.1.3",
|
|
56
59
|
"jest": "^29.0.3",
|