@@ -88,7 +88,6 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
8888 const prevPositionsRef = useRef < Map < number , { x : number ; y : number } > > ( new Map ( ) ) ;
8989 const [ dimensions , setDimensions ] = useState ( { width : propWidth , height : propHeight } ) ;
9090 const zoomRef = useRef < d3 . ZoomBehavior < SVGSVGElement , unknown > | null > ( null ) ;
91- const [ zoomScale , setZoomScale ] = useState ( 1 ) ;
9291
9392 // 监听容器大小变化
9493 useEffect ( ( ) => {
@@ -118,7 +117,7 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
118117 const width = dimensions . width ;
119118 const height = dimensions . height ;
120119
121- // 初始化 zoom 行为
120+ // 初始化 zoom 行为(支持鼠标滚轮缩放和拖动平移)
122121 useEffect ( ( ) => {
123122 if ( ! svgRef . current ) return ;
124123
@@ -131,7 +130,6 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
131130 const mainGroup = svg . select ( '.main-group' ) ;
132131 if ( ! mainGroup . empty ( ) ) {
133132 mainGroup . attr ( 'transform' , event . transform ) ;
134- setZoomScale ( event . transform . k ) ;
135133 }
136134 } ) ;
137135
@@ -154,27 +152,6 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
154152 setTreeState ( tree . clone ( ) ) ;
155153 prevPositionsRef . current . clear ( ) ;
156154 } , [ tree ] ) ;
157-
158- // 重置缩放当树改变时
159- const resetZoom = ( ) => {
160- if ( ! svgRef . current || ! zoomRef . current ) return ;
161- const svg = d3 . select ( svgRef . current ) ;
162- svg . transition ( ) . duration ( 300 ) . call ( zoomRef . current . transform , d3 . zoomIdentity ) ;
163- } ;
164-
165- // 放大
166- const zoomIn = ( ) => {
167- if ( ! svgRef . current || ! zoomRef . current ) return ;
168- const svg = d3 . select ( svgRef . current ) ;
169- svg . transition ( ) . duration ( 200 ) . call ( zoomRef . current . scaleBy , 1.3 ) ;
170- } ;
171-
172- // 缩小
173- const zoomOut = ( ) => {
174- if ( ! svgRef . current || ! zoomRef . current ) return ;
175- const svg = d3 . select ( svgRef . current ) ;
176- svg . transition ( ) . duration ( 200 ) . call ( zoomRef . current . scaleBy , 0.7 ) ;
177- } ;
178155
179156 // 应用步骤到树上
180157 useEffect ( ( ) => {
@@ -213,13 +190,6 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
213190 const nodePositions : NodePosition [ ] = [ ] ;
214191 const linkData : LinkData [ ] = [ ] ;
215192
216- // 计算树的深度
217- const getTreeDepth = ( node : TreeNode | null ) : number => {
218- if ( ! node ) return 0 ;
219- return 1 + Math . max ( getTreeDepth ( node . left ) , getTreeDepth ( node . right ) ) ;
220- } ;
221-
222- const treeDepth = getTreeDepth ( treeState ) ;
223193 // 使用画布宽度减去边距作为有效宽度
224194 const leftPadding = 80 ; // 左边距,用于标注显示
225195 const rightPadding = 60 ; // 右边距
@@ -607,24 +577,7 @@ const TreeCanvas: React.FC<TreeCanvasProps> = ({
607577 return (
608578 < div className = "tree-canvas-container" ref = { containerRef } >
609579 < svg ref = { svgRef } width = { width } height = { height } />
610- < div className = "zoom-controls" >
611- < button className = "zoom-btn" onClick = { zoomIn } title = "放大" >
612- < svg viewBox = "0 0 24 24" width = "18" height = "18" fill = "currentColor" >
613- < path d = "M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
614- </ svg >
615- </ button >
616- < span className = "zoom-level" > { Math . round ( zoomScale * 100 ) } %</ span >
617- < button className = "zoom-btn" onClick = { zoomOut } title = "缩小" >
618- < svg viewBox = "0 0 24 24" width = "18" height = "18" fill = "currentColor" >
619- < path d = "M19 13H5v-2h14v2z" />
620- </ svg >
621- </ button >
622- < button className = "zoom-btn" onClick = { resetZoom } title = "重置缩放" >
623- < svg viewBox = "0 0 24 24" width = "18" height = "18" fill = "currentColor" >
624- < path d = "M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z" />
625- </ svg >
626- </ button >
627- </ div >
580+ { /* 缩放控制按钮已隐藏,用户可使用鼠标滚轮缩放、拖动平移 */ }
628581 < div className = "zoom-hint" >
629582 < span > 🖱️ 拖动平移 | 滚轮缩放</ span >
630583 </ div >
0 commit comments