office-viewer 0.1.4 → 0.2.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 +0 -2
- package/esm/Word.d.ts +9 -1
- package/esm/Word.js +92 -43
- 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/renderParagraph.js +21 -42
- package/esm/util/fileType.d.ts +9 -0
- package/esm/util/fileType.js +66 -0
- package/esm/util/mergeRun.js +7 -1
- 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 +91 -42
- package/lib/package/PackageParser.d.ts +4 -0
- package/lib/package/XMLPackageParser.d.ts +2 -1
- package/lib/package/ZipPackageParser.d.ts +5 -1
- package/lib/package/ZipPackageParser.js +9 -0
- package/lib/render/renderParagraph.js +21 -42
- package/lib/util/fileType.d.ts +9 -0
- package/lib/util/fileType.js +70 -0
- package/lib/util/mergeRun.js +7 -1
- package/lib/util/replaceVar.d.ts +7 -1
- package/lib/util/replaceVar.js +276 -113
- package/package.json +4 -2
|
@@ -19,7 +19,8 @@ export default class XMLPackageParser implements PackageParser {
|
|
|
19
19
|
/**
|
|
20
20
|
* 在 xml 下基本不用这个
|
|
21
21
|
*/
|
|
22
|
-
getFileByType(filePath: string, type: 'string' | 'blob'): string |
|
|
22
|
+
getFileByType(filePath: string, type: 'string' | 'blob'): string | Blob | Uint8Array | null;
|
|
23
|
+
saveFile(filePath: string, content: Uint8Array | string): void;
|
|
23
24
|
/**
|
|
24
25
|
* 判断文件是否存在
|
|
25
26
|
*/
|
|
@@ -17,7 +17,11 @@ export default class ZipPackageParser implements PackageParser {
|
|
|
17
17
|
/**
|
|
18
18
|
* 根据类型读取文件
|
|
19
19
|
*/
|
|
20
|
-
getFileByType(filePath: string, type: 'string' | 'blob' | 'uint8array'): string |
|
|
20
|
+
getFileByType(filePath: string, type: 'string' | 'blob' | 'uint8array'): string | Blob | Uint8Array | 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
|
*/
|
|
@@ -15,14 +15,13 @@ var setElementStyle = require('./setElementStyle.js');
|
|
|
15
15
|
var renderTab = require('./renderTab.js');
|
|
16
16
|
var OMath = require('../openxml/math/OMath.js');
|
|
17
17
|
var renderMath = require('./renderMath.js');
|
|
18
|
-
var Tab = require('../openxml/word/Tab.js');
|
|
19
18
|
|
|
20
19
|
/**
|
|
21
20
|
* 渲染段落
|
|
22
21
|
* @param renderEmptySpace 如果是 true 的话,当内容为空时会自动加上
|
|
23
22
|
*/
|
|
24
23
|
function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
25
|
-
var e_1, _a
|
|
24
|
+
var e_1, _a;
|
|
26
25
|
if (renderEmptySpace === void 0) { renderEmptySpace = true; }
|
|
27
26
|
if (inHeader === void 0) { inHeader = false; }
|
|
28
27
|
word.currentParagraph = paragraph;
|
|
@@ -42,45 +41,25 @@ function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
|
42
41
|
// 虽然目前这个实现很 hack,但可以支持常见情况
|
|
43
42
|
dom.appendChild(p, renderTab.renderTab(word, properties.tabs[0], true));
|
|
44
43
|
// 同时删掉第一个 run 中的 tab
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
64
|
-
finally {
|
|
65
|
-
try {
|
|
66
|
-
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
67
|
-
}
|
|
68
|
-
finally { if (e_2) throw e_2.error; }
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
74
|
-
finally {
|
|
75
|
-
try {
|
|
76
|
-
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
77
|
-
}
|
|
78
|
-
finally { if (e_1) throw e_1.error; }
|
|
79
|
-
}
|
|
44
|
+
// let done = false;
|
|
45
|
+
// for (const child of paragraph.children) {
|
|
46
|
+
// if (done) {
|
|
47
|
+
// break;
|
|
48
|
+
// }
|
|
49
|
+
// if (child instanceof Run) {
|
|
50
|
+
// for (const runChild of child.children) {
|
|
51
|
+
// if (runChild instanceof Tab) {
|
|
52
|
+
// child.children.splice(child.children.indexOf(runChild), 1);
|
|
53
|
+
// done = true;
|
|
54
|
+
// break;
|
|
55
|
+
// }
|
|
56
|
+
// }
|
|
57
|
+
// }
|
|
58
|
+
// }
|
|
80
59
|
}
|
|
81
60
|
try {
|
|
82
|
-
for (var
|
|
83
|
-
var child =
|
|
61
|
+
for (var _b = tslib.__values(paragraph.children), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
62
|
+
var child = _c.value;
|
|
84
63
|
if (child instanceof Run.Run) {
|
|
85
64
|
if (child.fldChar === 'begin') {
|
|
86
65
|
inFldChar = true;
|
|
@@ -105,12 +84,12 @@ function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
|
|
|
105
84
|
}
|
|
106
85
|
}
|
|
107
86
|
}
|
|
108
|
-
catch (
|
|
87
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
109
88
|
finally {
|
|
110
89
|
try {
|
|
111
|
-
if (
|
|
90
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
112
91
|
}
|
|
113
|
-
finally { if (
|
|
92
|
+
finally { if (e_1) throw e_1.error; }
|
|
114
93
|
}
|
|
115
94
|
// 空行自动加个空格,不然会没高度
|
|
116
95
|
if (p.innerHTML === '' && renderEmptySpace) {
|
|
@@ -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/mergeRun.js
CHANGED
|
@@ -39,7 +39,13 @@ function canMerge(element) {
|
|
|
39
39
|
if (childChild.tagName === 'w:t') {
|
|
40
40
|
hasText = true;
|
|
41
41
|
textHasSpace = childChild.getAttribute('xml:space') === 'preserve';
|
|
42
|
-
|
|
42
|
+
if (textHasSpace) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// 有 tab 的情况下不能合并
|
|
47
|
+
if (childChild.tagName === 'w:tab') {
|
|
48
|
+
return false;
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
}
|
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>;
|