@heikeson/md-engine 0.5.1 → 0.5.2
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 +2 -1
- package/dist/mermaid/index.d.ts +4 -0
- package/dist/mermaid/index.js +343 -78
- package/dist/mermaid/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/styles/mermaid.css +55 -18
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +19 -5
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
> @updated 2026-11-02 20:50 版本口径对齐 0.5.0
|
|
9
9
|
> @updated 2026-11-02 21:33 容器语法表标注 `TypeItem` 可自闭合
|
|
10
10
|
> @updated 2026-11-02 21:40 版本口径对齐 0.5.1
|
|
11
|
-
> @
|
|
11
|
+
> @updated 2026-11-02 23:14 版本口径对齐 0.5.2
|
|
12
|
+
> @status v0.5.2
|
|
12
13
|
|
|
13
14
|
一个面向 React 的 Markdown 内容引擎。在标准 Markdown(GFM + 数学公式)之上,把
|
|
14
15
|
`<Callout>`、`<Cards><Card /></Cards>` 这类**尖括号容器**纳入解析流程,并保证
|
package/dist/mermaid/index.d.ts
CHANGED
|
@@ -5,12 +5,15 @@ import { a as MermaidProps } from '../mermaid-placeholder-DYfFG3_f.js';
|
|
|
5
5
|
* @file 交互式图表视图(客户端)
|
|
6
6
|
* @description 卡片结构:顶部工具栏 + 图表区(或源码区)+ 右下角悬浮缩放条。
|
|
7
7
|
* 未出图前一律显示源码——无 JS、加载失败、超限、空源码四种情况都不会白屏。
|
|
8
|
+
* 图表区里手势绑在「视口」层:整块区域都能拖,不再只有图片本身能拖。
|
|
8
9
|
* @created 2026-11-01 21:30
|
|
9
10
|
* @updated 2026-11-01 22:29 改为「顶部工具栏 + 右下角缩放条」的卡片式布局
|
|
10
11
|
* @updated 2026-11-01 22:37 标题并入工具栏;暗色与取色按图表容器判定;手势仅在出图后启用
|
|
11
12
|
* @updated 2026-11-01 22:51 导出 PNG 底色改走 resolveChartTokens
|
|
12
13
|
* @updated 2026-11-01 23:18 height 未指定 / 空 / auto 一律自动高度
|
|
13
14
|
* @updated 2026-11-02 10:38 固定文案改走 UIText(config.text 可覆写)
|
|
15
|
+
* @updated 2026-11-02 22:58 新增视口层:手势面铺满图表区,初始按适配比例居中
|
|
16
|
+
* @updated 2026-11-02 23:12 变换层改用回调 ref,transform 交由 use-zoom 直写(手势期不再触发重渲染)
|
|
14
17
|
*/
|
|
15
18
|
|
|
16
19
|
/**
|
|
@@ -156,6 +159,7 @@ declare function downloadPng(source: string, background: string): Promise<void>;
|
|
|
156
159
|
* @updated 2026-11-01 22:37 令牌三级回落(引擎令牌→宿主语义变量→内置)+ oklch/hsl 归一;取色改按图表元素
|
|
157
160
|
* @updated 2026-11-01 22:51 取色改为「JS 配置 → 组件自带 --lkes-chart-* → 内置」,不再读宿主变量
|
|
158
161
|
* @updated 2026-11-02 10:35 缓存加 LRU 上限(防编辑长文时内存只涨不落);超时改为可识别异常,文案由视图侧本地化
|
|
162
|
+
* @updated 2026-11-02 22:57 新增 readSvgSize:从产物解析图表自然尺寸,供交互视图做初始适配
|
|
159
163
|
*/
|
|
160
164
|
|
|
161
165
|
/**
|
package/dist/mermaid/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useUIText } from '../chunk-OJQDOFGL.js';
|
|
2
2
|
import '../chunk-OG47YL3H.js';
|
|
3
3
|
import '../chunk-YJGGJP2V.js';
|
|
4
|
-
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
4
|
+
import { useState, useEffect, useRef, useCallback, useMemo, useLayoutEffect } from 'react';
|
|
5
5
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
6
6
|
import DOMPurify from 'dompurify';
|
|
7
7
|
|
|
@@ -426,6 +426,25 @@ function sanitizeSvg(svg) {
|
|
|
426
426
|
ADD_ATTR: ["dominant-baseline"]
|
|
427
427
|
});
|
|
428
428
|
}
|
|
429
|
+
function attribute(tag, name) {
|
|
430
|
+
const matched = new RegExp(`\\s${name}\\s*=\\s*"([^"]*)"`, "i").exec(tag);
|
|
431
|
+
return matched ? matched[1] : "";
|
|
432
|
+
}
|
|
433
|
+
function readSvgSize(svg) {
|
|
434
|
+
if (!svg) return null;
|
|
435
|
+
const open = /<svg\b[^>]*>/i.exec(svg)?.[0] ?? "";
|
|
436
|
+
if (!open) return null;
|
|
437
|
+
const box = /viewBox\s*=\s*"([^"]+)"/i.exec(open)?.[1];
|
|
438
|
+
if (box) {
|
|
439
|
+
const parts = box.trim().split(/[\s,]+/).map(Number);
|
|
440
|
+
if (parts.length === 4 && parts[2] > 0 && parts[3] > 0) {
|
|
441
|
+
return { width: parts[2], height: parts[3] };
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
const width = Number(attribute(open, "width"));
|
|
445
|
+
const height = Number(attribute(open, "height"));
|
|
446
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
447
|
+
}
|
|
429
448
|
function isParsableColor(value) {
|
|
430
449
|
return /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value) || /^rgba?\([^)]*\)$/i.test(value);
|
|
431
450
|
}
|
|
@@ -483,7 +502,7 @@ async function renderChart(source, tokens) {
|
|
|
483
502
|
return clean;
|
|
484
503
|
}
|
|
485
504
|
function useMermaidRender(source, enabled, dark, elementRef) {
|
|
486
|
-
const [state, setState] = useState({ status: "idle", svg: "", error: "" });
|
|
505
|
+
const [state, setState] = useState({ status: "idle", svg: "", size: null, error: "" });
|
|
487
506
|
const text = useUIText();
|
|
488
507
|
useEffect(() => {
|
|
489
508
|
if (!enabled) return;
|
|
@@ -492,12 +511,12 @@ function useMermaidRender(source, enabled, dark, elementRef) {
|
|
|
492
511
|
void (async () => {
|
|
493
512
|
try {
|
|
494
513
|
const svg = await renderChart(source, resolveChartTokens(dark, elementRef?.current ?? null));
|
|
495
|
-
if (!cancelled) setState({ status: "ready", svg, error: "" });
|
|
514
|
+
if (!cancelled) setState({ status: "ready", svg, size: readSvgSize(svg), error: "" });
|
|
496
515
|
} catch (error) {
|
|
497
516
|
if (!cancelled) {
|
|
498
517
|
const message = error instanceof MermaidRenderTimeoutError ? text.mermaidTimeout : error instanceof Error && error.message !== "MERMAID_RENDER_TIMEOUT" ? error.message : text.mermaidFallbackError;
|
|
499
518
|
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 });
|
|
519
|
+
setState({ status: "error", svg: "", size: null, error: message });
|
|
501
520
|
}
|
|
502
521
|
}
|
|
503
522
|
})();
|
|
@@ -513,6 +532,10 @@ var MAX_SCALE = 8;
|
|
|
513
532
|
var STEP_MIN = 0.75;
|
|
514
533
|
var STEP_MAX = 1.33;
|
|
515
534
|
var K = 15e-4;
|
|
535
|
+
var OVERSHOOT = 1;
|
|
536
|
+
var ANIMATION_DURATION = 200;
|
|
537
|
+
var CLICK_SLOP = 5;
|
|
538
|
+
var PAN_EXCLUDE = "a, img, image, button, input, textarea, select, option, [contenteditable='true']";
|
|
516
539
|
function normalizeDelta(event) {
|
|
517
540
|
if (event.deltaMode === 1) return event.deltaY * 16;
|
|
518
541
|
if (event.deltaMode === 2) return event.deltaY * 100;
|
|
@@ -521,97 +544,314 @@ function normalizeDelta(event) {
|
|
|
521
544
|
function clampScale(value) {
|
|
522
545
|
return Math.min(MAX_SCALE, Math.max(MIN_SCALE, value));
|
|
523
546
|
}
|
|
524
|
-
function
|
|
525
|
-
const
|
|
526
|
-
|
|
547
|
+
function axisBounds(viewportSize, contentSize) {
|
|
548
|
+
const slack = viewportSize - contentSize;
|
|
549
|
+
return { min: Math.min(0, slack), max: Math.max(0, slack) };
|
|
550
|
+
}
|
|
551
|
+
function alignToBounds(value, viewport, size) {
|
|
552
|
+
const boundsX = axisBounds(viewport.width, size.width * value.scale);
|
|
553
|
+
const boundsY = axisBounds(viewport.height, size.height * value.scale);
|
|
554
|
+
const x = Math.min(boundsX.max, Math.max(boundsX.min, value.x));
|
|
555
|
+
const y = Math.min(boundsY.max, Math.max(boundsY.min, value.y));
|
|
556
|
+
return x === value.x && y === value.y ? value : { scale: value.scale, x, y };
|
|
557
|
+
}
|
|
558
|
+
function clampOvershoot(value, bounds, viewportSize) {
|
|
559
|
+
const slack = viewportSize * OVERSHOOT;
|
|
560
|
+
return Math.min(bounds.max + slack, Math.max(bounds.min - slack, value));
|
|
561
|
+
}
|
|
562
|
+
function easeOut(progress) {
|
|
563
|
+
const inverse = 1 - progress;
|
|
564
|
+
return 1 - inverse * inverse * inverse;
|
|
565
|
+
}
|
|
566
|
+
function transformStyle(value) {
|
|
567
|
+
const scale = Number.parseFloat(value.scale.toFixed(8));
|
|
568
|
+
const x = Number.parseFloat(value.x.toFixed(2));
|
|
569
|
+
const y = Number.parseFloat(value.y.toFixed(2));
|
|
570
|
+
return `translate(${x}px, ${y}px) scale(${scale})`;
|
|
571
|
+
}
|
|
572
|
+
function distance(a, b) {
|
|
573
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
574
|
+
}
|
|
575
|
+
function midpoint(a, b) {
|
|
576
|
+
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
577
|
+
}
|
|
578
|
+
function usePanZoom(active, size, limit) {
|
|
579
|
+
const viewportRef = useRef(null);
|
|
580
|
+
const stageRef = useRef(null);
|
|
581
|
+
const [width, setWidth] = useState(0);
|
|
582
|
+
const [dragging, setDragging] = useState(false);
|
|
583
|
+
const current = useRef(IDENTITY);
|
|
527
584
|
const drag = useRef(null);
|
|
585
|
+
const pinch = useRef(null);
|
|
586
|
+
const pointers = useRef(/* @__PURE__ */ new Map());
|
|
587
|
+
const animation = useRef(null);
|
|
588
|
+
const cancelAlign = useCallback(() => {
|
|
589
|
+
if (animation.current !== null) {
|
|
590
|
+
cancelAnimationFrame(animation.current);
|
|
591
|
+
animation.current = null;
|
|
592
|
+
}
|
|
593
|
+
}, []);
|
|
594
|
+
const applyTransform = useCallback((next) => {
|
|
595
|
+
current.current = next;
|
|
596
|
+
const node = stageRef.current;
|
|
597
|
+
if (node) node.style.transform = transformStyle(next);
|
|
598
|
+
}, []);
|
|
599
|
+
const setStage = useCallback(
|
|
600
|
+
(node) => {
|
|
601
|
+
stageRef.current = node;
|
|
602
|
+
if (node) node.style.transform = transformStyle(current.current);
|
|
603
|
+
},
|
|
604
|
+
[]
|
|
605
|
+
);
|
|
606
|
+
const viewport = useMemo(() => {
|
|
607
|
+
if (!size || width <= 0 || size.width <= 0 || size.height <= 0) return null;
|
|
608
|
+
const fitByHeight = limit ? limit / size.height : Number.POSITIVE_INFINITY;
|
|
609
|
+
const fit = Math.min(1, width / size.width, fitByHeight);
|
|
610
|
+
return { width, height: limit ?? size.height * fit, fit };
|
|
611
|
+
}, [size, width, limit]);
|
|
612
|
+
const base = useMemo(() => {
|
|
613
|
+
if (!size || !viewport) return null;
|
|
614
|
+
return {
|
|
615
|
+
scale: viewport.fit,
|
|
616
|
+
x: (viewport.width - size.width * viewport.fit) / 2,
|
|
617
|
+
y: (viewport.height - size.height * viewport.fit) / 2
|
|
618
|
+
};
|
|
619
|
+
}, [size, viewport]);
|
|
620
|
+
useLayoutEffect(() => {
|
|
621
|
+
const node = viewportRef.current;
|
|
622
|
+
if (!node || !active) {
|
|
623
|
+
setWidth(0);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const sync = () => {
|
|
627
|
+
setWidth((prev) => prev === node.clientWidth ? prev : node.clientWidth);
|
|
628
|
+
};
|
|
629
|
+
sync();
|
|
630
|
+
if (typeof ResizeObserver === "undefined") {
|
|
631
|
+
window.addEventListener("resize", sync);
|
|
632
|
+
return () => window.removeEventListener("resize", sync);
|
|
633
|
+
}
|
|
634
|
+
const observer2 = new ResizeObserver(sync);
|
|
635
|
+
observer2.observe(node);
|
|
636
|
+
return () => observer2.disconnect();
|
|
637
|
+
}, [active]);
|
|
638
|
+
useLayoutEffect(() => {
|
|
639
|
+
if (!base) return;
|
|
640
|
+
cancelAlign();
|
|
641
|
+
applyTransform(base);
|
|
642
|
+
}, [base, applyTransform, cancelAlign]);
|
|
643
|
+
const animateTo = useCallback(
|
|
644
|
+
(target, duration = ANIMATION_DURATION) => {
|
|
645
|
+
cancelAlign();
|
|
646
|
+
const from = current.current;
|
|
647
|
+
const settled = Math.abs(target.x - from.x) < 0.5 && Math.abs(target.y - from.y) < 0.5 && Math.abs(target.scale - from.scale) < 1e-3;
|
|
648
|
+
if (settled) {
|
|
649
|
+
applyTransform(target);
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const startedAt = performance.now();
|
|
653
|
+
const step = (now) => {
|
|
654
|
+
const progress = Math.min(1, (now - startedAt) / duration);
|
|
655
|
+
const eased = easeOut(progress);
|
|
656
|
+
applyTransform({
|
|
657
|
+
scale: from.scale + (target.scale - from.scale) * eased,
|
|
658
|
+
x: from.x + (target.x - from.x) * eased,
|
|
659
|
+
y: from.y + (target.y - from.y) * eased
|
|
660
|
+
});
|
|
661
|
+
animation.current = progress < 1 ? requestAnimationFrame(step) : null;
|
|
662
|
+
};
|
|
663
|
+
animation.current = requestAnimationFrame(step);
|
|
664
|
+
},
|
|
665
|
+
[applyTransform, cancelAlign]
|
|
666
|
+
);
|
|
667
|
+
const settle = useCallback(() => {
|
|
668
|
+
if (!size || !viewport) return;
|
|
669
|
+
const target = alignToBounds(current.current, viewport, size);
|
|
670
|
+
animateTo(target);
|
|
671
|
+
}, [size, viewport, animateTo]);
|
|
672
|
+
useEffect(() => cancelAlign, [cancelAlign]);
|
|
528
673
|
const zoomAt = useCallback(
|
|
529
|
-
(factor, point,
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
674
|
+
(factor, point, animated = false) => {
|
|
675
|
+
if (!size || !viewport) return;
|
|
676
|
+
const prev = current.current;
|
|
677
|
+
const scale = clampScale(prev.scale * factor);
|
|
678
|
+
if (scale === prev.scale) return;
|
|
679
|
+
const ratio = scale / prev.scale;
|
|
680
|
+
const target = alignToBounds(
|
|
681
|
+
{
|
|
682
|
+
scale,
|
|
536
683
|
x: point.x - (point.x - prev.x) * ratio,
|
|
537
684
|
y: point.y - (point.y - prev.y) * ratio
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
baseY: moved.y,
|
|
545
|
-
startX: cursor.x,
|
|
546
|
-
startY: cursor.y
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
return moved;
|
|
550
|
-
});
|
|
685
|
+
},
|
|
686
|
+
viewport,
|
|
687
|
+
size
|
|
688
|
+
);
|
|
689
|
+
if (animated) animateTo(target);
|
|
690
|
+
else applyTransform(target);
|
|
551
691
|
},
|
|
552
|
-
[]
|
|
692
|
+
[size, viewport, applyTransform, animateTo]
|
|
553
693
|
);
|
|
554
694
|
useEffect(() => {
|
|
555
|
-
const node =
|
|
695
|
+
const node = viewportRef.current;
|
|
556
696
|
if (!node || !active) return;
|
|
557
697
|
const onWheel = (event) => {
|
|
698
|
+
if (pinch.current) return;
|
|
558
699
|
if (!event.ctrlKey && !event.metaKey && drag.current === null) return;
|
|
559
700
|
event.preventDefault();
|
|
701
|
+
cancelAlign();
|
|
560
702
|
const rect = node.getBoundingClientRect();
|
|
561
703
|
const factor = Math.min(STEP_MAX, Math.max(STEP_MIN, Math.exp(-normalizeDelta(event) * K)));
|
|
562
|
-
zoomAt(
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
704
|
+
zoomAt(factor, { x: event.clientX - rect.left, y: event.clientY - rect.top });
|
|
705
|
+
const session = drag.current;
|
|
706
|
+
if (session) {
|
|
707
|
+
drag.current = {
|
|
708
|
+
...session,
|
|
709
|
+
baseX: current.current.x,
|
|
710
|
+
baseY: current.current.y,
|
|
711
|
+
startX: event.clientX,
|
|
712
|
+
startY: event.clientY
|
|
713
|
+
};
|
|
714
|
+
}
|
|
567
715
|
};
|
|
568
716
|
node.addEventListener("wheel", onWheel, { passive: false });
|
|
569
717
|
return () => node.removeEventListener("wheel", onWheel);
|
|
570
|
-
}, [active, zoomAt]);
|
|
718
|
+
}, [active, zoomAt, cancelAlign]);
|
|
719
|
+
const beginPinch = useCallback(() => {
|
|
720
|
+
const list = [...pointers.current.values()].slice(0, 2);
|
|
721
|
+
if (list.length < 2) return;
|
|
722
|
+
pinch.current = {
|
|
723
|
+
startDistance: distance(list[0], list[1]),
|
|
724
|
+
startMid: midpoint(list[0], list[1]),
|
|
725
|
+
start: current.current
|
|
726
|
+
};
|
|
727
|
+
drag.current = null;
|
|
728
|
+
setDragging(false);
|
|
729
|
+
}, []);
|
|
571
730
|
const onPointerDown = useCallback(
|
|
572
731
|
(event) => {
|
|
573
732
|
if (!active || event.button !== 0) return;
|
|
733
|
+
if (event.target.closest(PAN_EXCLUDE)) return;
|
|
734
|
+
cancelAlign();
|
|
735
|
+
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
736
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
737
|
+
if (pointers.current.size >= 2) {
|
|
738
|
+
beginPinch();
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
574
741
|
drag.current = {
|
|
575
742
|
pointerId: event.pointerId,
|
|
576
|
-
baseX:
|
|
577
|
-
baseY:
|
|
743
|
+
baseX: current.current.x,
|
|
744
|
+
baseY: current.current.y,
|
|
578
745
|
startX: event.clientX,
|
|
579
|
-
startY: event.clientY
|
|
746
|
+
startY: event.clientY,
|
|
747
|
+
moved: false
|
|
580
748
|
};
|
|
581
|
-
|
|
749
|
+
setDragging(true);
|
|
582
750
|
},
|
|
583
|
-
[active,
|
|
751
|
+
[active, cancelAlign, beginPinch]
|
|
584
752
|
);
|
|
585
|
-
const onPointerMove = useCallback(
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
753
|
+
const onPointerMove = useCallback(
|
|
754
|
+
(event) => {
|
|
755
|
+
if (!size || !viewport) return;
|
|
756
|
+
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
757
|
+
const zooming = pinch.current;
|
|
758
|
+
if (zooming && pointers.current.size >= 2) {
|
|
759
|
+
const list = [...pointers.current.values()].slice(0, 2);
|
|
760
|
+
const spread = distance(list[0], list[1]);
|
|
761
|
+
if (zooming.startDistance <= 0 || spread <= 0) return;
|
|
762
|
+
const scale2 = clampScale(zooming.start.scale * (spread / zooming.startDistance));
|
|
763
|
+
const ratio = scale2 / zooming.start.scale;
|
|
764
|
+
const mid = midpoint(list[0], list[1]);
|
|
765
|
+
applyTransform({
|
|
766
|
+
scale: scale2,
|
|
767
|
+
x: clampOvershoot(
|
|
768
|
+
mid.x - (zooming.startMid.x - zooming.start.x) * ratio,
|
|
769
|
+
axisBounds(viewport.width, size.width * scale2),
|
|
770
|
+
viewport.width
|
|
771
|
+
),
|
|
772
|
+
y: clampOvershoot(
|
|
773
|
+
mid.y - (zooming.startMid.y - zooming.start.y) * ratio,
|
|
774
|
+
axisBounds(viewport.height, size.height * scale2),
|
|
775
|
+
viewport.height
|
|
776
|
+
)
|
|
777
|
+
});
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
const session = drag.current;
|
|
781
|
+
if (!session || session.pointerId !== event.pointerId) return;
|
|
782
|
+
const dx = event.clientX - session.startX;
|
|
783
|
+
const dy = event.clientY - session.startY;
|
|
784
|
+
if (!session.moved && Math.hypot(dx, dy) > CLICK_SLOP) session.moved = true;
|
|
785
|
+
const scale = current.current.scale;
|
|
786
|
+
applyTransform({
|
|
787
|
+
scale,
|
|
788
|
+
x: clampOvershoot(
|
|
789
|
+
session.baseX + dx,
|
|
790
|
+
axisBounds(viewport.width, size.width * scale),
|
|
791
|
+
viewport.width
|
|
792
|
+
),
|
|
793
|
+
y: clampOvershoot(
|
|
794
|
+
session.baseY + dy,
|
|
795
|
+
axisBounds(viewport.height, size.height * scale),
|
|
796
|
+
viewport.height
|
|
797
|
+
)
|
|
798
|
+
});
|
|
799
|
+
},
|
|
800
|
+
[size, viewport, applyTransform]
|
|
801
|
+
);
|
|
802
|
+
const onPointerUp = useCallback(
|
|
803
|
+
(event) => {
|
|
804
|
+
pointers.current.delete(event.pointerId);
|
|
805
|
+
const session = drag.current;
|
|
806
|
+
if (session && session.pointerId === event.pointerId) {
|
|
807
|
+
drag.current = null;
|
|
808
|
+
setDragging(false);
|
|
809
|
+
if (session.moved) settle();
|
|
810
|
+
}
|
|
811
|
+
if (pinch.current && pointers.current.size < 2) {
|
|
812
|
+
pinch.current = null;
|
|
813
|
+
settle();
|
|
814
|
+
}
|
|
815
|
+
},
|
|
816
|
+
[settle]
|
|
817
|
+
);
|
|
818
|
+
const center = useMemo(
|
|
819
|
+
() => viewport ? { x: viewport.width / 2, y: viewport.height / 2 } : { x: 0, y: 0 },
|
|
820
|
+
[viewport]
|
|
821
|
+
);
|
|
822
|
+
const zoomIn = useCallback(() => zoomAt(1.25, center, true), [zoomAt, center]);
|
|
823
|
+
const zoomOut = useCallback(() => zoomAt(0.8, center, true), [zoomAt, center]);
|
|
599
824
|
const reset = useCallback(() => {
|
|
600
825
|
drag.current = null;
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
826
|
+
pinch.current = null;
|
|
827
|
+
pointers.current.clear();
|
|
828
|
+
setDragging(false);
|
|
829
|
+
if (base) animateTo(base);
|
|
830
|
+
}, [base, animateTo]);
|
|
831
|
+
return {
|
|
832
|
+
/** 视口(手势面)ref */
|
|
833
|
+
ref: viewportRef,
|
|
834
|
+
/** 变换层回调 ref:手势期间由本 hook 直写它的 style */
|
|
835
|
+
stage: setStage,
|
|
836
|
+
dragging,
|
|
837
|
+
/** 视口高度:指定 height 时取上限,否则取初始适配高度 */
|
|
838
|
+
viewportHeight: viewport?.height ?? null,
|
|
839
|
+
/** 是否已算出初始视图(false 时应隐藏变换层,避免先 1:1 再跳) */
|
|
840
|
+
ready: base !== null,
|
|
841
|
+
zoomIn,
|
|
842
|
+
zoomOut,
|
|
843
|
+
reset,
|
|
844
|
+
onPointerDown,
|
|
845
|
+
onPointerMove,
|
|
846
|
+
onPointerUp
|
|
847
|
+
};
|
|
608
848
|
}
|
|
609
849
|
var PRELOAD_MARGIN = "200px 0px";
|
|
610
850
|
function viewportHeight(height) {
|
|
611
851
|
const raw = (height ?? "").trim().toLowerCase();
|
|
612
852
|
if (!raw || raw === "auto") return void 0;
|
|
613
853
|
const numeric = Number(raw);
|
|
614
|
-
return Number.isFinite(numeric) && numeric > 0 ?
|
|
854
|
+
return Number.isFinite(numeric) && numeric > 0 ? Math.round(numeric) : void 0;
|
|
615
855
|
}
|
|
616
856
|
function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
617
857
|
const source = String(raw ?? code ?? "");
|
|
@@ -621,10 +861,11 @@ function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
|
621
861
|
const dark = useIsDark(frameRef);
|
|
622
862
|
const [visible, setVisible] = useState(false);
|
|
623
863
|
const [showSource, setShowSource] = useState(disabled);
|
|
624
|
-
const { status, svg, error } = useMermaidRender(source, visible && !disabled, dark, frameRef);
|
|
864
|
+
const { status, svg, size, error } = useMermaidRender(source, visible && !disabled, dark, frameRef);
|
|
625
865
|
const canExport = status === "ready";
|
|
626
866
|
const ready = canExport && !showSource;
|
|
627
|
-
const
|
|
867
|
+
const heightLimit = viewportHeight(height);
|
|
868
|
+
const zoom = usePanZoom(ready && size !== null, size, heightLimit);
|
|
628
869
|
useEffect(() => {
|
|
629
870
|
const node = frameRef.current;
|
|
630
871
|
if (!node || disabled) return;
|
|
@@ -661,20 +902,44 @@ function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
|
661
902
|
onExportPng: () => void downloadPng(svg, resolveChartTokens(dark, frameRef.current).bg)
|
|
662
903
|
}
|
|
663
904
|
),
|
|
664
|
-
/* @__PURE__ */ jsx("div", { ref: frameRef, className: "lkes-mermaid-body",
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
905
|
+
/* @__PURE__ */ jsx("div", { ref: frameRef, className: "lkes-mermaid-body", children: ready ? (
|
|
906
|
+
// 视口层:手势面铺满整块图表区,空白处也能拖
|
|
907
|
+
/* @__PURE__ */ jsx(
|
|
908
|
+
"div",
|
|
909
|
+
{
|
|
910
|
+
ref: zoom.ref,
|
|
911
|
+
className: "lkes-mermaid-viewport",
|
|
912
|
+
"data-dragging": zoom.dragging ? "true" : void 0,
|
|
913
|
+
style: zoom.viewportHeight ? { height: `${Math.round(zoom.viewportHeight)}px` } : void 0,
|
|
914
|
+
onPointerDown: zoom.onPointerDown,
|
|
915
|
+
onPointerMove: zoom.onPointerMove,
|
|
916
|
+
onPointerUp: zoom.onPointerUp,
|
|
917
|
+
onPointerCancel: zoom.onPointerUp,
|
|
918
|
+
onDoubleClick: zoom.reset,
|
|
919
|
+
children: /* @__PURE__ */ jsx(
|
|
920
|
+
"div",
|
|
921
|
+
{
|
|
922
|
+
ref: zoom.stage,
|
|
923
|
+
className: "lkes-mermaid-canvas",
|
|
924
|
+
"data-static": size ? void 0 : "true",
|
|
925
|
+
style: {
|
|
926
|
+
// 自然尺寸:变换层坐标原点即视口左上角,缩放锚点才与指针一致。
|
|
927
|
+
// transform 不放在这里:手势期间由 use-zoom 直写,避免每帧 React 重渲染
|
|
928
|
+
width: size ? `${Math.round(size.width)}px` : void 0,
|
|
929
|
+
height: size ? `${Math.round(size.height)}px` : void 0,
|
|
930
|
+
// 尺寸算出来前先不亮相,避免先 1:1 再跳成适配视图;
|
|
931
|
+
// 尺寸根本解析不到时不做适配,也不能把它藏起来
|
|
932
|
+
visibility: size && !zoom.ready ? "hidden" : void 0
|
|
933
|
+
},
|
|
934
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
935
|
+
}
|
|
936
|
+
)
|
|
937
|
+
}
|
|
938
|
+
)
|
|
939
|
+
) : (
|
|
940
|
+
// 限高直接落在源码块上,body 不限高,百分比高度才不会失效(`overflow` 仍能滚动)
|
|
941
|
+
/* @__PURE__ */ jsx("pre", { className: "lkes-mermaid-source", style: { maxHeight: heightLimit ? `${heightLimit}px` : void 0 }, children: /* @__PURE__ */ jsx("code", { children: source }) })
|
|
942
|
+
) }),
|
|
678
943
|
status === "loading" ? /* @__PURE__ */ jsx("p", { className: "lkes-mermaid-hint", children: text.mermaidLoading }) : null,
|
|
679
944
|
error ? /* @__PURE__ */ jsxs("p", { className: "lkes-mermaid-error", children: [
|
|
680
945
|
text.mermaidErrorPrefix,
|