Skip to content

Commit 2f66300

Browse files
committed
fix(core): better smoothstep edge label position
1 parent 71aa00b commit 2f66300

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ function getPoints({
119119
points = sourceDir.y === currDir ? sourceTarget : targetSource
120120
}
121121

122+
if (sourcePosition === targetPosition) {
123+
const diff = Math.abs(source[dirAccessor] - target[dirAccessor])
124+
125+
// if an edge goes from right to right for example (sourcePosition === targetPosition) and the distance between source.x and target.x is less than the offset, the added point and the gapped source/target will overlap. This leads to a weird edge path. To avoid this we add a gapOffset to the source/target
126+
if (diff <= offset) {
127+
const gapOffset = Math.min(offset - 1, offset - diff)
128+
if (sourceDir[dirAccessor] === currDir) {
129+
sourceGapOffset[dirAccessor] = gapOffset
130+
} else {
131+
targetGapOffset[dirAccessor] = gapOffset
132+
}
133+
}
134+
}
135+
122136
// these are conditions for handling mixed handle positions like Right -> Bottom for example
123137
if (sourcePosition !== targetPosition) {
124138
const dirAccessorOpposite = dirAccessor === 'x' ? 'y' : 'x'
@@ -132,22 +146,21 @@ function getPoints({
132146
if (flipSourceTarget) {
133147
points = dirAccessor === 'x' ? sourceTarget : targetSource
134148
}
135-
} else {
136-
const diff = Math.abs(source[dirAccessor] - target[dirAccessor])
137-
138-
// if an edge goes from right to right for example (sourcePosition === targetPosition) and the distance between source.x and target.x is less than the offset, the added point and the gapped source/target will overlap. This leads to a weird edge path. To avoid this we add a gapOffset to the source/target
139-
if (diff <= offset) {
140-
const gapOffset = Math.min(offset - 1, offset - diff)
141-
if (sourceDir[dirAccessor] === currDir) {
142-
sourceGapOffset[dirAccessor] = gapOffset
143-
} else {
144-
targetGapOffset[dirAccessor] = gapOffset
145-
}
146-
}
147149
}
148150

149-
centerX = points[0].x
150-
centerY = points[0].y
151+
const sourceGapPoint = { x: sourceGapped.x - sourceGapOffset.x, y: sourceGapped.y - sourceGapOffset.y }
152+
const targetGapPoint = { x: targetGapped.x - targetGapOffset.x, y: targetGapped.y - targetGapOffset.y }
153+
const maxXDistance = Math.max(Math.abs(sourceGapPoint.x - points[0].x), Math.abs(targetGapPoint.x - points[0].x))
154+
const maxYDistance = Math.max(Math.abs(sourceGapPoint.y - points[0].y), Math.abs(targetGapPoint.y - points[0].y))
155+
156+
// we want to place the label on the longest segment of the edge
157+
if (maxXDistance >= maxYDistance) {
158+
centerX = (sourceGapPoint.x + targetGapPoint.x) / 2
159+
centerY = points[0].y
160+
} else {
161+
centerX = points[0].x
162+
centerY = (sourceGapPoint.y + targetGapPoint.y) / 2
163+
}
151164
}
152165

153166
const pathPoints = [

0 commit comments

Comments
 (0)