@file-viewer/renderer-typst 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 +48 -0
- package/README.md +48 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +17 -0
- package/dist/typst.d.ts +8 -0
- package/dist/typst.js +544 -0
- package/package.json +69 -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,48 @@
|
|
|
1
|
+
# @file-viewer/renderer-typst
|
|
2
|
+
|
|
3
|
+
Standalone Typst renderer package for Flyfish File Viewer. It reads `.typ` / `.typst` source files directly and uses the browser WASM compiler / renderer from `@myriaddreamin/typst.ts` to produce paged SVG output, so source-code fallback is never treated as a successful preview.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import FileViewer from '@file-viewer/vue3'
|
|
9
|
+
import { typstRenderer } from '@file-viewer/renderer-typst'
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
rendererMode: 'replace',
|
|
13
|
+
renderers: typstRenderer,
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can also compose it with other renderer packages:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
21
|
+
import { typstRenderer } from '@file-viewer/renderer-typst'
|
|
22
|
+
|
|
23
|
+
const options = {
|
|
24
|
+
rendererMode: 'replace',
|
|
25
|
+
renderers: [pdfRenderer, typstRenderer],
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Capabilities
|
|
30
|
+
|
|
31
|
+
- Supports `.typ` and `.typst`.
|
|
32
|
+
- Compiles Typst source in the browser and renders SVG pages.
|
|
33
|
+
- Preserves page size metadata for preview, zoom, print, and HTML export.
|
|
34
|
+
- Supports `options.typst.renderTimeoutMs` for long-running documents.
|
|
35
|
+
- Reports clear diagnostics for missing WASM assets, wrong MIME types, network failures, and Typst compile errors.
|
|
36
|
+
|
|
37
|
+
## Offline Assets
|
|
38
|
+
|
|
39
|
+
The default asset paths are:
|
|
40
|
+
|
|
41
|
+
- `wasm/typst/typst_ts_web_compiler_bg.wasm`
|
|
42
|
+
- `wasm/typst/typst_ts_renderer_bg.wasm`
|
|
43
|
+
|
|
44
|
+
For private deployments, override them with `options.typst.compilerWasmUrl` and `options.typst.rendererWasmUrl`. The runtime does not depend on public CDNs.
|
|
45
|
+
|
|
46
|
+
## Migration Note
|
|
47
|
+
|
|
48
|
+
Typst rendering has moved out of `@file-viewer/core` into this package. Core only keeps the `renderFileViewerTypst()` compatibility export with a clear installation error, and no longer installs `@myriaddreamin/*` by default. Install this renderer explicitly, or use `@file-viewer/preset-all`, when real Typst preview is required.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @file-viewer/renderer-typst
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立 Typst renderer 包。它直接读取 `.typ` / `.typst` 源文件,并使用 `@myriaddreamin/typst.ts` 的浏览器 WASM compiler / renderer 输出按页 SVG,避免用源码视图冒充预览成功。
|
|
4
|
+
|
|
5
|
+
## 用法
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import FileViewer from '@file-viewer/vue3'
|
|
9
|
+
import { typstRenderer } from '@file-viewer/renderer-typst'
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
rendererMode: 'replace',
|
|
13
|
+
renderers: typstRenderer,
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
也可以和其他 renderer 一起组合:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
21
|
+
import { typstRenderer } from '@file-viewer/renderer-typst'
|
|
22
|
+
|
|
23
|
+
const options = {
|
|
24
|
+
rendererMode: 'replace',
|
|
25
|
+
renderers: [pdfRenderer, typstRenderer],
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 能力边界
|
|
30
|
+
|
|
31
|
+
- 支持 `.typ`、`.typst`。
|
|
32
|
+
- 浏览器端真实编译 Typst 源文件并输出 SVG 页面。
|
|
33
|
+
- 支持页面尺寸识别、缩放、打印和 HTML 导出。
|
|
34
|
+
- 支持 `options.typst.renderTimeoutMs` 编译超时控制。
|
|
35
|
+
- WASM 缺失、MIME 错误、网络错误或编译错误会给出明确诊断。
|
|
36
|
+
|
|
37
|
+
## 离线资产
|
|
38
|
+
|
|
39
|
+
默认会从 viewer assets 下读取:
|
|
40
|
+
|
|
41
|
+
- `wasm/typst/typst_ts_web_compiler_bg.wasm`
|
|
42
|
+
- `wasm/typst/typst_ts_renderer_bg.wasm`
|
|
43
|
+
|
|
44
|
+
私有化部署时可以通过 `options.typst.compilerWasmUrl` 和 `options.typst.rendererWasmUrl` 覆盖。预览运行时不会访问公共 CDN。
|
|
45
|
+
|
|
46
|
+
## 迁移说明
|
|
47
|
+
|
|
48
|
+
Typst 渲染已经从 `@file-viewer/core` 迁移到本包。core 只保留 `renderFileViewerTypst()` 兼容导出并给出明确安装提示,不再默认安装 `@myriaddreamin/*`。需要 Typst 真实预览时,请显式安装本包,或直接使用 `@file-viewer/preset-all`。
|
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 typstRendererDefinition: RendererDefinition;
|
|
3
|
+
export declare const renderFileViewerTypst: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
4
|
+
export declare const typstRenderer: FileViewerRendererPlugin<FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>>;
|
|
5
|
+
export default typstRenderer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
|
+
const typstDefinition = DEFAULT_RENDERER_DEFINITIONS.find(definition => definition.id === 'typst');
|
|
3
|
+
if (!typstDefinition) {
|
|
4
|
+
throw new Error('@file-viewer/renderer-typst could not locate the core Typst renderer definition.');
|
|
5
|
+
}
|
|
6
|
+
export const typstRendererDefinition = typstDefinition;
|
|
7
|
+
export const renderFileViewerTypst = (buffer, target, type, context) => import('./typst.js').then(({ default: renderTypst }) => renderTypst(buffer, target, type, context));
|
|
8
|
+
export const typstRenderer = {
|
|
9
|
+
id: 'file-viewer-renderer-typst',
|
|
10
|
+
label: 'Flyfish File Viewer Typst renderer',
|
|
11
|
+
definitions: [typstRendererDefinition],
|
|
12
|
+
handlers: [{
|
|
13
|
+
rendererId: typstRendererDefinition.id,
|
|
14
|
+
handler: renderFileViewerTypst,
|
|
15
|
+
}],
|
|
16
|
+
};
|
|
17
|
+
export default typstRenderer;
|
package/dist/typst.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FileRenderContext, type FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
__FLYFISH_TYPST_COMPILER_WASM_URL__?: string;
|
|
5
|
+
__FLYFISH_TYPST_RENDERER_WASM_URL__?: string;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export default function renderTypst(buffer: ArrayBuffer, target: HTMLDivElement, _type?: string, context?: FileRenderContext): Promise<FileViewerRenderedInstance>;
|
package/dist/typst.js
ADDED
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
import { $typst } from '@myriaddreamin/typst.ts';
|
|
2
|
+
import { resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstRendererWasmUrl, } from '@file-viewer/core/assets';
|
|
3
|
+
import { createFileViewerZoomChangeEmitter, formatCssPixels, registerFileViewerZoomProvider, readFileViewerText, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
4
|
+
const typstStyle = `
|
|
5
|
+
.typst-viewer{min-height:100%;overflow:auto;background:#eef1f4;color:#172033}
|
|
6
|
+
.typst-toolbar{position:sticky;top:0;z-index:2;display:flex;min-height:52px;align-items:center;justify-content:space-between;gap:16px;padding:10px 18px;border-bottom:1px solid rgba(120,134,155,.18);background:rgba(248,250,252,.92);backdrop-filter:blur(16px)}
|
|
7
|
+
.typst-toolbar div{min-width:0}
|
|
8
|
+
.typst-toolbar strong,.typst-toolbar span{display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
9
|
+
.typst-toolbar strong{color:#172033;font-size:14px;font-weight:800}
|
|
10
|
+
.typst-toolbar span,.typst-toolbar em{color:#6a778b;font-size:12px;font-style:normal;font-weight:700}
|
|
11
|
+
.typst-pages{display:flex;min-height:calc(100% - 52px);flex-direction:column;align-items:center;gap:22px;box-sizing:border-box;padding:28px 16px 44px}
|
|
12
|
+
.typst-page-shell{max-width:100%;overflow:hidden;border:1px solid rgba(20,35,53,.1);background:#fff;box-shadow:0 18px 44px rgba(15,23,42,.14)}
|
|
13
|
+
.typst-page-content svg{display:block;width:100%;height:auto}
|
|
14
|
+
.typst-loading,.typst-error{width:min(520px,calc(100% - 32px));box-sizing:border-box;margin:80px auto;padding:26px;border:1px solid rgba(120,134,155,.18);border-radius:14px;background:#fff;box-shadow:0 18px 44px rgba(15,23,42,.12)}
|
|
15
|
+
.typst-loading{display:grid;justify-items:center;gap:10px;text-align:center}
|
|
16
|
+
.typst-loading span{width:34px;height:34px;border:3px solid rgba(46,130,94,.18);border-top-color:#239661;border-radius:999px;animation:typst-spin .8s linear infinite}
|
|
17
|
+
.typst-loading strong,.typst-error strong{color:#172033;font-size:16px}
|
|
18
|
+
.typst-loading p{margin:0;color:#6a778b;font-size:13px}
|
|
19
|
+
.typst-error{color:#9f1d1d}
|
|
20
|
+
.typst-error pre{max-height:360px;margin:14px 0 0;overflow:auto;border-radius:10px;background:#fff1f2;color:#9f1d1d;font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,'Liberation Mono',monospace;font-size:12px;line-height:1.7;padding:14px;white-space:pre-wrap}
|
|
21
|
+
.file-viewer[data-viewer-theme='dark'] .typst-viewer{background:#101820;color:#e6edf3}
|
|
22
|
+
.file-viewer[data-viewer-theme='dark'] .typst-toolbar{border-bottom-color:rgba(139,148,158,.22);background:rgba(15,23,42,.9)}
|
|
23
|
+
.file-viewer[data-viewer-theme='dark'] .typst-toolbar strong{color:#f8fafc}
|
|
24
|
+
.file-viewer[data-viewer-theme='dark'] .typst-toolbar span,.file-viewer[data-viewer-theme='dark'] .typst-toolbar em{color:#9aa7b8}
|
|
25
|
+
.file-viewer[data-viewer-theme='dark'] .typst-page-shell{border-color:rgba(139,148,158,.26);box-shadow:0 24px 56px rgba(0,0,0,.38)}
|
|
26
|
+
.file-viewer[data-viewer-theme='dark'] .typst-loading,.file-viewer[data-viewer-theme='dark'] .typst-error{border-color:rgba(139,148,158,.22);background:#151b23;box-shadow:0 24px 56px rgba(0,0,0,.32)}
|
|
27
|
+
.file-viewer[data-viewer-theme='dark'] .typst-loading strong,.file-viewer[data-viewer-theme='dark'] .typst-error strong{color:#f8fafc}
|
|
28
|
+
@keyframes typst-spin{to{transform:rotate(360deg)}}
|
|
29
|
+
@media (max-width:767px){.typst-toolbar{align-items:flex-start;flex-direction:column;gap:4px}.typst-pages{gap:16px;padding:16px 10px 28px}}
|
|
30
|
+
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .typst-viewer{background:#101820;color:#e6edf3}.file-viewer[data-viewer-theme='system'] .typst-toolbar{border-bottom-color:rgba(139,148,158,.22);background:rgba(15,23,42,.9)}.file-viewer[data-viewer-theme='system'] .typst-toolbar strong{color:#f8fafc}.file-viewer[data-viewer-theme='system'] .typst-toolbar span,.file-viewer[data-viewer-theme='system'] .typst-toolbar em{color:#9aa7b8}.file-viewer[data-viewer-theme='system'] .typst-page-shell{border-color:rgba(139,148,158,.26);box-shadow:0 24px 56px rgba(0,0,0,.38)}.file-viewer[data-viewer-theme='system'] .typst-loading,.file-viewer[data-viewer-theme='system'] .typst-error{border-color:rgba(139,148,158,.22);background:#151b23;box-shadow:0 24px 56px rgba(0,0,0,.32)}.file-viewer[data-viewer-theme='system'] .typst-loading strong,.file-viewer[data-viewer-theme='system'] .typst-error strong{color:#f8fafc}}
|
|
31
|
+
`;
|
|
32
|
+
let typstEngineConfigKey = '';
|
|
33
|
+
const DEFAULT_TYPST_RENDER_TIMEOUT_MS = 60000;
|
|
34
|
+
class TypstRenderTimeoutError extends Error {
|
|
35
|
+
constructor(timeoutMs) {
|
|
36
|
+
super(`Typst 编译超过 ${Math.round(timeoutMs / 1000)} 秒`);
|
|
37
|
+
this.name = 'TypstRenderTimeoutError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const createStyle = (documentRef) => {
|
|
41
|
+
const style = documentRef.createElement('style');
|
|
42
|
+
style.textContent = typstStyle;
|
|
43
|
+
return style;
|
|
44
|
+
};
|
|
45
|
+
const createElement = (documentRef, tagName, className, text) => {
|
|
46
|
+
const element = documentRef.createElement(tagName);
|
|
47
|
+
if (className) {
|
|
48
|
+
element.className = className;
|
|
49
|
+
}
|
|
50
|
+
if (text !== undefined) {
|
|
51
|
+
element.textContent = text;
|
|
52
|
+
}
|
|
53
|
+
return element;
|
|
54
|
+
};
|
|
55
|
+
const getWindowOverride = (key) => {
|
|
56
|
+
if (typeof window === 'undefined') {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return window[key];
|
|
60
|
+
};
|
|
61
|
+
const configureTypstEngine = (compilerWasmUrl, rendererWasmUrl) => {
|
|
62
|
+
const configKey = `${compilerWasmUrl}\n${rendererWasmUrl}`;
|
|
63
|
+
if (typstEngineConfigKey === configKey) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
$typst.setCompilerInitOptions({
|
|
67
|
+
getModule: () => compilerWasmUrl,
|
|
68
|
+
});
|
|
69
|
+
$typst.setRendererInitOptions({
|
|
70
|
+
getModule: () => rendererWasmUrl,
|
|
71
|
+
});
|
|
72
|
+
typstEngineConfigKey = configKey;
|
|
73
|
+
};
|
|
74
|
+
const pushUniqueTypstCandidate = (candidates, candidate) => {
|
|
75
|
+
if (candidates.some(item => item.compilerWasmUrl === candidate.compilerWasmUrl &&
|
|
76
|
+
item.rendererWasmUrl === candidate.rendererWasmUrl)) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
candidates.push(candidate);
|
|
80
|
+
};
|
|
81
|
+
const resolveTypstEngineCandidates = (context, documentBaseUrl) => {
|
|
82
|
+
var _a;
|
|
83
|
+
const typstOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst;
|
|
84
|
+
const compilerOverride = getWindowOverride('__FLYFISH_TYPST_COMPILER_WASM_URL__');
|
|
85
|
+
const rendererOverride = getWindowOverride('__FLYFISH_TYPST_RENDERER_WASM_URL__');
|
|
86
|
+
const compilerWasmUrl = resolveFileViewerTypstCompilerWasmUrl(typstOptions, [
|
|
87
|
+
compilerOverride,
|
|
88
|
+
], documentBaseUrl);
|
|
89
|
+
const rendererWasmUrl = resolveFileViewerTypstRendererWasmUrl(typstOptions, [
|
|
90
|
+
rendererOverride,
|
|
91
|
+
], documentBaseUrl);
|
|
92
|
+
const hasConfiguredAsset = Boolean((typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.compilerWasmUrl) ||
|
|
93
|
+
(typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.rendererWasmUrl) ||
|
|
94
|
+
compilerOverride ||
|
|
95
|
+
rendererOverride);
|
|
96
|
+
const candidates = [];
|
|
97
|
+
pushUniqueTypstCandidate(candidates, {
|
|
98
|
+
compilerWasmUrl,
|
|
99
|
+
rendererWasmUrl,
|
|
100
|
+
source: hasConfiguredAsset ? 'configured' : 'local',
|
|
101
|
+
preflight: !hasConfiguredAsset,
|
|
102
|
+
});
|
|
103
|
+
return candidates;
|
|
104
|
+
};
|
|
105
|
+
const isHttpUrl = (url) => /^https?:\/\//i.test(url);
|
|
106
|
+
const isKnownMissingWasmUrl = async (url) => {
|
|
107
|
+
if (typeof fetch !== 'function' || !isHttpUrl(url)) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const response = await fetch(url, {
|
|
112
|
+
cache: 'force-cache',
|
|
113
|
+
method: 'HEAD',
|
|
114
|
+
});
|
|
115
|
+
return response.status === 404 || response.status === 410;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const resolveKnownMissingTypstAsset = async (candidate) => {
|
|
122
|
+
if (await isKnownMissingWasmUrl(candidate.compilerWasmUrl)) {
|
|
123
|
+
return `Typst compiler WASM missing: ${candidate.compilerWasmUrl}`;
|
|
124
|
+
}
|
|
125
|
+
if (await isKnownMissingWasmUrl(candidate.rendererWasmUrl)) {
|
|
126
|
+
return `Typst renderer WASM missing: ${candidate.rendererWasmUrl}`;
|
|
127
|
+
}
|
|
128
|
+
return '';
|
|
129
|
+
};
|
|
130
|
+
const isTypstAssetLoadError = (error) => {
|
|
131
|
+
if (Array.isArray(error)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
135
|
+
return /wasm|webassembly|fetch|module|instantiate|compile|network|404|410/i.test(message);
|
|
136
|
+
};
|
|
137
|
+
const escapeAttribute = (value) => {
|
|
138
|
+
return value.replace(/[&<>"']/g, char => {
|
|
139
|
+
const entities = {
|
|
140
|
+
'&': '&',
|
|
141
|
+
'<': '<',
|
|
142
|
+
'>': '>',
|
|
143
|
+
'"': '"',
|
|
144
|
+
"'": ''',
|
|
145
|
+
};
|
|
146
|
+
return entities[char] || char;
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
const readNumberAttribute = (element, name) => {
|
|
150
|
+
const value = Number.parseFloat(element.getAttribute(name) || '');
|
|
151
|
+
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
152
|
+
};
|
|
153
|
+
const removeUnsafeSvgContent = (root) => {
|
|
154
|
+
root.querySelectorAll('script').forEach(script => script.remove());
|
|
155
|
+
root.querySelectorAll('*').forEach(element => {
|
|
156
|
+
Array.from(element.attributes).forEach(attribute => {
|
|
157
|
+
const name = attribute.name.toLowerCase();
|
|
158
|
+
const value = attribute.value.trim().toLowerCase();
|
|
159
|
+
if (name.startsWith('on') || value.startsWith('javascript:')) {
|
|
160
|
+
element.removeAttribute(attribute.name);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
const serializeNode = (node) => {
|
|
166
|
+
return new XMLSerializer().serializeToString(node);
|
|
167
|
+
};
|
|
168
|
+
const parseTypstSvgPages = (svgText) => {
|
|
169
|
+
const parser = new DOMParser();
|
|
170
|
+
const documentSvg = parser.parseFromString(svgText, 'image/svg+xml');
|
|
171
|
+
const parseError = documentSvg.querySelector('parsererror');
|
|
172
|
+
if (parseError) {
|
|
173
|
+
throw new Error(parseError.textContent || 'Typst SVG 解析失败');
|
|
174
|
+
}
|
|
175
|
+
removeUnsafeSvgContent(documentSvg);
|
|
176
|
+
const root = documentSvg.documentElement;
|
|
177
|
+
const sharedNodes = Array.from(root.children)
|
|
178
|
+
.filter(child => ['style', 'defs'].includes(child.tagName.toLowerCase()))
|
|
179
|
+
.map(serializeNode)
|
|
180
|
+
.join('');
|
|
181
|
+
const pageGroups = Array.from(root.querySelectorAll('g.typst-page'));
|
|
182
|
+
const fallbackWidth = readNumberAttribute(root, 'data-width') ||
|
|
183
|
+
readNumberAttribute(root, 'width') ||
|
|
184
|
+
596;
|
|
185
|
+
const fallbackHeight = readNumberAttribute(root, 'data-height') ||
|
|
186
|
+
readNumberAttribute(root, 'height') ||
|
|
187
|
+
842;
|
|
188
|
+
if (!pageGroups.length) {
|
|
189
|
+
return [{
|
|
190
|
+
index: 1,
|
|
191
|
+
width: fallbackWidth,
|
|
192
|
+
height: fallbackHeight,
|
|
193
|
+
svg: svgText,
|
|
194
|
+
}];
|
|
195
|
+
}
|
|
196
|
+
return pageGroups.map((group, index) => {
|
|
197
|
+
const pageWidth = readNumberAttribute(group, 'data-page-width') || fallbackWidth;
|
|
198
|
+
const pageHeight = readNumberAttribute(group, 'data-page-height') || fallbackHeight;
|
|
199
|
+
const pageClone = group.cloneNode(true);
|
|
200
|
+
pageClone.setAttribute('transform', 'translate(0, 0)');
|
|
201
|
+
const pageSvg = [
|
|
202
|
+
`<svg style="overflow:visible;" class="typst-doc" viewBox="0 0 ${pageWidth} ${pageHeight}" width="${pageWidth}" height="${pageHeight}" data-width="${pageWidth}" data-height="${pageHeight}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">`,
|
|
203
|
+
sharedNodes,
|
|
204
|
+
serializeNode(pageClone),
|
|
205
|
+
'</svg>',
|
|
206
|
+
].join('');
|
|
207
|
+
return {
|
|
208
|
+
index: index + 1,
|
|
209
|
+
width: pageWidth,
|
|
210
|
+
height: pageHeight,
|
|
211
|
+
svg: pageSvg,
|
|
212
|
+
};
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
const formatTypstError = (error) => {
|
|
216
|
+
if (Array.isArray(error)) {
|
|
217
|
+
return error.map(item => {
|
|
218
|
+
if (item && typeof item === 'object' && 'message' in item) {
|
|
219
|
+
const severity = 'severity' in item ? String(item.severity) : 'Error';
|
|
220
|
+
return `${severity}: ${String(item.message)}`;
|
|
221
|
+
}
|
|
222
|
+
return String(item);
|
|
223
|
+
}).join('\n');
|
|
224
|
+
}
|
|
225
|
+
if (error instanceof Error) {
|
|
226
|
+
return error.message;
|
|
227
|
+
}
|
|
228
|
+
return String(error);
|
|
229
|
+
};
|
|
230
|
+
const formatTypstRuntimeError = (error) => {
|
|
231
|
+
const message = formatTypstError(error);
|
|
232
|
+
if (error instanceof TypstRenderTimeoutError) {
|
|
233
|
+
return [
|
|
234
|
+
message,
|
|
235
|
+
'请检查 Typst 源文件复杂度,或通过 options.typst.renderTimeoutMs 调大浏览器端编译超时。'
|
|
236
|
+
].join('\n\n');
|
|
237
|
+
}
|
|
238
|
+
if (isTypstAssetLoadError(error)) {
|
|
239
|
+
return [
|
|
240
|
+
message,
|
|
241
|
+
'Typst 需要本地 compiler / renderer WASM。请运行 file-viewer-copy-assets,或配置 options.typst.compilerWasmUrl / options.typst.rendererWasmUrl,并确认服务器以 application/wasm 返回资源。'
|
|
242
|
+
].join('\n\n');
|
|
243
|
+
}
|
|
244
|
+
return message;
|
|
245
|
+
};
|
|
246
|
+
const clampZoom = (value) => {
|
|
247
|
+
return Math.min(3, Math.max(0.3, Number(value.toFixed(2))));
|
|
248
|
+
};
|
|
249
|
+
const normalizeRenderTimeoutMs = (timeoutMs) => {
|
|
250
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs === undefined) {
|
|
251
|
+
return DEFAULT_TYPST_RENDER_TIMEOUT_MS;
|
|
252
|
+
}
|
|
253
|
+
return Math.max(0, timeoutMs);
|
|
254
|
+
};
|
|
255
|
+
const withRenderTimeout = async (promise, timeoutMs) => {
|
|
256
|
+
if (timeoutMs <= 0) {
|
|
257
|
+
return promise;
|
|
258
|
+
}
|
|
259
|
+
let timeoutId;
|
|
260
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
261
|
+
timeoutId = setTimeout(() => reject(new TypstRenderTimeoutError(timeoutMs)), timeoutMs);
|
|
262
|
+
});
|
|
263
|
+
try {
|
|
264
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
if (timeoutId) {
|
|
268
|
+
clearTimeout(timeoutId);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
const buildExportStyles = () => `
|
|
273
|
+
<style>
|
|
274
|
+
.typst-export-document {
|
|
275
|
+
box-sizing: border-box;
|
|
276
|
+
display: flex;
|
|
277
|
+
flex-direction: column;
|
|
278
|
+
align-items: center;
|
|
279
|
+
gap: 18px;
|
|
280
|
+
margin: 0;
|
|
281
|
+
padding: 24px;
|
|
282
|
+
background: #eef1f4;
|
|
283
|
+
}
|
|
284
|
+
.typst-export-page {
|
|
285
|
+
box-sizing: border-box;
|
|
286
|
+
flex: 0 0 auto;
|
|
287
|
+
overflow: hidden;
|
|
288
|
+
background: #ffffff;
|
|
289
|
+
box-shadow: 0 18px 42px rgba(15, 23, 42, 0.14);
|
|
290
|
+
}
|
|
291
|
+
.typst-export-page svg {
|
|
292
|
+
display: block;
|
|
293
|
+
width: 100%;
|
|
294
|
+
height: auto;
|
|
295
|
+
}
|
|
296
|
+
</style>
|
|
297
|
+
`;
|
|
298
|
+
const buildExportHtml = (pages, filename) => {
|
|
299
|
+
return `${buildExportStyles()}<main class="typst-export-document" aria-label="${escapeAttribute(filename || 'Typst document')}">${pages.map(page => {
|
|
300
|
+
const width = formatCssPixels(page.width);
|
|
301
|
+
const height = formatCssPixels(page.height);
|
|
302
|
+
return `<section class="typst-export-page viewer-print-page" style="--viewer-print-page-width:${width};--viewer-print-page-height:${height};width:${width};height:${height};" aria-label="Page ${page.index}">${page.svg}</section>`;
|
|
303
|
+
}).join('')}</main>`;
|
|
304
|
+
};
|
|
305
|
+
const buildPrintStyle = (pages) => {
|
|
306
|
+
const firstPage = pages[0];
|
|
307
|
+
const width = firstPage ? formatCssPixels(firstPage.width) : '596px';
|
|
308
|
+
const height = firstPage ? formatCssPixels(firstPage.height) : '842px';
|
|
309
|
+
return `
|
|
310
|
+
@page { size: ${width} ${height}; margin: 0; }
|
|
311
|
+
@media print {
|
|
312
|
+
html,
|
|
313
|
+
body {
|
|
314
|
+
width: ${width};
|
|
315
|
+
min-width: ${width};
|
|
316
|
+
margin: 0 !important;
|
|
317
|
+
background: #ffffff !important;
|
|
318
|
+
}
|
|
319
|
+
.typst-export-document {
|
|
320
|
+
display: block !important;
|
|
321
|
+
margin: 0 !important;
|
|
322
|
+
padding: 0 !important;
|
|
323
|
+
background: #ffffff !important;
|
|
324
|
+
}
|
|
325
|
+
.typst-export-page {
|
|
326
|
+
display: block !important;
|
|
327
|
+
margin: 0 !important;
|
|
328
|
+
border: 0 !important;
|
|
329
|
+
box-shadow: none !important;
|
|
330
|
+
break-after: page;
|
|
331
|
+
page-break-after: always;
|
|
332
|
+
}
|
|
333
|
+
.typst-export-page:last-child {
|
|
334
|
+
break-after: auto;
|
|
335
|
+
page-break-after: auto;
|
|
336
|
+
}
|
|
337
|
+
.typst-export-page svg {
|
|
338
|
+
width: 100% !important;
|
|
339
|
+
height: auto !important;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
`;
|
|
343
|
+
};
|
|
344
|
+
const buildExportAdapter = (pages, filename) => {
|
|
345
|
+
if (!pages.length) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
includeDocumentStyles: false,
|
|
350
|
+
print: true,
|
|
351
|
+
exportHtml: true,
|
|
352
|
+
printStyle: () => buildPrintStyle(pages),
|
|
353
|
+
toHtml: () => buildExportHtml(pages, filename),
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
const getPageSummary = (pages) => {
|
|
357
|
+
if (!pages.length) {
|
|
358
|
+
return '0 pages';
|
|
359
|
+
}
|
|
360
|
+
const firstPage = pages[0];
|
|
361
|
+
return `${pages.length} pages / ${Math.round(firstPage.width)} x ${Math.round(firstPage.height)} pt`;
|
|
362
|
+
};
|
|
363
|
+
export default async function renderTypst(buffer, target, _type, context) {
|
|
364
|
+
const source = await readFileViewerText(buffer);
|
|
365
|
+
const documentRef = target.ownerDocument || document;
|
|
366
|
+
const zoomEmitter = createFileViewerZoomChangeEmitter();
|
|
367
|
+
let state = 'loading';
|
|
368
|
+
let pages = [];
|
|
369
|
+
let errorMessage = '';
|
|
370
|
+
let zoom = 1;
|
|
371
|
+
let renderToken = 0;
|
|
372
|
+
let disposed = false;
|
|
373
|
+
const pageShells = new Map();
|
|
374
|
+
const root = createElement(documentRef, 'div', 'typst-viewer');
|
|
375
|
+
root.dataset.viewerZoomProvider = 'typst';
|
|
376
|
+
const toolbar = createElement(documentRef, 'header', 'typst-toolbar');
|
|
377
|
+
const titleGroup = createElement(documentRef, 'div');
|
|
378
|
+
const title = createElement(documentRef, 'strong', undefined, (context === null || context === void 0 ? void 0 : context.filename) || 'Typst document');
|
|
379
|
+
const summary = createElement(documentRef, 'span', undefined, 'Typst WASM renderer');
|
|
380
|
+
const status = createElement(documentRef, 'em', undefined, '正在编译');
|
|
381
|
+
const body = createElement(documentRef, 'div');
|
|
382
|
+
titleGroup.append(title, summary);
|
|
383
|
+
toolbar.append(titleGroup, status);
|
|
384
|
+
root.append(toolbar, body);
|
|
385
|
+
target.replaceChildren(createStyle(documentRef), root);
|
|
386
|
+
const getZoomState = () => ({
|
|
387
|
+
scale: zoom,
|
|
388
|
+
label: `${Math.round(zoom * 100)}%`,
|
|
389
|
+
canZoomIn: zoom < 3,
|
|
390
|
+
canZoomOut: zoom > 0.3,
|
|
391
|
+
canReset: zoom !== 1,
|
|
392
|
+
minScale: 0.3,
|
|
393
|
+
maxScale: 3,
|
|
394
|
+
});
|
|
395
|
+
const applyPageZoom = () => {
|
|
396
|
+
pages.forEach(page => {
|
|
397
|
+
const shell = pageShells.get(page.index);
|
|
398
|
+
if (!shell) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
shell.style.width = `${page.width * zoom}px`;
|
|
402
|
+
shell.style.maxWidth = '100%';
|
|
403
|
+
shell.style.height = 'auto';
|
|
404
|
+
});
|
|
405
|
+
};
|
|
406
|
+
const setZoom = (scale) => {
|
|
407
|
+
zoom = clampZoom(scale);
|
|
408
|
+
applyPageZoom();
|
|
409
|
+
zoomEmitter.emit();
|
|
410
|
+
return getZoomState();
|
|
411
|
+
};
|
|
412
|
+
registerFileViewerZoomProvider(root, {
|
|
413
|
+
zoomIn: () => setZoom(zoom + 0.1),
|
|
414
|
+
zoomOut: () => setZoom(zoom - 0.1),
|
|
415
|
+
resetZoom: () => setZoom(1),
|
|
416
|
+
setZoom,
|
|
417
|
+
getState: getZoomState,
|
|
418
|
+
subscribe: zoomEmitter.subscribe,
|
|
419
|
+
});
|
|
420
|
+
const registerExportAdapter = () => {
|
|
421
|
+
var _a;
|
|
422
|
+
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, buildExportAdapter(pages, context.filename));
|
|
423
|
+
};
|
|
424
|
+
const renderLoading = () => {
|
|
425
|
+
const loading = createElement(documentRef, 'div', 'typst-loading');
|
|
426
|
+
loading.setAttribute('role', 'status');
|
|
427
|
+
loading.append(createElement(documentRef, 'span'), createElement(documentRef, 'strong', undefined, '正在解析 Typst'), createElement(documentRef, 'p', undefined, '加载编译器并生成页面预览...'));
|
|
428
|
+
body.replaceChildren(loading);
|
|
429
|
+
};
|
|
430
|
+
const renderError = () => {
|
|
431
|
+
const error = createElement(documentRef, 'div', 'typst-error');
|
|
432
|
+
error.append(createElement(documentRef, 'strong', undefined, 'Typst 渲染失败'), createElement(documentRef, 'pre', undefined, errorMessage));
|
|
433
|
+
body.replaceChildren(error);
|
|
434
|
+
};
|
|
435
|
+
const renderPages = () => {
|
|
436
|
+
pageShells.clear();
|
|
437
|
+
const pagesRoot = createElement(documentRef, 'main', 'typst-pages');
|
|
438
|
+
pagesRoot.setAttribute('aria-label', 'Typst preview pages');
|
|
439
|
+
pages.forEach(page => {
|
|
440
|
+
const shell = createElement(documentRef, 'section', 'typst-page-shell');
|
|
441
|
+
shell.setAttribute('aria-label', `Page ${page.index}`);
|
|
442
|
+
const content = createElement(documentRef, 'div', 'typst-page-content');
|
|
443
|
+
content.innerHTML = page.svg;
|
|
444
|
+
shell.append(content);
|
|
445
|
+
pageShells.set(page.index, shell);
|
|
446
|
+
pagesRoot.append(shell);
|
|
447
|
+
});
|
|
448
|
+
body.replaceChildren(pagesRoot);
|
|
449
|
+
applyPageZoom();
|
|
450
|
+
};
|
|
451
|
+
const syncUi = () => {
|
|
452
|
+
summary.textContent = state === 'ready'
|
|
453
|
+
? getPageSummary(pages)
|
|
454
|
+
: 'Typst WASM renderer';
|
|
455
|
+
status.textContent = state === 'loading'
|
|
456
|
+
? '正在编译'
|
|
457
|
+
: state === 'error'
|
|
458
|
+
? '编译失败'
|
|
459
|
+
: '已渲染';
|
|
460
|
+
if (state === 'loading') {
|
|
461
|
+
renderLoading();
|
|
462
|
+
}
|
|
463
|
+
else if (state === 'error') {
|
|
464
|
+
renderError();
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
renderPages();
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
const renderTypstSvg = async () => {
|
|
471
|
+
var _a, _b;
|
|
472
|
+
const candidates = resolveTypstEngineCandidates(context, documentRef.baseURI);
|
|
473
|
+
const timeoutMs = normalizeRenderTimeoutMs((_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst) === null || _b === void 0 ? void 0 : _b.renderTimeoutMs);
|
|
474
|
+
let lastError;
|
|
475
|
+
for (const candidate of candidates) {
|
|
476
|
+
const missingAsset = candidate.preflight
|
|
477
|
+
? await resolveKnownMissingTypstAsset(candidate)
|
|
478
|
+
: '';
|
|
479
|
+
if (missingAsset) {
|
|
480
|
+
lastError = new Error(missingAsset);
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
configureTypstEngine(candidate.compilerWasmUrl, candidate.rendererWasmUrl);
|
|
485
|
+
return await withRenderTimeout($typst.svg({
|
|
486
|
+
mainContent: source,
|
|
487
|
+
data_selection: {
|
|
488
|
+
body: true,
|
|
489
|
+
defs: true,
|
|
490
|
+
css: true,
|
|
491
|
+
js: false,
|
|
492
|
+
},
|
|
493
|
+
}), timeoutMs);
|
|
494
|
+
}
|
|
495
|
+
catch (error) {
|
|
496
|
+
lastError = error;
|
|
497
|
+
if (error instanceof TypstRenderTimeoutError || !isTypstAssetLoadError(error)) {
|
|
498
|
+
throw error;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
throw lastError instanceof Error ? lastError : new Error('Typst WASM 加载失败');
|
|
503
|
+
};
|
|
504
|
+
const render = async () => {
|
|
505
|
+
var _a, _b;
|
|
506
|
+
const token = ++renderToken;
|
|
507
|
+
state = 'loading';
|
|
508
|
+
errorMessage = '';
|
|
509
|
+
pages = [];
|
|
510
|
+
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, null);
|
|
511
|
+
syncUi();
|
|
512
|
+
try {
|
|
513
|
+
const svg = await renderTypstSvg();
|
|
514
|
+
if (disposed || token !== renderToken) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
pages = parseTypstSvgPages(svg);
|
|
518
|
+
state = 'ready';
|
|
519
|
+
syncUi();
|
|
520
|
+
registerExportAdapter();
|
|
521
|
+
(_b = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _b === void 0 ? void 0 : _b.call(context);
|
|
522
|
+
}
|
|
523
|
+
catch (error) {
|
|
524
|
+
if (disposed || token !== renderToken) {
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
errorMessage = formatTypstRuntimeError(error);
|
|
528
|
+
state = 'error';
|
|
529
|
+
syncUi();
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
void render();
|
|
533
|
+
return {
|
|
534
|
+
$el: target,
|
|
535
|
+
unmount() {
|
|
536
|
+
var _a;
|
|
537
|
+
disposed = true;
|
|
538
|
+
renderToken += 1;
|
|
539
|
+
unregisterFileViewerZoomProvider(root);
|
|
540
|
+
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, null);
|
|
541
|
+
target.replaceChildren();
|
|
542
|
+
},
|
|
543
|
+
};
|
|
544
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@file-viewer/renderer-typst",
|
|
3
|
+
"version": "2.0.11",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Standalone Typst renderer plugin for Flyfish File Viewer powered by browser WASM.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"file-viewer",
|
|
9
|
+
"renderer",
|
|
10
|
+
"typst",
|
|
11
|
+
"wasm",
|
|
12
|
+
"document-preview",
|
|
13
|
+
"document-viewer",
|
|
14
|
+
"file-preview",
|
|
15
|
+
"self-hosted"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
|
+
"author": {
|
|
22
|
+
"name": "Wangyu",
|
|
23
|
+
"email": "wybaby168@gmail.com"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/flyfish-dev/file-viewer-renderer-typst.git",
|
|
28
|
+
"directory": "packages/renderers/typst"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://doc.file-viewer.app/guide/on-demand-renderers",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/flyfish-dev/file-viewer-renderer-typst/issues"
|
|
33
|
+
},
|
|
34
|
+
"funding": {
|
|
35
|
+
"type": "individual",
|
|
36
|
+
"url": "https://dev.flyfish.group/shop"
|
|
37
|
+
},
|
|
38
|
+
"main": "./dist/index.js",
|
|
39
|
+
"module": "./dist/index.js",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
44
|
+
"import": "./dist/index.js",
|
|
45
|
+
"default": "./dist/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"README.md",
|
|
52
|
+
"README.en.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@file-viewer/core": "^2.0.11",
|
|
57
|
+
"@myriaddreamin/typst-ts-renderer": "0.7.0",
|
|
58
|
+
"@myriaddreamin/typst-ts-web-compiler": "0.7.0",
|
|
59
|
+
"@myriaddreamin/typst.ts": "0.7.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"typescript": "^6.0.3"
|
|
63
|
+
},
|
|
64
|
+
"license": "Apache-2.0",
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsc -b tsconfig.json",
|
|
67
|
+
"type-check": "tsc -b tsconfig.json"
|
|
68
|
+
}
|
|
69
|
+
}
|