refactor(edges): handle mixed edges #961

This commit is contained in:
moklick
2021-03-28 17:01:50 +02:00
parent b894722ac1
commit 9a7ccbf829
3 changed files with 26 additions and 3 deletions
+1 -1
View File
@@ -63,7 +63,7 @@ export default memo(
arrowHeadType, arrowHeadType,
markerEndId, markerEndId,
}: EdgeProps) => { }: EdgeProps) => {
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY }); const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition });
const path = getBezierPath({ const path = getBezierPath({
sourceX, sourceX,
sourceY, sourceY,
+1 -1
View File
@@ -124,7 +124,7 @@ export default memo(
markerEndId, markerEndId,
borderRadius = 5, borderRadius = 5,
}: EdgeSmoothStepProps) => { }: EdgeSmoothStepProps) => {
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY }); const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition });
const path = getSmoothStepPath({ const path = getSmoothStepPath({
sourceX, sourceX,
+24 -1
View File
@@ -1,4 +1,4 @@
import { ArrowHeadType } from '../../types'; import { ArrowHeadType, Position } from '../../types';
export const getMarkerEnd = (arrowHeadType?: ArrowHeadType, markerEndId?: string): string => { export const getMarkerEnd = (arrowHeadType?: ArrowHeadType, markerEndId?: string): string => {
if (typeof markerEndId !== 'undefined' && markerEndId) { if (typeof markerEndId !== 'undefined' && markerEndId) {
@@ -13,14 +13,37 @@ export interface GetCenterParams {
sourceY: number; sourceY: number;
targetX: number; targetX: number;
targetY: number; targetY: number;
sourcePosition?: Position;
targetPosition?: Position;
} }
const LeftOrRight = [Position.Left, Position.Right];
export const getCenter = ({ export const getCenter = ({
sourceX, sourceX,
sourceY, sourceY,
targetX, targetX,
targetY, targetY,
sourcePosition = Position.Bottom,
targetPosition = Position.Top,
}: GetCenterParams): [number, number, number, number] => { }: GetCenterParams): [number, number, number, number] => {
const sourceIsLeftOrRight = LeftOrRight.includes(sourcePosition);
const targetIsLeftOrRight = LeftOrRight.includes(targetPosition);
// we expect flows to be horizontal or vertical (all handles left or right respectively top or bottom)
// a mixed edge is when one the source is on the left and the target is on the top for example.
const mixedEdge = (sourceIsLeftOrRight && !targetIsLeftOrRight) || (targetIsLeftOrRight && !sourceIsLeftOrRight);
if (mixedEdge) {
const xOffset = sourceIsLeftOrRight ? Math.abs(targetX - sourceX) : 0;
const centerX = sourceX > targetX ? sourceX - xOffset : sourceX + xOffset;
const yOffset = sourceIsLeftOrRight ? 0 : Math.abs(targetY - sourceY);
const centerY = sourceY < targetY ? sourceY + yOffset : sourceY - yOffset;
return [centerX, centerY, xOffset, yOffset];
}
const xOffset = Math.abs(targetX - sourceX) / 2; const xOffset = Math.abs(targetX - sourceX) / 2;
const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset; const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset;