chore(edges): cleanup types

This commit is contained in:
moklick
2022-09-27 16:00:02 +02:00
parent ae1ae785aa
commit 90418e36fa
5 changed files with 28 additions and 58 deletions
@@ -1,7 +1,8 @@
import { memo } from 'react'; import { memo } from 'react';
import { BezierEdgeProps, Position } from '../../types';
import BaseEdge from './BaseEdge'; import BaseEdge from './BaseEdge';
import { getBezierEdgeCenter } from './utils'; import { getBezierEdgeCenter } from './utils';
import { BezierEdgeProps, Position } from '../../types';
export interface GetBezierPathParams { export interface GetBezierPathParams {
sourceX: number; sourceX: number;
@@ -25,40 +26,22 @@ interface GetControlWithCurvatureParams {
function calculateControlOffset(distance: number, curvature: number): number { function calculateControlOffset(distance: number, curvature: number): number {
if (distance >= 0) { if (distance >= 0) {
return 0.5 * distance; return 0.5 * distance;
} else {
return curvature * 25 * Math.sqrt(-distance);
} }
return curvature * 25 * Math.sqrt(-distance);
} }
function getControlWithCurvature({ pos, x1, y1, x2, y2, c }: GetControlWithCurvatureParams): [number, number] { function getControlWithCurvature({ pos, x1, y1, x2, y2, c }: GetControlWithCurvatureParams): [number, number] {
let ctX: number, ctY: number;
switch (pos) { switch (pos) {
case Position.Left: case Position.Left:
{ return [x1 - calculateControlOffset(x1 - x2, c), y1];
ctX = x1 - calculateControlOffset(x1 - x2, c);
ctY = y1;
}
break;
case Position.Right: case Position.Right:
{ return [x1 + calculateControlOffset(x2 - x1, c), y1];
ctX = x1 + calculateControlOffset(x2 - x1, c);
ctY = y1;
}
break;
case Position.Top: case Position.Top:
{ return [x1, y1 - calculateControlOffset(y1 - y2, c)];
ctX = x1;
ctY = y1 - calculateControlOffset(y1 - y2, c);
}
break;
case Position.Bottom: case Position.Bottom:
{ return [x1, y1 + calculateControlOffset(y2 - y1, c)];
ctX = x1;
ctY = y1 + calculateControlOffset(y2 - y1, c);
}
break;
} }
return [ctX, ctY];
} }
export function getBezierPath({ export function getBezierPath({
@@ -69,7 +52,7 @@ export function getBezierPath({
targetY, targetY,
targetPosition = Position.Top, targetPosition = Position.Top,
curvature = 0.25, curvature = 0.25,
}: GetBezierPathParams): [string, number, number, number, number] { }: GetBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
const [sourceControlX, sourceControlY] = getControlWithCurvature({ const [sourceControlX, sourceControlY] = getControlWithCurvature({
pos: sourcePosition, pos: sourcePosition,
x1: sourceX, x1: sourceX,
@@ -86,7 +69,7 @@ export function getBezierPath({
y2: sourceY, y2: sourceY,
c: curvature, c: curvature,
}); });
const [centerX, centerY, offsetX, offsetY] = getBezierEdgeCenter({ const [labelX, labelY, offsetX, offsetY] = getBezierEdgeCenter({
sourceX, sourceX,
sourceY, sourceY,
targetX, targetX,
@@ -99,8 +82,8 @@ export function getBezierPath({
return [ return [
`M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`, `M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`,
centerX, labelX,
centerY, labelY,
offsetX, offsetX,
offsetY, offsetY,
]; ];
@@ -21,24 +21,11 @@ interface GetControlParams {
} }
function getControl({ pos, x1, y1, x2, y2 }: GetControlParams): [number, number] { function getControl({ pos, x1, y1, x2, y2 }: GetControlParams): [number, number] {
let ctX: number, ctY: number; if (pos === Position.Left || pos === Position.Right) {
switch (pos) { return [0.5 * (x1 + x2), y1];
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];
return [x1, 0.5 * (y1 + y2)];
} }
export function getSimpleBezierPath({ export function getSimpleBezierPath({
@@ -48,7 +35,7 @@ export function getSimpleBezierPath({
targetX, targetX,
targetY, targetY,
targetPosition = Position.Top, targetPosition = Position.Top,
}: GetSimpleBezierPathParams): [string, number, number, number, number] { }: GetSimpleBezierPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
const [sourceControlX, sourceControlY] = getControl({ const [sourceControlX, sourceControlY] = getControl({
pos: sourcePosition, pos: sourcePosition,
x1: sourceX, x1: sourceX,
@@ -63,7 +50,7 @@ export function getSimpleBezierPath({
x2: sourceX, x2: sourceX,
y2: sourceY, y2: sourceY,
}); });
const [centerX, centerY, offsetX, offsetY] = getBezierEdgeCenter({ const [labelX, labelY, offsetX, offsetY] = getBezierEdgeCenter({
sourceX, sourceX,
sourceY, sourceY,
targetX, targetX,
@@ -76,8 +63,8 @@ export function getSimpleBezierPath({
return [ return [
`M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`, `M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`,
centerX, labelX,
centerY, labelY,
offsetX, offsetX,
offsetY, offsetY,
]; ];
@@ -2,7 +2,7 @@ import { memo } from 'react';
import { SmoothStepEdgeProps, Position, XYPosition } from '../../types'; import { SmoothStepEdgeProps, Position, XYPosition } from '../../types';
import BaseEdge from './BaseEdge'; import BaseEdge from './BaseEdge';
import { getSimpleEdgeCenter } from './utils'; import { getEdgeCenter } from './utils';
export interface GetSmoothStepPathParams { export interface GetSmoothStepPathParams {
sourceX: number; sourceX: number;
@@ -72,7 +72,7 @@ function getPoints({
let points: XYPosition[] = []; let points: XYPosition[] = [];
let centerX, centerY; let centerX, centerY;
const [defaultCenterX, defaultCenterY, defaultOffsetX, defaultOffsetY] = getSimpleEdgeCenter({ const [defaultCenterX, defaultCenterY, defaultOffsetX, defaultOffsetY] = getEdgeCenter({
sourceX: source.x, sourceX: source.x,
sourceY: source.y, sourceY: source.y,
targetX: target.x, targetX: target.x,
@@ -170,7 +170,7 @@ export function getSmoothStepPath({
centerX, centerX,
centerY, centerY,
offset = 20, offset = 20,
}: GetSmoothStepPathParams): [string, number, number, number, number] { }: GetSmoothStepPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
const [points, labelX, labelY, offsetX, offsetY] = getPoints({ const [points, labelX, labelY, offsetX, offsetY] = getPoints({
source: { x: sourceX, y: sourceY }, source: { x: sourceX, y: sourceY },
sourcePosition, sourcePosition,
@@ -2,7 +2,7 @@ import { memo } from 'react';
import BaseEdge from './BaseEdge'; import BaseEdge from './BaseEdge';
import { EdgeProps } from '../../types'; import { EdgeProps } from '../../types';
import { getSimpleEdgeCenter } from './utils'; import { getEdgeCenter } from './utils';
export type GetStraightPathParams = { export type GetStraightPathParams = {
sourceX: number; sourceX: number;
@@ -16,15 +16,15 @@ export function getStraightPath({
sourceY, sourceY,
targetX, targetX,
targetY, targetY,
}: GetStraightPathParams): [string, number, number, number, number] { }: GetStraightPathParams): [path: string, labelX: number, labelY: number, offsetX: number, offsetY: number] {
const [centerX, centerY, offsetX, offsetY] = getSimpleEdgeCenter({ const [labelX, labelY, offsetX, offsetY] = getEdgeCenter({
sourceX, sourceX,
sourceY, sourceY,
targetX, targetX,
targetY, targetY,
}); });
return [`M ${sourceX},${sourceY}L ${targetX},${targetY}`, centerX, centerY, offsetX, offsetY]; return [`M ${sourceX},${sourceY}L ${targetX},${targetY}`, labelX, labelY, offsetX, offsetY];
} }
const StraightEdge = memo( const StraightEdge = memo(
+1 -1
View File
@@ -28,7 +28,7 @@ export function getMouseHandler(
} }
// this is used for straight edges and simple smoothstep edges (LTR, RTL, BTT, TTB) // this is used for straight edges and simple smoothstep edges (LTR, RTL, BTT, TTB)
export function getSimpleEdgeCenter({ export function getEdgeCenter({
sourceX, sourceX,
sourceY, sourceY,
targetX, targetX,