|
1 | 1 | import { defineComponent, h } from 'vue'
|
| 2 | +import { getBezierEdgeCenter } from '@xyflow/system' |
2 | 3 | import type { SimpleBezierEdgeProps } from '../../types'
|
3 | 4 | import { Position } from '../../types'
|
4 | 5 | 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 | +} |
6 | 97 |
|
7 | 98 | const SimpleBezierEdge = defineComponent<SimpleBezierEdgeProps>({
|
8 | 99 | name: 'SimpleBezierEdge',
|
|
0 commit comments