@file-viewer/renderer-ofd 2.1.15 → 2.1.17
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.en.md +1 -1
- package/README.md +1 -1
- package/package.json +6 -7
- package/vendor/dltech/ofd/ofd_parser.js +61 -3
package/README.en.md
CHANGED
|
@@ -41,7 +41,7 @@ const options = {
|
|
|
41
41
|
|
|
42
42
|
## Migration Note
|
|
43
43
|
|
|
44
|
-
OFD rendering has moved out of `@file-viewer/core` into this package. `jszip`,
|
|
44
|
+
OFD rendering has moved out of `@file-viewer/core` into this package. `jszip`, the local XML parsing logic, and the DLTech21/ofd.js vendor files are now owned by `@file-viewer/renderer-ofd`. Installing core or a standard component package no longer pulls OFD parsing dependencies; explicitly assemble this renderer when OFD preview is needed, or use `@file-viewer/preset-all`.
|
|
45
45
|
|
|
46
46
|
## Documentation
|
|
47
47
|
|
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ const options = {
|
|
|
41
41
|
|
|
42
42
|
## 迁移说明
|
|
43
43
|
|
|
44
|
-
OFD 渲染已经从 `@file-viewer/core` 移入本包,`jszip
|
|
44
|
+
OFD 渲染已经从 `@file-viewer/core` 移入本包,`jszip`、本地 XML 解析逻辑和 DLTech21/ofd.js vendor 只由 `@file-viewer/renderer-ofd` 承接。只安装 core 或标准组件包时不会再拉取 OFD 解析依赖;需要 OFD 预览时请显式装配本 renderer,或使用 `@file-viewer/preset-all`。
|
|
45
45
|
|
|
46
46
|
## 文档
|
|
47
47
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@file-viewer/renderer-ofd",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.17",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Standalone OFD renderer plugin for Flyfish File Viewer powered by DLTech21/ofd.js.",
|
|
@@ -23,16 +23,16 @@
|
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/flyfish-dev/file-viewer
|
|
26
|
+
"url": "git+https://github.com/flyfish-dev/file-viewer.git",
|
|
27
27
|
"directory": "packages/renderers/ofd"
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://doc.file-viewer.app/guide/on-demand-renderers",
|
|
30
30
|
"bugs": {
|
|
31
|
-
"url": "https://github.com/flyfish-dev/file-viewer
|
|
31
|
+
"url": "https://github.com/flyfish-dev/file-viewer/issues"
|
|
32
32
|
},
|
|
33
33
|
"funding": {
|
|
34
34
|
"type": "individual",
|
|
35
|
-
"url": "https://dev.flyfish.group/
|
|
35
|
+
"url": "https://dev.flyfish.group/donate?source=npm"
|
|
36
36
|
},
|
|
37
37
|
"main": "./dist/index.js",
|
|
38
38
|
"module": "./dist/index.js",
|
|
@@ -53,9 +53,8 @@
|
|
|
53
53
|
"LICENSE"
|
|
54
54
|
],
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@file-viewer/core": "^2.1.
|
|
57
|
-
"jszip": "^3.10.1"
|
|
58
|
-
"ofd-xml-parser": "^0.0.6"
|
|
56
|
+
"@file-viewer/core": "^2.1.17",
|
|
57
|
+
"jszip": "^3.10.1"
|
|
59
58
|
},
|
|
60
59
|
"devDependencies": {
|
|
61
60
|
"typescript": "^6.0.3"
|
|
@@ -19,12 +19,11 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import JsZip from "jszip";
|
|
22
|
-
import parser from 'ofd-xml-parser';
|
|
23
22
|
import {parseStBox, getExtensionByPath, replaceFirstSlash} from "./ofd_util.js";
|
|
24
23
|
import {Jbig2Image} from '../jbig2/jbig2.js';
|
|
25
24
|
|
|
26
25
|
const ensureBrowserGlobal = () => {
|
|
27
|
-
//
|
|
26
|
+
// DLTech21/ofd.js 的一部分遗留逻辑读取 Node 风格 global。
|
|
28
27
|
// 浏览器端把它映射到 globalThis,避免解析 XML 时抛出 global is not defined。
|
|
29
28
|
if (!globalThis.global) {
|
|
30
29
|
globalThis.global = globalThis;
|
|
@@ -32,6 +31,65 @@ const ensureBrowserGlobal = () => {
|
|
|
32
31
|
globalThis.xmlParseFlag = 0;
|
|
33
32
|
}
|
|
34
33
|
|
|
34
|
+
const appendChildValue = function (target, key, value) {
|
|
35
|
+
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
|
36
|
+
target[key] = Array.isArray(target[key])
|
|
37
|
+
? target[key].concat(value)
|
|
38
|
+
: [target[key], value];
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
target[key] = value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const parseXmlElement = function (element, options) {
|
|
45
|
+
const attrPrefix = options.attributeNamePrefix ?? '@_';
|
|
46
|
+
const result = {};
|
|
47
|
+
if (options.ignoreAttributes !== true) {
|
|
48
|
+
for (const attr of Array.from(element.attributes || [])) {
|
|
49
|
+
result[`${attrPrefix}${attr.name}`] = attr.value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const textParts = [];
|
|
54
|
+
for (const child of Array.from(element.childNodes || [])) {
|
|
55
|
+
if (child.nodeType === 1) {
|
|
56
|
+
appendChildValue(result, child.nodeName, parseXmlElement(child, options));
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (child.nodeType === 3 || child.nodeType === 4) {
|
|
60
|
+
textParts.push(child.nodeValue || '');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const text = textParts.join('').trim();
|
|
65
|
+
if (text) {
|
|
66
|
+
if (Object.keys(result).length === 0) {
|
|
67
|
+
return text;
|
|
68
|
+
}
|
|
69
|
+
result['#text'] = text;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const parseXmlToJson = function (xmlData, options = {}) {
|
|
75
|
+
const DOMParserCtor = globalThis.DOMParser;
|
|
76
|
+
if (typeof DOMParserCtor !== 'function') {
|
|
77
|
+
throw new Error('OFD XML parser requires DOMParser in the current runtime');
|
|
78
|
+
}
|
|
79
|
+
const document = new DOMParserCtor().parseFromString(xmlData, 'application/xml');
|
|
80
|
+
const parserError = document.getElementsByTagName('parsererror')[0];
|
|
81
|
+
if (parserError) {
|
|
82
|
+
throw new Error(parserError.textContent || 'OFD XML parse failed');
|
|
83
|
+
}
|
|
84
|
+
const root = document.documentElement;
|
|
85
|
+
if (!root) {
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
[root.nodeName]: parseXmlElement(root, options)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
35
93
|
const asArray = function (value) {
|
|
36
94
|
if (!value) {
|
|
37
95
|
return [];
|
|
@@ -486,7 +544,7 @@ const getJsonFromXmlContent = async function (zip, xmlName) {
|
|
|
486
544
|
parseNodeValue: false,
|
|
487
545
|
trimValues: false
|
|
488
546
|
};
|
|
489
|
-
let jsonObj =
|
|
547
|
+
let jsonObj = parseXmlToJson(content, ops);
|
|
490
548
|
let result = {'xml': content, 'json': jsonObj};
|
|
491
549
|
resolve(result);
|
|
492
550
|
}, function error(e) {
|