|
| 1 | +<script setup> |
| 2 | +import { Position, getBezierPath, getSmoothStepPath } from '@vue-flow/core' |
| 3 | +import { computed } from 'vue' |
| 4 | +
|
| 5 | +const props = defineProps({ |
| 6 | + id: { |
| 7 | + type: String, |
| 8 | + required: true, |
| 9 | + }, |
| 10 | + sourceX: { |
| 11 | + type: Number, |
| 12 | + required: true, |
| 13 | + }, |
| 14 | + sourceY: { |
| 15 | + type: Number, |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + targetX: { |
| 19 | + type: Number, |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + targetY: { |
| 23 | + type: Number, |
| 24 | + required: true, |
| 25 | + }, |
| 26 | + sourcePosition: { |
| 27 | + type: String, |
| 28 | + required: true, |
| 29 | + }, |
| 30 | + targetPosition: { |
| 31 | + type: String, |
| 32 | + required: true, |
| 33 | + }, |
| 34 | + sourceNode: { |
| 35 | + type: Object, |
| 36 | + required: true, |
| 37 | + }, |
| 38 | + targetNode: { |
| 39 | + type: Object, |
| 40 | + required: true, |
| 41 | + }, |
| 42 | + data: { |
| 43 | + type: Object, |
| 44 | + required: true, |
| 45 | + }, |
| 46 | +}) |
| 47 | +
|
| 48 | +const path = computed(() => { |
| 49 | + if (props.sourceNode && props.targetNode) { |
| 50 | + if (props.data.pathType === 'bezier') { |
| 51 | + if ( |
| 52 | + (props.sourcePosition === Position.Bottom && props.targetPosition === Position.Top) || |
| 53 | + (props.sourcePosition === Position.Top && props.targetPosition === Position.Bottom) |
| 54 | + ) { |
| 55 | + // for horizontal loopback edges |
| 56 | + const radiusX = 60 |
| 57 | + const radiusY = props.sourceY - props.targetY |
| 58 | +
|
| 59 | + return [`M ${props.sourceX} ${props.sourceY} A ${radiusX} ${radiusY} 0 1 0 ${props.targetX} ${props.targetY}`] |
| 60 | + } else if ( |
| 61 | + (props.sourcePosition === Position.Left && props.targetPosition === Position.Right) || |
| 62 | + (props.sourcePosition === Position.Right && props.targetPosition === Position.Left) |
| 63 | + ) { |
| 64 | + // for vertical loopback edges |
| 65 | + const radiusX = (props.sourceX - props.targetX) * 0.6 |
| 66 | + const radiusY = 50 |
| 67 | +
|
| 68 | + return [`M ${props.sourceX} ${props.sourceY} A ${radiusX} ${radiusY} 0 1 0 ${props.targetX} ${props.targetY}`] |
| 69 | + } |
| 70 | + } else if (props.data.pathType === 'smoothstep') { |
| 71 | + let centerX, centerY |
| 72 | + if (props.sourceNode === props.targetNode) { |
| 73 | + if ( |
| 74 | + (props.sourcePosition === Position.Bottom && props.targetPosition === Position.Top) || |
| 75 | + (props.sourcePosition === Position.Top && props.targetPosition === Position.Bottom) |
| 76 | + ) { |
| 77 | + const source = props.sourceNode |
| 78 | + centerX = props.sourceX - 40 - source.dimensions.width / 2 |
| 79 | + centerY = (props.sourceY + props.targetY) / 2 |
| 80 | + } else if ( |
| 81 | + (props.sourcePosition === Position.Left && props.targetPosition === Position.Right) || |
| 82 | + (props.sourcePosition === Position.Right && props.targetPosition === Position.Left) |
| 83 | + ) { |
| 84 | + const source = props.sourceNode |
| 85 | + centerX = (props.sourceX + props.targetX) / 2 |
| 86 | + centerY = props.sourceY + 40 + source.dimensions.height / 2 |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + return getSmoothStepPath({ |
| 91 | + sourcePosition: props.sourcePosition, |
| 92 | + targetPosition: props.targetPosition, |
| 93 | + centerX, |
| 94 | + centerY, |
| 95 | + ...props, |
| 96 | + }) |
| 97 | + } |
| 98 | + } |
| 99 | +
|
| 100 | + // default to bezier path |
| 101 | + return getBezierPath(props) |
| 102 | +}) |
| 103 | +</script> |
| 104 | + |
| 105 | +<script> |
| 106 | +export default { |
| 107 | + inheritAttrs: false, |
| 108 | +} |
| 109 | +</script> |
| 110 | + |
| 111 | +<template> |
| 112 | + <path :d="path[0]" fill="none" stroke="black" /> |
| 113 | +</template> |
0 commit comments