Skip to content

Commit c2b743f

Browse files
committed
Merge branch 'develop' into feature-shared-#211
2 parents 740314b + 93cbfe8 commit c2b743f

File tree

2 files changed

+58
-84
lines changed

2 files changed

+58
-84
lines changed

apps/frontend/src/components/canvas/index.tsx

Lines changed: 24 additions & 84 deletions
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
import ELK from "elkjs";
3532

@@ -50,7 +47,6 @@ function Flow({ className }: CanvasProps) {
5047
const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);
5148
const { pages } = usePages();
5249
const queryClient = useQueryClient();
53-
5450
const { ydoc } = useYDocStore();
5551

5652
const { cursors, handleMouseMove, handleNodeDrag, handleMouseLeave } =
@@ -257,56 +253,25 @@ function Flow({ className }: CanvasProps) {
257253
};
258254
nodesMap.set(change.id, updatedYNode);
259255

260-
edges.forEach((edge) => {
261-
if (edge.source === change.id || edge.target === change.id) {
262-
const sourceNode = nodes.find((n) => n.id === edge.source);
263-
const targetNode = nodes.find((n) => n.id === edge.target);
264-
265-
if (sourceNode && targetNode) {
266-
const handlePositions = [
267-
Position.Left,
268-
Position.Right,
269-
Position.Top,
270-
Position.Bottom,
271-
];
272-
let shortestDistance = Infinity;
273-
let bestHandles = {
274-
source: edge.sourceHandle,
275-
target: edge.targetHandle,
276-
};
277-
278-
handlePositions.forEach((sourceHandle) => {
279-
handlePositions.forEach((targetHandle) => {
280-
const sourcePosition = getHandlePosition(
281-
sourceNode,
282-
sourceHandle,
283-
);
284-
const targetPosition = getHandlePosition(
285-
targetNode,
286-
targetHandle,
287-
);
288-
const distance = Math.sqrt(
289-
Math.pow(sourcePosition.x - targetPosition.x, 2) +
290-
Math.pow(sourcePosition.y - targetPosition.y, 2),
291-
);
292-
293-
if (distance < shortestDistance) {
294-
shortestDistance = distance;
295-
bestHandles = {
296-
source: sourceHandle,
297-
target: targetHandle,
298-
};
299-
}
300-
});
301-
});
302-
303-
const updatedEdge = {
304-
...edge,
305-
sourceHandle: bestHandles.source,
306-
targetHandle: bestHandles.target,
307-
};
308-
edgesMap.set(edge.id, updatedEdge);
309-
}
256+
const affectedEdges = edges.filter(
257+
(edge) => edge.source === change.id || edge.target === change.id,
258+
);
259+
260+
affectedEdges.forEach((edge) => {
261+
const sourceNode = nodes.find((n) => n.id === edge.source);
262+
const targetNode = nodes.find((n) => n.id === edge.target);
263+
264+
if (sourceNode && targetNode) {
265+
const bestHandles = calculateBestHandles(
266+
sourceNode,
267+
targetNode,
268+
);
269+
const updatedEdge = {
270+
...edge,
271+
sourceHandle: bestHandles.source,
272+
targetHandle: bestHandles.target,
273+
};
274+
edgesMap.set(edge.id, updatedEdge);
310275
}
311276
});
312277
}
@@ -352,40 +317,14 @@ function Flow({ className }: CanvasProps) {
352317
const targetNode = nodes.find((n) => n.id === connection.target);
353318

354319
if (sourceNode && targetNode) {
355-
const handlePositions = [
356-
Position.Left,
357-
Position.Right,
358-
Position.Top,
359-
Position.Bottom,
360-
];
361-
let shortestDistance = Infinity;
362-
let closestHandles = {
363-
source: connection.sourceHandle,
364-
target: connection.targetHandle,
365-
};
366-
367-
handlePositions.forEach((sourceHandle) => {
368-
handlePositions.forEach((targetHandle) => {
369-
const sourcePosition = getHandlePosition(sourceNode, sourceHandle);
370-
const targetPosition = getHandlePosition(targetNode, targetHandle);
371-
const distance = Math.sqrt(
372-
Math.pow(sourcePosition.x - targetPosition.x, 2) +
373-
Math.pow(sourcePosition.y - targetPosition.y, 2),
374-
);
375-
376-
if (distance < shortestDistance) {
377-
shortestDistance = distance;
378-
closestHandles = { source: sourceHandle, target: targetHandle };
379-
}
380-
});
381-
});
320+
const bestHandles = calculateBestHandles(sourceNode, targetNode);
382321

383322
const newEdge: Edge = {
384323
id: `e${connection.source}-${connection.target}`,
385324
source: connection.source,
386325
target: connection.target,
387-
sourceHandle: closestHandles.source,
388-
targetHandle: closestHandles.target,
326+
sourceHandle: bestHandles.source,
327+
targetHandle: bestHandles.target,
389328
};
390329

391330
ydoc.getMap("edges").set(newEdge.id, newEdge);
@@ -414,6 +353,7 @@ function Flow({ className }: CanvasProps) {
414353
},
415354
[ydoc],
416355
);
356+
417357
const nodeTypes = useMemo(() => ({ note: NoteNode }), []);
418358

419359
return (
Lines changed: 34 additions & 0 deletions
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)