Skip to content

Commit 565f52f

Browse files
committed
🐛 修复中心对齐功能的bug
1 parent 3ab5472 commit 565f52f

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/core/stage/stageManager/concreteMethods/StageNodeMoveManager.tsx

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,36 @@ export namespace StageNodeMoveManager {
8080
}
8181

8282
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+
9692
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;
9896
}
9997
}
10098

10199
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+
115109
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;
117113
}
118114
}
119115

0 commit comments

Comments
 (0)