@heikeson/md-engine 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -6
- package/dist/{chunk-4TDQECWJ.js → chunk-KC6CBSFU.js} +114 -17
- package/dist/chunk-KC6CBSFU.js.map +1 -0
- package/dist/chunk-OG47YL3H.js +68 -0
- package/dist/chunk-OG47YL3H.js.map +1 -0
- package/dist/chunk-OJQDOFGL.js +313 -0
- package/dist/chunk-OJQDOFGL.js.map +1 -0
- package/dist/{chunk-SUI5PVAV.js → chunk-USP2GQZF.js} +321 -127
- package/dist/chunk-USP2GQZF.js.map +1 -0
- package/dist/chunk-YJGGJP2V.js +68 -0
- package/dist/chunk-YJGGJP2V.js.map +1 -0
- package/dist/components/index.d.ts +6 -2
- package/dist/components/index.js +4 -1
- package/dist/editor/index.d.ts +30 -11
- package/dist/editor/index.js +193 -88
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +155 -41
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/locales/en-US.d.ts +68 -0
- package/dist/locales/en-US.js +3 -0
- package/dist/locales/en-US.js.map +1 -0
- package/dist/locales/zh-CN.d.ts +68 -0
- package/dist/locales/zh-CN.js +3 -0
- package/dist/locales/zh-CN.js.map +1 -0
- package/dist/mermaid/index.d.ts +209 -0
- package/dist/mermaid/index.js +708 -0
- package/dist/mermaid/index.js.map +1 -0
- package/dist/mermaid-placeholder-DYfFG3_f.d.ts +40 -0
- package/dist/render-DbUepYgE.d.ts +247 -0
- package/dist/styles.css +1 -1
- package/package.json +29 -3
- package/styles/accordion.css +1 -1
- package/styles/callout.css +2 -2
- package/styles/cards.css +9 -1
- package/styles/editor.css +1 -1
- package/styles/files.css +12 -5
- package/styles/image.css +5 -5
- package/styles/index.css +3 -0
- package/styles/mermaid.css +269 -0
- package/styles/steps.css +1 -1
- package/styles/tokens.css +32 -14
- package/styles/type-table.css +2 -2
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +180 -7
- package/dist/chunk-4TDQECWJ.js.map +0 -1
- package/dist/chunk-SUI5PVAV.js.map +0 -1
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
import { useUIText } from '../chunk-OJQDOFGL.js';
|
|
2
|
+
import '../chunk-OG47YL3H.js';
|
|
3
|
+
import '../chunk-YJGGJP2V.js';
|
|
4
|
+
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
5
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
6
|
+
import DOMPurify from 'dompurify';
|
|
7
|
+
|
|
8
|
+
// src/mermaid/export.ts
|
|
9
|
+
var XMLNS = "http://www.w3.org/2000/svg";
|
|
10
|
+
var XLINK = "http://www.w3.org/1999/xlink";
|
|
11
|
+
var PNG_SCALE = 2;
|
|
12
|
+
var FALLBACK_SIZE = { width: 800, height: 600 };
|
|
13
|
+
function sizeOf(svg) {
|
|
14
|
+
const viewBox = (svg.getAttribute("viewBox") ?? "").split(/[\s,]+/).map(Number);
|
|
15
|
+
if (viewBox.length === 4 && Number.isFinite(viewBox[2]) && Number.isFinite(viewBox[3])) {
|
|
16
|
+
const width2 = viewBox[2];
|
|
17
|
+
const height2 = viewBox[3];
|
|
18
|
+
if (width2 > 0 && height2 > 0) return { width: width2, height: height2 };
|
|
19
|
+
}
|
|
20
|
+
const width = Number.parseFloat(svg.getAttribute("width") ?? "");
|
|
21
|
+
const height = Number.parseFloat(svg.getAttribute("height") ?? "");
|
|
22
|
+
return {
|
|
23
|
+
width: Number.isFinite(width) && width > 0 ? width : FALLBACK_SIZE.width,
|
|
24
|
+
height: Number.isFinite(height) && height > 0 ? height : FALLBACK_SIZE.height
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function prepareSvg(source) {
|
|
28
|
+
const doc = new DOMParser().parseFromString(source, "image/svg+xml");
|
|
29
|
+
const svg = doc.documentElement;
|
|
30
|
+
if (!svg || svg.nodeName !== "svg") return { markup: source, ...FALLBACK_SIZE };
|
|
31
|
+
const { width, height } = sizeOf(svg);
|
|
32
|
+
svg.setAttribute("xmlns", XMLNS);
|
|
33
|
+
svg.setAttribute("xmlns:xlink", XLINK);
|
|
34
|
+
const style = (svg.getAttribute("style") ?? "").replace(/max-width\s*:[^;]+;?/gi, "").trim();
|
|
35
|
+
if (style) svg.setAttribute("style", style);
|
|
36
|
+
else svg.removeAttribute("style");
|
|
37
|
+
svg.setAttribute("width", String(width));
|
|
38
|
+
svg.setAttribute("height", String(height));
|
|
39
|
+
return { markup: new XMLSerializer().serializeToString(svg), width, height };
|
|
40
|
+
}
|
|
41
|
+
function fileName(ext) {
|
|
42
|
+
return `mermaid-${Date.now()}.${ext}`;
|
|
43
|
+
}
|
|
44
|
+
function downloadBlob(blob, name) {
|
|
45
|
+
const url = URL.createObjectURL(blob);
|
|
46
|
+
const link = document.createElement("a");
|
|
47
|
+
link.href = url;
|
|
48
|
+
link.download = name;
|
|
49
|
+
link.rel = "noopener";
|
|
50
|
+
link.click();
|
|
51
|
+
setTimeout(() => URL.revokeObjectURL(url), 2e3);
|
|
52
|
+
}
|
|
53
|
+
function downloadSvg(source) {
|
|
54
|
+
const { markup } = prepareSvg(source);
|
|
55
|
+
downloadBlob(new Blob([markup], { type: "image/svg+xml;charset=utf-8" }), fileName("svg"));
|
|
56
|
+
}
|
|
57
|
+
async function svgToPngBlob(source, background) {
|
|
58
|
+
const { markup, width, height } = prepareSvg(source);
|
|
59
|
+
const canvas = document.createElement("canvas");
|
|
60
|
+
canvas.width = Math.round(width * PNG_SCALE);
|
|
61
|
+
canvas.height = Math.round(height * PNG_SCALE);
|
|
62
|
+
const ctx = canvas.getContext("2d");
|
|
63
|
+
if (!ctx) throw new Error("\u65E0\u6CD5\u521B\u5EFA\u753B\u5E03\u4E0A\u4E0B\u6587");
|
|
64
|
+
ctx.scale(PNG_SCALE, PNG_SCALE);
|
|
65
|
+
ctx.fillStyle = background;
|
|
66
|
+
ctx.fillRect(0, 0, width, height);
|
|
67
|
+
const image = new Image();
|
|
68
|
+
image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(markup)}`;
|
|
69
|
+
await image.decode();
|
|
70
|
+
ctx.drawImage(image, 0, 0, width, height);
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("PNG \u751F\u6210\u5931\u8D25")), "image/png");
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
async function downloadPng(source, background) {
|
|
76
|
+
const blob = await svgToPngBlob(source, background);
|
|
77
|
+
downloadBlob(blob, fileName("png"));
|
|
78
|
+
}
|
|
79
|
+
var STROKE = {
|
|
80
|
+
fill: "none",
|
|
81
|
+
stroke: "currentColor",
|
|
82
|
+
strokeWidth: 1.75,
|
|
83
|
+
strokeLinecap: "round",
|
|
84
|
+
strokeLinejoin: "round"
|
|
85
|
+
};
|
|
86
|
+
function Svg({ size = 16, children }) {
|
|
87
|
+
return /* @__PURE__ */ jsx("svg", { width: size, height: size, viewBox: "0 0 24 24", "aria-hidden": "true", focusable: "false", ...STROKE, children });
|
|
88
|
+
}
|
|
89
|
+
function CodeIcon(props) {
|
|
90
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
91
|
+
/* @__PURE__ */ jsx("polyline", { points: "16 18 22 12 16 6" }),
|
|
92
|
+
/* @__PURE__ */ jsx("polyline", { points: "8 6 2 12 8 18" })
|
|
93
|
+
] });
|
|
94
|
+
}
|
|
95
|
+
function CopyIcon(props) {
|
|
96
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
97
|
+
/* @__PURE__ */ jsx("rect", { x: "9", y: "9", width: "12", height: "12", rx: "2" }),
|
|
98
|
+
/* @__PURE__ */ jsx("path", { d: "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" })
|
|
99
|
+
] });
|
|
100
|
+
}
|
|
101
|
+
function DownloadIcon(props) {
|
|
102
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
103
|
+
/* @__PURE__ */ jsx("path", { d: "M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" }),
|
|
104
|
+
/* @__PURE__ */ jsx("polyline", { points: "7 10 12 15 17 10" }),
|
|
105
|
+
/* @__PURE__ */ jsx("line", { x1: "12", y1: "15", x2: "12", y2: "3" })
|
|
106
|
+
] });
|
|
107
|
+
}
|
|
108
|
+
function ImageIcon(props) {
|
|
109
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
110
|
+
/* @__PURE__ */ jsx("rect", { x: "3", y: "3", width: "18", height: "18", rx: "2" }),
|
|
111
|
+
/* @__PURE__ */ jsx("circle", { cx: "9", cy: "9", r: "2" }),
|
|
112
|
+
/* @__PURE__ */ jsx("path", { d: "m21 15-3.1-3.1a2 2 0 0 0-2.8 0L6 21" })
|
|
113
|
+
] });
|
|
114
|
+
}
|
|
115
|
+
function ZoomInIcon(props) {
|
|
116
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
117
|
+
/* @__PURE__ */ jsx("circle", { cx: "11", cy: "11", r: "7" }),
|
|
118
|
+
/* @__PURE__ */ jsx("line", { x1: "21", y1: "21", x2: "16.7", y2: "16.7" }),
|
|
119
|
+
/* @__PURE__ */ jsx("line", { x1: "11", y1: "8", x2: "11", y2: "14" }),
|
|
120
|
+
/* @__PURE__ */ jsx("line", { x1: "8", y1: "11", x2: "14", y2: "11" })
|
|
121
|
+
] });
|
|
122
|
+
}
|
|
123
|
+
function ZoomOutIcon(props) {
|
|
124
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
125
|
+
/* @__PURE__ */ jsx("circle", { cx: "11", cy: "11", r: "7" }),
|
|
126
|
+
/* @__PURE__ */ jsx("line", { x1: "21", y1: "21", x2: "16.7", y2: "16.7" }),
|
|
127
|
+
/* @__PURE__ */ jsx("line", { x1: "8", y1: "11", x2: "14", y2: "11" })
|
|
128
|
+
] });
|
|
129
|
+
}
|
|
130
|
+
function ResetIcon(props) {
|
|
131
|
+
return /* @__PURE__ */ jsxs(Svg, { ...props, children: [
|
|
132
|
+
/* @__PURE__ */ jsx("path", { d: "M3.5 12a8.5 8.5 0 1 0 2.6-6.1" }),
|
|
133
|
+
/* @__PURE__ */ jsx("polyline", { points: "3 3.5 3 9.5 9 9.5" })
|
|
134
|
+
] });
|
|
135
|
+
}
|
|
136
|
+
function MermaidHeader(props) {
|
|
137
|
+
const { showSource, canExport, source, title, onToggleView, onExportSvg, onExportPng } = props;
|
|
138
|
+
const text = useUIText();
|
|
139
|
+
const [copied, setCopied] = useState(false);
|
|
140
|
+
const timer = useRef(null);
|
|
141
|
+
const onCopy = async () => {
|
|
142
|
+
try {
|
|
143
|
+
await navigator.clipboard.writeText(source);
|
|
144
|
+
setCopied(true);
|
|
145
|
+
if (timer.current !== null) window.clearTimeout(timer.current);
|
|
146
|
+
timer.current = window.setTimeout(() => setCopied(false), 1500);
|
|
147
|
+
} catch {
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return /* @__PURE__ */ jsxs("div", { className: "lkes-mermaid-header", children: [
|
|
151
|
+
/* @__PURE__ */ jsxs("div", { className: "lkes-mermaid-leading", children: [
|
|
152
|
+
/* @__PURE__ */ jsxs(
|
|
153
|
+
"button",
|
|
154
|
+
{
|
|
155
|
+
type: "button",
|
|
156
|
+
className: "lkes-mermaid-mode",
|
|
157
|
+
onClick: onToggleView,
|
|
158
|
+
"aria-pressed": showSource,
|
|
159
|
+
title: showSource ? text.mermaidToChart : text.mermaidToSource,
|
|
160
|
+
children: [
|
|
161
|
+
/* @__PURE__ */ jsx(CodeIcon, {}),
|
|
162
|
+
/* @__PURE__ */ jsx("span", { children: showSource ? text.mermaidChartView : text.mermaidSourceView })
|
|
163
|
+
]
|
|
164
|
+
}
|
|
165
|
+
),
|
|
166
|
+
title ? /* @__PURE__ */ jsx("span", { className: "lkes-mermaid-title", children: title }) : null
|
|
167
|
+
] }),
|
|
168
|
+
/* @__PURE__ */ jsxs("div", { className: "lkes-mermaid-actions", children: [
|
|
169
|
+
/* @__PURE__ */ jsxs("button", { type: "button", onClick: onCopy, children: [
|
|
170
|
+
/* @__PURE__ */ jsx(CopyIcon, {}),
|
|
171
|
+
/* @__PURE__ */ jsx("span", { children: copied ? text.mermaidCopied : text.mermaidCopySource })
|
|
172
|
+
] }),
|
|
173
|
+
/* @__PURE__ */ jsxs("button", { type: "button", onClick: onExportSvg, disabled: !canExport, children: [
|
|
174
|
+
/* @__PURE__ */ jsx(DownloadIcon, {}),
|
|
175
|
+
/* @__PURE__ */ jsx("span", { children: text.mermaidDownloadSvg })
|
|
176
|
+
] }),
|
|
177
|
+
/* @__PURE__ */ jsxs("button", { type: "button", onClick: onExportPng, disabled: !canExport, children: [
|
|
178
|
+
/* @__PURE__ */ jsx(ImageIcon, {}),
|
|
179
|
+
/* @__PURE__ */ jsx("span", { children: text.mermaidDownloadPng })
|
|
180
|
+
] })
|
|
181
|
+
] })
|
|
182
|
+
] });
|
|
183
|
+
}
|
|
184
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
185
|
+
var observer = null;
|
|
186
|
+
function notify() {
|
|
187
|
+
for (const listener of listeners) listener();
|
|
188
|
+
}
|
|
189
|
+
function subscribe(listener) {
|
|
190
|
+
if (typeof MutationObserver !== "undefined" && !observer) {
|
|
191
|
+
observer = new MutationObserver(notify);
|
|
192
|
+
observer.observe(document.documentElement, {
|
|
193
|
+
attributes: true,
|
|
194
|
+
attributeFilter: ["class"],
|
|
195
|
+
subtree: true
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
listeners.add(listener);
|
|
199
|
+
return () => {
|
|
200
|
+
listeners.delete(listener);
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function isDarkNow(element) {
|
|
204
|
+
if (typeof document === "undefined") return false;
|
|
205
|
+
if (element && element.closest(".dark")) return true;
|
|
206
|
+
return document.documentElement.classList.contains("dark") || document.body.classList.contains("dark");
|
|
207
|
+
}
|
|
208
|
+
function useIsDark(elementRef) {
|
|
209
|
+
const [dark, setDark] = useState(false);
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
const compute = () => setDark(isDarkNow(elementRef?.current ?? null));
|
|
212
|
+
compute();
|
|
213
|
+
return subscribe(compute);
|
|
214
|
+
}, [elementRef]);
|
|
215
|
+
return dark;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/mermaid/tokens.ts
|
|
219
|
+
var LIGHT_TOKENS = {
|
|
220
|
+
bg: "#ffffff",
|
|
221
|
+
fg: "#1f2937",
|
|
222
|
+
node: "#e7f0ea",
|
|
223
|
+
nodeBorder: "#c9d8cf",
|
|
224
|
+
line: "#9aa8a0",
|
|
225
|
+
series: ["#01c4b6", "#38bdf8", "#818cf8", "#c084fc", "#f472b6", "#fb923c"],
|
|
226
|
+
fontFamily: "system-ui, -apple-system, 'Segoe UI', sans-serif"
|
|
227
|
+
};
|
|
228
|
+
var DARK_TOKENS = {
|
|
229
|
+
bg: "#161c22",
|
|
230
|
+
fg: "#e8eef2",
|
|
231
|
+
node: "#1e3037",
|
|
232
|
+
nodeBorder: "#2f4a52",
|
|
233
|
+
line: "#7a8a92",
|
|
234
|
+
series: ["#19d3c5", "#38bdf8", "#818cf8", "#c084fc", "#f472b6", "#fb923c"],
|
|
235
|
+
fontFamily: "system-ui, -apple-system, 'Segoe UI', sans-serif"
|
|
236
|
+
};
|
|
237
|
+
var DEFAULT_CHART_THEME = { light: LIGHT_TOKENS, dark: DARK_TOKENS };
|
|
238
|
+
var configuredTheme = null;
|
|
239
|
+
function configureChartTheme(theme) {
|
|
240
|
+
configuredTheme = {
|
|
241
|
+
light: { ...DEFAULT_CHART_THEME.light, ...theme.light ?? {} },
|
|
242
|
+
dark: { ...DEFAULT_CHART_THEME.dark, ...theme.dark ?? {} }
|
|
243
|
+
};
|
|
244
|
+
return configuredTheme;
|
|
245
|
+
}
|
|
246
|
+
function getChartTheme() {
|
|
247
|
+
return configuredTheme ?? DEFAULT_CHART_THEME;
|
|
248
|
+
}
|
|
249
|
+
function hasCustomChartTheme() {
|
|
250
|
+
return configuredTheme !== null;
|
|
251
|
+
}
|
|
252
|
+
function resetChartTheme() {
|
|
253
|
+
configuredTheme = null;
|
|
254
|
+
}
|
|
255
|
+
var VAR_TOKENS = {
|
|
256
|
+
primaryColor: "node",
|
|
257
|
+
mainBkg: "node",
|
|
258
|
+
nodeBkg: "node",
|
|
259
|
+
actorBkg: "node",
|
|
260
|
+
stateBkg: "node",
|
|
261
|
+
rectBkgColor: "node",
|
|
262
|
+
compositeTitleBackground: "node",
|
|
263
|
+
compositeBackground: "bg",
|
|
264
|
+
secondaryColor: "node",
|
|
265
|
+
tertiaryColor: "node",
|
|
266
|
+
clusterBkg: "node",
|
|
267
|
+
activationBkgColor: "node",
|
|
268
|
+
labelBoxBkgColor: "node",
|
|
269
|
+
primaryBorderColor: "nodeBorder",
|
|
270
|
+
nodeBorder: "nodeBorder",
|
|
271
|
+
actorBorder: "nodeBorder",
|
|
272
|
+
compositeBorder: "line",
|
|
273
|
+
clusterBorder: "line",
|
|
274
|
+
border1: "line",
|
|
275
|
+
border2: "line",
|
|
276
|
+
labelBoxBorderColor: "line",
|
|
277
|
+
noteBorderColor: "line",
|
|
278
|
+
taskBorderColor: "line",
|
|
279
|
+
activationBorderColor: "line",
|
|
280
|
+
doneTaskBorderColor: "line",
|
|
281
|
+
primaryTextColor: "fg",
|
|
282
|
+
textColor: "fg",
|
|
283
|
+
nodeTextColor: "fg",
|
|
284
|
+
titleColor: "fg",
|
|
285
|
+
actorTextColor: "fg",
|
|
286
|
+
stateLabelColor: "fg",
|
|
287
|
+
signalTextColor: "fg",
|
|
288
|
+
labelTextColor: "fg",
|
|
289
|
+
loopTextColor: "fg",
|
|
290
|
+
transitionLabelColor: "fg",
|
|
291
|
+
classText: "fg",
|
|
292
|
+
taskTextColor: "fg",
|
|
293
|
+
taskTextLightColor: "fg",
|
|
294
|
+
taskTextDarkColor: "fg",
|
|
295
|
+
taskTextOutsideColor: "fg",
|
|
296
|
+
pieTitleTextColor: "fg",
|
|
297
|
+
pieLegendTextColor: "fg",
|
|
298
|
+
pieStrokeColor: "bg",
|
|
299
|
+
lineColor: "line",
|
|
300
|
+
defaultLinkColor: "line",
|
|
301
|
+
arrowheadColor: "line",
|
|
302
|
+
transitionColor: "line",
|
|
303
|
+
signalColor: "line",
|
|
304
|
+
actorLineColor: "line",
|
|
305
|
+
gridColor: "line",
|
|
306
|
+
vertLineColor: "line",
|
|
307
|
+
specialStateColor: "line",
|
|
308
|
+
innerEndBackground: "line",
|
|
309
|
+
noteBkgColor: "node",
|
|
310
|
+
noteTextColor: "fg",
|
|
311
|
+
edgeLabelBackground: "bg",
|
|
312
|
+
sequenceNumberColor: "bg",
|
|
313
|
+
sectionBkgColor: "node",
|
|
314
|
+
sectionBkgColor2: "node",
|
|
315
|
+
altSectionBkgColor: "node",
|
|
316
|
+
altBackground: "node",
|
|
317
|
+
taskBkgColor: "node",
|
|
318
|
+
activeTaskBkgColor: "node",
|
|
319
|
+
doneTaskBkgColor: "node",
|
|
320
|
+
excludeBkgColor: "node",
|
|
321
|
+
rowOdd: "node",
|
|
322
|
+
rowEven: "bg",
|
|
323
|
+
attributeBackgroundColorOdd: "node",
|
|
324
|
+
attributeBackgroundColorEven: "bg"
|
|
325
|
+
};
|
|
326
|
+
function seriesAt(tokens, index) {
|
|
327
|
+
const list = tokens.series.length > 0 ? tokens.series : LIGHT_TOKENS.series;
|
|
328
|
+
return list[index % list.length];
|
|
329
|
+
}
|
|
330
|
+
function pieColor(tokens, index) {
|
|
331
|
+
const half = Math.max(1, Math.floor(tokens.series.length / 2));
|
|
332
|
+
const offset = index % 2 === 0 ? index / 2 : half + Math.floor(index / 2);
|
|
333
|
+
return seriesAt(tokens, offset);
|
|
334
|
+
}
|
|
335
|
+
function buildThemeVariables(tokens) {
|
|
336
|
+
const vars = {
|
|
337
|
+
// 图底透明:页面正文底即图底,避免出现与页面不同色的矩形;项目规范禁用渐变
|
|
338
|
+
background: "transparent",
|
|
339
|
+
fontSize: "16px",
|
|
340
|
+
useGradient: false,
|
|
341
|
+
fontFamily: tokens.fontFamily
|
|
342
|
+
};
|
|
343
|
+
for (const [name, key] of Object.entries(VAR_TOKENS)) vars[name] = tokens[key];
|
|
344
|
+
vars.activeTaskBorderColor = seriesAt(tokens, 0);
|
|
345
|
+
vars.todayLineColor = seriesAt(tokens, tokens.series.length - 1);
|
|
346
|
+
vars.critBkgColor = seriesAt(tokens, tokens.series.length - 1);
|
|
347
|
+
vars.critBorderColor = seriesAt(tokens, tokens.series.length - 1);
|
|
348
|
+
vars.taskTextClickableColor = seriesAt(tokens, 0);
|
|
349
|
+
vars.fillType0 = tokens.bg;
|
|
350
|
+
vars.fillType1 = tokens.node;
|
|
351
|
+
for (let i = 0; i < 12; i += 1) {
|
|
352
|
+
vars[`cScale${i}`] = pieColor(tokens, i);
|
|
353
|
+
vars[`pie${i + 1}`] = pieColor(tokens, i);
|
|
354
|
+
if (i >= 2 && i < 8) vars[`fillType${i}`] = pieColor(tokens, i);
|
|
355
|
+
}
|
|
356
|
+
return vars;
|
|
357
|
+
}
|
|
358
|
+
function buildMermaidConfig(tokens) {
|
|
359
|
+
return {
|
|
360
|
+
startOnLoad: false,
|
|
361
|
+
theme: "base",
|
|
362
|
+
securityLevel: "strict",
|
|
363
|
+
// 文本一律用 SVG <text>,不用 <foreignObject>:
|
|
364
|
+
// ① 净化时(DOMPurify svg profile)foreignObject 里的 HTML 会被清空 —— 表现为"图有线没字";
|
|
365
|
+
// ② Canvas 不绘制 foreignObject —— 导出 PNG 必空白。
|
|
366
|
+
htmlLabels: false,
|
|
367
|
+
themeVariables: buildThemeVariables(tokens),
|
|
368
|
+
fontFamily: tokens.fontFamily,
|
|
369
|
+
flowchart: { useMaxWidth: true, htmlLabels: false },
|
|
370
|
+
sequence: { useMaxWidth: true, htmlLabels: false },
|
|
371
|
+
state: { useMaxWidth: true },
|
|
372
|
+
er: { useMaxWidth: true },
|
|
373
|
+
class: { useMaxWidth: true, htmlLabels: false },
|
|
374
|
+
gantt: { useMaxWidth: true }
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
function tokenFingerprint(tokens) {
|
|
378
|
+
return [tokens.bg, tokens.fg, tokens.node, tokens.nodeBorder, tokens.line, tokens.series.join(","), tokens.fontFamily].join("|");
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/mermaid/use-render.ts
|
|
382
|
+
var MermaidRenderTimeoutError = class extends Error {
|
|
383
|
+
constructor() {
|
|
384
|
+
super("MERMAID_RENDER_TIMEOUT");
|
|
385
|
+
this.name = "MermaidRenderTimeoutError";
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
var RENDER_TIMEOUT = 1e4;
|
|
389
|
+
var CACHE = /* @__PURE__ */ new Map();
|
|
390
|
+
var CACHE_LIMIT = 32;
|
|
391
|
+
var seq = 0;
|
|
392
|
+
var chain = Promise.resolve();
|
|
393
|
+
var loader = null;
|
|
394
|
+
function nextId() {
|
|
395
|
+
seq += 1;
|
|
396
|
+
return `lkes-mermaid-${seq}`;
|
|
397
|
+
}
|
|
398
|
+
function runExclusive(task) {
|
|
399
|
+
const result = chain.then(task, task);
|
|
400
|
+
chain = result.catch(() => void 0);
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
function withTimeout(task, ms) {
|
|
404
|
+
return new Promise((resolve, reject) => {
|
|
405
|
+
const timer = setTimeout(() => reject(new MermaidRenderTimeoutError()), ms);
|
|
406
|
+
task.then(
|
|
407
|
+
(value) => {
|
|
408
|
+
clearTimeout(timer);
|
|
409
|
+
resolve(value);
|
|
410
|
+
},
|
|
411
|
+
(error) => {
|
|
412
|
+
clearTimeout(timer);
|
|
413
|
+
reject(error);
|
|
414
|
+
}
|
|
415
|
+
);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
function loadMermaid() {
|
|
419
|
+
loader ??= import('mermaid').then((mod) => mod.default);
|
|
420
|
+
return loader;
|
|
421
|
+
}
|
|
422
|
+
function sanitizeSvg(svg) {
|
|
423
|
+
return DOMPurify.sanitize(svg, {
|
|
424
|
+
USE_PROFILES: { svg: true, svgFilters: true },
|
|
425
|
+
ADD_TAGS: ["style"],
|
|
426
|
+
ADD_ATTR: ["dominant-baseline"]
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
function isParsableColor(value) {
|
|
430
|
+
return /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value) || /^rgba?\([^)]*\)$/i.test(value);
|
|
431
|
+
}
|
|
432
|
+
function normalizeColor(value) {
|
|
433
|
+
if (isParsableColor(value)) return value;
|
|
434
|
+
if (typeof document === "undefined" || !document.body) return "";
|
|
435
|
+
const probe = document.createElement("span");
|
|
436
|
+
probe.style.color = value;
|
|
437
|
+
probe.style.display = "none";
|
|
438
|
+
document.body.appendChild(probe);
|
|
439
|
+
const rgb = getComputedStyle(probe).color;
|
|
440
|
+
probe.remove();
|
|
441
|
+
return isParsableColor(rgb) ? rgb : "";
|
|
442
|
+
}
|
|
443
|
+
function resolveChartTokens(dark, element) {
|
|
444
|
+
const theme = getChartTheme();
|
|
445
|
+
const base = dark ? theme.dark : theme.light;
|
|
446
|
+
if (hasCustomChartTheme() || typeof window === "undefined") return base;
|
|
447
|
+
const style = getComputedStyle(element ?? document.documentElement);
|
|
448
|
+
const read = (name, fallback) => {
|
|
449
|
+
const value = style.getPropertyValue(name).trim();
|
|
450
|
+
if (!value) return fallback;
|
|
451
|
+
return normalizeColor(value) || fallback;
|
|
452
|
+
};
|
|
453
|
+
return {
|
|
454
|
+
bg: read("--lkes-chart-bg", base.bg),
|
|
455
|
+
fg: read("--lkes-chart-fg", base.fg),
|
|
456
|
+
node: read("--lkes-chart-node", base.node),
|
|
457
|
+
nodeBorder: read("--lkes-chart-node-border", base.nodeBorder),
|
|
458
|
+
line: read("--lkes-chart-line", base.line),
|
|
459
|
+
series: base.series.map((fallback, index) => read(`--lkes-chart-series-${index + 1}`, fallback)),
|
|
460
|
+
fontFamily: base.fontFamily
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
async function renderChart(source, tokens) {
|
|
464
|
+
const key = `${tokenFingerprint(tokens)}#${source}`;
|
|
465
|
+
const cached = CACHE.get(key);
|
|
466
|
+
if (cached) {
|
|
467
|
+
CACHE.delete(key);
|
|
468
|
+
CACHE.set(key, cached);
|
|
469
|
+
return cached;
|
|
470
|
+
}
|
|
471
|
+
const mermaid = await loadMermaid();
|
|
472
|
+
const svg = await runExclusive(async () => {
|
|
473
|
+
mermaid.initialize(buildMermaidConfig(tokens));
|
|
474
|
+
const out = await withTimeout(mermaid.render(nextId(), source), RENDER_TIMEOUT);
|
|
475
|
+
return out.svg;
|
|
476
|
+
});
|
|
477
|
+
const clean = sanitizeSvg(svg);
|
|
478
|
+
CACHE.set(key, clean);
|
|
479
|
+
if (CACHE.size > CACHE_LIMIT) {
|
|
480
|
+
const oldest = CACHE.keys().next().value;
|
|
481
|
+
if (oldest !== void 0 && oldest !== key) CACHE.delete(oldest);
|
|
482
|
+
}
|
|
483
|
+
return clean;
|
|
484
|
+
}
|
|
485
|
+
function useMermaidRender(source, enabled, dark, elementRef) {
|
|
486
|
+
const [state, setState] = useState({ status: "idle", svg: "", error: "" });
|
|
487
|
+
const text = useUIText();
|
|
488
|
+
useEffect(() => {
|
|
489
|
+
if (!enabled) return;
|
|
490
|
+
let cancelled = false;
|
|
491
|
+
setState((prev) => prev.status === "ready" ? prev : { ...prev, status: "loading", error: "" });
|
|
492
|
+
void (async () => {
|
|
493
|
+
try {
|
|
494
|
+
const svg = await renderChart(source, resolveChartTokens(dark, elementRef?.current ?? null));
|
|
495
|
+
if (!cancelled) setState({ status: "ready", svg, error: "" });
|
|
496
|
+
} catch (error) {
|
|
497
|
+
if (!cancelled) {
|
|
498
|
+
const message = error instanceof MermaidRenderTimeoutError ? text.mermaidTimeout : error instanceof Error && error.message !== "MERMAID_RENDER_TIMEOUT" ? error.message : text.mermaidFallbackError;
|
|
499
|
+
console.warn("[md-engine] mermaid \u6E32\u67D3\u5931\u8D25\uFF0C\u5DF2\u964D\u7EA7\u4E3A\u6E90\u7801\uFF1A", message);
|
|
500
|
+
setState({ status: "error", svg: "", error: message });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
})();
|
|
504
|
+
return () => {
|
|
505
|
+
cancelled = true;
|
|
506
|
+
};
|
|
507
|
+
}, [source, enabled, dark, text]);
|
|
508
|
+
return state;
|
|
509
|
+
}
|
|
510
|
+
var IDENTITY = { scale: 1, x: 0, y: 0 };
|
|
511
|
+
var MIN_SCALE = 0.25;
|
|
512
|
+
var MAX_SCALE = 8;
|
|
513
|
+
var STEP_MIN = 0.75;
|
|
514
|
+
var STEP_MAX = 1.33;
|
|
515
|
+
var K = 15e-4;
|
|
516
|
+
function normalizeDelta(event) {
|
|
517
|
+
if (event.deltaMode === 1) return event.deltaY * 16;
|
|
518
|
+
if (event.deltaMode === 2) return event.deltaY * 100;
|
|
519
|
+
return event.deltaY;
|
|
520
|
+
}
|
|
521
|
+
function clampScale(value) {
|
|
522
|
+
return Math.min(MAX_SCALE, Math.max(MIN_SCALE, value));
|
|
523
|
+
}
|
|
524
|
+
function usePanZoom(active) {
|
|
525
|
+
const ref = useRef(null);
|
|
526
|
+
const [transform, setTransform] = useState(IDENTITY);
|
|
527
|
+
const drag = useRef(null);
|
|
528
|
+
const zoomAt = useCallback(
|
|
529
|
+
(factor, point, cursor) => {
|
|
530
|
+
setTransform((prev) => {
|
|
531
|
+
const next = clampScale(prev.scale * factor);
|
|
532
|
+
if (next === prev.scale) return prev;
|
|
533
|
+
const ratio = next / prev.scale;
|
|
534
|
+
const moved = {
|
|
535
|
+
scale: next,
|
|
536
|
+
x: point.x - (point.x - prev.x) * ratio,
|
|
537
|
+
y: point.y - (point.y - prev.y) * ratio
|
|
538
|
+
};
|
|
539
|
+
const session = drag.current;
|
|
540
|
+
if (session && cursor) {
|
|
541
|
+
drag.current = {
|
|
542
|
+
pointerId: session.pointerId,
|
|
543
|
+
baseX: moved.x,
|
|
544
|
+
baseY: moved.y,
|
|
545
|
+
startX: cursor.x,
|
|
546
|
+
startY: cursor.y
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
return moved;
|
|
550
|
+
});
|
|
551
|
+
},
|
|
552
|
+
[]
|
|
553
|
+
);
|
|
554
|
+
useEffect(() => {
|
|
555
|
+
const node = ref.current;
|
|
556
|
+
if (!node || !active) return;
|
|
557
|
+
const onWheel = (event) => {
|
|
558
|
+
if (!event.ctrlKey && !event.metaKey && drag.current === null) return;
|
|
559
|
+
event.preventDefault();
|
|
560
|
+
const rect = node.getBoundingClientRect();
|
|
561
|
+
const factor = Math.min(STEP_MAX, Math.max(STEP_MIN, Math.exp(-normalizeDelta(event) * K)));
|
|
562
|
+
zoomAt(
|
|
563
|
+
factor,
|
|
564
|
+
{ x: event.clientX - rect.left, y: event.clientY - rect.top },
|
|
565
|
+
{ x: event.clientX, y: event.clientY }
|
|
566
|
+
);
|
|
567
|
+
};
|
|
568
|
+
node.addEventListener("wheel", onWheel, { passive: false });
|
|
569
|
+
return () => node.removeEventListener("wheel", onWheel);
|
|
570
|
+
}, [active, zoomAt]);
|
|
571
|
+
const onPointerDown = useCallback(
|
|
572
|
+
(event) => {
|
|
573
|
+
if (!active || event.button !== 0) return;
|
|
574
|
+
drag.current = {
|
|
575
|
+
pointerId: event.pointerId,
|
|
576
|
+
baseX: transform.x,
|
|
577
|
+
baseY: transform.y,
|
|
578
|
+
startX: event.clientX,
|
|
579
|
+
startY: event.clientY
|
|
580
|
+
};
|
|
581
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
582
|
+
},
|
|
583
|
+
[active, transform.x, transform.y]
|
|
584
|
+
);
|
|
585
|
+
const onPointerMove = useCallback((event) => {
|
|
586
|
+
const session = drag.current;
|
|
587
|
+
if (!session || session.pointerId !== event.pointerId) return;
|
|
588
|
+
setTransform((prev) => ({
|
|
589
|
+
scale: prev.scale,
|
|
590
|
+
x: session.baseX + (event.clientX - session.startX),
|
|
591
|
+
y: session.baseY + (event.clientY - session.startY)
|
|
592
|
+
}));
|
|
593
|
+
}, []);
|
|
594
|
+
const onPointerUp = useCallback((event) => {
|
|
595
|
+
if (drag.current?.pointerId === event.pointerId) drag.current = null;
|
|
596
|
+
}, []);
|
|
597
|
+
const zoomIn = useCallback(() => zoomAt(1.25, centerOf(ref.current)), [zoomAt]);
|
|
598
|
+
const zoomOut = useCallback(() => zoomAt(0.8, centerOf(ref.current)), [zoomAt]);
|
|
599
|
+
const reset = useCallback(() => {
|
|
600
|
+
drag.current = null;
|
|
601
|
+
setTransform(IDENTITY);
|
|
602
|
+
}, []);
|
|
603
|
+
return { ref, transform, zoomIn, zoomOut, reset, onPointerDown, onPointerMove, onPointerUp };
|
|
604
|
+
}
|
|
605
|
+
function centerOf(node) {
|
|
606
|
+
if (!node) return { x: 0, y: 0 };
|
|
607
|
+
return { x: node.clientWidth / 2, y: node.clientHeight / 2 };
|
|
608
|
+
}
|
|
609
|
+
var PRELOAD_MARGIN = "200px 0px";
|
|
610
|
+
function viewportHeight(height) {
|
|
611
|
+
const raw = (height ?? "").trim().toLowerCase();
|
|
612
|
+
if (!raw || raw === "auto") return void 0;
|
|
613
|
+
const numeric = Number(raw);
|
|
614
|
+
return Number.isFinite(numeric) && numeric > 0 ? `${Math.round(numeric)}px` : void 0;
|
|
615
|
+
}
|
|
616
|
+
function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
617
|
+
const source = String(raw ?? code ?? "");
|
|
618
|
+
const disabled = Boolean(tooLarge) || source.trim() === "";
|
|
619
|
+
const text = useUIText();
|
|
620
|
+
const frameRef = useRef(null);
|
|
621
|
+
const dark = useIsDark(frameRef);
|
|
622
|
+
const [visible, setVisible] = useState(false);
|
|
623
|
+
const [showSource, setShowSource] = useState(disabled);
|
|
624
|
+
const { status, svg, error } = useMermaidRender(source, visible && !disabled, dark, frameRef);
|
|
625
|
+
const canExport = status === "ready";
|
|
626
|
+
const ready = canExport && !showSource;
|
|
627
|
+
const zoom = usePanZoom(ready);
|
|
628
|
+
useEffect(() => {
|
|
629
|
+
const node = frameRef.current;
|
|
630
|
+
if (!node || disabled) return;
|
|
631
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
632
|
+
setVisible(true);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const observer2 = new IntersectionObserver(
|
|
636
|
+
(entries) => {
|
|
637
|
+
if (entries.some((entry) => entry.isIntersecting)) {
|
|
638
|
+
setVisible(true);
|
|
639
|
+
observer2.disconnect();
|
|
640
|
+
}
|
|
641
|
+
},
|
|
642
|
+
{ rootMargin: PRELOAD_MARGIN }
|
|
643
|
+
);
|
|
644
|
+
observer2.observe(node);
|
|
645
|
+
return () => observer2.disconnect();
|
|
646
|
+
}, [disabled]);
|
|
647
|
+
useEffect(() => {
|
|
648
|
+
if (status === "error") setShowSource(true);
|
|
649
|
+
}, [status]);
|
|
650
|
+
const state = disabled ? tooLarge ? "too-large" : "empty" : showSource ? "source" : status;
|
|
651
|
+
return /* @__PURE__ */ jsxs("figure", { className: "lkes-mermaid", "data-mermaid": "", "data-state": state, children: [
|
|
652
|
+
/* @__PURE__ */ jsx(
|
|
653
|
+
MermaidHeader,
|
|
654
|
+
{
|
|
655
|
+
showSource,
|
|
656
|
+
canExport,
|
|
657
|
+
source,
|
|
658
|
+
title,
|
|
659
|
+
onToggleView: () => setShowSource((prev) => !prev),
|
|
660
|
+
onExportSvg: () => downloadSvg(svg),
|
|
661
|
+
onExportPng: () => void downloadPng(svg, resolveChartTokens(dark, frameRef.current).bg)
|
|
662
|
+
}
|
|
663
|
+
),
|
|
664
|
+
/* @__PURE__ */ jsx("div", { ref: frameRef, className: "lkes-mermaid-body", style: { maxHeight: viewportHeight(height) }, children: ready ? /* @__PURE__ */ jsx(
|
|
665
|
+
"div",
|
|
666
|
+
{
|
|
667
|
+
ref: zoom.ref,
|
|
668
|
+
className: "lkes-mermaid-canvas",
|
|
669
|
+
style: { transform: `translate(${zoom.transform.x}px, ${zoom.transform.y}px) scale(${zoom.transform.scale})` },
|
|
670
|
+
onPointerDown: zoom.onPointerDown,
|
|
671
|
+
onPointerMove: zoom.onPointerMove,
|
|
672
|
+
onPointerUp: zoom.onPointerUp,
|
|
673
|
+
onPointerCancel: zoom.onPointerUp,
|
|
674
|
+
onDoubleClick: zoom.reset,
|
|
675
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
676
|
+
}
|
|
677
|
+
) : /* @__PURE__ */ jsx("pre", { className: "lkes-mermaid-source", children: /* @__PURE__ */ jsx("code", { children: source }) }) }),
|
|
678
|
+
status === "loading" ? /* @__PURE__ */ jsx("p", { className: "lkes-mermaid-hint", children: text.mermaidLoading }) : null,
|
|
679
|
+
error ? /* @__PURE__ */ jsxs("p", { className: "lkes-mermaid-error", children: [
|
|
680
|
+
text.mermaidErrorPrefix,
|
|
681
|
+
error,
|
|
682
|
+
text.mermaidErrorSuffix
|
|
683
|
+
] }) : null,
|
|
684
|
+
ready ? /* @__PURE__ */ jsxs("div", { className: "lkes-mermaid-zoom", role: "group", "aria-label": text.mermaidZoomGroup, children: [
|
|
685
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: zoom.zoomIn, "aria-label": text.mermaidZoomIn, title: text.mermaidZoomIn, children: /* @__PURE__ */ jsx(ZoomInIcon, {}) }),
|
|
686
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: zoom.zoomOut, "aria-label": text.mermaidZoomOut, title: text.mermaidZoomOut, children: /* @__PURE__ */ jsx(ZoomOutIcon, {}) }),
|
|
687
|
+
/* @__PURE__ */ jsx(
|
|
688
|
+
"button",
|
|
689
|
+
{
|
|
690
|
+
type: "button",
|
|
691
|
+
onClick: zoom.reset,
|
|
692
|
+
"aria-label": text.mermaidZoomReset,
|
|
693
|
+
title: text.mermaidResetTitle,
|
|
694
|
+
children: /* @__PURE__ */ jsx(ResetIcon, {})
|
|
695
|
+
}
|
|
696
|
+
)
|
|
697
|
+
] }) : null
|
|
698
|
+
] });
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// src/mermaid/index.tsx
|
|
702
|
+
var mermaidComponents = {
|
|
703
|
+
Mermaid: MermaidChart
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
export { DARK_TOKENS, DEFAULT_CHART_THEME, LIGHT_TOKENS, MermaidChart, buildMermaidConfig, buildThemeVariables, configureChartTheme, downloadPng, downloadSvg, getChartTheme, hasCustomChartTheme, mermaidComponents, prepareSvg, resetChartTheme, resolveChartTokens, svgToPngBlob, tokenFingerprint, useIsDark };
|
|
707
|
+
//# sourceMappingURL=index.js.map
|
|
708
|
+
//# sourceMappingURL=index.js.map
|