@file-viewer/renderer-ofd 2.0.11
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/LICENSE +160 -0
- package/README.en.md +49 -0
- package/README.md +49 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +17 -0
- package/dist/ofd.d.ts +2 -0
- package/dist/ofd.js +255 -0
- package/package.json +68 -0
- package/vendor/dltech/jbig2/arithmetic_decoder.js +183 -0
- package/vendor/dltech/jbig2/ccitt.js +1070 -0
- package/vendor/dltech/jbig2/compatibility.js +12 -0
- package/vendor/dltech/jbig2/core_utils.js +180 -0
- package/vendor/dltech/jbig2/is_node.js +27 -0
- package/vendor/dltech/jbig2/jbig2.js +2589 -0
- package/vendor/dltech/jbig2/jbig2_stream.js +81 -0
- package/vendor/dltech/jbig2/primitives.js +387 -0
- package/vendor/dltech/jbig2/stream.js +1348 -0
- package/vendor/dltech/jbig2/util.js +972 -0
- package/vendor/dltech/ofd/ofd.d.ts +11 -0
- package/vendor/dltech/ofd/ofd.js +100 -0
- package/vendor/dltech/ofd/ofd_parser.js +395 -0
- package/vendor/dltech/ofd/ofd_render.js +473 -0
- package/vendor/dltech/ofd/ofd_util.js +350 -0
- package/vendor/dltech/ofd/pipeline.js +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work by
|
|
47
|
+
the copyright owner or by an individual or Legal Entity authorized to submit on
|
|
48
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
49
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
50
|
+
Licensor or its representatives, including but not limited to communication on
|
|
51
|
+
electronic mailing lists, source code control systems, and issue tracking systems
|
|
52
|
+
that are managed by, or on behalf of, the Licensor for the purpose of discussing
|
|
53
|
+
and improving the Work, but excluding communication that is conspicuously marked
|
|
54
|
+
or otherwise designated in writing by the copyright owner as "Not a
|
|
55
|
+
Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
|
75
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
102
|
+
Works; within the Source form or documentation, if provided along with the
|
|
103
|
+
Derivative Works; or, within a display generated by the Derivative Works, if and
|
|
104
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
105
|
+
file are for informational purposes only and do not modify the License. You may
|
|
106
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
107
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
108
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
111
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
112
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
113
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
114
|
+
with the conditions stated in this License.
|
|
115
|
+
|
|
116
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
117
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
118
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
119
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
120
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
121
|
+
executed with Licensor regarding such Contributions.
|
|
122
|
+
|
|
123
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
124
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
125
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
126
|
+
reproducing the content of the NOTICE file.
|
|
127
|
+
|
|
128
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
129
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
130
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
131
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
132
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
135
|
+
associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
138
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
139
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
140
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
141
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
142
|
+
arising as a result of this License or out of the use or inability to use the
|
|
143
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
144
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
145
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
146
|
+
damages.
|
|
147
|
+
|
|
148
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
149
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
150
|
+
acceptance of support, warranty, indemnity, or other liability obligations and/or
|
|
151
|
+
rights consistent with this License. However, in accepting such obligations, You
|
|
152
|
+
may act only on Your own behalf and on Your sole responsibility, not on behalf of
|
|
153
|
+
any other Contributor, and only if You agree to indemnify, defend, and hold each
|
|
154
|
+
Contributor harmless for any liability incurred by, or claims asserted against,
|
|
155
|
+
such Contributor by reason of your accepting any such warranty or additional
|
|
156
|
+
liability.
|
|
157
|
+
|
|
158
|
+
END OF TERMS AND CONDITIONS
|
|
159
|
+
|
|
160
|
+
Copyright 2026 Flyfish Viewer
|
package/README.en.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @file-viewer/renderer-ofd
|
|
2
|
+
|
|
3
|
+
Standalone OFD renderer for Flyfish File Viewer. It uses the pure-browser DLTech21/ofd.js pipeline and lazy-loads the OFD vendor, XML parser, and ZIP parser only when an `.ofd` document is opened.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @file-viewer/core @file-viewer/renderer-ofd
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import FileViewer from '@file-viewer/vue3'
|
|
15
|
+
import { ofdRenderer } from '@file-viewer/renderer-ofd'
|
|
16
|
+
|
|
17
|
+
const options = {
|
|
18
|
+
rendererMode: 'replace',
|
|
19
|
+
renderers: ofdRenderer,
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Use `@file-viewer/preset-all` when you need the same full format matrix as the official demo, or compose it with other renderer packages:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { ofdRenderer } from '@file-viewer/renderer-ofd'
|
|
27
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
28
|
+
|
|
29
|
+
const options = {
|
|
30
|
+
rendererMode: 'replace',
|
|
31
|
+
renderers: [pdfRenderer, ofdRenderer],
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- Preview `.ofd` documents in the browser.
|
|
38
|
+
- Lazy-load the OFD parser and renderer at the format boundary.
|
|
39
|
+
- Ship the vendor files inside the npm package for offline and intranet deployments.
|
|
40
|
+
- Support unified zoom, print, HTML export, and lifecycle context integration.
|
|
41
|
+
|
|
42
|
+
## Migration Note
|
|
43
|
+
|
|
44
|
+
OFD rendering has moved out of `@file-viewer/core` into this package. `jszip`, `ofd-xml-parser`, 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
|
+
|
|
46
|
+
## Documentation
|
|
47
|
+
|
|
48
|
+
- On-demand renderer architecture: <https://doc.file-viewer.app/guide/on-demand-renderers>
|
|
49
|
+
- Supported formats: <https://doc.file-viewer.app/guide/formats>
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @file-viewer/renderer-ofd
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立 OFD 渲染器。它基于 DLTech21/ofd.js 的纯前端解析链路,按需加载 OFD vendor、XML parser 和 ZIP 解析依赖,适合电子发票、公文和国产版式文件在线预览。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @file-viewer/core @file-viewer/renderer-ofd
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 用法
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import FileViewer from '@file-viewer/vue3'
|
|
15
|
+
import { ofdRenderer } from '@file-viewer/renderer-ofd'
|
|
16
|
+
|
|
17
|
+
const options = {
|
|
18
|
+
rendererMode: 'replace',
|
|
19
|
+
renderers: ofdRenderer,
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
也可以通过 `@file-viewer/preset-all` 一次性启用官方 demo 的完整格式矩阵,或与其他 renderer 组合:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { ofdRenderer } from '@file-viewer/renderer-ofd'
|
|
27
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
28
|
+
|
|
29
|
+
const options = {
|
|
30
|
+
rendererMode: 'replace',
|
|
31
|
+
renderers: [pdfRenderer, ofdRenderer],
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 能力
|
|
36
|
+
|
|
37
|
+
- 支持 `.ofd` 国产版式文件。
|
|
38
|
+
- 按需动态导入 OFD 解析/渲染代码,不污染首屏。
|
|
39
|
+
- vendor 文件随 npm 包发布,支持企业内网和纯离线部署。
|
|
40
|
+
- 支持统一缩放、打印、HTML 导出和生命周期上下文。
|
|
41
|
+
|
|
42
|
+
## 迁移说明
|
|
43
|
+
|
|
44
|
+
OFD 渲染已经从 `@file-viewer/core` 移入本包,`jszip`、`ofd-xml-parser` 和 DLTech21/ofd.js vendor 只由 `@file-viewer/renderer-ofd` 承接。只安装 core 或标准组件包时不会再拉取 OFD 解析依赖;需要 OFD 预览时请显式装配本 renderer,或使用 `@file-viewer/preset-all`。
|
|
45
|
+
|
|
46
|
+
## 文档
|
|
47
|
+
|
|
48
|
+
- 按需渲染架构: <https://doc.file-viewer.app/guide/on-demand-renderers>
|
|
49
|
+
- 支持格式: <https://doc.file-viewer.app/guide/formats>
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type FileRenderHandler, type FileViewerRenderedInstance, type FileViewerRendererPlugin, type RendererDefinition } from '@file-viewer/core';
|
|
2
|
+
export declare const ofdRendererDefinition: RendererDefinition;
|
|
3
|
+
export declare const renderFileViewerOfd: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
4
|
+
export declare const ofdRenderer: FileViewerRendererPlugin<FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>>;
|
|
5
|
+
export default ofdRenderer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
|
+
const ofdDefinition = DEFAULT_RENDERER_DEFINITIONS.find(definition => definition.id === 'ofd');
|
|
3
|
+
if (!ofdDefinition) {
|
|
4
|
+
throw new Error('@file-viewer/renderer-ofd could not locate the core OFD renderer definition.');
|
|
5
|
+
}
|
|
6
|
+
export const ofdRendererDefinition = ofdDefinition;
|
|
7
|
+
export const renderFileViewerOfd = (buffer, target, _type, context) => import('./ofd.js').then(({ default: renderOfd }) => renderOfd(buffer, target, context));
|
|
8
|
+
export const ofdRenderer = {
|
|
9
|
+
id: 'file-viewer-renderer-ofd',
|
|
10
|
+
label: 'Flyfish File Viewer OFD renderer',
|
|
11
|
+
definitions: [ofdRendererDefinition],
|
|
12
|
+
handlers: [{
|
|
13
|
+
rendererId: ofdRendererDefinition.id,
|
|
14
|
+
handler: renderFileViewerOfd,
|
|
15
|
+
}],
|
|
16
|
+
};
|
|
17
|
+
export default ofdRenderer;
|
package/dist/ofd.d.ts
ADDED
package/dist/ofd.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { createFileViewerZoomChangeEmitter, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
2
|
+
const OFD_MIN_SCALE = 0.35;
|
|
3
|
+
const OFD_MAX_SCALE = 3;
|
|
4
|
+
const OFD_ZOOM_STEP = 0.1;
|
|
5
|
+
const ofdStyle = `
|
|
6
|
+
.ofd-viewer{position:relative;box-sizing:border-box;min-height:100%;background:#e9edf2;color:#172033;font-family:Aptos,'Segoe UI','PingFang SC','Microsoft YaHei',sans-serif}
|
|
7
|
+
.ofd-viewer *{box-sizing:border-box}
|
|
8
|
+
.ofd-stage{min-height:100%;padding:18px 0 28px;overflow:auto;scrollbar-gutter:stable}
|
|
9
|
+
.ofd-state{position:absolute;inset:0;z-index:1;display:flex;align-items:center;justify-content:center;background:rgba(246,248,250,.92);color:#64748b;font-size:14px}
|
|
10
|
+
.ofd-state[hidden]{display:none!important}
|
|
11
|
+
.ofd-state.error{color:#b42318}
|
|
12
|
+
.ofd-page-frame{position:relative;display:block;margin:0 auto 20px;overflow:visible}
|
|
13
|
+
.ofd-page{display:block;margin-left:auto!important;margin-right:auto!important;box-shadow:0 10px 26px rgba(15,23,42,.12);transition:transform .16s ease}
|
|
14
|
+
.file-viewer[data-viewer-theme='dark'] .ofd-viewer{background:#172033;color:#e5eef8}
|
|
15
|
+
.file-viewer[data-viewer-theme='dark'] .ofd-state{background:rgba(15,23,42,.9);color:#cbd5e1}
|
|
16
|
+
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .ofd-viewer{background:#172033;color:#e5eef8}.file-viewer[data-viewer-theme='system'] .ofd-state{background:rgba(15,23,42,.9);color:#cbd5e1}}
|
|
17
|
+
@media print{.ofd-viewer{background:#fff!important}.ofd-stage{padding:0!important;overflow:visible!important}.ofd-page-frame{break-after:page;page-break-after:always;margin:0 auto!important}.ofd-page-frame:last-child{break-after:auto;page-break-after:auto}.ofd-page{box-shadow:none!important;transition:none!important}}
|
|
18
|
+
`;
|
|
19
|
+
const loadOfdModule = (() => {
|
|
20
|
+
let modulePromise = null;
|
|
21
|
+
return () => {
|
|
22
|
+
modulePromise || (modulePromise = import('../vendor/dltech/ofd/ofd.js'));
|
|
23
|
+
return modulePromise;
|
|
24
|
+
};
|
|
25
|
+
})();
|
|
26
|
+
const createStyle = (documentRef) => {
|
|
27
|
+
const style = documentRef.createElement('style');
|
|
28
|
+
style.textContent = ofdStyle;
|
|
29
|
+
return style;
|
|
30
|
+
};
|
|
31
|
+
const createElement = (documentRef, tagName, className, text) => {
|
|
32
|
+
const element = documentRef.createElement(tagName);
|
|
33
|
+
if (className) {
|
|
34
|
+
element.className = className;
|
|
35
|
+
}
|
|
36
|
+
if (text !== undefined) {
|
|
37
|
+
element.textContent = text;
|
|
38
|
+
}
|
|
39
|
+
return element;
|
|
40
|
+
};
|
|
41
|
+
const normalizeError = (reason) => {
|
|
42
|
+
if (reason instanceof Error) {
|
|
43
|
+
return reason.message;
|
|
44
|
+
}
|
|
45
|
+
if (typeof reason === 'string') {
|
|
46
|
+
return reason;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return JSON.stringify(reason);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return String(reason || 'OFD 文件解析失败');
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const clampZoom = (value) => Math.min(OFD_MAX_SCALE, Math.max(OFD_MIN_SCALE, Number(value.toFixed(2))));
|
|
56
|
+
const waitForPaint = (view) => new Promise(resolve => {
|
|
57
|
+
if (view === null || view === void 0 ? void 0 : view.requestAnimationFrame) {
|
|
58
|
+
view.requestAnimationFrame(() => resolve());
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
globalThis.setTimeout(resolve, 0);
|
|
62
|
+
});
|
|
63
|
+
const parseWithOfdJs = (ofd, buffer) => {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
// 使用 DLTech21/ofd.js 的纯 JS 解析入口,签章验签链路不阻断正文预览。
|
|
66
|
+
ofd.parseOfdDocument({
|
|
67
|
+
ofd: buffer,
|
|
68
|
+
success: documents => resolve(documents),
|
|
69
|
+
fail: reason => reject(reason),
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const appendPages = (documentRef, target, pages) => {
|
|
74
|
+
const fragment = documentRef.createDocumentFragment();
|
|
75
|
+
pages.forEach(page => {
|
|
76
|
+
const frame = documentRef.createElement('div');
|
|
77
|
+
frame.className = 'ofd-page-frame';
|
|
78
|
+
page.classList.add('ofd-page');
|
|
79
|
+
frame.appendChild(page);
|
|
80
|
+
fragment.appendChild(frame);
|
|
81
|
+
});
|
|
82
|
+
target.appendChild(fragment);
|
|
83
|
+
};
|
|
84
|
+
export default async function renderOfd(buffer, target, context) {
|
|
85
|
+
const documentRef = target.ownerDocument || document;
|
|
86
|
+
const targetWindow = documentRef.defaultView || (typeof window !== 'undefined' ? window : null);
|
|
87
|
+
const zoomEmitter = createFileViewerZoomChangeEmitter();
|
|
88
|
+
let disposed = false;
|
|
89
|
+
let renderVersion = 0;
|
|
90
|
+
let resizeObserver = null;
|
|
91
|
+
let resizeTimer = 0;
|
|
92
|
+
let lastRenderedWidth = 0;
|
|
93
|
+
let state = 'loading';
|
|
94
|
+
let errorMessage = '';
|
|
95
|
+
let zoom = 1;
|
|
96
|
+
let ofdDocumentPromise = null;
|
|
97
|
+
const style = createStyle(documentRef);
|
|
98
|
+
const viewer = createElement(documentRef, 'div', 'ofd-viewer');
|
|
99
|
+
viewer.dataset.viewerZoomProvider = 'ofd';
|
|
100
|
+
const stateNode = createElement(documentRef, 'div', 'ofd-state', '正在解析 OFD...');
|
|
101
|
+
stateNode.setAttribute('aria-live', 'polite');
|
|
102
|
+
const stage = createElement(documentRef, 'div', 'ofd-stage');
|
|
103
|
+
viewer.append(stateNode, stage);
|
|
104
|
+
target.replaceChildren(style, viewer);
|
|
105
|
+
const clearStage = () => {
|
|
106
|
+
stage.replaceChildren();
|
|
107
|
+
};
|
|
108
|
+
const syncState = () => {
|
|
109
|
+
stateNode.hidden = state === 'ready';
|
|
110
|
+
stateNode.classList.toggle('error', state === 'error');
|
|
111
|
+
stateNode.textContent = state === 'error' ? errorMessage : '正在解析 OFD...';
|
|
112
|
+
};
|
|
113
|
+
const getOfdDocument = async (ofd) => {
|
|
114
|
+
// OFD 解压和 XML 解析成本较高,尺寸变化只重排页面,不重复解析压缩包。
|
|
115
|
+
ofdDocumentPromise || (ofdDocumentPromise = parseWithOfdJs(ofd, buffer).then(documents => {
|
|
116
|
+
const ofdDocument = documents[0];
|
|
117
|
+
if (!ofdDocument) {
|
|
118
|
+
throw new Error('OFD 文件中没有可渲染的文档');
|
|
119
|
+
}
|
|
120
|
+
return ofdDocument;
|
|
121
|
+
}));
|
|
122
|
+
return ofdDocumentPromise;
|
|
123
|
+
};
|
|
124
|
+
const getRenderWidth = () => {
|
|
125
|
+
const baseWidth = viewer.getBoundingClientRect().width || stage.getBoundingClientRect().width || 0;
|
|
126
|
+
return Math.max(Math.floor(baseWidth - 48), 240);
|
|
127
|
+
};
|
|
128
|
+
const getZoomState = () => ({
|
|
129
|
+
scale: zoom,
|
|
130
|
+
label: `${Math.round(zoom * 100)}%`,
|
|
131
|
+
canZoomIn: zoom < OFD_MAX_SCALE,
|
|
132
|
+
canZoomOut: zoom > OFD_MIN_SCALE,
|
|
133
|
+
canReset: zoom !== 1,
|
|
134
|
+
minScale: OFD_MIN_SCALE,
|
|
135
|
+
maxScale: OFD_MAX_SCALE,
|
|
136
|
+
});
|
|
137
|
+
const syncPageZoom = () => {
|
|
138
|
+
const HTMLElementCtor = (targetWindow === null || targetWindow === void 0 ? void 0 : targetWindow.HTMLElement) || globalThis.HTMLElement;
|
|
139
|
+
stage.querySelectorAll('.ofd-page-frame').forEach(frame => {
|
|
140
|
+
const page = frame.firstElementChild;
|
|
141
|
+
if (!HTMLElementCtor || !(page instanceof HTMLElementCtor)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
page.style.position = 'absolute';
|
|
145
|
+
page.style.top = '0';
|
|
146
|
+
page.style.left = '50%';
|
|
147
|
+
page.style.transform = `translateX(-50%) scale(${zoom})`;
|
|
148
|
+
page.style.transformOrigin = 'top center';
|
|
149
|
+
page.style.marginLeft = '0';
|
|
150
|
+
page.style.marginRight = '0';
|
|
151
|
+
const pageWidth = page.offsetWidth;
|
|
152
|
+
const pageHeight = page.offsetHeight;
|
|
153
|
+
if (!pageWidth || !pageHeight) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
frame.style.width = `${Math.ceil(pageWidth * zoom)}px`;
|
|
157
|
+
frame.style.height = `${Math.ceil(pageHeight * zoom)}px`;
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
const setZoom = (nextZoom) => {
|
|
161
|
+
zoom = clampZoom(nextZoom);
|
|
162
|
+
syncPageZoom();
|
|
163
|
+
zoomEmitter.emit();
|
|
164
|
+
return getZoomState();
|
|
165
|
+
};
|
|
166
|
+
registerFileViewerZoomProvider(viewer, {
|
|
167
|
+
zoomIn: () => setZoom(zoom + OFD_ZOOM_STEP),
|
|
168
|
+
zoomOut: () => setZoom(zoom - OFD_ZOOM_STEP),
|
|
169
|
+
resetZoom: () => setZoom(1),
|
|
170
|
+
setZoom,
|
|
171
|
+
getState: getZoomState,
|
|
172
|
+
subscribe: zoomEmitter.subscribe,
|
|
173
|
+
});
|
|
174
|
+
const renderWithOfdJs = async (width) => {
|
|
175
|
+
const ofd = await loadOfdModule();
|
|
176
|
+
const ofdDocument = await getOfdDocument(ofd);
|
|
177
|
+
if (disposed) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
return Promise.resolve(ofd.renderOfd(width, ofdDocument));
|
|
181
|
+
};
|
|
182
|
+
const render = async (options = {}) => {
|
|
183
|
+
var _a;
|
|
184
|
+
if (disposed) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const width = getRenderWidth();
|
|
188
|
+
if (!options.force && state === 'ready' && Math.abs(width - lastRenderedWidth) < 8) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const version = ++renderVersion;
|
|
192
|
+
if (options.showLoading || state !== 'ready') {
|
|
193
|
+
state = 'loading';
|
|
194
|
+
clearStage();
|
|
195
|
+
syncState();
|
|
196
|
+
}
|
|
197
|
+
errorMessage = '';
|
|
198
|
+
try {
|
|
199
|
+
await waitForPaint(targetWindow);
|
|
200
|
+
const pages = await renderWithOfdJs(width);
|
|
201
|
+
if (disposed || version !== renderVersion) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
clearStage();
|
|
205
|
+
appendPages(documentRef, stage, pages);
|
|
206
|
+
lastRenderedWidth = width;
|
|
207
|
+
await waitForPaint(targetWindow);
|
|
208
|
+
syncPageZoom();
|
|
209
|
+
state = 'ready';
|
|
210
|
+
syncState();
|
|
211
|
+
zoomEmitter.emit();
|
|
212
|
+
(_a = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _a === void 0 ? void 0 : _a.call(context);
|
|
213
|
+
}
|
|
214
|
+
catch (reason) {
|
|
215
|
+
if (disposed || version !== renderVersion) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
console.error(reason);
|
|
219
|
+
state = 'error';
|
|
220
|
+
errorMessage = normalizeError(reason) || 'OFD 文件解析失败';
|
|
221
|
+
syncState();
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const startResizeObserver = () => {
|
|
225
|
+
if (!(targetWindow === null || targetWindow === void 0 ? void 0 : targetWindow.ResizeObserver) || resizeObserver) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
resizeObserver = new targetWindow.ResizeObserver(() => {
|
|
229
|
+
targetWindow.clearTimeout(resizeTimer);
|
|
230
|
+
resizeTimer = targetWindow.setTimeout(() => {
|
|
231
|
+
void render({ showLoading: false });
|
|
232
|
+
}, 180);
|
|
233
|
+
});
|
|
234
|
+
resizeObserver.observe(viewer);
|
|
235
|
+
};
|
|
236
|
+
void render({ force: true, showLoading: true }).finally(() => {
|
|
237
|
+
if (!disposed) {
|
|
238
|
+
startResizeObserver();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
$el: viewer,
|
|
243
|
+
unmount() {
|
|
244
|
+
var _a;
|
|
245
|
+
disposed = true;
|
|
246
|
+
renderVersion += 1;
|
|
247
|
+
targetWindow === null || targetWindow === void 0 ? void 0 : targetWindow.clearTimeout(resizeTimer);
|
|
248
|
+
resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
|
|
249
|
+
resizeObserver = null;
|
|
250
|
+
unregisterFileViewerZoomProvider(viewer);
|
|
251
|
+
clearStage();
|
|
252
|
+
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, null);
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@file-viewer/renderer-ofd",
|
|
3
|
+
"version": "2.0.11",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Standalone OFD renderer plugin for Flyfish File Viewer powered by DLTech21/ofd.js.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"file-viewer",
|
|
9
|
+
"renderer",
|
|
10
|
+
"ofd",
|
|
11
|
+
"document-preview",
|
|
12
|
+
"document-viewer",
|
|
13
|
+
"file-preview",
|
|
14
|
+
"self-hosted"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Wangyu",
|
|
22
|
+
"email": "wybaby168@gmail.com"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/flyfish-dev/file-viewer-renderer-ofd.git",
|
|
27
|
+
"directory": "packages/renderers/ofd"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://doc.file-viewer.app/guide/on-demand-renderers",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/flyfish-dev/file-viewer-renderer-ofd/issues"
|
|
32
|
+
},
|
|
33
|
+
"funding": {
|
|
34
|
+
"type": "individual",
|
|
35
|
+
"url": "https://dev.flyfish.group/shop"
|
|
36
|
+
},
|
|
37
|
+
"main": "./dist/index.js",
|
|
38
|
+
"module": "./dist/index.js",
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"import": "./dist/index.js",
|
|
44
|
+
"default": "./dist/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"vendor",
|
|
51
|
+
"README.md",
|
|
52
|
+
"README.en.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@file-viewer/core": "^2.0.11",
|
|
57
|
+
"jszip": "^3.10.1",
|
|
58
|
+
"ofd-xml-parser": "^0.0.6"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"typescript": "^6.0.3"
|
|
62
|
+
},
|
|
63
|
+
"license": "Apache-2.0",
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsc -b tsconfig.json",
|
|
66
|
+
"type-check": "tsc -b tsconfig.json"
|
|
67
|
+
}
|
|
68
|
+
}
|