@@ -80,40 +80,36 @@ export namespace StageNodeMoveManager {
80
80
}
81
81
82
82
export function alignCenterHorizontal ( ) {
83
- const nodes = Array . from ( StageManager . nodes ) . filter (
84
- ( node ) => node . isSelected ,
85
- ) ;
86
- const centerY =
87
- Math . min ( ...nodes . map ( ( node ) => node . rectangle . location . y ) ) +
88
- Math . max (
89
- ...nodes . map (
90
- ( node ) =>
91
- node . rectangle . location . y +
92
- node . rectangle . size . y -
93
- node . rectangle . size . y / 2 ,
94
- ) ,
95
- ) ;
83
+ const nodes = Array . from ( StageManager . nodes ) . filter ( node => node . isSelected ) ;
84
+ if ( nodes . length <= 1 ) return ; // 如果只有一个或没有选中的节点,则不需要重新排列
85
+
86
+ // 计算所有选中节点的总高度和最小 y 坐标
87
+ const minY = Math . min ( ...nodes . map ( node => node . rectangle . location . y ) ) ;
88
+ const maxY = Math . max ( ...nodes . map ( node => node . rectangle . location . y + node . rectangle . size . y ) ) ;
89
+ const totalHeight = maxY - minY ;
90
+ const centerY = minY + totalHeight / 2 ;
91
+
96
92
for ( const node of nodes ) {
97
- node . rectangle . location . y = centerY - node . rectangle . size . y / 2 ;
93
+ const nodeCenterY = node . rectangle . location . y + node . rectangle . size . y / 2 ;
94
+ const newY = centerY - ( nodeCenterY - node . rectangle . location . y ) ;
95
+ node . rectangle . location . y = newY ;
98
96
}
99
97
}
100
98
101
99
export function alignCenterVertical ( ) {
102
- const nodes = Array . from ( StageManager . nodes ) . filter (
103
- ( node ) => node . isSelected ,
104
- ) ;
105
- const centerX =
106
- Math . min ( ...nodes . map ( ( node ) => node . rectangle . location . x ) ) +
107
- Math . max (
108
- ...nodes . map (
109
- ( node ) =>
110
- node . rectangle . location . x +
111
- node . rectangle . size . x -
112
- node . rectangle . size . x / 2 ,
113
- ) ,
114
- ) ;
100
+ const nodes = Array . from ( StageManager . nodes ) . filter ( node => node . isSelected ) ;
101
+ if ( nodes . length <= 1 ) return ; // 如果只有一个或没有选中的节点,则不需要重新排列
102
+
103
+ // 计算所有选中节点的总宽度和最小 x 坐标
104
+ const minX = Math . min ( ...nodes . map ( node => node . rectangle . location . x ) ) ;
105
+ const maxX = Math . max ( ...nodes . map ( node => node . rectangle . location . x + node . rectangle . size . x ) ) ;
106
+ const totalWidth = maxX - minX ;
107
+ const centerX = minX + totalWidth / 2 ;
108
+
115
109
for ( const node of nodes ) {
116
- node . rectangle . location . x = centerX - node . rectangle . size . x / 2 ;
110
+ const nodeCenterX = node . rectangle . location . x + node . rectangle . size . x / 2 ;
111
+ const newX = centerX - ( nodeCenterX - node . rectangle . location . x ) ;
112
+ node . rectangle . location . x = newX ;
117
113
}
118
114
}
119
115
0 commit comments