Skip to content

Commit d2db7d0

Browse files
authored
refactor(core,edges): replace edge utils with existing @xyflow/system exports (#1547)
* refactor(core,edges): replace edge helpers with xyflow/system exports Signed-off-by: braks <[email protected]> * chore(core): cleanup Signed-off-by: braks <[email protected]> * chore(changeset): add Signed-off-by: braks <[email protected]> * chore(core): cleanup exports Signed-off-by: braks <[email protected]> --------- Signed-off-by: braks <[email protected]>
1 parent da649be commit d2db7d0

File tree

13 files changed

+105
-561
lines changed

13 files changed

+105
-561
lines changed

.changeset/ten-snails-flash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Replace existing edge utils with ones that are already provided by `@xyflow/system` and re-export them

packages/core/src/components/ConnectionLine/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { computed, defineComponent, h, inject } from 'vue'
2+
import { getBezierPath, getSmoothStepPath } from '@xyflow/system'
23
import type { HandleElement } from '../../types'
34
import { ConnectionLineType, ConnectionMode, Position } from '../../types'
45
import { getHandlePosition, getMarkerId } from '../../utils'
56
import { useVueFlow } from '../../composables'
67
import { Slots } from '../../context'
7-
import { getBezierPath, getSimpleBezierPath, getSmoothStepPath } from '../Edges/utils'
8+
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge'
89

910
const oppositePosition = {
1011
[Position.Left]: Position.Right,

packages/core/src/components/Edges/BezierEdge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineComponent, h } from 'vue'
2+
import { getBezierPath } from '@xyflow/system'
23
import type { BezierEdgeProps } from '../../types'
34
import { Position } from '../../types'
45
import BaseEdge from './BaseEdge.vue'
5-
import { getBezierPath } from './utils'
66

77
const BezierEdge = defineComponent<BezierEdgeProps>({
88
name: 'BezierEdge',

packages/core/src/components/Edges/SimpleBezierEdge.ts

+92-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,99 @@
11
import { defineComponent, h } from 'vue'
2+
import { getBezierEdgeCenter } from '@xyflow/system'
23
import type { SimpleBezierEdgeProps } from '../../types'
34
import { Position } from '../../types'
45
import BaseEdge from './BaseEdge.vue'
5-
import { getSimpleBezierPath } from './utils'
6+
7+
export interface GetSimpleBezierPathParams {
8+
sourceX: number
9+
sourceY: number
10+
sourcePosition?: Position
11+
targetX: number
12+
targetY: number
13+
targetPosition?: Position
14+
}
15+
16+
interface GetControlParams {
17+
pos: Position
18+
x1: number
19+
y1: number
20+
x2: number
21+
y2: number
22+
}
23+
24+
function getControl({ pos, x1, y1, x2, y2 }: GetControlParams): [number, number] {
25+
let ctX: number, ctY: number
26+
switch (pos) {
27+
case Position.Left:
28+
case Position.Right:
29+
ctX = 0.5 * (x1 + x2)
30+
ctY = y1
31+
break
32+
case Position.Top:
33+
case Position.Bottom:
34+
ctX = x1
35+
ctY = 0.5 * (y1 + y2)
36+
break
37+
}
38+
return [ctX, ctY]
39+
}
40+
41+
/**
42+
* Get a simple bezier path from source to target handle (no curvature)
43+
* @public
44+
*
45+
* @param simpleBezierPathParams
46+
* @param simpleBezierPathParams.sourceX - The x position of the source handle
47+
* @param simpleBezierPathParams.sourceY - The y position of the source handle
48+
* @param simpleBezierPathParams.sourcePosition - The position of the source handle (default: Position.Bottom)
49+
* @param simpleBezierPathParams.targetX - The x position of the target handle
50+
* @param simpleBezierPathParams.targetY - The y position of the target handle
51+
* @param simpleBezierPathParams.targetPosition - The position of the target handle (default: Position.Top)
52+
* @returns A path string you can use in an SVG, the labelX and labelY position (center of path) and offsetX, offsetY between source handle and label
53+
*/
54+
export function getSimpleBezierPath({
55+
sourceX,
56+
sourceY,
57+
sourcePosition = Position.Bottom,
58+
targetX,
59+
targetY,
60+
targetPosition = Position.Top,
61+
}: GetSimpleBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
62+
const [sourceControlX, sourceControlY] = getControl({
63+
pos: sourcePosition,
64+
x1: sourceX,
65+
y1: sourceY,
66+
x2: targetX,
67+
y2: targetY,
68+
})
69+
70+
const [targetControlX, targetControlY] = getControl({
71+
pos: targetPosition,
72+
x1: targetX,
73+
y1: targetY,
74+
x2: sourceX,
75+
y2: sourceY,
76+
})
77+
78+
const [labelX, labelY, offsetX, offsetY] = getBezierEdgeCenter({
79+
sourceX,
80+
sourceY,
81+
targetX,
82+
targetY,
83+
sourceControlX,
84+
sourceControlY,
85+
targetControlX,
86+
targetControlY,
87+
})
88+
89+
return [
90+
`M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`,
91+
labelX,
92+
labelY,
93+
offsetX,
94+
offsetY,
95+
]
96+
}
697

798
const SimpleBezierEdge = defineComponent<SimpleBezierEdgeProps>({
899
name: 'SimpleBezierEdge',

packages/core/src/components/Edges/SmoothStepEdge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineComponent, h } from 'vue'
2+
import { getSmoothStepPath } from '@xyflow/system'
23
import type { SmoothStepEdgeProps } from '../../types'
34
import { Position } from '../../types'
45
import BaseEdge from './BaseEdge.vue'
5-
import { getSmoothStepPath } from './utils'
66

77
const SmoothStepEdge = defineComponent<SmoothStepEdgeProps>({
88
name: 'SmoothStepEdge',

packages/core/src/components/Edges/StraightEdge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineComponent, h } from 'vue'
2+
import { getStraightPath } from '@xyflow/system'
23
import type { StraightEdgeProps } from '../../types'
34
import BaseEdge from './BaseEdge.vue'
4-
import { getStraightPath } from './utils'
55

66
const StraightEdge = defineComponent<StraightEdgeProps>({
77
name: 'StraightEdge',

packages/core/src/components/Edges/utils/bezier.ts

-114
This file was deleted.

packages/core/src/components/Edges/utils/general.ts

-51
This file was deleted.

packages/core/src/components/Edges/utils/index.ts

-5
This file was deleted.

0 commit comments

Comments
 (0)