Skip to content

Commit

Permalink
feat(edges): add interactionWidth option #1580 #1114
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Sep 7, 2022
1 parent 7a96817 commit 51e957e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 61 deletions.
8 changes: 5 additions & 3 deletions examples/nextjs/pages/EdgeRouting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ const edges: Edge[] = [
markerEnd: {
type: MarkerType.ArrowClosed,
},
options: {
pathOptions: {
offset: 30,
},
interactionWidth: 0,
},
{
id: 'e3-4',
Expand All @@ -93,9 +94,10 @@ const edges: Edge[] = [
markerEnd: {
type: MarkerType.ArrowClosed,
},
options: {
pathOptions: {
borderRadius: 2,
},
interactionWidth: 0,
},

{
Expand All @@ -121,7 +123,7 @@ const edges: Edge[] = [

const defaultEdgeOptions = {
style: {
strokeWidth: 2,
strokeWidth: 1,
},
};

Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/components/Edges/BaseEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@ const BaseEdge = ({
style,
markerEnd,
markerStart,
interactionWidth = 15,
}: BaseEdgeProps) => {
const text = label ? (
<EdgeText
x={centerX}
y={centerY}
label={label}
labelStyle={labelStyle}
labelShowBg={labelShowBg}
labelBgStyle={labelBgStyle}
labelBgPadding={labelBgPadding}
labelBgBorderRadius={labelBgBorderRadius}
/>
) : null;

return (
<>
<path
Expand All @@ -38,7 +26,19 @@ const BaseEdge = ({
markerEnd={markerEnd}
markerStart={markerStart}
/>
{text}
{interactionWidth && <path d={path} fill="none" strokeWidth={interactionWidth} />}
{label ? (
<EdgeText
x={centerX}
y={centerY}
label={label}
labelStyle={labelStyle}
labelShowBg={labelShowBg}
labelBgStyle={labelBgStyle}
labelBgPadding={labelBgPadding}
labelBgBorderRadius={labelBgBorderRadius}
/>
) : null}
</>
);
};
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/components/Edges/BezierEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ const BezierEdge = memo(
style,
markerEnd,
markerStart,
options,
pathOptions,
interactionWidth,
}: BezierEdgeProps) => {
const params = {
sourceX,
Expand All @@ -152,7 +153,7 @@ const BezierEdge = memo(
targetX,
targetY,
targetPosition,
curvature: options?.curvature,
curvature: pathOptions?.curvature,
};
const path = getBezierPath(params);
const [centerX, centerY] = getBezierCenter(params);
Expand All @@ -171,6 +172,7 @@ const BezierEdge = memo(
style={style}
markerEnd={markerEnd}
markerStart={markerStart}
interactionWidth={interactionWidth}
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/Edges/SimpleBezierEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const SimpleBezierEdge = memo(
style,
markerEnd,
markerStart,
interactionWidth,
}: EdgeProps) => {
const params = {
sourceX,
Expand All @@ -143,6 +144,7 @@ const SimpleBezierEdge = memo(
style={style}
markerEnd={markerEnd}
markerStart={markerStart}
interactionWidth={interactionWidth}
/>
);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/components/Edges/SmoothStepEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ const SmoothStepEdge = memo(
targetPosition = Position.Top,
markerEnd,
markerStart,
options,
pathOptions,
interactionWidth,
}: SmoothStepEdgeProps) => {
const [centerX, centerY] = getCenter({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition });

Expand All @@ -213,8 +214,8 @@ const SmoothStepEdge = memo(
targetX,
targetY,
targetPosition,
borderRadius: options?.borderRadius,
offset: options?.offset,
borderRadius: pathOptions?.borderRadius,
offset: pathOptions?.offset,
});

return (
Expand All @@ -231,6 +232,7 @@ const SmoothStepEdge = memo(
style={style}
markerEnd={markerEnd}
markerStart={markerStart}
interactionWidth={interactionWidth}
/>
);
}
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/components/Edges/StepEdge.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { memo } from 'react';
import { memo, useMemo } from 'react';

import { EdgeSmoothStepProps } from '../../types';
import { SmoothStepEdgeProps } from '../../types';
import SmoothStepEdge from './SmoothStepEdge';

const StepEdge = memo((props: EdgeSmoothStepProps) => <SmoothStepEdge {...props} borderRadius={0} />);
const StepEdge = memo((props: SmoothStepEdgeProps) => (
<SmoothStepEdge
{...props}
pathOptions={useMemo(() => ({ borderRadius: 0, offset: props.pathOptions?.offset }), [props.pathOptions?.offset])}
/>
));

StepEdge.displayName = 'StepEdge';

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/components/Edges/StraightEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ const StraightEdge = memo(
style,
markerEnd,
markerStart,
interactionWidth,
}: EdgeProps) => {
const yOffset = Math.abs(targetY - sourceY) / 2;
const centerY = targetY < sourceY ? targetY + yOffset : targetY - yOffset;

const xOffset = Math.abs(targetX - sourceX) / 2;
const centerX = targetX < sourceX ? targetX + xOffset : targetX - xOffset;
const path = `M ${sourceX},${sourceY}L ${targetX},${targetY}`;

return (
<BaseEdge
path={path}
path={`M ${sourceX},${sourceY}L ${targetX},${targetY}`}
centerX={centerX}
centerY={centerY}
label={label}
Expand All @@ -40,6 +40,7 @@ const StraightEdge = memo(
style={style}
markerEnd={markerEnd}
markerStart={markerStart}
interactionWidth={interactionWidth}
/>
);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/components/Edges/wrapEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
rfId,
ariaLabel,
disableKeyboardA11y,
options,
pathOptions,
interactionWidth,
}: WrapEdgeProps): JSX.Element | null => {
const edgeRef = useRef<SVGGElement>(null);
const [updateHover, setUpdateHover] = useState<boolean>(false);
Expand Down Expand Up @@ -190,7 +191,8 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
targetHandleId={targetHandleId}
markerStart={markerStartUrl}
markerEnd={markerEndUrl}
options={options}
pathOptions={pathOptions}
interactionWidth={interactionWidth}
/>
)}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/container/EdgeRenderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
rfId={props.rfId}
ariaLabel={edge.ariaLabel}
disableKeyboardA11y={props.disableKeyboardA11y}
options={'options' in edge ? edge.options : undefined}
pathOptions={'pathOptions' in edge ? edge.pathOptions : undefined}
interactionWidth={edge.interactionWidth}
/>
);
})}
Expand Down
46 changes: 15 additions & 31 deletions packages/core/src/types/edges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ type DefaultEdge<T = any> = {
markerEnd?: EdgeMarkerType;
zIndex?: number;
ariaLabel?: string;
interactionWidth?: number;
};

export type SmoothStepEdgeOptions = {
export type SmoothStepPathOptions = {
offset?: number;
borderRadius?: number;
};

type SmoothStepEdgeType<T> = DefaultEdge<T> & {
type: 'smoothstep';
options?: SmoothStepEdgeOptions;
pathOptions?: SmoothStepPathOptions;
};

export type BezierEdgeOptions = {
export type BezierPathOptions = {
curvature?: number;
};

type BezierEdgeType<T> = DefaultEdge<T> & {
type: 'default';
options?: BezierEdgeOptions;
pathOptions?: BezierPathOptions;
};

export type Edge<T = any> = DefaultEdge<T> | SmoothStepEdgeType<T> | BezierEdgeType<T>;
Expand All @@ -61,7 +62,7 @@ export type DefaultEdgeOptions = Omit<
>;

// props that get passed to a custom edge
export interface EdgeProps<T = any> {
export type EdgeProps<T = any> = {
id: string;
source: string;
target: string;
Expand All @@ -85,8 +86,9 @@ export interface EdgeProps<T = any> {
targetHandleId?: string | null;
markerStart?: string;
markerEnd?: string;
options?: BezierEdgeOptions | SmoothStepEdgeOptions;
}
pathOptions?: BezierPathOptions | SmoothStepPathOptions;
interactionWidth?: number;
};

export type BaseEdgeProps = Pick<
EdgeProps,
Expand All @@ -99,6 +101,7 @@ export type BaseEdgeProps = Pick<
| 'style'
| 'markerStart'
| 'markerEnd'
| 'interactionWidth'
> & {
centerX: number;
centerY: number;
Expand All @@ -107,24 +110,9 @@ export type BaseEdgeProps = Pick<

export type EdgeMouseHandler = (event: React.MouseEvent, edge: Edge) => void;

export interface WrapEdgeProps<T = any> {
id: string;
className?: string;
type: string;
data?: T;
export type WrapEdgeProps<T = any> = Omit<Edge<T>, 'sourceHandle' | 'targetHandle'> & {
onClick?: EdgeMouseHandler;
onEdgeDoubleClick?: EdgeMouseHandler;
selected: boolean;
animated?: boolean;
label?: string | ReactNode;
labelStyle?: CSSProperties;
labelShowBg?: boolean;
labelBgStyle?: CSSProperties;
labelBgPadding?: [number, number];
labelBgBorderRadius?: number;
style?: CSSProperties;
source: string;
target: string;
sourceHandleId?: string | null;
targetHandleId?: string | null;
sourceX: number;
Expand All @@ -134,7 +122,6 @@ export interface WrapEdgeProps<T = any> {
sourcePosition: Position;
targetPosition: Position;
elementsSelectable?: boolean;
hidden?: boolean;
onEdgeUpdate?: OnEdgeUpdateFunc;
onContextMenu?: EdgeMouseHandler;
onMouseEnter?: EdgeMouseHandler;
Expand All @@ -143,20 +130,17 @@ export interface WrapEdgeProps<T = any> {
edgeUpdaterRadius?: number;
onEdgeUpdateStart?: (event: React.MouseEvent, edge: Edge, handleType: HandleType) => void;
onEdgeUpdateEnd?: (event: MouseEvent, edge: Edge, handleType: HandleType) => void;
markerStart?: EdgeMarkerType;
markerEnd?: EdgeMarkerType;
rfId?: string;
ariaLabel?: string | null;
disableKeyboardA11y: boolean;
options?: SmoothStepEdgeOptions | BezierEdgeOptions;
}
pathOptions?: BezierPathOptions | SmoothStepPathOptions;
};

export interface SmoothStepEdgeProps<T = any> extends EdgeProps<T> {
options?: SmoothStepEdgeOptions;
pathOptions?: SmoothStepPathOptions;
}

export interface BezierEdgeProps<T = any> extends EdgeProps<T> {
options?: BezierEdgeOptions;
pathOptions?: BezierPathOptions;
}
export interface EdgeTextProps extends HTMLAttributes<SVGElement> {
x: number;
Expand Down

0 comments on commit 51e957e

Please sign in to comment.