@@ -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";
2524import { cn } from "@/lib/utils" ;
2625import { useQueryClient } from "@tanstack/react-query" ;
2726import useYDocStore from "@/store/useYDocStore" ;
28-
2927import { useCollaborativeCursors } from "@/hooks/useCursor" ;
3028import { CollaborativeCursors } from "../CursorView" ;
31-
32- import { getHandlePosition } from "@/lib/getHandlePosition" ;
29+ import { calculateBestHandles } from "@/lib/calculateBestHandles" ;
3330
3431const 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 (
0 commit comments