@file-viewer/renderer-drawing 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 +32 -0
- package/README.md +32 -0
- package/dist/diagram.d.ts +18 -0
- package/dist/diagram.js +205 -0
- package/dist/drawing.d.ts +10 -0
- package/dist/drawing.js +915 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +17 -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,32 @@
|
|
|
1
|
+
# @file-viewer/renderer-drawing
|
|
2
|
+
|
|
3
|
+
Standalone Draw.io, Excalidraw, Mermaid, and PlantUML renderer for Flyfish File Viewer.
|
|
4
|
+
|
|
5
|
+
## Highlights
|
|
6
|
+
|
|
7
|
+
- Uses the bundled diagrams.net offline viewer for `drawio` / `dio` files.
|
|
8
|
+
- Uses Excalidraw's official `restore` and `exportToSvg` APIs, with a `roughjs` SVG fallback.
|
|
9
|
+
- Lazy-loads the official `mermaid` renderer for `mermaid` / `mmd` files and outputs theme-aware SVG.
|
|
10
|
+
- Keeps `plantuml` / `puml` files fully offline by default with an SVG source preview; configure `options.drawing.plantumlServerUrl` for full PlantUML SVG rendering through a self-hosted endpoint. When that endpoint is unavailable, the renderer still falls back to the offline preview instead of a blank view or broken image.
|
|
11
|
+
- Mermaid and PlantUML previews use `@panzoom/panzoom` for drag panning, Ctrl/Command wheel zoom, and common toolbar zoom sync.
|
|
12
|
+
- Supports unified zoom, print, HTML export, and `options.drawing` self-hosted asset configuration.
|
|
13
|
+
- Ships as an independent renderer package for applications that only need drawing preview.
|
|
14
|
+
- `@file-viewer/core` no longer bundles the drawing renderer and no longer depends directly on `@excalidraw/excalidraw`, `mermaid`, `plantuml-encoder`, `@panzoom/panzoom`, or `roughjs`.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createFileViewerCore, createFileViewerRendererRegistry } from '@file-viewer/core';
|
|
20
|
+
import { drawingRenderer } from '@file-viewer/renderer-drawing';
|
|
21
|
+
|
|
22
|
+
const registry = createFileViewerRendererRegistry({
|
|
23
|
+
renderers: [drawingRenderer],
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const viewer = createFileViewerCore({
|
|
27
|
+
target: document.querySelector('#viewer')!,
|
|
28
|
+
rendererRegistry: registry,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
See the [official on-demand renderer guide](https://doc.file-viewer.app/guide/on-demand-renderers) for production setup.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @file-viewer/renderer-drawing
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立绘图渲染器,覆盖 `drawio`、`dio`、`excalidraw`、`mermaid`、`mmd`、`plantuml` 和 `puml`。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- `drawio` / `dio` 默认使用随 viewer assets 分发的 diagrams.net offline viewer。
|
|
8
|
+
- `excalidraw` 使用官方 `restore` 与 `exportToSvg`,失败时回退到 `roughjs` 只读 SVG。
|
|
9
|
+
- `mermaid` / `mmd` 按需加载官方 `mermaid`,输出主题适配 SVG。
|
|
10
|
+
- `plantuml` / `puml` 默认保持离线 SVG 源码预览;需要完整 PlantUML 渲染时,可通过 `options.drawing.plantumlServerUrl` 指向自托管 SVG 端点,端点不可用时仍会回落到离线预览,避免白屏或 broken image。
|
|
11
|
+
- Mermaid / PlantUML 预览使用 `@panzoom/panzoom` 提供拖拽平移、Ctrl/Command 滚轮缩放和统一工具栏缩放联动。
|
|
12
|
+
- 支持统一缩放、打印、HTML 导出和 `options.drawing` 自托管资源配置。
|
|
13
|
+
- 独立安装、独立发布,适合只需要绘图预览的业务按需装配。
|
|
14
|
+
- `@file-viewer/core` 已不再内置 drawing renderer,也不再直接依赖 `@excalidraw/excalidraw`、`mermaid`、`plantuml-encoder`、`@panzoom/panzoom` 或 `roughjs`。
|
|
15
|
+
|
|
16
|
+
## 使用
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createFileViewerCore, createFileViewerRendererRegistry } from '@file-viewer/core';
|
|
20
|
+
import { drawingRenderer } from '@file-viewer/renderer-drawing';
|
|
21
|
+
|
|
22
|
+
const registry = createFileViewerRendererRegistry({
|
|
23
|
+
renderers: [drawingRenderer],
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const viewer = createFileViewerCore({
|
|
27
|
+
target: document.querySelector('#viewer')!,
|
|
28
|
+
rendererRegistry: registry,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
完整按需加载方案见 [官方文档](https://doc.file-viewer.app/guide/on-demand-renderers)。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileViewerDrawingOptions, FileViewerThemeMode } from '@file-viewer/core';
|
|
2
|
+
export type DiagramKind = 'mermaid' | 'plantuml';
|
|
3
|
+
export interface DiagramController {
|
|
4
|
+
setZoom: (scale: number) => void;
|
|
5
|
+
reset: () => void;
|
|
6
|
+
getScale: () => number;
|
|
7
|
+
destroy: () => void;
|
|
8
|
+
}
|
|
9
|
+
interface RenderDiagramParams {
|
|
10
|
+
documentRef: Document;
|
|
11
|
+
text: string;
|
|
12
|
+
target: HTMLElement;
|
|
13
|
+
kind: DiagramKind;
|
|
14
|
+
options?: FileViewerDrawingOptions;
|
|
15
|
+
theme?: FileViewerThemeMode;
|
|
16
|
+
}
|
|
17
|
+
export declare const renderDiagram: ({ documentRef, text, target, kind, options, theme }: RenderDiagramParams) => Promise<DiagramController>;
|
|
18
|
+
export {};
|
package/dist/diagram.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import Panzoom, {} from '@panzoom/panzoom';
|
|
2
|
+
const getOwnerWindow = (documentRef) => {
|
|
3
|
+
return documentRef.defaultView || (typeof window !== 'undefined' ? window : undefined);
|
|
4
|
+
};
|
|
5
|
+
const isDarkTheme = (documentRef, theme) => {
|
|
6
|
+
if (theme === 'dark') {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (theme === 'light') {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return Boolean(getOwnerWindow(documentRef)?.matchMedia?.('(prefers-color-scheme: dark)').matches);
|
|
13
|
+
};
|
|
14
|
+
const createElement = (documentRef, tagName, className) => {
|
|
15
|
+
const element = documentRef.createElement(tagName);
|
|
16
|
+
if (className) {
|
|
17
|
+
element.className = className;
|
|
18
|
+
}
|
|
19
|
+
return element;
|
|
20
|
+
};
|
|
21
|
+
const normalizePlantumlServer = (documentRef, value) => {
|
|
22
|
+
const base = value;
|
|
23
|
+
const normalized = base.endsWith('/') ? base : `${base}/`;
|
|
24
|
+
try {
|
|
25
|
+
return new URL(normalized, documentRef.baseURI).href;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const sanitizeSvg = (documentRef, svg) => {
|
|
32
|
+
const parsed = new DOMParser().parseFromString(svg, 'image/svg+xml');
|
|
33
|
+
const parseError = parsed.querySelector('parsererror');
|
|
34
|
+
if (parseError) {
|
|
35
|
+
throw new Error(parseError.textContent || 'SVG 解析失败');
|
|
36
|
+
}
|
|
37
|
+
parsed.querySelectorAll('script,iframe,object,embed').forEach(node => node.remove());
|
|
38
|
+
parsed.querySelectorAll('*').forEach(node => {
|
|
39
|
+
for (const attribute of Array.from(node.attributes)) {
|
|
40
|
+
if (/^on/i.test(attribute.name)) {
|
|
41
|
+
node.removeAttribute(attribute.name);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const svgNode = parsed.documentElement;
|
|
46
|
+
return documentRef.importNode(svgNode, true);
|
|
47
|
+
};
|
|
48
|
+
const renderMermaidSvg = async (documentRef, text, theme) => {
|
|
49
|
+
const mermaidModule = await import('mermaid');
|
|
50
|
+
const mermaid = mermaidModule.default;
|
|
51
|
+
const id = `file-viewer-mermaid-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
52
|
+
mermaid.initialize({
|
|
53
|
+
startOnLoad: false,
|
|
54
|
+
securityLevel: 'strict',
|
|
55
|
+
theme: isDarkTheme(documentRef, theme) ? 'dark' : 'default'
|
|
56
|
+
});
|
|
57
|
+
const rendered = await mermaid.render(id, text);
|
|
58
|
+
return sanitizeSvg(documentRef, rendered.svg);
|
|
59
|
+
};
|
|
60
|
+
const appendSvgText = (documentRef, parent, text, x, dy, weight = '500') => {
|
|
61
|
+
const line = documentRef.createElementNS('http://www.w3.org/2000/svg', 'tspan');
|
|
62
|
+
line.setAttribute('x', String(x));
|
|
63
|
+
line.setAttribute('dy', dy);
|
|
64
|
+
line.setAttribute('font-weight', weight);
|
|
65
|
+
line.textContent = text;
|
|
66
|
+
parent.appendChild(line);
|
|
67
|
+
};
|
|
68
|
+
const renderPlantumlFallbackSvg = (documentRef, text, message) => {
|
|
69
|
+
const width = 1100;
|
|
70
|
+
const height = 720;
|
|
71
|
+
const svg = documentRef.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
72
|
+
svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
|
73
|
+
svg.setAttribute('width', String(width));
|
|
74
|
+
svg.setAttribute('height', String(height));
|
|
75
|
+
const background = documentRef.createElementNS('http://www.w3.org/2000/svg', 'rect');
|
|
76
|
+
background.setAttribute('width', '100%');
|
|
77
|
+
background.setAttribute('height', '100%');
|
|
78
|
+
background.setAttribute('rx', '28');
|
|
79
|
+
background.setAttribute('fill', '#f8fbff');
|
|
80
|
+
svg.appendChild(background);
|
|
81
|
+
const panel = documentRef.createElementNS('http://www.w3.org/2000/svg', 'rect');
|
|
82
|
+
panel.setAttribute('x', '42');
|
|
83
|
+
panel.setAttribute('y', '42');
|
|
84
|
+
panel.setAttribute('width', String(width - 84));
|
|
85
|
+
panel.setAttribute('height', String(height - 84));
|
|
86
|
+
panel.setAttribute('rx', '22');
|
|
87
|
+
panel.setAttribute('fill', '#ffffff');
|
|
88
|
+
panel.setAttribute('stroke', '#cbd5e1');
|
|
89
|
+
svg.appendChild(panel);
|
|
90
|
+
const title = documentRef.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
91
|
+
title.setAttribute('x', '74');
|
|
92
|
+
title.setAttribute('y', '92');
|
|
93
|
+
title.setAttribute('fill', '#0f766e');
|
|
94
|
+
title.setAttribute('font-family', 'Inter, Arial, sans-serif');
|
|
95
|
+
title.setAttribute('font-size', '24');
|
|
96
|
+
title.setAttribute('font-weight', '800');
|
|
97
|
+
title.textContent = 'PlantUML source preview';
|
|
98
|
+
svg.appendChild(title);
|
|
99
|
+
const note = documentRef.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
100
|
+
note.setAttribute('x', '74');
|
|
101
|
+
note.setAttribute('y', '128');
|
|
102
|
+
note.setAttribute('fill', '#64748b');
|
|
103
|
+
note.setAttribute('font-family', 'Inter, Arial, sans-serif');
|
|
104
|
+
note.setAttribute('font-size', '15');
|
|
105
|
+
note.textContent = message;
|
|
106
|
+
svg.appendChild(note);
|
|
107
|
+
const source = documentRef.createElementNS('http://www.w3.org/2000/svg', 'text');
|
|
108
|
+
source.setAttribute('x', '74');
|
|
109
|
+
source.setAttribute('y', '172');
|
|
110
|
+
source.setAttribute('fill', '#172033');
|
|
111
|
+
source.setAttribute('font-family', 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace');
|
|
112
|
+
source.setAttribute('font-size', '16');
|
|
113
|
+
source.setAttribute('xml:space', 'preserve');
|
|
114
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n').slice(0, 26);
|
|
115
|
+
lines.forEach((line, index) => {
|
|
116
|
+
appendSvgText(documentRef, source, line.slice(0, 110), 74, index === 0 ? '0' : '24', /^\s*@/.test(line) ? '800' : '500');
|
|
117
|
+
});
|
|
118
|
+
if (lines.length < text.split('\n').length) {
|
|
119
|
+
appendSvgText(documentRef, source, '... truncated for preview ...', 74, '24', '700');
|
|
120
|
+
}
|
|
121
|
+
svg.appendChild(source);
|
|
122
|
+
return svg;
|
|
123
|
+
};
|
|
124
|
+
const renderPlantumlSvg = async (documentRef, text, options) => {
|
|
125
|
+
if (!options?.plantumlServerUrl) {
|
|
126
|
+
return renderPlantumlFallbackSvg(documentRef, text, 'Offline mode is active. Configure options.drawing.plantumlServerUrl for server-rendered PlantUML SVG.');
|
|
127
|
+
}
|
|
128
|
+
const { encode } = await import('plantuml-encoder');
|
|
129
|
+
const url = `${normalizePlantumlServer(documentRef, options.plantumlServerUrl)}${encode(text)}`;
|
|
130
|
+
const ownerWindow = getOwnerWindow(documentRef);
|
|
131
|
+
const controller = new AbortController();
|
|
132
|
+
const timeout = ownerWindow?.setTimeout(() => controller.abort(), Math.max(1000, options?.plantumlTimeoutMs || 8000));
|
|
133
|
+
try {
|
|
134
|
+
const response = await fetch(url, {
|
|
135
|
+
signal: controller.signal
|
|
136
|
+
});
|
|
137
|
+
if (!response.ok) {
|
|
138
|
+
throw new Error(`PlantUML SVG 渲染失败:${response.status}`);
|
|
139
|
+
}
|
|
140
|
+
return sanitizeSvg(documentRef, await response.text());
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error instanceof DOMException && error.name === 'AbortError') {
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
return renderPlantumlFallbackSvg(documentRef, text, 'Configure options.drawing.plantumlServerUrl for full PlantUML SVG rendering.');
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
if (timeout !== undefined) {
|
|
150
|
+
ownerWindow?.clearTimeout(timeout);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const applySvgDefaults = (svg, kind) => {
|
|
155
|
+
svg.classList.add('drawing-svg', 'drawing-diagram-svg');
|
|
156
|
+
svg.setAttribute('role', 'img');
|
|
157
|
+
svg.setAttribute('aria-label', `${kind} diagram preview`);
|
|
158
|
+
if (!svg.getAttribute('width') && svg.viewBox.baseVal.width) {
|
|
159
|
+
svg.setAttribute('width', String(Math.ceil(svg.viewBox.baseVal.width)));
|
|
160
|
+
}
|
|
161
|
+
if (!svg.getAttribute('height') && svg.viewBox.baseVal.height) {
|
|
162
|
+
svg.setAttribute('height', String(Math.ceil(svg.viewBox.baseVal.height)));
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
export const renderDiagram = async ({ documentRef, text, target, kind, options, theme }) => {
|
|
166
|
+
const shell = createElement(documentRef, 'div', 'drawing-diagram-shell');
|
|
167
|
+
const panTarget = createElement(documentRef, 'div', 'drawing-diagram-pan');
|
|
168
|
+
shell.appendChild(panTarget);
|
|
169
|
+
target.replaceChildren(shell);
|
|
170
|
+
const svg = kind === 'mermaid'
|
|
171
|
+
? await renderMermaidSvg(documentRef, text, theme)
|
|
172
|
+
: await renderPlantumlSvg(documentRef, text, options);
|
|
173
|
+
applySvgDefaults(svg, kind);
|
|
174
|
+
panTarget.replaceChildren(svg);
|
|
175
|
+
const panzoom = Panzoom(panTarget, {
|
|
176
|
+
minScale: 0.4,
|
|
177
|
+
maxScale: 4,
|
|
178
|
+
contain: 'outside',
|
|
179
|
+
canvas: true
|
|
180
|
+
});
|
|
181
|
+
const wheelTarget = shell.parentElement || shell;
|
|
182
|
+
const handleWheel = (event) => {
|
|
183
|
+
if (!event.ctrlKey && !event.metaKey) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
panzoom.zoomWithWheel(event);
|
|
188
|
+
};
|
|
189
|
+
wheelTarget.addEventListener('wheel', handleWheel, { passive: false });
|
|
190
|
+
return {
|
|
191
|
+
setZoom(scale) {
|
|
192
|
+
panzoom.zoom(scale, { animate: true });
|
|
193
|
+
},
|
|
194
|
+
reset() {
|
|
195
|
+
panzoom.reset({ animate: true });
|
|
196
|
+
},
|
|
197
|
+
getScale() {
|
|
198
|
+
return panzoom.getScale();
|
|
199
|
+
},
|
|
200
|
+
destroy() {
|
|
201
|
+
wheelTarget.removeEventListener('wheel', handleWheel);
|
|
202
|
+
panzoom.destroy();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FileRenderContext, FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
GraphViewer?: {
|
|
5
|
+
createViewerForElement: (element: HTMLElement, callback?: (viewer: unknown) => void) => unknown;
|
|
6
|
+
processElements: (className?: string) => void;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export default function renderDrawing(buffer: ArrayBuffer, target: HTMLDivElement, type?: string, context?: FileRenderContext): Promise<FileViewerRenderedInstance>;
|