Skip to content

Commit 93cbfe8

Browse files
authored
Merge pull request #244 from boostcampwm-2024/bug-fe-#243
가까운 엣지 계산 최적화
2 parents f2572a5 + d838207 commit 93cbfe8

File tree

2 files changed

+58
-84
lines changed

2 files changed

+58
-84
lines changed

Diff for: apps/frontend/src/components/canvas/index.tsx

+24-84
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
BackgroundVariant,
1111
ConnectionMode,
1212
type Node,
13-
Position,
1413
NodeChange,
1514
Edge,
1615
EdgeChange,
@@ -25,11 +24,9 @@ import { SocketIOProvider } from "y-socket.io";
2524
import { cn } from "@/lib/utils";
2625
import { useQueryClient } from "@tanstack/react-query";
2726
import useYDocStore from "@/store/useYDocStore";
28-
2927
import { useCollaborativeCursors } from "@/hooks/useCursor";
3028
import { CollaborativeCursors } from "../CursorView";
31-
32-
import { getHandlePosition } from "@/lib/getHandlePosition";
29+
import { calculateBestHandles } from "@/lib/calculateBestHandles";
3330

3431
const proOptions = { hideAttribution: true };
3532

@@ -46,7 +43,6 @@ function Flow({ className }: CanvasProps) {
4643
const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);
4744
const { pages } = usePages();
4845
const queryClient = useQueryClient();
49-
5046
const { ydoc } = useYDocStore();
5147

5248
const { cursors, handleMouseMove, handleNodeDrag, handleMouseLeave } =
@@ -218,56 +214,25 @@ function Flow({ className }: CanvasProps) {
218214
};
219215
nodesMap.set(change.id, updatedYNode);
220216

221-
edges.forEach((edge) => {
222-
if (edge.source === change.id || edge.target === change.id) {
223-
const sourceNode = nodes.find((n) => n.id === edge.source);
224-
const targetNode = nodes.find((n) => n.id === edge.target);
225-
226-
if (sourceNode && targetNode) {
227-
const handlePositions = [
228-
Position.Left,
229-
Position.Right,
230-
Position.Top,
231-
Position.Bottom,
232-
];
233-
let shortestDistance = Infinity;
234-
let bestHandles = {
235-
source: edge.sourceHandle,
236-
target: edge.targetHandle,
237-
};
238-
239-
handlePositions.forEach((sourceHandle) => {
240-
handlePositions.forEach((targetHandle) => {
241-
const sourcePosition = getHandlePosition(
242-
sourceNode,
243-
sourceHandle,
244-
);
245-
const targetPosition = getHandlePosition(
246-
targetNode,
247-
targetHandle,
248-
);
249-
const distance = Math.sqrt(
250-
Math.pow(sourcePosition.x - targetPosition.x, 2) +
251-
Math.pow(sourcePosition.y - targetPosition.y, 2),
252-
);
253-
254-
if (distance < shortestDistance) {
255-
shortestDistance = distance;
256-
bestHandles = {
257-
source: sourceHandle,
258-
target: targetHandle,
259-
};
260-
}
261-
});
262-
});
263-
264-
const updatedEdge = {
265-
...edge,
266-
sourceHandle: bestHandles.source,
267-
targetHandle: bestHandles.target,
268-
};
269-
edgesMap.set(edge.id, updatedEdge);
270-
}
217+
const affectedEdges = edges.filter(
218+
(edge) => edge.source === change.id || edge.target === change.id,
219+
);
220+
221+
affectedEdges.forEach((edge) => {
222+
const sourceNode = nodes.find((n) => n.id === edge.source);
223+
const targetNode = nodes.find((n) => n.id === edge.target);
224+
225+
if (sourceNode && targetNode) {
226+
const bestHandles = calculateBestHandles(
227+
sourceNode,
228+
targetNode,
229+
);
230+
const updatedEdge = {
231+
...edge,
232+
sourceHandle: bestHandles.source,
233+
targetHandle: bestHandles.target,
234+
};
235+
edgesMap.set(edge.id, updatedEdge);
271236
}
272237
});
273238
}
@@ -313,40 +278,14 @@ function Flow({ className }: CanvasProps) {
313278
const targetNode = nodes.find((n) => n.id === connection.target);
314279

315280
if (sourceNode && targetNode) {
316-
const handlePositions = [
317-
Position.Left,
318-
Position.Right,
319-
Position.Top,
320-
Position.Bottom,
321-
];
322-
let shortestDistance = Infinity;
323-
let closestHandles = {
324-
source: connection.sourceHandle,
325-
target: connection.targetHandle,
326-
};
327-
328-
handlePositions.forEach((sourceHandle) => {
329-
handlePositions.forEach((targetHandle) => {
330-
const sourcePosition = getHandlePosition(sourceNode, sourceHandle);
331-
const targetPosition = getHandlePosition(targetNode, targetHandle);
332-
const distance = Math.sqrt(
333-
Math.pow(sourcePosition.x - targetPosition.x, 2) +
334-
Math.pow(sourcePosition.y - targetPosition.y, 2),
335-
);
336-
337-
if (distance < shortestDistance) {
338-
shortestDistance = distance;
339-
closestHandles = { source: sourceHandle, target: targetHandle };
340-
}
341-
});
342-
});
281+
const bestHandles = calculateBestHandles(sourceNode, targetNode);
343282

344283
const newEdge: Edge = {
345284
id: `e${connection.source}-${connection.target}`,
346285
source: connection.source,
347286
target: connection.target,
348-
sourceHandle: closestHandles.source,
349-
targetHandle: closestHandles.target,
287+
sourceHandle: bestHandles.source,
288+
targetHandle: bestHandles.target,
350289
};
351290

352291
ydoc.getMap("edges").set(newEdge.id, newEdge);
@@ -375,6 +314,7 @@ function Flow({ className }: CanvasProps) {
375314
},
376315
[ydoc],
377316
);
317+
378318
const nodeTypes = useMemo(() => ({ note: NoteNode }), []);
379319

380320
return (

Diff for: apps/frontend/src/lib/calculateBestHandles.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Position, Node } from "@xyflow/react";
2+
import { getHandlePosition } from "./getHandlePosition";
3+
4+
export const calculateBestHandles = (sourceNode: Node, targetNode: Node) => {
5+
const handlePositions = [
6+
Position.Left,
7+
Position.Right,
8+
Position.Top,
9+
Position.Bottom,
10+
];
11+
let shortestDistance = Infinity;
12+
let bestHandles = {
13+
source: handlePositions[0],
14+
target: handlePositions[0],
15+
};
16+
17+
handlePositions.forEach((sourceHandle) => {
18+
const sourcePosition = getHandlePosition(sourceNode, sourceHandle);
19+
handlePositions.forEach((targetHandle) => {
20+
const targetPosition = getHandlePosition(targetNode, targetHandle);
21+
const distance = Math.hypot(
22+
sourcePosition.x - targetPosition.x,
23+
sourcePosition.y - targetPosition.y,
24+
);
25+
26+
if (distance < shortestDistance) {
27+
shortestDistance = distance;
28+
bestHandles = { source: sourceHandle, target: targetHandle };
29+
}
30+
});
31+
});
32+
33+
return bestHandles;
34+
};

0 commit comments

Comments
 (0)