@heikeson/md-engine 0.5.1 → 0.5.3
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 +35 -22
- package/dist/{chunk-RJZKMZOI.js → chunk-EJGRPSVC.js} +144 -91
- package/dist/chunk-EJGRPSVC.js.map +1 -0
- package/dist/{chunk-KC6CBSFU.js → chunk-ONVXIYKH.js} +10 -10
- package/dist/chunk-ONVXIYKH.js.map +1 -0
- package/dist/components/index.d.ts +14 -17
- package/dist/components/index.js +1 -1
- package/dist/editor/index.d.ts +2 -2
- package/dist/editor/index.js +3 -3
- package/dist/editor/index.js.map +1 -1
- package/dist/index.d.ts +20 -3
- package/dist/index.js +3 -3
- package/dist/mermaid/index.d.ts +4 -0
- package/dist/mermaid/index.js +349 -78
- package/dist/mermaid/index.js.map +1 -1
- package/dist/{render-DbUepYgE.d.ts → render-BxUxfxMc.d.ts} +1 -0
- package/dist/styles.css +1 -1
- package/package.json +2 -1
- package/styles/mermaid.css +55 -18
- package//345/257/271/346/216/245/345/245/221/347/272/246.md +35 -7
- package/dist/chunk-KC6CBSFU.js.map +0 -1
- package/dist/chunk-RJZKMZOI.js.map +0 -1
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,31 @@ function sanitizeSvg(svg) {
|
|
|
426
426
|
ADD_ATTR: ["dominant-baseline"]
|
|
427
427
|
});
|
|
428
428
|
}
|
|
429
|
+
var TAG_ATTR_RE = /\s([A-Za-z_:][-A-Za-z0-9_:.]*)\s*=\s*"([^"]*)"/g;
|
|
430
|
+
function attribute(tag, name) {
|
|
431
|
+
TAG_ATTR_RE.lastIndex = 0;
|
|
432
|
+
const target = name.toLowerCase();
|
|
433
|
+
let matched;
|
|
434
|
+
while ((matched = TAG_ATTR_RE.exec(tag)) !== null) {
|
|
435
|
+
if ((matched[1] ?? "").toLowerCase() === target) return matched[2] ?? "";
|
|
436
|
+
}
|
|
437
|
+
return "";
|
|
438
|
+
}
|
|
439
|
+
function readSvgSize(svg) {
|
|
440
|
+
if (!svg) return null;
|
|
441
|
+
const open = /<svg\b[^>]*>/i.exec(svg)?.[0] ?? "";
|
|
442
|
+
if (!open) return null;
|
|
443
|
+
const box = /viewBox\s*=\s*"([^"]+)"/i.exec(open)?.[1];
|
|
444
|
+
if (box) {
|
|
445
|
+
const parts = box.trim().split(/[\s,]+/).map(Number);
|
|
446
|
+
if (parts.length === 4 && parts[2] > 0 && parts[3] > 0) {
|
|
447
|
+
return { width: parts[2], height: parts[3] };
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
const width = Number(attribute(open, "width"));
|
|
451
|
+
const height = Number(attribute(open, "height"));
|
|
452
|
+
return width > 0 && height > 0 ? { width, height } : null;
|
|
453
|
+
}
|
|
429
454
|
function isParsableColor(value) {
|
|
430
455
|
return /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value) || /^rgba?\([^)]*\)$/i.test(value);
|
|
431
456
|
}
|
|
@@ -483,7 +508,7 @@ async function renderChart(source, tokens) {
|
|
|
483
508
|
return clean;
|
|
484
509
|
}
|
|
485
510
|
function useMermaidRender(source, enabled, dark, elementRef) {
|
|
486
|
-
const [state, setState] = useState({ status: "idle", svg: "", error: "" });
|
|
511
|
+
const [state, setState] = useState({ status: "idle", svg: "", size: null, error: "" });
|
|
487
512
|
const text = useUIText();
|
|
488
513
|
useEffect(() => {
|
|
489
514
|
if (!enabled) return;
|
|
@@ -492,12 +517,12 @@ function useMermaidRender(source, enabled, dark, elementRef) {
|
|
|
492
517
|
void (async () => {
|
|
493
518
|
try {
|
|
494
519
|
const svg = await renderChart(source, resolveChartTokens(dark, elementRef?.current ?? null));
|
|
495
|
-
if (!cancelled) setState({ status: "ready", svg, error: "" });
|
|
520
|
+
if (!cancelled) setState({ status: "ready", svg, size: readSvgSize(svg), error: "" });
|
|
496
521
|
} catch (error) {
|
|
497
522
|
if (!cancelled) {
|
|
498
523
|
const message = error instanceof MermaidRenderTimeoutError ? text.mermaidTimeout : error instanceof Error && error.message !== "MERMAID_RENDER_TIMEOUT" ? error.message : text.mermaidFallbackError;
|
|
499
524
|
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 });
|
|
525
|
+
setState({ status: "error", svg: "", size: null, error: message });
|
|
501
526
|
}
|
|
502
527
|
}
|
|
503
528
|
})();
|
|
@@ -513,6 +538,10 @@ var MAX_SCALE = 8;
|
|
|
513
538
|
var STEP_MIN = 0.75;
|
|
514
539
|
var STEP_MAX = 1.33;
|
|
515
540
|
var K = 15e-4;
|
|
541
|
+
var OVERSHOOT = 1;
|
|
542
|
+
var ANIMATION_DURATION = 200;
|
|
543
|
+
var CLICK_SLOP = 5;
|
|
544
|
+
var PAN_EXCLUDE = "a, img, image, button, input, textarea, select, option, [contenteditable='true']";
|
|
516
545
|
function normalizeDelta(event) {
|
|
517
546
|
if (event.deltaMode === 1) return event.deltaY * 16;
|
|
518
547
|
if (event.deltaMode === 2) return event.deltaY * 100;
|
|
@@ -521,97 +550,314 @@ function normalizeDelta(event) {
|
|
|
521
550
|
function clampScale(value) {
|
|
522
551
|
return Math.min(MAX_SCALE, Math.max(MIN_SCALE, value));
|
|
523
552
|
}
|
|
524
|
-
function
|
|
525
|
-
const
|
|
526
|
-
|
|
553
|
+
function axisBounds(viewportSize, contentSize) {
|
|
554
|
+
const slack = viewportSize - contentSize;
|
|
555
|
+
return { min: Math.min(0, slack), max: Math.max(0, slack) };
|
|
556
|
+
}
|
|
557
|
+
function alignToBounds(value, viewport, size) {
|
|
558
|
+
const boundsX = axisBounds(viewport.width, size.width * value.scale);
|
|
559
|
+
const boundsY = axisBounds(viewport.height, size.height * value.scale);
|
|
560
|
+
const x = Math.min(boundsX.max, Math.max(boundsX.min, value.x));
|
|
561
|
+
const y = Math.min(boundsY.max, Math.max(boundsY.min, value.y));
|
|
562
|
+
return x === value.x && y === value.y ? value : { scale: value.scale, x, y };
|
|
563
|
+
}
|
|
564
|
+
function clampOvershoot(value, bounds, viewportSize) {
|
|
565
|
+
const slack = viewportSize * OVERSHOOT;
|
|
566
|
+
return Math.min(bounds.max + slack, Math.max(bounds.min - slack, value));
|
|
567
|
+
}
|
|
568
|
+
function easeOut(progress) {
|
|
569
|
+
const inverse = 1 - progress;
|
|
570
|
+
return 1 - inverse * inverse * inverse;
|
|
571
|
+
}
|
|
572
|
+
function transformStyle(value) {
|
|
573
|
+
const scale = Number.parseFloat(value.scale.toFixed(8));
|
|
574
|
+
const x = Number.parseFloat(value.x.toFixed(2));
|
|
575
|
+
const y = Number.parseFloat(value.y.toFixed(2));
|
|
576
|
+
return `translate(${x}px, ${y}px) scale(${scale})`;
|
|
577
|
+
}
|
|
578
|
+
function distance(a, b) {
|
|
579
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
580
|
+
}
|
|
581
|
+
function midpoint(a, b) {
|
|
582
|
+
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
583
|
+
}
|
|
584
|
+
function usePanZoom(active, size, limit) {
|
|
585
|
+
const viewportRef = useRef(null);
|
|
586
|
+
const stageRef = useRef(null);
|
|
587
|
+
const [width, setWidth] = useState(0);
|
|
588
|
+
const [dragging, setDragging] = useState(false);
|
|
589
|
+
const current = useRef(IDENTITY);
|
|
527
590
|
const drag = useRef(null);
|
|
591
|
+
const pinch = useRef(null);
|
|
592
|
+
const pointers = useRef(/* @__PURE__ */ new Map());
|
|
593
|
+
const animation = useRef(null);
|
|
594
|
+
const cancelAlign = useCallback(() => {
|
|
595
|
+
if (animation.current !== null) {
|
|
596
|
+
cancelAnimationFrame(animation.current);
|
|
597
|
+
animation.current = null;
|
|
598
|
+
}
|
|
599
|
+
}, []);
|
|
600
|
+
const applyTransform = useCallback((next) => {
|
|
601
|
+
current.current = next;
|
|
602
|
+
const node = stageRef.current;
|
|
603
|
+
if (node) node.style.transform = transformStyle(next);
|
|
604
|
+
}, []);
|
|
605
|
+
const setStage = useCallback(
|
|
606
|
+
(node) => {
|
|
607
|
+
stageRef.current = node;
|
|
608
|
+
if (node) node.style.transform = transformStyle(current.current);
|
|
609
|
+
},
|
|
610
|
+
[]
|
|
611
|
+
);
|
|
612
|
+
const viewport = useMemo(() => {
|
|
613
|
+
if (!size || width <= 0 || size.width <= 0 || size.height <= 0) return null;
|
|
614
|
+
const fitByHeight = limit ? limit / size.height : Number.POSITIVE_INFINITY;
|
|
615
|
+
const fit = Math.min(1, width / size.width, fitByHeight);
|
|
616
|
+
return { width, height: limit ?? size.height * fit, fit };
|
|
617
|
+
}, [size, width, limit]);
|
|
618
|
+
const base = useMemo(() => {
|
|
619
|
+
if (!size || !viewport) return null;
|
|
620
|
+
return {
|
|
621
|
+
scale: viewport.fit,
|
|
622
|
+
x: (viewport.width - size.width * viewport.fit) / 2,
|
|
623
|
+
y: (viewport.height - size.height * viewport.fit) / 2
|
|
624
|
+
};
|
|
625
|
+
}, [size, viewport]);
|
|
626
|
+
useLayoutEffect(() => {
|
|
627
|
+
const node = viewportRef.current;
|
|
628
|
+
if (!node || !active) {
|
|
629
|
+
setWidth(0);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
const sync = () => {
|
|
633
|
+
setWidth((prev) => prev === node.clientWidth ? prev : node.clientWidth);
|
|
634
|
+
};
|
|
635
|
+
sync();
|
|
636
|
+
if (typeof ResizeObserver === "undefined") {
|
|
637
|
+
window.addEventListener("resize", sync);
|
|
638
|
+
return () => window.removeEventListener("resize", sync);
|
|
639
|
+
}
|
|
640
|
+
const observer2 = new ResizeObserver(sync);
|
|
641
|
+
observer2.observe(node);
|
|
642
|
+
return () => observer2.disconnect();
|
|
643
|
+
}, [active]);
|
|
644
|
+
useLayoutEffect(() => {
|
|
645
|
+
if (!base) return;
|
|
646
|
+
cancelAlign();
|
|
647
|
+
applyTransform(base);
|
|
648
|
+
}, [base, applyTransform, cancelAlign]);
|
|
649
|
+
const animateTo = useCallback(
|
|
650
|
+
(target, duration = ANIMATION_DURATION) => {
|
|
651
|
+
cancelAlign();
|
|
652
|
+
const from = current.current;
|
|
653
|
+
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;
|
|
654
|
+
if (settled) {
|
|
655
|
+
applyTransform(target);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const startedAt = performance.now();
|
|
659
|
+
const step = (now) => {
|
|
660
|
+
const progress = Math.min(1, (now - startedAt) / duration);
|
|
661
|
+
const eased = easeOut(progress);
|
|
662
|
+
applyTransform({
|
|
663
|
+
scale: from.scale + (target.scale - from.scale) * eased,
|
|
664
|
+
x: from.x + (target.x - from.x) * eased,
|
|
665
|
+
y: from.y + (target.y - from.y) * eased
|
|
666
|
+
});
|
|
667
|
+
animation.current = progress < 1 ? requestAnimationFrame(step) : null;
|
|
668
|
+
};
|
|
669
|
+
animation.current = requestAnimationFrame(step);
|
|
670
|
+
},
|
|
671
|
+
[applyTransform, cancelAlign]
|
|
672
|
+
);
|
|
673
|
+
const settle = useCallback(() => {
|
|
674
|
+
if (!size || !viewport) return;
|
|
675
|
+
const target = alignToBounds(current.current, viewport, size);
|
|
676
|
+
animateTo(target);
|
|
677
|
+
}, [size, viewport, animateTo]);
|
|
678
|
+
useEffect(() => cancelAlign, [cancelAlign]);
|
|
528
679
|
const zoomAt = useCallback(
|
|
529
|
-
(factor, point,
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
680
|
+
(factor, point, animated = false) => {
|
|
681
|
+
if (!size || !viewport) return;
|
|
682
|
+
const prev = current.current;
|
|
683
|
+
const scale = clampScale(prev.scale * factor);
|
|
684
|
+
if (scale === prev.scale) return;
|
|
685
|
+
const ratio = scale / prev.scale;
|
|
686
|
+
const target = alignToBounds(
|
|
687
|
+
{
|
|
688
|
+
scale,
|
|
536
689
|
x: point.x - (point.x - prev.x) * ratio,
|
|
537
690
|
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
|
-
});
|
|
691
|
+
},
|
|
692
|
+
viewport,
|
|
693
|
+
size
|
|
694
|
+
);
|
|
695
|
+
if (animated) animateTo(target);
|
|
696
|
+
else applyTransform(target);
|
|
551
697
|
},
|
|
552
|
-
[]
|
|
698
|
+
[size, viewport, applyTransform, animateTo]
|
|
553
699
|
);
|
|
554
700
|
useEffect(() => {
|
|
555
|
-
const node =
|
|
701
|
+
const node = viewportRef.current;
|
|
556
702
|
if (!node || !active) return;
|
|
557
703
|
const onWheel = (event) => {
|
|
704
|
+
if (pinch.current) return;
|
|
558
705
|
if (!event.ctrlKey && !event.metaKey && drag.current === null) return;
|
|
559
706
|
event.preventDefault();
|
|
707
|
+
cancelAlign();
|
|
560
708
|
const rect = node.getBoundingClientRect();
|
|
561
709
|
const factor = Math.min(STEP_MAX, Math.max(STEP_MIN, Math.exp(-normalizeDelta(event) * K)));
|
|
562
|
-
zoomAt(
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
710
|
+
zoomAt(factor, { x: event.clientX - rect.left, y: event.clientY - rect.top });
|
|
711
|
+
const session = drag.current;
|
|
712
|
+
if (session) {
|
|
713
|
+
drag.current = {
|
|
714
|
+
...session,
|
|
715
|
+
baseX: current.current.x,
|
|
716
|
+
baseY: current.current.y,
|
|
717
|
+
startX: event.clientX,
|
|
718
|
+
startY: event.clientY
|
|
719
|
+
};
|
|
720
|
+
}
|
|
567
721
|
};
|
|
568
722
|
node.addEventListener("wheel", onWheel, { passive: false });
|
|
569
723
|
return () => node.removeEventListener("wheel", onWheel);
|
|
570
|
-
}, [active, zoomAt]);
|
|
724
|
+
}, [active, zoomAt, cancelAlign]);
|
|
725
|
+
const beginPinch = useCallback(() => {
|
|
726
|
+
const list = [...pointers.current.values()].slice(0, 2);
|
|
727
|
+
if (list.length < 2) return;
|
|
728
|
+
pinch.current = {
|
|
729
|
+
startDistance: distance(list[0], list[1]),
|
|
730
|
+
startMid: midpoint(list[0], list[1]),
|
|
731
|
+
start: current.current
|
|
732
|
+
};
|
|
733
|
+
drag.current = null;
|
|
734
|
+
setDragging(false);
|
|
735
|
+
}, []);
|
|
571
736
|
const onPointerDown = useCallback(
|
|
572
737
|
(event) => {
|
|
573
738
|
if (!active || event.button !== 0) return;
|
|
739
|
+
if (event.target.closest(PAN_EXCLUDE)) return;
|
|
740
|
+
cancelAlign();
|
|
741
|
+
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
742
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
743
|
+
if (pointers.current.size >= 2) {
|
|
744
|
+
beginPinch();
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
574
747
|
drag.current = {
|
|
575
748
|
pointerId: event.pointerId,
|
|
576
|
-
baseX:
|
|
577
|
-
baseY:
|
|
749
|
+
baseX: current.current.x,
|
|
750
|
+
baseY: current.current.y,
|
|
578
751
|
startX: event.clientX,
|
|
579
|
-
startY: event.clientY
|
|
752
|
+
startY: event.clientY,
|
|
753
|
+
moved: false
|
|
580
754
|
};
|
|
581
|
-
|
|
755
|
+
setDragging(true);
|
|
582
756
|
},
|
|
583
|
-
[active,
|
|
757
|
+
[active, cancelAlign, beginPinch]
|
|
584
758
|
);
|
|
585
|
-
const onPointerMove = useCallback(
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
759
|
+
const onPointerMove = useCallback(
|
|
760
|
+
(event) => {
|
|
761
|
+
if (!size || !viewport) return;
|
|
762
|
+
pointers.current.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
763
|
+
const zooming = pinch.current;
|
|
764
|
+
if (zooming && pointers.current.size >= 2) {
|
|
765
|
+
const list = [...pointers.current.values()].slice(0, 2);
|
|
766
|
+
const spread = distance(list[0], list[1]);
|
|
767
|
+
if (zooming.startDistance <= 0 || spread <= 0) return;
|
|
768
|
+
const scale2 = clampScale(zooming.start.scale * (spread / zooming.startDistance));
|
|
769
|
+
const ratio = scale2 / zooming.start.scale;
|
|
770
|
+
const mid = midpoint(list[0], list[1]);
|
|
771
|
+
applyTransform({
|
|
772
|
+
scale: scale2,
|
|
773
|
+
x: clampOvershoot(
|
|
774
|
+
mid.x - (zooming.startMid.x - zooming.start.x) * ratio,
|
|
775
|
+
axisBounds(viewport.width, size.width * scale2),
|
|
776
|
+
viewport.width
|
|
777
|
+
),
|
|
778
|
+
y: clampOvershoot(
|
|
779
|
+
mid.y - (zooming.startMid.y - zooming.start.y) * ratio,
|
|
780
|
+
axisBounds(viewport.height, size.height * scale2),
|
|
781
|
+
viewport.height
|
|
782
|
+
)
|
|
783
|
+
});
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
const session = drag.current;
|
|
787
|
+
if (!session || session.pointerId !== event.pointerId) return;
|
|
788
|
+
const dx = event.clientX - session.startX;
|
|
789
|
+
const dy = event.clientY - session.startY;
|
|
790
|
+
if (!session.moved && Math.hypot(dx, dy) > CLICK_SLOP) session.moved = true;
|
|
791
|
+
const scale = current.current.scale;
|
|
792
|
+
applyTransform({
|
|
793
|
+
scale,
|
|
794
|
+
x: clampOvershoot(
|
|
795
|
+
session.baseX + dx,
|
|
796
|
+
axisBounds(viewport.width, size.width * scale),
|
|
797
|
+
viewport.width
|
|
798
|
+
),
|
|
799
|
+
y: clampOvershoot(
|
|
800
|
+
session.baseY + dy,
|
|
801
|
+
axisBounds(viewport.height, size.height * scale),
|
|
802
|
+
viewport.height
|
|
803
|
+
)
|
|
804
|
+
});
|
|
805
|
+
},
|
|
806
|
+
[size, viewport, applyTransform]
|
|
807
|
+
);
|
|
808
|
+
const onPointerUp = useCallback(
|
|
809
|
+
(event) => {
|
|
810
|
+
pointers.current.delete(event.pointerId);
|
|
811
|
+
const session = drag.current;
|
|
812
|
+
if (session && session.pointerId === event.pointerId) {
|
|
813
|
+
drag.current = null;
|
|
814
|
+
setDragging(false);
|
|
815
|
+
if (session.moved) settle();
|
|
816
|
+
}
|
|
817
|
+
if (pinch.current && pointers.current.size < 2) {
|
|
818
|
+
pinch.current = null;
|
|
819
|
+
settle();
|
|
820
|
+
}
|
|
821
|
+
},
|
|
822
|
+
[settle]
|
|
823
|
+
);
|
|
824
|
+
const center = useMemo(
|
|
825
|
+
() => viewport ? { x: viewport.width / 2, y: viewport.height / 2 } : { x: 0, y: 0 },
|
|
826
|
+
[viewport]
|
|
827
|
+
);
|
|
828
|
+
const zoomIn = useCallback(() => zoomAt(1.25, center, true), [zoomAt, center]);
|
|
829
|
+
const zoomOut = useCallback(() => zoomAt(0.8, center, true), [zoomAt, center]);
|
|
599
830
|
const reset = useCallback(() => {
|
|
600
831
|
drag.current = null;
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
832
|
+
pinch.current = null;
|
|
833
|
+
pointers.current.clear();
|
|
834
|
+
setDragging(false);
|
|
835
|
+
if (base) animateTo(base);
|
|
836
|
+
}, [base, animateTo]);
|
|
837
|
+
return {
|
|
838
|
+
/** 视口(手势面)ref */
|
|
839
|
+
ref: viewportRef,
|
|
840
|
+
/** 变换层回调 ref:手势期间由本 hook 直写它的 style */
|
|
841
|
+
stage: setStage,
|
|
842
|
+
dragging,
|
|
843
|
+
/** 视口高度:指定 height 时取上限,否则取初始适配高度 */
|
|
844
|
+
viewportHeight: viewport?.height ?? null,
|
|
845
|
+
/** 是否已算出初始视图(false 时应隐藏变换层,避免先 1:1 再跳) */
|
|
846
|
+
ready: base !== null,
|
|
847
|
+
zoomIn,
|
|
848
|
+
zoomOut,
|
|
849
|
+
reset,
|
|
850
|
+
onPointerDown,
|
|
851
|
+
onPointerMove,
|
|
852
|
+
onPointerUp
|
|
853
|
+
};
|
|
608
854
|
}
|
|
609
855
|
var PRELOAD_MARGIN = "200px 0px";
|
|
610
856
|
function viewportHeight(height) {
|
|
611
857
|
const raw = (height ?? "").trim().toLowerCase();
|
|
612
858
|
if (!raw || raw === "auto") return void 0;
|
|
613
859
|
const numeric = Number(raw);
|
|
614
|
-
return Number.isFinite(numeric) && numeric > 0 ?
|
|
860
|
+
return Number.isFinite(numeric) && numeric > 0 ? Math.round(numeric) : void 0;
|
|
615
861
|
}
|
|
616
862
|
function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
617
863
|
const source = String(raw ?? code ?? "");
|
|
@@ -621,10 +867,11 @@ function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
|
621
867
|
const dark = useIsDark(frameRef);
|
|
622
868
|
const [visible, setVisible] = useState(false);
|
|
623
869
|
const [showSource, setShowSource] = useState(disabled);
|
|
624
|
-
const { status, svg, error } = useMermaidRender(source, visible && !disabled, dark, frameRef);
|
|
870
|
+
const { status, svg, size, error } = useMermaidRender(source, visible && !disabled, dark, frameRef);
|
|
625
871
|
const canExport = status === "ready";
|
|
626
872
|
const ready = canExport && !showSource;
|
|
627
|
-
const
|
|
873
|
+
const heightLimit = viewportHeight(height);
|
|
874
|
+
const zoom = usePanZoom(ready && size !== null, size, heightLimit);
|
|
628
875
|
useEffect(() => {
|
|
629
876
|
const node = frameRef.current;
|
|
630
877
|
if (!node || disabled) return;
|
|
@@ -661,20 +908,44 @@ function MermaidChart({ raw, code, title, height, tooLarge }) {
|
|
|
661
908
|
onExportPng: () => void downloadPng(svg, resolveChartTokens(dark, frameRef.current).bg)
|
|
662
909
|
}
|
|
663
910
|
),
|
|
664
|
-
/* @__PURE__ */ jsx("div", { ref: frameRef, className: "lkes-mermaid-body",
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
911
|
+
/* @__PURE__ */ jsx("div", { ref: frameRef, className: "lkes-mermaid-body", children: ready ? (
|
|
912
|
+
// 视口层:手势面铺满整块图表区,空白处也能拖
|
|
913
|
+
/* @__PURE__ */ jsx(
|
|
914
|
+
"div",
|
|
915
|
+
{
|
|
916
|
+
ref: zoom.ref,
|
|
917
|
+
className: "lkes-mermaid-viewport",
|
|
918
|
+
"data-dragging": zoom.dragging ? "true" : void 0,
|
|
919
|
+
style: zoom.viewportHeight ? { height: `${Math.round(zoom.viewportHeight)}px` } : void 0,
|
|
920
|
+
onPointerDown: zoom.onPointerDown,
|
|
921
|
+
onPointerMove: zoom.onPointerMove,
|
|
922
|
+
onPointerUp: zoom.onPointerUp,
|
|
923
|
+
onPointerCancel: zoom.onPointerUp,
|
|
924
|
+
onDoubleClick: zoom.reset,
|
|
925
|
+
children: /* @__PURE__ */ jsx(
|
|
926
|
+
"div",
|
|
927
|
+
{
|
|
928
|
+
ref: zoom.stage,
|
|
929
|
+
className: "lkes-mermaid-canvas",
|
|
930
|
+
"data-static": size ? void 0 : "true",
|
|
931
|
+
style: {
|
|
932
|
+
// 自然尺寸:变换层坐标原点即视口左上角,缩放锚点才与指针一致。
|
|
933
|
+
// transform 不放在这里:手势期间由 use-zoom 直写,避免每帧 React 重渲染
|
|
934
|
+
width: size ? `${Math.round(size.width)}px` : void 0,
|
|
935
|
+
height: size ? `${Math.round(size.height)}px` : void 0,
|
|
936
|
+
// 尺寸算出来前先不亮相,避免先 1:1 再跳成适配视图;
|
|
937
|
+
// 尺寸根本解析不到时不做适配,也不能把它藏起来
|
|
938
|
+
visibility: size && !zoom.ready ? "hidden" : void 0
|
|
939
|
+
},
|
|
940
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
941
|
+
}
|
|
942
|
+
)
|
|
943
|
+
}
|
|
944
|
+
)
|
|
945
|
+
) : (
|
|
946
|
+
// 限高直接落在源码块上,body 不限高,百分比高度才不会失效(`overflow` 仍能滚动)
|
|
947
|
+
/* @__PURE__ */ jsx("pre", { className: "lkes-mermaid-source", style: { maxHeight: heightLimit ? `${heightLimit}px` : void 0 }, children: /* @__PURE__ */ jsx("code", { children: source }) })
|
|
948
|
+
) }),
|
|
678
949
|
status === "loading" ? /* @__PURE__ */ jsx("p", { className: "lkes-mermaid-hint", children: text.mermaidLoading }) : null,
|
|
679
950
|
error ? /* @__PURE__ */ jsxs("p", { className: "lkes-mermaid-error", children: [
|
|
680
951
|
text.mermaidErrorPrefix,
|