@file-viewer/renderer-ofd 2.1.16 → 2.1.18
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/dist/ofd.js +10 -12
- package/package.json +4 -5
- 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/dist/ofd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFileViewerZoomChangeEmitter, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
1
|
+
import { createFileViewerTranslator, createFileViewerZoomChangeEmitter, normalizeFileViewerErrorMessage, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
2
2
|
const OFD_MIN_SCALE = 0.35;
|
|
3
3
|
const OFD_MAX_SCALE = 3;
|
|
4
4
|
const OFD_ZOOM_STEP = 0.1;
|
|
@@ -38,18 +38,15 @@ const createElement = (documentRef, tagName, className, text) => {
|
|
|
38
38
|
}
|
|
39
39
|
return element;
|
|
40
40
|
};
|
|
41
|
-
const normalizeError = (reason) => {
|
|
42
|
-
if (reason instanceof Error) {
|
|
43
|
-
return reason.
|
|
44
|
-
}
|
|
45
|
-
if (typeof reason === 'string') {
|
|
46
|
-
return reason;
|
|
41
|
+
const normalizeError = (reason, fallback, context) => {
|
|
42
|
+
if (reason instanceof Error || typeof reason === 'string') {
|
|
43
|
+
return normalizeFileViewerErrorMessage(reason, context === null || context === void 0 ? void 0 : context.options);
|
|
47
44
|
}
|
|
48
45
|
try {
|
|
49
46
|
return JSON.stringify(reason);
|
|
50
47
|
}
|
|
51
48
|
catch {
|
|
52
|
-
return String(reason ||
|
|
49
|
+
return String(reason || fallback);
|
|
53
50
|
}
|
|
54
51
|
};
|
|
55
52
|
const clampZoom = (value) => Math.min(OFD_MAX_SCALE, Math.max(OFD_MIN_SCALE, Number(value.toFixed(2))));
|
|
@@ -82,6 +79,7 @@ const appendPages = (documentRef, target, pages) => {
|
|
|
82
79
|
target.appendChild(fragment);
|
|
83
80
|
};
|
|
84
81
|
export default async function renderOfd(buffer, target, context) {
|
|
82
|
+
const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
|
|
85
83
|
const documentRef = target.ownerDocument || document;
|
|
86
84
|
const targetWindow = documentRef.defaultView || (typeof window !== 'undefined' ? window : null);
|
|
87
85
|
const zoomEmitter = createFileViewerZoomChangeEmitter();
|
|
@@ -97,7 +95,7 @@ export default async function renderOfd(buffer, target, context) {
|
|
|
97
95
|
const style = createStyle(documentRef);
|
|
98
96
|
const viewer = createElement(documentRef, 'div', 'ofd-viewer');
|
|
99
97
|
viewer.dataset.viewerZoomProvider = 'ofd';
|
|
100
|
-
const stateNode = createElement(documentRef, 'div', 'ofd-state', '
|
|
98
|
+
const stateNode = createElement(documentRef, 'div', 'ofd-state', t('ofd.state.loading'));
|
|
101
99
|
stateNode.setAttribute('aria-live', 'polite');
|
|
102
100
|
const stage = createElement(documentRef, 'div', 'ofd-stage');
|
|
103
101
|
viewer.append(stateNode, stage);
|
|
@@ -108,14 +106,14 @@ export default async function renderOfd(buffer, target, context) {
|
|
|
108
106
|
const syncState = () => {
|
|
109
107
|
stateNode.hidden = state === 'ready';
|
|
110
108
|
stateNode.classList.toggle('error', state === 'error');
|
|
111
|
-
stateNode.textContent = state === 'error' ? errorMessage : '
|
|
109
|
+
stateNode.textContent = state === 'error' ? errorMessage : t('ofd.state.loading');
|
|
112
110
|
};
|
|
113
111
|
const getOfdDocument = async (ofd) => {
|
|
114
112
|
// OFD 解压和 XML 解析成本较高,尺寸变化只重排页面,不重复解析压缩包。
|
|
115
113
|
ofdDocumentPromise || (ofdDocumentPromise = parseWithOfdJs(ofd, buffer).then(documents => {
|
|
116
114
|
const ofdDocument = documents[0];
|
|
117
115
|
if (!ofdDocument) {
|
|
118
|
-
throw new Error('
|
|
116
|
+
throw new Error(t('ofd.error.empty'));
|
|
119
117
|
}
|
|
120
118
|
return ofdDocument;
|
|
121
119
|
}));
|
|
@@ -217,7 +215,7 @@ export default async function renderOfd(buffer, target, context) {
|
|
|
217
215
|
}
|
|
218
216
|
console.error(reason);
|
|
219
217
|
state = 'error';
|
|
220
|
-
errorMessage = normalizeError(reason) || '
|
|
218
|
+
errorMessage = normalizeError(reason, t('ofd.error.parseFailed'), context) || t('ofd.error.parseFailed');
|
|
221
219
|
syncState();
|
|
222
220
|
}
|
|
223
221
|
};
|
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.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Standalone OFD renderer plugin for Flyfish File Viewer powered by DLTech21/ofd.js.",
|
|
@@ -32,7 +32,7 @@
|
|
|
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.18",
|
|
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) {
|