@@ -10,7 +10,6 @@ import {
10
10
BackgroundVariant ,
11
11
ConnectionMode ,
12
12
type Node ,
13
- Position ,
14
13
NodeChange ,
15
14
Edge ,
16
15
EdgeChange ,
@@ -25,11 +24,9 @@ import { SocketIOProvider } from "y-socket.io";
25
24
import { cn } from "@/lib/utils" ;
26
25
import { useQueryClient } from "@tanstack/react-query" ;
27
26
import useYDocStore from "@/store/useYDocStore" ;
28
-
29
27
import { useCollaborativeCursors } from "@/hooks/useCursor" ;
30
28
import { CollaborativeCursors } from "../CursorView" ;
31
-
32
- import { getHandlePosition } from "@/lib/getHandlePosition" ;
29
+ import { calculateBestHandles } from "@/lib/calculateBestHandles" ;
33
30
34
31
const proOptions = { hideAttribution : true } ;
35
32
@@ -46,7 +43,6 @@ function Flow({ className }: CanvasProps) {
46
43
const [ edges , setEdges , onEdgesChange ] = useEdgesState < Edge > ( [ ] ) ;
47
44
const { pages } = usePages ( ) ;
48
45
const queryClient = useQueryClient ( ) ;
49
-
50
46
const { ydoc } = useYDocStore ( ) ;
51
47
52
48
const { cursors, handleMouseMove, handleNodeDrag, handleMouseLeave } =
@@ -218,56 +214,25 @@ function Flow({ className }: CanvasProps) {
218
214
} ;
219
215
nodesMap . set ( change . id , updatedYNode ) ;
220
216
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 ) ;
271
236
}
272
237
} ) ;
273
238
}
@@ -313,40 +278,14 @@ function Flow({ className }: CanvasProps) {
313
278
const targetNode = nodes . find ( ( n ) => n . id === connection . target ) ;
314
279
315
280
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 ) ;
343
282
344
283
const newEdge : Edge = {
345
284
id : `e${ connection . source } -${ connection . target } ` ,
346
285
source : connection . source ,
347
286
target : connection . target ,
348
- sourceHandle : closestHandles . source ,
349
- targetHandle : closestHandles . target ,
287
+ sourceHandle : bestHandles . source ,
288
+ targetHandle : bestHandles . target ,
350
289
} ;
351
290
352
291
ydoc . getMap ( "edges" ) . set ( newEdge . id , newEdge ) ;
@@ -375,6 +314,7 @@ function Flow({ className }: CanvasProps) {
375
314
} ,
376
315
[ ydoc ] ,
377
316
) ;
317
+
378
318
const nodeTypes = useMemo ( ( ) => ( { note : NoteNode } ) , [ ] ) ;
379
319
380
320
return (
0 commit comments