Skip to content

Commit 9b4e3b2

Browse files
committed
feat(core): add EdgePathParams type to edge utils (#1479)
* feat(core): add `EdgePathParams` type to edge utils * chore(changeset): add * chore(core): cleanup jsdocs
1 parent cff55c8 commit 9b4e3b2

File tree

6 files changed

+97
-42
lines changed

6 files changed

+97
-42
lines changed

.changeset/twenty-chicken-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Add `EdgePathParams` type and export it

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Position } from '../../../types'
2+
import type { EdgePathParams } from './general'
23
import { getBezierEdgeCenter } from './general'
34

45
export interface GetBezierPathParams {
@@ -51,15 +52,31 @@ function getControlWithCurvature({ pos, x1, y1, x2, y2, c }: GetControlWithCurva
5152
return [ctX, ctY]
5253
}
5354

54-
export function getBezierPath({
55-
sourceX,
56-
sourceY,
57-
sourcePosition = Position.Bottom,
58-
targetX,
59-
targetY,
60-
targetPosition = Position.Top,
61-
curvature = 0.25,
62-
}: GetBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
55+
/**
56+
* Get a bezier path from source to target handle
57+
* @public
58+
*
59+
* @param bezierPathParams
60+
* @param bezierPathParams.sourceX - The x position of the source handle
61+
* @param bezierPathParams.sourceY - The y position of the source handle
62+
* @param bezierPathParams.sourcePosition - The position of the source handle (default: Position.Bottom)
63+
* @param bezierPathParams.targetX - The x position of the target handle
64+
* @param bezierPathParams.targetY - The y position of the target handle
65+
* @param bezierPathParams.targetPosition - The position of the target handle (default: Position.Top)
66+
* @param bezierPathParams.curvature - The curvature of the edge (default: 0.25)
67+
* @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
68+
*/
69+
export function getBezierPath(bezierPathParams: GetBezierPathParams): EdgePathParams {
70+
const {
71+
sourceX,
72+
sourceY,
73+
sourcePosition = Position.Bottom,
74+
targetX,
75+
targetY,
76+
targetPosition = Position.Top,
77+
curvature = 0.25,
78+
} = bezierPathParams
79+
6380
const [sourceControlX, sourceControlY] = getControlWithCurvature({
6481
pos: sourcePosition,
6582
x1: sourceX,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type EdgePathParams = [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number]
2+
13
// this is used for straight edges and simple smoothstep edges (LTR, RTL, BTT, TTB)
24
export function getSimpleEdgeCenter({
35
sourceX,

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Position } from '../../../types'
2+
import type { EdgePathParams } from './general'
23
import { getBezierEdgeCenter } from './general'
34

45
export interface GetSimpleBezierPathParams {
@@ -35,14 +36,29 @@ function getControl({ pos, x1, y1, x2, y2 }: GetControlParams): [number, number]
3536
return [ctX, ctY]
3637
}
3738

38-
export function getSimpleBezierPath({
39-
sourceX,
40-
sourceY,
41-
sourcePosition = Position.Bottom,
42-
targetX,
43-
targetY,
44-
targetPosition = Position.Top,
45-
}: GetSimpleBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
39+
/**
40+
* Get a simple bezier path from source to target handle (no curvature)
41+
* @public
42+
*
43+
* @param simpleBezierPathParams
44+
* @param simpleBezierPathParams.sourceX - The x position of the source handle
45+
* @param simpleBezierPathParams.sourceY - The y position of the source handle
46+
* @param simpleBezierPathParams.sourcePosition - The position of the source handle (default: Position.Bottom)
47+
* @param simpleBezierPathParams.targetX - The x position of the target handle
48+
* @param simpleBezierPathParams.targetY - The y position of the target handle
49+
* @param simpleBezierPathParams.targetPosition - The position of the target handle (default: Position.Top)
50+
* @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
51+
*/
52+
export function getSimpleBezierPath(simpleBezierPathParams: GetSimpleBezierPathParams): EdgePathParams {
53+
const {
54+
sourceX,
55+
sourceY,
56+
sourcePosition = Position.Bottom,
57+
targetX,
58+
targetY,
59+
targetPosition = Position.Top,
60+
} = simpleBezierPathParams
61+
4662
const [sourceControlX, sourceControlY] = getControl({
4763
pos: sourcePosition,
4864
x1: sourceX,

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { XYPosition } from '../../../types'
22
import { Position } from '../../../types'
3+
import type { EdgePathParams } from './general'
34
import { getSimpleEdgeCenter } from './general'
45

56
export interface GetSmoothStepPathParams {
@@ -196,27 +197,32 @@ function getBend(a: XYPosition, b: XYPosition, c: XYPosition, size: number): str
196197

197198
/**
198199
* Get a smooth step path from source to target handle
199-
* @param params
200-
* @param params.sourceX - The x position of the source handle
201-
* @param params.sourceY - The y position of the source handle
202-
* @param params.sourcePosition - The position of the source handle (default: Position.Bottom)
203-
* @param params.targetX - The x position of the target handle
204-
* @param params.targetY - The y position of the target handle
205-
* @param params.targetPosition - The position of the target handle (default: Position.Top)
200+
* @public
201+
*
202+
* @param smoothStepPathParams
203+
* @param smoothStepPathParams.sourceX - The x position of the source handle
204+
* @param smoothStepPathParams.sourceY - The y position of the source handle
205+
* @param smoothStepPathParams.sourcePosition - The position of the source handle (default: Position.Bottom)
206+
* @param smoothStepPathParams.targetX - The x position of the target handle
207+
* @param smoothStepPathParams.targetY - The y position of the target handle
208+
* @param smoothStepPathParams.targetPosition - The position of the target handle (default: Position.Top)
209+
* @param smoothStepPathParams.borderRadius - The border radius of the edge (default: 5)
206210
* @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
207211
*/
208-
export function getSmoothStepPath({
209-
sourceX,
210-
sourceY,
211-
sourcePosition = Position.Bottom,
212-
targetX,
213-
targetY,
214-
targetPosition = Position.Top,
215-
borderRadius = 5,
216-
centerX,
217-
centerY,
218-
offset = 20,
219-
}: GetSmoothStepPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
212+
export function getSmoothStepPath(smoothStepPathParams: GetSmoothStepPathParams): EdgePathParams {
213+
const {
214+
sourceX,
215+
sourceY,
216+
sourcePosition = Position.Bottom,
217+
targetX,
218+
targetY,
219+
targetPosition = Position.Top,
220+
borderRadius = 5,
221+
centerX,
222+
centerY,
223+
offset = 20,
224+
} = smoothStepPathParams
225+
220226
const [points, labelX, labelY, offsetX, offsetY] = getPoints({
221227
source: { x: sourceX, y: sourceY },
222228
sourcePosition,

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EdgePathParams } from './general'
12
import { getSimpleEdgeCenter } from './general'
23

34
export interface GetStraightPathParams {
@@ -7,12 +8,20 @@ export interface GetStraightPathParams {
78
targetY: number
89
}
910

10-
export function getStraightPath({
11-
sourceX,
12-
sourceY,
13-
targetX,
14-
targetY,
15-
}: GetStraightPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
11+
/**
12+
* Get a straight path from source to target handle
13+
* @public
14+
*
15+
* @param straightEdgeParams
16+
* @param straightEdgeParams.sourceX - The x position of the source handle
17+
* @param straightEdgeParams.sourceY - The y position of the source handle
18+
* @param straightEdgeParams.targetX - The x position of the target handle
19+
* @param straightEdgeParams.targetY - The y position of the target handle
20+
* @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
21+
*/
22+
export function getStraightPath(straightEdgeParams: GetStraightPathParams): EdgePathParams {
23+
const { sourceX, sourceY, targetX, targetY } = straightEdgeParams
24+
1625
const [centerX, centerY, offsetX, offsetY] = getSimpleEdgeCenter({
1726
sourceX,
1827
sourceY,

0 commit comments

Comments
 (0)