Refactor: Better routing for smoothstep and step edge (#2407)
* refactor(step/smoothstep-edge): better default routing * chore(edges): pass options * chore(exports): add BaseEdge
This commit is contained in:
@@ -0,0 +1,132 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import ReactFlow, { Node, Edge, Position, MarkerType } from 'reactflow';
|
||||||
|
|
||||||
|
const nodes: Node[] = [
|
||||||
|
// LTR
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
position: { x: 50, y: -100 },
|
||||||
|
data: { label: 'Source' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Right,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
position: { x: -100, y: 0 },
|
||||||
|
data: { label: 'Target' },
|
||||||
|
sourcePosition: Position.Left,
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
// Right Right
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
position: { x: -100, y: 250 },
|
||||||
|
data: { label: 'Source' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Right,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '4',
|
||||||
|
position: { x: 50, y: 150 },
|
||||||
|
data: { label: 'Target' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Right,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
// Right Top
|
||||||
|
{
|
||||||
|
id: '5',
|
||||||
|
position: { x: -100, y: 450 },
|
||||||
|
data: { label: 'Source' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Right,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '6',
|
||||||
|
position: { x: 100, y: 400 },
|
||||||
|
data: { label: 'Target' },
|
||||||
|
sourcePosition: Position.Top,
|
||||||
|
targetPosition: Position.Top,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
// Right Bottom
|
||||||
|
{
|
||||||
|
id: '7',
|
||||||
|
position: { x: 100, y: 700 },
|
||||||
|
data: { label: 'Source' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Right,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '8',
|
||||||
|
position: { x: -100, y: 600 },
|
||||||
|
data: { label: 'Target' },
|
||||||
|
sourcePosition: Position.Bottom,
|
||||||
|
targetPosition: Position.Bottom,
|
||||||
|
style: { background: 'rgba(255,255,255,0.5)' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const edges: Edge[] = [
|
||||||
|
{
|
||||||
|
id: 'e1-2',
|
||||||
|
source: '1',
|
||||||
|
target: '2',
|
||||||
|
type: 'smoothstep',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
offset: 30,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e3-4',
|
||||||
|
source: '3',
|
||||||
|
target: '4',
|
||||||
|
type: 'smoothstep',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
borderRadius: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: 'e4-5',
|
||||||
|
source: '5',
|
||||||
|
target: '6',
|
||||||
|
type: 'smoothstep',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
id: 'e7-8',
|
||||||
|
source: '7',
|
||||||
|
target: '8',
|
||||||
|
type: 'smoothstep',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const defaultEdgeOptions = {
|
||||||
|
style: {
|
||||||
|
strokeWidth: 2,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const SimpleEdge = () => {
|
||||||
|
return <ReactFlow defaultNodes={nodes} defaultEdges={edges} fitView defaultEdgeOptions={defaultEdgeOptions} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SimpleEdge;
|
||||||
@@ -41,7 +41,6 @@ const EdgeTypesFlow = () => {
|
|||||||
onInit={onInit}
|
onInit={onInit}
|
||||||
onConnect={onConnect}
|
onConnect={onConnect}
|
||||||
minZoom={0.2}
|
minZoom={0.2}
|
||||||
zoomOnScroll={false}
|
|
||||||
selectionKeyCode="a+s"
|
selectionKeyCode="a+s"
|
||||||
multiSelectionKeyCode={multiSelectionKeyCode}
|
multiSelectionKeyCode={multiSelectionKeyCode}
|
||||||
deleteKeyCode={deleteKeyCode}
|
deleteKeyCode={deleteKeyCode}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '01',
|
target: '01',
|
||||||
sourceHandle: 'left',
|
sourceHandle: 'left',
|
||||||
targetHandle: 'bottom',
|
targetHandle: 'bottom',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-1b',
|
id: 'e0-1b',
|
||||||
@@ -88,7 +88,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '01',
|
target: '01',
|
||||||
sourceHandle: 'top',
|
sourceHandle: 'top',
|
||||||
targetHandle: 'right',
|
targetHandle: 'right',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-2a',
|
id: 'e0-2a',
|
||||||
@@ -96,7 +96,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '02',
|
target: '02',
|
||||||
sourceHandle: 'top',
|
sourceHandle: 'top',
|
||||||
targetHandle: 'left',
|
targetHandle: 'left',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-2b',
|
id: 'e0-2b',
|
||||||
@@ -104,7 +104,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '02',
|
target: '02',
|
||||||
sourceHandle: 'right',
|
sourceHandle: 'right',
|
||||||
targetHandle: 'bottom',
|
targetHandle: 'bottom',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-3a',
|
id: 'e0-3a',
|
||||||
@@ -112,7 +112,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '03',
|
target: '03',
|
||||||
sourceHandle: 'right',
|
sourceHandle: 'right',
|
||||||
targetHandle: 'top',
|
targetHandle: 'top',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-3b',
|
id: 'e0-3b',
|
||||||
@@ -120,7 +120,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '03',
|
target: '03',
|
||||||
sourceHandle: 'bottom',
|
sourceHandle: 'bottom',
|
||||||
targetHandle: 'left',
|
targetHandle: 'left',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-4a',
|
id: 'e0-4a',
|
||||||
@@ -128,7 +128,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '04',
|
target: '04',
|
||||||
sourceHandle: 'bottom',
|
sourceHandle: 'bottom',
|
||||||
targetHandle: 'right',
|
targetHandle: 'right',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-4b',
|
id: 'e0-4b',
|
||||||
@@ -136,7 +136,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '04',
|
target: '04',
|
||||||
sourceHandle: 'left',
|
sourceHandle: 'left',
|
||||||
targetHandle: 'top',
|
targetHandle: 'top',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-10',
|
id: 'e0-10',
|
||||||
@@ -144,7 +144,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '10',
|
target: '10',
|
||||||
sourceHandle: 'top',
|
sourceHandle: 'top',
|
||||||
targetHandle: 'bottom',
|
targetHandle: 'bottom',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-20',
|
id: 'e0-20',
|
||||||
@@ -152,7 +152,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '20',
|
target: '20',
|
||||||
sourceHandle: 'right',
|
sourceHandle: 'right',
|
||||||
targetHandle: 'left',
|
targetHandle: 'left',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-30',
|
id: 'e0-30',
|
||||||
@@ -160,7 +160,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '30',
|
target: '30',
|
||||||
sourceHandle: 'bottom',
|
sourceHandle: 'bottom',
|
||||||
targetHandle: 'top',
|
targetHandle: 'top',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'e0-40',
|
id: 'e0-40',
|
||||||
@@ -168,7 +168,7 @@ const initialEdges: Edge[] = [
|
|||||||
target: '40',
|
target: '40',
|
||||||
sourceHandle: 'left',
|
sourceHandle: 'left',
|
||||||
targetHandle: 'right',
|
targetHandle: 'right',
|
||||||
type: 'default',
|
type: 'smoothstep',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import React from 'react';
|
import React, { ChangeEventHandler } from 'react';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
|
import { AppProps } from 'next/app';
|
||||||
|
|
||||||
import '../styles/globals.css';
|
import '../styles/globals.css';
|
||||||
|
|
||||||
// Unfortunately this doesn't work because preconsruct clears the dist folder and there is
|
// Unfortunately this doesn't work because preconsruct clears the dist folder and there is
|
||||||
@@ -18,6 +20,7 @@ const routes = [
|
|||||||
'/DefaultNodes',
|
'/DefaultNodes',
|
||||||
'/DragHandle',
|
'/DragHandle',
|
||||||
'/DragNDrop',
|
'/DragNDrop',
|
||||||
|
'/EdgeRouting',
|
||||||
'/EdgeTypes',
|
'/EdgeTypes',
|
||||||
'/Edges',
|
'/Edges',
|
||||||
'/Empty',
|
'/Empty',
|
||||||
@@ -45,10 +48,10 @@ const routes = [
|
|||||||
'/Validation',
|
'/Validation',
|
||||||
];
|
];
|
||||||
|
|
||||||
function MyApp({ Component, pageProps }) {
|
function App({ Component, pageProps }: AppProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const onRouteChange = (evt) => {
|
const onRouteChange: ChangeEventHandler<HTMLSelectElement> = (evt) => {
|
||||||
router.push(evt.target.value);
|
router.push(evt.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,4 +74,4 @@ function MyApp({ Component, pageProps }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MyApp;
|
export default App;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { memo } from 'react';
|
import { memo } from 'react';
|
||||||
import { EdgeProps, Position } from '../../types';
|
import { BezierEdgeProps, Position } from '../../types';
|
||||||
import BaseEdge from './BaseEdge';
|
import BaseEdge from './BaseEdge';
|
||||||
|
|
||||||
export interface GetBezierPathParams {
|
export interface GetBezierPathParams {
|
||||||
@@ -143,8 +143,8 @@ const BezierEdge = memo(
|
|||||||
style,
|
style,
|
||||||
markerEnd,
|
markerEnd,
|
||||||
markerStart,
|
markerStart,
|
||||||
curvature,
|
options,
|
||||||
}: EdgeProps) => {
|
}: BezierEdgeProps) => {
|
||||||
const params = {
|
const params = {
|
||||||
sourceX,
|
sourceX,
|
||||||
sourceY,
|
sourceY,
|
||||||
@@ -152,7 +152,7 @@ const BezierEdge = memo(
|
|||||||
targetX,
|
targetX,
|
||||||
targetY,
|
targetY,
|
||||||
targetPosition,
|
targetPosition,
|
||||||
curvature,
|
curvature: options?.curvature,
|
||||||
};
|
};
|
||||||
const path = getBezierPath(params);
|
const path = getBezierPath(params);
|
||||||
const [centerX, centerY] = getBezierCenter(params);
|
const [centerX, centerY] = getBezierCenter(params);
|
||||||
|
|||||||
@@ -1,26 +1,9 @@
|
|||||||
import { memo } from 'react';
|
import { memo } from 'react';
|
||||||
|
|
||||||
import { getCenter } from './utils';
|
import { getCenter } from './utils';
|
||||||
import { EdgeSmoothStepProps, Position } from '../../types';
|
import { SmoothStepEdgeProps, Position, XYPosition } from '../../types';
|
||||||
import BaseEdge from './BaseEdge';
|
import BaseEdge from './BaseEdge';
|
||||||
|
|
||||||
// These are some helper methods for drawing the round corners
|
|
||||||
// The name indicates the direction of the path. "bottomLeftCorner" goes
|
|
||||||
// from bottom to the left and "leftBottomCorner" goes from left to the bottom.
|
|
||||||
// We have to consider the direction of the paths because of the animated lines.
|
|
||||||
const bottomLeftCorner = (x: number, y: number, size: number): string =>
|
|
||||||
`L ${x},${y - size}Q ${x},${y} ${x + size},${y}`;
|
|
||||||
const leftBottomCorner = (x: number, y: number, size: number): string =>
|
|
||||||
`L ${x + size},${y}Q ${x},${y} ${x},${y - size}`;
|
|
||||||
const bottomRightCorner = (x: number, y: number, size: number): string =>
|
|
||||||
`L ${x},${y - size}Q ${x},${y} ${x - size},${y}`;
|
|
||||||
const rightBottomCorner = (x: number, y: number, size: number): string =>
|
|
||||||
`L ${x - size},${y}Q ${x},${y} ${x},${y - size}`;
|
|
||||||
const leftTopCorner = (x: number, y: number, size: number): string => `L ${x + size},${y}Q ${x},${y} ${x},${y + size}`;
|
|
||||||
const topLeftCorner = (x: number, y: number, size: number): string => `L ${x},${y + size}Q ${x},${y} ${x + size},${y}`;
|
|
||||||
const topRightCorner = (x: number, y: number, size: number): string => `L ${x},${y + size}Q ${x},${y} ${x - size},${y}`;
|
|
||||||
const rightTopCorner = (x: number, y: number, size: number): string => `L ${x - size},${y}Q ${x},${y} ${x},${y + size}`;
|
|
||||||
|
|
||||||
export interface GetSmoothStepPathParams {
|
export interface GetSmoothStepPathParams {
|
||||||
sourceX: number;
|
sourceX: number;
|
||||||
sourceY: number;
|
sourceY: number;
|
||||||
@@ -31,6 +14,135 @@ export interface GetSmoothStepPathParams {
|
|||||||
borderRadius?: number;
|
borderRadius?: number;
|
||||||
centerX?: number;
|
centerX?: number;
|
||||||
centerY?: number;
|
centerY?: number;
|
||||||
|
offset?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDirections = {
|
||||||
|
[Position.Left]: { x: -1, y: 0 },
|
||||||
|
[Position.Right]: { x: 1, y: 0 },
|
||||||
|
[Position.Top]: { x: 0, y: -1 },
|
||||||
|
[Position.Bottom]: { x: 0, y: 1 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDirection = ({
|
||||||
|
source,
|
||||||
|
sourcePosition = Position.Bottom,
|
||||||
|
target,
|
||||||
|
}: {
|
||||||
|
source: XYPosition;
|
||||||
|
sourcePosition: Position;
|
||||||
|
target: XYPosition;
|
||||||
|
}): XYPosition => {
|
||||||
|
if (sourcePosition === Position.Left || sourcePosition === Position.Right) {
|
||||||
|
return source.x < target.x ? { x: 1, y: 0 } : { x: -1, y: 0 };
|
||||||
|
}
|
||||||
|
return source.y < target.y ? { x: 0, y: 1 } : { x: 0, y: -1 };
|
||||||
|
};
|
||||||
|
|
||||||
|
const distance = (a: XYPosition, b: XYPosition) => Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
|
||||||
|
|
||||||
|
// ith this function we try to mimic a orthogonal edge routing behaviour
|
||||||
|
// It's not as good as a real orthogonal edge routing but it's faster and good enough as a default for step and smooth step edges
|
||||||
|
function getPoints({
|
||||||
|
source,
|
||||||
|
sourcePosition = Position.Bottom,
|
||||||
|
target,
|
||||||
|
targetPosition = Position.Top,
|
||||||
|
center,
|
||||||
|
offset,
|
||||||
|
}: {
|
||||||
|
source: XYPosition;
|
||||||
|
sourcePosition: Position;
|
||||||
|
target: XYPosition;
|
||||||
|
targetPosition: Position;
|
||||||
|
center: XYPosition;
|
||||||
|
offset: number;
|
||||||
|
}): XYPosition[] {
|
||||||
|
const sourceDir = handleDirections[sourcePosition];
|
||||||
|
const targetDir = handleDirections[targetPosition];
|
||||||
|
const sourceGapped: XYPosition = { x: source.x + sourceDir.x * offset, y: source.y + sourceDir.y * offset };
|
||||||
|
const targetGapped: XYPosition = { x: target.x + targetDir.x * offset, y: target.y + targetDir.y * offset };
|
||||||
|
const dir = getDirection({
|
||||||
|
source: sourceGapped,
|
||||||
|
sourcePosition,
|
||||||
|
target: targetGapped,
|
||||||
|
});
|
||||||
|
const dirAccessor = dir.x !== 0 ? 'x' : 'y';
|
||||||
|
const currDir = dir[dirAccessor];
|
||||||
|
|
||||||
|
let points: XYPosition[] = [];
|
||||||
|
|
||||||
|
// opposite handle positions, default case
|
||||||
|
if (sourceDir[dirAccessor] * targetDir[dirAccessor] === -1) {
|
||||||
|
// --->
|
||||||
|
// |
|
||||||
|
// >---
|
||||||
|
const verticalSplit: XYPosition[] = [
|
||||||
|
{ x: center.x, y: sourceGapped.y },
|
||||||
|
{ x: center.x, y: targetGapped.y },
|
||||||
|
];
|
||||||
|
// |
|
||||||
|
// ---
|
||||||
|
// |
|
||||||
|
const horizontalSplit: XYPosition[] = [
|
||||||
|
{ x: sourceGapped.x, y: center.y },
|
||||||
|
{ x: targetGapped.x, y: center.y },
|
||||||
|
];
|
||||||
|
|
||||||
|
if (sourceDir[dirAccessor] === currDir) {
|
||||||
|
points = dirAccessor === 'x' ? verticalSplit : horizontalSplit;
|
||||||
|
} else {
|
||||||
|
points = dirAccessor === 'x' ? horizontalSplit : verticalSplit;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// sourceTarget means we take x from source and y from target, targetSource is the opposite
|
||||||
|
const sourceTarget: XYPosition[] = [{ x: sourceGapped.x, y: targetGapped.y }];
|
||||||
|
const targetSource: XYPosition[] = [{ x: targetGapped.x, y: sourceGapped.y }];
|
||||||
|
// this handles edges with same handle positions
|
||||||
|
if (dirAccessor === 'x') {
|
||||||
|
points = sourceDir.x === currDir ? targetSource : sourceTarget;
|
||||||
|
} else {
|
||||||
|
points = sourceDir.y === currDir ? sourceTarget : targetSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
// these are conditions for handling mixed handle positions like Right -> Bottom for example
|
||||||
|
if (sourcePosition !== targetPosition) {
|
||||||
|
const dirAccessorOpposite = dirAccessor === 'x' ? 'y' : 'x';
|
||||||
|
const isSameDir = sourceDir[dirAccessor] === targetDir[dirAccessorOpposite];
|
||||||
|
const sourceGtTargetOppo = sourceGapped[dirAccessorOpposite] > targetGapped[dirAccessorOpposite];
|
||||||
|
const sourceLtTargetOppo = sourceGapped[dirAccessorOpposite] < targetGapped[dirAccessorOpposite];
|
||||||
|
const flipSourceTarget =
|
||||||
|
(sourceDir[dirAccessor] === 1 && ((!isSameDir && sourceGtTargetOppo) || (isSameDir && sourceLtTargetOppo))) ||
|
||||||
|
(sourceDir[dirAccessor] !== 1 && ((!isSameDir && sourceLtTargetOppo) || (isSameDir && sourceGtTargetOppo)));
|
||||||
|
|
||||||
|
if (flipSourceTarget) {
|
||||||
|
points = dirAccessor === 'x' ? sourceTarget : targetSource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [source, sourceGapped, ...points, targetGapped, target];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBend(a: XYPosition, b: XYPosition, c: XYPosition, size: number): string {
|
||||||
|
const bendSize = Math.min(distance(a, b) / 2, distance(b, c) / 2, size);
|
||||||
|
const { x, y } = b;
|
||||||
|
|
||||||
|
// no bend
|
||||||
|
if ((a.x === x && x === c.x) || (a.y === y && y === c.y)) {
|
||||||
|
return `L${x} ${y}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// first segment is horizontal
|
||||||
|
if (a.y === y) {
|
||||||
|
const xDir = a.x < c.x ? -1 : 1;
|
||||||
|
const yDir = a.y < c.y ? 1 : -1;
|
||||||
|
return `L ${x + bendSize * xDir},${y}Q ${x},${y} ${x},${y + bendSize * yDir}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const xDir = a.x < c.x ? 1 : -1;
|
||||||
|
const yDir = a.y < c.y ? -1 : 1;
|
||||||
|
return `L ${x},${y + bendSize * yDir}Q ${x},${y} ${x + bendSize * xDir},${y}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSmoothStepPath({
|
export function getSmoothStepPath({
|
||||||
@@ -43,76 +155,34 @@ export function getSmoothStepPath({
|
|||||||
borderRadius = 5,
|
borderRadius = 5,
|
||||||
centerX,
|
centerX,
|
||||||
centerY,
|
centerY,
|
||||||
|
offset = 20,
|
||||||
}: GetSmoothStepPathParams): string {
|
}: GetSmoothStepPathParams): string {
|
||||||
const [_centerX, _centerY, offsetX, offsetY] = getCenter({ sourceX, sourceY, targetX, targetY });
|
const [_centerX, _centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
|
||||||
const cornerWidth = Math.min(borderRadius, Math.abs(targetX - sourceX));
|
|
||||||
const cornerHeight = Math.min(borderRadius, Math.abs(targetY - sourceY));
|
|
||||||
const cornerSize = Math.min(cornerWidth, cornerHeight, offsetX, offsetY);
|
|
||||||
const leftAndRight = [Position.Left, Position.Right];
|
|
||||||
const cX = typeof centerX !== 'undefined' ? centerX : _centerX;
|
const cX = typeof centerX !== 'undefined' ? centerX : _centerX;
|
||||||
const cY = typeof centerY !== 'undefined' ? centerY : _centerY;
|
const cY = typeof centerY !== 'undefined' ? centerY : _centerY;
|
||||||
|
|
||||||
let firstCornerPath = null;
|
const points = getPoints({
|
||||||
let secondCornerPath = null;
|
source: { x: sourceX, y: sourceY },
|
||||||
|
sourcePosition,
|
||||||
|
target: { x: targetX, y: targetY },
|
||||||
|
targetPosition,
|
||||||
|
center: { x: cX, y: cY },
|
||||||
|
offset,
|
||||||
|
});
|
||||||
|
|
||||||
if (sourceX <= targetX) {
|
return points.reduce<string>((res, p, i) => {
|
||||||
firstCornerPath =
|
let segment = '';
|
||||||
sourceY <= targetY ? bottomLeftCorner(sourceX, cY, cornerSize) : topLeftCorner(sourceX, cY, cornerSize);
|
|
||||||
secondCornerPath =
|
|
||||||
sourceY <= targetY ? rightTopCorner(targetX, cY, cornerSize) : rightBottomCorner(targetX, cY, cornerSize);
|
|
||||||
} else {
|
|
||||||
firstCornerPath =
|
|
||||||
sourceY < targetY ? bottomRightCorner(sourceX, cY, cornerSize) : topRightCorner(sourceX, cY, cornerSize);
|
|
||||||
secondCornerPath =
|
|
||||||
sourceY < targetY ? leftTopCorner(targetX, cY, cornerSize) : leftBottomCorner(targetX, cY, cornerSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (leftAndRight.includes(sourcePosition) && leftAndRight.includes(targetPosition)) {
|
if (i > 0 && i < points.length - 1) {
|
||||||
if (sourceX <= targetX) {
|
segment = getBend(points[i - 1], p, points[i + 1], borderRadius);
|
||||||
firstCornerPath =
|
|
||||||
sourceY <= targetY ? rightTopCorner(cX, sourceY, cornerSize) : rightBottomCorner(cX, sourceY, cornerSize);
|
|
||||||
secondCornerPath =
|
|
||||||
sourceY <= targetY ? bottomLeftCorner(cX, targetY, cornerSize) : topLeftCorner(cX, targetY, cornerSize);
|
|
||||||
} else if (
|
|
||||||
(sourcePosition === Position.Right && targetPosition === Position.Left) ||
|
|
||||||
(sourcePosition === Position.Left && targetPosition === Position.Right) ||
|
|
||||||
(sourcePosition === Position.Left && targetPosition === Position.Left)
|
|
||||||
) {
|
|
||||||
// and sourceX > targetX
|
|
||||||
firstCornerPath =
|
|
||||||
sourceY <= targetY ? leftTopCorner(cX, sourceY, cornerSize) : leftBottomCorner(cX, sourceY, cornerSize);
|
|
||||||
secondCornerPath =
|
|
||||||
sourceY <= targetY ? bottomRightCorner(cX, targetY, cornerSize) : topRightCorner(cX, targetY, cornerSize);
|
|
||||||
}
|
|
||||||
} else if (leftAndRight.includes(sourcePosition) && !leftAndRight.includes(targetPosition)) {
|
|
||||||
if (sourceX <= targetX) {
|
|
||||||
firstCornerPath =
|
|
||||||
sourceY <= targetY
|
|
||||||
? rightTopCorner(targetX, sourceY, cornerSize)
|
|
||||||
: rightBottomCorner(targetX, sourceY, cornerSize);
|
|
||||||
} else {
|
} else {
|
||||||
firstCornerPath =
|
segment = `${i === 0 ? 'M' : 'L'}${p.x} ${p.y}`;
|
||||||
sourceY <= targetY
|
|
||||||
? leftTopCorner(targetX, sourceY, cornerSize)
|
|
||||||
: leftBottomCorner(targetX, sourceY, cornerSize);
|
|
||||||
}
|
}
|
||||||
secondCornerPath = '';
|
|
||||||
} else if (!leftAndRight.includes(sourcePosition) && leftAndRight.includes(targetPosition)) {
|
|
||||||
if (sourceX <= targetX) {
|
|
||||||
firstCornerPath =
|
|
||||||
sourceY <= targetY
|
|
||||||
? bottomLeftCorner(sourceX, targetY, cornerSize)
|
|
||||||
: topLeftCorner(sourceX, targetY, cornerSize);
|
|
||||||
} else {
|
|
||||||
firstCornerPath =
|
|
||||||
sourceY <= targetY
|
|
||||||
? bottomRightCorner(sourceX, targetY, cornerSize)
|
|
||||||
: topRightCorner(sourceX, targetY, cornerSize);
|
|
||||||
}
|
|
||||||
secondCornerPath = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return `M ${sourceX},${sourceY}${firstCornerPath}${secondCornerPath}L ${targetX},${targetY}`;
|
res += segment;
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
const SmoothStepEdge = memo(
|
const SmoothStepEdge = memo(
|
||||||
@@ -132,8 +202,8 @@ const SmoothStepEdge = memo(
|
|||||||
targetPosition = Position.Top,
|
targetPosition = Position.Top,
|
||||||
markerEnd,
|
markerEnd,
|
||||||
markerStart,
|
markerStart,
|
||||||
borderRadius = 5,
|
options,
|
||||||
}: EdgeSmoothStepProps) => {
|
}: SmoothStepEdgeProps) => {
|
||||||
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition });
|
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition });
|
||||||
|
|
||||||
const path = getSmoothStepPath({
|
const path = getSmoothStepPath({
|
||||||
@@ -143,7 +213,8 @@ const SmoothStepEdge = memo(
|
|||||||
targetX,
|
targetX,
|
||||||
targetY,
|
targetY,
|
||||||
targetPosition,
|
targetPosition,
|
||||||
borderRadius,
|
borderRadius: options?.borderRadius,
|
||||||
|
offset: options?.offset,
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
|
|||||||
rfId,
|
rfId,
|
||||||
ariaLabel,
|
ariaLabel,
|
||||||
disableKeyboardA11y,
|
disableKeyboardA11y,
|
||||||
|
options,
|
||||||
}: WrapEdgeProps): JSX.Element | null => {
|
}: WrapEdgeProps): JSX.Element | null => {
|
||||||
const edgeRef = useRef<SVGGElement>(null);
|
const edgeRef = useRef<SVGGElement>(null);
|
||||||
const [updateHover, setUpdateHover] = useState<boolean>(false);
|
const [updateHover, setUpdateHover] = useState<boolean>(false);
|
||||||
@@ -189,6 +190,7 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
|
|||||||
targetHandleId={targetHandleId}
|
targetHandleId={targetHandleId}
|
||||||
markerStart={markerStartUrl}
|
markerStart={markerStartUrl}
|
||||||
markerEnd={markerEndUrl}
|
markerEnd={markerEndUrl}
|
||||||
|
options={options}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ export default (NodeComponent: ComponentType<NodeProps>) => {
|
|||||||
dragHandle,
|
dragHandle,
|
||||||
zIndex,
|
zIndex,
|
||||||
isParent,
|
isParent,
|
||||||
noPanClassName,
|
|
||||||
noDragClassName,
|
noDragClassName,
|
||||||
initialized,
|
initialized,
|
||||||
disableKeyboardA11y,
|
disableKeyboardA11y,
|
||||||
|
|||||||
@@ -126,7 +126,8 @@ const StoreUpdater = ({
|
|||||||
const store = useStoreApi();
|
const store = useStoreApi();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setDefaultNodesAndEdges(defaultNodes, defaultEdges);
|
const edgesWithDefaults = defaultEdges?.map((e) => ({ ...e, ...defaultEdgeOptions }));
|
||||||
|
setDefaultNodesAndEdges(defaultNodes, edgesWithDefaults);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
reset();
|
reset();
|
||||||
|
|||||||
@@ -182,6 +182,7 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
|
|||||||
rfId={props.rfId}
|
rfId={props.rfId}
|
||||||
ariaLabel={edge.ariaLabel}
|
ariaLabel={edge.ariaLabel}
|
||||||
disableKeyboardA11y={props.disableKeyboardA11y}
|
disableKeyboardA11y={props.disableKeyboardA11y}
|
||||||
|
options={'options' in edge ? edge.options : undefined}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ const NodeRenderer = (props: NodeRendererProps) => {
|
|||||||
zIndex={node[internalsSymbol]?.z ?? 0}
|
zIndex={node[internalsSymbol]?.z ?? 0}
|
||||||
isParent={!!node[internalsSymbol]?.isParent}
|
isParent={!!node[internalsSymbol]?.isParent}
|
||||||
noDragClassName={props.noDragClassName}
|
noDragClassName={props.noDragClassName}
|
||||||
noPanClassName={props.noPanClassName}
|
|
||||||
initialized={!!node.width && !!node.height}
|
initialized={!!node.width && !!node.height}
|
||||||
rfId={props.rfId}
|
rfId={props.rfId}
|
||||||
disableKeyboardA11y={props.disableKeyboardA11y}
|
disableKeyboardA11y={props.disableKeyboardA11y}
|
||||||
|
|||||||
@@ -28,14 +28,3 @@ export function useNodeOrEdgeTypes(nodeOrEdgeTypes: any, createTypes: any): any
|
|||||||
|
|
||||||
return typesParsed;
|
return typesParsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function injectStyle(css: string): void {
|
|
||||||
if (!css || typeof document === 'undefined') return;
|
|
||||||
|
|
||||||
const head = document.head || document.getElementsByTagName('head')[0];
|
|
||||||
const style = document.createElement('style');
|
|
||||||
|
|
||||||
head.appendChild(style);
|
|
||||||
|
|
||||||
style.appendChild(document.createTextNode(css));
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export {
|
|||||||
getSimpleBezierCenter as getSimpleBezierEdgeCenter,
|
getSimpleBezierCenter as getSimpleBezierEdgeCenter,
|
||||||
} from './components/Edges/SimpleBezierEdge';
|
} from './components/Edges/SimpleBezierEdge';
|
||||||
export { default as SmoothStepEdge, getSmoothStepPath } from './components/Edges/SmoothStepEdge';
|
export { default as SmoothStepEdge, getSmoothStepPath } from './components/Edges/SmoothStepEdge';
|
||||||
|
export { default as BaseEdge } from './components/Edges/BaseEdge';
|
||||||
|
|
||||||
export { internalsSymbol, rectToBox, boxToRect, getBoundsOfRects } from './utils';
|
export { internalsSymbol, rectToBox, boxToRect, getBoundsOfRects } from './utils';
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { Node } from './nodes';
|
|||||||
import { Position } from './utils';
|
import { Position } from './utils';
|
||||||
|
|
||||||
// interface for the user edge items
|
// interface for the user edge items
|
||||||
export interface Edge<T = any> {
|
type DefaultEdge<T = any> = {
|
||||||
id: string;
|
id: string;
|
||||||
type?: string;
|
type?: string;
|
||||||
source: string;
|
source: string;
|
||||||
@@ -32,7 +32,28 @@ export interface Edge<T = any> {
|
|||||||
markerEnd?: EdgeMarkerType;
|
markerEnd?: EdgeMarkerType;
|
||||||
zIndex?: number;
|
zIndex?: number;
|
||||||
ariaLabel?: string;
|
ariaLabel?: string;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export type SmoothStepEdgeOptions = {
|
||||||
|
offset?: number;
|
||||||
|
borderRadius?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type SmoothStepEdgeType<T> = DefaultEdge<T> & {
|
||||||
|
type: 'smoothstep';
|
||||||
|
options?: SmoothStepEdgeOptions;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type BezierEdgeOptions = {
|
||||||
|
curvature?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type BezierEdgeType<T> = DefaultEdge<T> & {
|
||||||
|
type: 'default';
|
||||||
|
options?: BezierEdgeOptions;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Edge<T = any> = DefaultEdge<T> | SmoothStepEdgeType<T> | BezierEdgeType<T>;
|
||||||
|
|
||||||
export type DefaultEdgeOptions = Omit<
|
export type DefaultEdgeOptions = Omit<
|
||||||
Edge,
|
Edge,
|
||||||
@@ -64,7 +85,7 @@ export interface EdgeProps<T = any> {
|
|||||||
targetHandleId?: string | null;
|
targetHandleId?: string | null;
|
||||||
markerStart?: string;
|
markerStart?: string;
|
||||||
markerEnd?: string;
|
markerEnd?: string;
|
||||||
curvature?: number;
|
options?: BezierEdgeOptions | SmoothStepEdgeOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BaseEdgeProps = Pick<
|
export type BaseEdgeProps = Pick<
|
||||||
@@ -127,12 +148,16 @@ export interface WrapEdgeProps<T = any> {
|
|||||||
rfId?: string;
|
rfId?: string;
|
||||||
ariaLabel?: string | null;
|
ariaLabel?: string | null;
|
||||||
disableKeyboardA11y: boolean;
|
disableKeyboardA11y: boolean;
|
||||||
|
options?: SmoothStepEdgeOptions | BezierEdgeOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EdgeSmoothStepProps<T = any> extends EdgeProps<T> {
|
export interface SmoothStepEdgeProps<T = any> extends EdgeProps<T> {
|
||||||
borderRadius?: number;
|
options?: SmoothStepEdgeOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface BezierEdgeProps<T = any> extends EdgeProps<T> {
|
||||||
|
options?: BezierEdgeOptions;
|
||||||
|
}
|
||||||
export interface EdgeTextProps extends HTMLAttributes<SVGElement> {
|
export interface EdgeTextProps extends HTMLAttributes<SVGElement> {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ export interface WrapNodeProps<T = any> {
|
|||||||
dragHandle?: string;
|
dragHandle?: string;
|
||||||
zIndex: number;
|
zIndex: number;
|
||||||
isParent: boolean;
|
isParent: boolean;
|
||||||
noPanClassName: string;
|
|
||||||
noDragClassName: string;
|
noDragClassName: string;
|
||||||
rfId: string;
|
rfId: string;
|
||||||
disableKeyboardA11y: boolean;
|
disableKeyboardA11y: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user