@@ -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
3431import 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 (
0 commit comments