diff --git a/apps/extension/src/components/Markdown/Mermaid.tsx b/apps/extension/src/components/Markdown/Mermaid.tsx index 7207a9e..b03ad0e 100644 --- a/apps/extension/src/components/Markdown/Mermaid.tsx +++ b/apps/extension/src/components/Markdown/Mermaid.tsx @@ -23,12 +23,60 @@ const MermaidChartEditor = ({ chart: initialChart, backgroundColor = 'lightcyan' const [svgContent, setSvgContent] = useState(''); const { t } = useTranslation() const [scale, setScale] = useState(1); + const [dragging, setDragging] = useState(false); + const [lastClientX, setLastClientX] = useState(0); + const [lastClientY, setLastClientY] = useState(0); + const [translateX, setTranslateX] = useState(0); + const [translateY, setTranslateY] = useState(0); + + const startDrag = (e) => { + setLastClientX(e.clientX); + setLastClientY(e.clientY); + setDragging(true); + }; + + const onDrag = (e) => { + if (!dragging) return; + const dx = e.clientX - lastClientX; + const dy = e.clientY - lastClientY; + setTranslateX(translateX + dx); + setTranslateY(translateY + dy); + setLastClientX(e.clientX); + setLastClientY(e.clientY); + }; + + const stopDrag = () => { + setDragging(false); + }; + + useEffect(() => { + if (dragging) { + window.addEventListener('mousemove', onDrag); + window.addEventListener('mouseup', stopDrag); + } + + return () => { + window.removeEventListener('mousemove', onDrag); + window.removeEventListener('mouseup', stopDrag); + }; + }, [dragging, onDrag, stopDrag]); const zoomIn = () => setScale(scale => scale + 0.1); const zoomOut = () => setScale(scale => Math.max(scale - 0.1, 0.1)); + + const handleWheelZoom = (e) => { + + e.preventDefault(); // 阻止默认的滚轮行为,即页面滚动 + const scaleChange = e.deltaY * -0.01; // 根据滚轮方向调整缩放变化量 + const newScale = Math.max(0.1, Math.min(scale + scaleChange, 5)); // 限制缩放级别在0.1到5之间 + setScale(newScale); + + }; + + useEffect(() => { setLoading(true); if (chart) { @@ -37,6 +85,7 @@ const MermaidChartEditor = ({ chart: initialChart, backgroundColor = 'lightcyan' try { const { svg, bindFunctions } = await mermaid.render('graphDiv', chart); setSvgContent(svg); + setLoading(false); if (bindFunctions && mermaidRef.current) { bindFunctions(mermaidRef.current); @@ -78,13 +127,13 @@ const MermaidChartEditor = ({ chart: initialChart, backgroundColor = 'lightcyan' const extra =
- +