Skip to content

Commit

Permalink
refactor(core,edges): replace edge utils with existing `@xyflow/syste…
Browse files Browse the repository at this point in the history
…m` 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]>
  • Loading branch information
bcakmakoglu authored Jul 15, 2024
1 parent da649be commit d2db7d0
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 561 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-snails-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---

Replace existing edge utils with ones that are already provided by `@xyflow/system` and re-export them
3 changes: 2 additions & 1 deletion packages/core/src/components/ConnectionLine/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { computed, defineComponent, h, inject } from 'vue'
import { getBezierPath, getSmoothStepPath } from '@xyflow/system'
import type { HandleElement } from '../../types'
import { ConnectionLineType, ConnectionMode, Position } from '../../types'
import { getHandlePosition, getMarkerId } from '../../utils'
import { useVueFlow } from '../../composables'
import { Slots } from '../../context'
import { getBezierPath, getSimpleBezierPath, getSmoothStepPath } from '../Edges/utils'
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge'

const oppositePosition = {
[Position.Left]: Position.Right,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/Edges/BezierEdge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineComponent, h } from 'vue'
import { getBezierPath } from '@xyflow/system'
import type { BezierEdgeProps } from '../../types'
import { Position } from '../../types'
import BaseEdge from './BaseEdge.vue'
import { getBezierPath } from './utils'

const BezierEdge = defineComponent<BezierEdgeProps>({
name: 'BezierEdge',
Expand Down
93 changes: 92 additions & 1 deletion packages/core/src/components/Edges/SimpleBezierEdge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,99 @@
import { defineComponent, h } from 'vue'
import { getBezierEdgeCenter } from '@xyflow/system'
import type { SimpleBezierEdgeProps } from '../../types'
import { Position } from '../../types'
import BaseEdge from './BaseEdge.vue'
import { getSimpleBezierPath } from './utils'

export interface GetSimpleBezierPathParams {
sourceX: number
sourceY: number
sourcePosition?: Position
targetX: number
targetY: number
targetPosition?: Position
}

interface GetControlParams {
pos: Position
x1: number
y1: number
x2: number
y2: number
}

function getControl({ pos, x1, y1, x2, y2 }: GetControlParams): [number, number] {
let ctX: number, ctY: number
switch (pos) {
case Position.Left:
case Position.Right:
ctX = 0.5 * (x1 + x2)
ctY = y1
break
case Position.Top:
case Position.Bottom:
ctX = x1
ctY = 0.5 * (y1 + y2)
break
}
return [ctX, ctY]
}

/**
* Get a simple bezier path from source to target handle (no curvature)
* @public
*
* @param simpleBezierPathParams
* @param simpleBezierPathParams.sourceX - The x position of the source handle
* @param simpleBezierPathParams.sourceY - The y position of the source handle
* @param simpleBezierPathParams.sourcePosition - The position of the source handle (default: Position.Bottom)
* @param simpleBezierPathParams.targetX - The x position of the target handle
* @param simpleBezierPathParams.targetY - The y position of the target handle
* @param simpleBezierPathParams.targetPosition - The position of the target handle (default: Position.Top)
* @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
*/
export function getSimpleBezierPath({
sourceX,
sourceY,
sourcePosition = Position.Bottom,
targetX,
targetY,
targetPosition = Position.Top,
}: GetSimpleBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
const [sourceControlX, sourceControlY] = getControl({
pos: sourcePosition,
x1: sourceX,
y1: sourceY,
x2: targetX,
y2: targetY,
})

const [targetControlX, targetControlY] = getControl({
pos: targetPosition,
x1: targetX,
y1: targetY,
x2: sourceX,
y2: sourceY,
})

const [labelX, labelY, offsetX, offsetY] = getBezierEdgeCenter({
sourceX,
sourceY,
targetX,
targetY,
sourceControlX,
sourceControlY,
targetControlX,
targetControlY,
})

return [
`M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`,
labelX,
labelY,
offsetX,
offsetY,
]
}

const SimpleBezierEdge = defineComponent<SimpleBezierEdgeProps>({
name: 'SimpleBezierEdge',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/Edges/SmoothStepEdge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineComponent, h } from 'vue'
import { getSmoothStepPath } from '@xyflow/system'
import type { SmoothStepEdgeProps } from '../../types'
import { Position } from '../../types'
import BaseEdge from './BaseEdge.vue'
import { getSmoothStepPath } from './utils'

const SmoothStepEdge = defineComponent<SmoothStepEdgeProps>({
name: 'SmoothStepEdge',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/Edges/StraightEdge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineComponent, h } from 'vue'
import { getStraightPath } from '@xyflow/system'
import type { StraightEdgeProps } from '../../types'
import BaseEdge from './BaseEdge.vue'
import { getStraightPath } from './utils'

const StraightEdge = defineComponent<StraightEdgeProps>({
name: 'StraightEdge',
Expand Down
114 changes: 0 additions & 114 deletions packages/core/src/components/Edges/utils/bezier.ts

This file was deleted.

51 changes: 0 additions & 51 deletions packages/core/src/components/Edges/utils/general.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/core/src/components/Edges/utils/index.ts

This file was deleted.

Loading

0 comments on commit d2db7d0

Please sign in to comment.