@file-viewer/renderer-word 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 +59 -0
- package/README.md +59 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +34 -0
- package/dist/openDocument.d.ts +2 -0
- package/dist/openDocument.js +122 -0
- package/dist/wordDoc.d.ts +5 -0
- package/dist/wordDoc.js +282 -0
- package/dist/wordDocx.d.ts +5 -0
- package/dist/wordDocx.js +983 -0
- package/package.json +74 -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,59 @@
|
|
|
1
|
+
# @file-viewer/renderer-word
|
|
2
|
+
|
|
3
|
+
Standalone Word and compatible document renderer package for Flyfish File Viewer. It owns the DOCX/DOCM/DOTX/DOTM, legacy DOC/DOT, RTF, ODT, and ODP preview path so `@file-viewer/core` no longer installs Word parsing engines directly.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import FileViewer from '@file-viewer/vue3'
|
|
9
|
+
import { wordRenderer } from '@file-viewer/renderer-word'
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
rendererMode: 'replace',
|
|
13
|
+
renderers: wordRenderer,
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can compose it with other renderers:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { wordRenderer } from '@file-viewer/renderer-word'
|
|
21
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
22
|
+
import { presentationRenderer } from '@file-viewer/renderer-presentation'
|
|
23
|
+
|
|
24
|
+
const options = {
|
|
25
|
+
rendererMode: 'replace',
|
|
26
|
+
renderers: [wordRenderer, pdfRenderer, presentationRenderer],
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Use `@file-viewer/preset-all` when you want the same complete matrix as the official demo.
|
|
31
|
+
|
|
32
|
+
## Capabilities
|
|
33
|
+
|
|
34
|
+
- DOCX / DOCM / DOTX / DOTM use the self-maintained `@file-viewer/docx` engine with Worker parsing, continuous reading layout, cached TOC fields, and async batched rendering.
|
|
35
|
+
- DOC / DOT use `msdoc-viewer` with a Word-like paper surface, zoom, print, and HTML export adapters.
|
|
36
|
+
- RTF uses `rtf.js`; ODT / ODP read `content.xml` from OpenDocument packages for safe structure previews.
|
|
37
|
+
- The renderer reuses core search, zoom, print, export, lifecycle, and operation APIs.
|
|
38
|
+
|
|
39
|
+
## Offline Assets
|
|
40
|
+
|
|
41
|
+
DOCX Worker defaults to viewer assets:
|
|
42
|
+
|
|
43
|
+
- `vendor/docx/docx.worker.js`
|
|
44
|
+
- `vendor/docx/jszip.min.js`
|
|
45
|
+
|
|
46
|
+
Override them for private deployments:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const options = {
|
|
50
|
+
docx: {
|
|
51
|
+
workerUrl: '/file-viewer/vendor/docx/docx.worker.js',
|
|
52
|
+
workerJsZipUrl: '/file-viewer/vendor/docx/jszip.min.js',
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Migration
|
|
58
|
+
|
|
59
|
+
`@file-viewer/core` no longer depends on `@file-viewer/docx`, `msdoc-viewer`, `rtf.js`, `linkedom`, or `@xmldom/xmldom` directly. Install and pass this renderer for full Word preview, or use `@file-viewer/preset-all`.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @file-viewer/renderer-word
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立 Word / 兼容文档 renderer 包。它承接 DOCX/DOCM/DOTX/DOTM、旧版 DOC/DOT、RTF、ODT/ODP 等文档预览链路,让 `@file-viewer/core` 不再直接安装 Word 解析与渲染依赖。
|
|
4
|
+
|
|
5
|
+
## 用法
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import FileViewer from '@file-viewer/vue3'
|
|
9
|
+
import { wordRenderer } from '@file-viewer/renderer-word'
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
rendererMode: 'replace',
|
|
13
|
+
renderers: wordRenderer,
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
也可以和其他 renderer 一起组合:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { wordRenderer } from '@file-viewer/renderer-word'
|
|
21
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
22
|
+
import { presentationRenderer } from '@file-viewer/renderer-presentation'
|
|
23
|
+
|
|
24
|
+
const options = {
|
|
25
|
+
rendererMode: 'replace',
|
|
26
|
+
renderers: [wordRenderer, pdfRenderer, presentationRenderer],
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
官方 Demo 和完整格式矩阵可以直接使用 `@file-viewer/preset-all`。
|
|
31
|
+
|
|
32
|
+
## 能力边界
|
|
33
|
+
|
|
34
|
+
- DOCX / DOCM / DOTX / DOTM 使用自研 `@file-viewer/docx`,默认 Worker 解析、连续流式阅读、目录字段缓存和异步分批渲染。
|
|
35
|
+
- DOC / DOT 使用 `msdoc-viewer`,并套用 Word 风格纸张阅读面、缩放、打印和 HTML 导出适配。
|
|
36
|
+
- RTF 使用 `rtf.js`,ODT / ODP 读取 OpenDocument 包内 `content.xml` 做安全结构预览。
|
|
37
|
+
- 继续复用 core 的统一搜索、缩放、打印、导出、生命周期和操作能力。
|
|
38
|
+
|
|
39
|
+
## 离线资产
|
|
40
|
+
|
|
41
|
+
DOCX Worker 默认读取 viewer assets 下的:
|
|
42
|
+
|
|
43
|
+
- `vendor/docx/docx.worker.js`
|
|
44
|
+
- `vendor/docx/jszip.min.js`
|
|
45
|
+
|
|
46
|
+
私有化部署时可以通过 `options.docx.workerUrl` 和 `options.docx.workerJsZipUrl` 覆盖:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const options = {
|
|
50
|
+
docx: {
|
|
51
|
+
workerUrl: '/file-viewer/vendor/docx/docx.worker.js',
|
|
52
|
+
workerJsZipUrl: '/file-viewer/vendor/docx/jszip.min.js',
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 迁移说明
|
|
58
|
+
|
|
59
|
+
`@file-viewer/core` 已不再直接依赖 `@file-viewer/docx`、`msdoc-viewer`、`rtf.js`、`linkedom` 或 `@xmldom/xmldom`。需要 Word 完整预览时,请安装本包并通过 `renderers` 传入,或使用 `@file-viewer/preset-all`。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type FileRenderHandler, type FileViewerRenderedInstance, type FileViewerRendererPlugin, type RendererDefinition } from '@file-viewer/core';
|
|
2
|
+
export declare const wordRendererDefinitions: RendererDefinition[];
|
|
3
|
+
export declare const renderFileViewerWordDocx: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
4
|
+
export declare const renderFileViewerWordDoc: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
5
|
+
export declare const renderFileViewerOpenDocument: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
6
|
+
export declare const wordRenderer: FileViewerRendererPlugin<FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>>;
|
|
7
|
+
export default wordRenderer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
|
+
const wordRendererIds = [
|
|
3
|
+
'office-word-openxml',
|
|
4
|
+
'office-word-binary',
|
|
5
|
+
'open-document',
|
|
6
|
+
];
|
|
7
|
+
const wordDefinitions = DEFAULT_RENDERER_DEFINITIONS.filter(definition => wordRendererIds.includes(definition.id));
|
|
8
|
+
if (wordDefinitions.length !== wordRendererIds.length) {
|
|
9
|
+
throw new Error('@file-viewer/renderer-word could not locate the shared Word renderer definitions.');
|
|
10
|
+
}
|
|
11
|
+
export const wordRendererDefinitions = wordDefinitions;
|
|
12
|
+
export const renderFileViewerWordDocx = (buffer, target, _type, context) => import('./wordDocx.js').then(({ default: renderWordDocx }) => renderWordDocx(buffer, target, context));
|
|
13
|
+
export const renderFileViewerWordDoc = (buffer, target, _type, context) => import('./wordDoc.js').then(({ default: renderWordDoc }) => renderWordDoc(buffer, target, context));
|
|
14
|
+
export const renderFileViewerOpenDocument = (buffer, target, type) => import('./openDocument.js').then(({ default: renderOpenDocument }) => renderOpenDocument(buffer, target, type));
|
|
15
|
+
export const wordRenderer = {
|
|
16
|
+
id: 'file-viewer-renderer-word',
|
|
17
|
+
label: 'Flyfish File Viewer Word renderer',
|
|
18
|
+
definitions: wordRendererDefinitions,
|
|
19
|
+
handlers: [
|
|
20
|
+
{
|
|
21
|
+
rendererId: 'office-word-openxml',
|
|
22
|
+
handler: renderFileViewerWordDocx,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
rendererId: 'office-word-binary',
|
|
26
|
+
handler: renderFileViewerWordDoc,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
rendererId: 'open-document',
|
|
30
|
+
handler: renderFileViewerOpenDocument,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
export default wordRenderer;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import JSZip from 'jszip';
|
|
2
|
+
const openDocumentStyle = `
|
|
3
|
+
.odf-viewer{min-height:100%;padding:28px;overflow:auto;background:#dfe5eb;box-sizing:border-box}
|
|
4
|
+
.odf-shell{width:min(100%,980px);margin:0 auto}.odf-shell>header{margin-bottom:18px;padding:18px 22px;border-radius:8px;background:#fff;box-shadow:0 10px 26px rgba(15,23,42,.1);box-sizing:border-box}
|
|
5
|
+
.odf-shell>header span{color:#0f766e;font-size:12px;font-weight:800}.odf-shell>header h2{margin:6px 0 0;color:#132235;font-size:24px}
|
|
6
|
+
.odf-page{min-height:360px;margin:0 auto 18px;padding:42px 48px;border-radius:4px;background:#fff;box-shadow:0 16px 38px rgba(15,23,42,.12);color:#1f2937;box-sizing:border-box}
|
|
7
|
+
.odf-page h3{margin:0 0 20px;color:#334155;font-size:18px}.odf-page p{margin:0 0 12px;font-size:15px;line-height:1.85;white-space:pre-wrap}
|
|
8
|
+
.flyfish-rtf-viewer{min-height:100%;padding:28px;overflow:auto;background:#dfe5eb;color:#1f2937;box-sizing:border-box}
|
|
9
|
+
.flyfish-rtf-header{width:min(100%,900px);margin:0 auto 18px;padding:18px 22px;border-radius:8px;background:#fff;box-shadow:0 10px 26px rgba(15,23,42,.1);box-sizing:border-box}
|
|
10
|
+
.flyfish-rtf-header span{display:block;color:#0f766e;font-size:12px;font-weight:800}.flyfish-rtf-header strong{display:block;margin-top:6px;color:#132235;font-size:24px}
|
|
11
|
+
.flyfish-rtf-paper{width:min(100%,900px);min-height:980px;margin:0 auto;padding:54px 62px;background:#fff;box-shadow:0 16px 38px rgba(15,23,42,.12);line-height:1.75;box-sizing:border-box}.flyfish-rtf-paper p{margin:0 0 12px}
|
|
12
|
+
.file-viewer[data-viewer-theme='dark'] .odf-viewer,.file-viewer[data-viewer-theme='dark'] .flyfish-rtf-viewer{background:#111827}.file-viewer[data-viewer-theme='dark'] .odf-shell>header,.file-viewer[data-viewer-theme='dark'] .odf-page,.file-viewer[data-viewer-theme='dark'] .flyfish-rtf-header,.file-viewer[data-viewer-theme='dark'] .flyfish-rtf-paper{background:#f8fafc;color:#1f2937}
|
|
13
|
+
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .odf-viewer,.file-viewer[data-viewer-theme='system'] .flyfish-rtf-viewer{background:#111827}.file-viewer[data-viewer-theme='system'] .odf-shell>header,.file-viewer[data-viewer-theme='system'] .odf-page,.file-viewer[data-viewer-theme='system'] .flyfish-rtf-header,.file-viewer[data-viewer-theme='system'] .flyfish-rtf-paper{background:#f8fafc;color:#1f2937}}
|
|
14
|
+
@media (max-width:720px){.odf-viewer,.flyfish-rtf-viewer{padding:14px}.odf-page{padding:28px 24px}.flyfish-rtf-paper{padding:36px 28px}}
|
|
15
|
+
`;
|
|
16
|
+
const createStyle = () => {
|
|
17
|
+
const style = document.createElement('style');
|
|
18
|
+
style.textContent = openDocumentStyle;
|
|
19
|
+
return style;
|
|
20
|
+
};
|
|
21
|
+
const appendText = (parent, tag, text, className) => {
|
|
22
|
+
const element = document.createElement(tag);
|
|
23
|
+
if (className) {
|
|
24
|
+
element.className = className;
|
|
25
|
+
}
|
|
26
|
+
element.textContent = text;
|
|
27
|
+
parent.appendChild(element);
|
|
28
|
+
return element;
|
|
29
|
+
};
|
|
30
|
+
const nodeText = (node) => {
|
|
31
|
+
return (node.textContent || '').replace(/\s+/g, ' ').trim();
|
|
32
|
+
};
|
|
33
|
+
const parseOdf = async (buffer, type) => {
|
|
34
|
+
var _a;
|
|
35
|
+
const zip = await JSZip.loadAsync(buffer);
|
|
36
|
+
const content = await ((_a = zip.file('content.xml')) === null || _a === void 0 ? void 0 : _a.async('text'));
|
|
37
|
+
if (!content) {
|
|
38
|
+
throw new Error('ODF 文件缺少 content.xml');
|
|
39
|
+
}
|
|
40
|
+
const doc = new DOMParser().parseFromString(content, 'application/xml');
|
|
41
|
+
const parseError = doc.querySelector('parsererror');
|
|
42
|
+
if (parseError) {
|
|
43
|
+
throw new Error(parseError.textContent || 'ODF XML 解析失败');
|
|
44
|
+
}
|
|
45
|
+
if (type === 'odp') {
|
|
46
|
+
const slides = Array.from(doc.getElementsByTagName('draw:page'));
|
|
47
|
+
return slides.map((slide, index) => {
|
|
48
|
+
const blocks = Array.from(slide.getElementsByTagName('text:p'))
|
|
49
|
+
.map(nodeText)
|
|
50
|
+
.filter(Boolean);
|
|
51
|
+
return { title: `第 ${index + 1} 页`, blocks: blocks.length ? blocks : ['该页没有可提取文本'] };
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const blocks = [
|
|
55
|
+
...Array.from(doc.getElementsByTagName('text:h')).map(nodeText),
|
|
56
|
+
...Array.from(doc.getElementsByTagName('text:p')).map(nodeText),
|
|
57
|
+
].filter(Boolean);
|
|
58
|
+
return [{ title: '正文', blocks: blocks.length ? blocks : ['没有提取到可读文本'] }];
|
|
59
|
+
};
|
|
60
|
+
const renderOdfPages = (type, title, pages) => {
|
|
61
|
+
const root = document.createElement('div');
|
|
62
|
+
root.className = 'odf-viewer';
|
|
63
|
+
const shell = document.createElement('section');
|
|
64
|
+
shell.className = 'odf-shell';
|
|
65
|
+
const header = document.createElement('header');
|
|
66
|
+
appendText(header, 'span', type.toUpperCase());
|
|
67
|
+
appendText(header, 'h2', title);
|
|
68
|
+
shell.appendChild(header);
|
|
69
|
+
pages.forEach(page => {
|
|
70
|
+
const article = document.createElement('article');
|
|
71
|
+
article.className = 'odf-page';
|
|
72
|
+
appendText(article, 'h3', page.title);
|
|
73
|
+
page.blocks.forEach(block => appendText(article, 'p', block));
|
|
74
|
+
shell.appendChild(article);
|
|
75
|
+
});
|
|
76
|
+
root.appendChild(shell);
|
|
77
|
+
return root;
|
|
78
|
+
};
|
|
79
|
+
const resolveRtfJs = async () => {
|
|
80
|
+
const rtfModule = await import('rtf.js/dist/RTFJS.bundle.js');
|
|
81
|
+
return rtfModule.RTFJS || rtfModule.default || rtfModule;
|
|
82
|
+
};
|
|
83
|
+
const renderRtf = async (buffer, target) => {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
const RTFJS = await resolveRtfJs();
|
|
86
|
+
(_a = RTFJS.loggingEnabled) === null || _a === void 0 ? void 0 : _a.call(RTFJS, false);
|
|
87
|
+
const doc = new RTFJS.Document(buffer, {});
|
|
88
|
+
const meta = ((_b = doc.metadata) === null || _b === void 0 ? void 0 : _b.call(doc)) || {};
|
|
89
|
+
const elements = await doc.render();
|
|
90
|
+
const stage = document.createElement('div');
|
|
91
|
+
stage.className = 'flyfish-rtf-viewer';
|
|
92
|
+
const header = document.createElement('div');
|
|
93
|
+
header.className = 'flyfish-rtf-header';
|
|
94
|
+
appendText(header, 'span', 'RTF');
|
|
95
|
+
appendText(header, 'strong', meta.title || 'RTF 文档预览');
|
|
96
|
+
const paper = document.createElement('article');
|
|
97
|
+
paper.className = 'flyfish-rtf-paper';
|
|
98
|
+
elements.forEach((element) => paper.appendChild(element));
|
|
99
|
+
stage.append(header, paper);
|
|
100
|
+
target.replaceChildren(createStyle(), stage);
|
|
101
|
+
return {
|
|
102
|
+
$el: target,
|
|
103
|
+
unmount() {
|
|
104
|
+
target.replaceChildren();
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
export default async function renderOpenDocument(buffer, target, type) {
|
|
109
|
+
const normalizedType = (type || 'odt').toLowerCase();
|
|
110
|
+
if (normalizedType === 'rtf') {
|
|
111
|
+
return renderRtf(buffer, target);
|
|
112
|
+
}
|
|
113
|
+
const pages = await parseOdf(buffer, normalizedType);
|
|
114
|
+
const title = normalizedType === 'odp' ? 'OpenDocument 演示文稿预览' : 'OpenDocument 文档预览';
|
|
115
|
+
target.replaceChildren(createStyle(), renderOdfPages(normalizedType, title, pages));
|
|
116
|
+
return {
|
|
117
|
+
$el: target,
|
|
118
|
+
unmount() {
|
|
119
|
+
target.replaceChildren();
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|