refactor(edgeRenderer): cleanup

This commit is contained in:
moklick
2020-11-15 18:21:32 +01:00
parent 393defc638
commit 2b1366fe46
2 changed files with 11 additions and 21 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ import { useStoreState } from '../../store/hooks';
import ConnectionLine from '../../components/ConnectionLine/index'; import ConnectionLine from '../../components/ConnectionLine/index';
import { isEdge } from '../../utils/graph'; import { isEdge } from '../../utils/graph';
import MarkerDefinitions from './MarkerDefinitions'; import MarkerDefinitions from './MarkerDefinitions';
import { getEdgePositions, getHandle, isEdgeVisible, getSourceTargetNode } from './utils'; import { getEdgePositions, getHandle, isEdgeVisible, getSourceTargetNodes } from './utils';
import { Position, Edge, Node, Elements, ConnectionLineType, ConnectionLineComponent, Transform } from '../../types'; import { Position, Edge, Node, Elements, ConnectionLineType, ConnectionLineComponent, Transform } from '../../types';
interface EdgeRendererProps { interface EdgeRendererProps {
@@ -31,7 +31,7 @@ function renderEdge(
) { ) {
const sourceHandleId = edge.sourceHandle || null; const sourceHandleId = edge.sourceHandle || null;
const targetHandleId = edge.targetHandle || null; const targetHandleId = edge.targetHandle || null;
const { sourceNode, targetNode } = getSourceTargetNode(edge, nodes); const { sourceNode, targetNode } = getSourceTargetNodes(edge, nodes);
if (!sourceNode) { if (!sourceNode) {
console.warn(`couldn't create edge for source id: ${edge.source}`); console.warn(`couldn't create edge for source id: ${edge.source}`);
+9 -19
View File
@@ -40,8 +40,8 @@ export function createEdgeTypes(edgeTypes: EdgeTypesType): EdgeTypesType {
} }
export function getHandlePosition(position: Position, node: Node, handle: any | null = null): XYPosition { export function getHandlePosition(position: Position, node: Node, handle: any | null = null): XYPosition {
const x = handle?.x || 0; const x = (handle?.x || 0) + node.__rf.position.x;
const y = handle?.y || 0; const y = (handle?.y || 0) + node.__rf.position.y;
const width = handle?.width || node.__rf.width; const width = handle?.width || node.__rf.width;
const height = handle?.height || node.__rf.height; const height = handle?.height || node.__rf.height;
@@ -74,21 +74,16 @@ export function getHandle(bounds: HandleElement[], handleId: ElementId | null):
return null; return null;
} }
let handle = null;
// there is no handleId when there are no multiple handles/ handles with ids // there is no handleId when there are no multiple handles/ handles with ids
// so we just pick the first one // so we just pick the first one
let handle = null;
if (bounds.length === 1 || !handleId) { if (bounds.length === 1 || !handleId) {
handle = bounds[0]; handle = bounds[0];
} else if (handleId) { } else if (handleId) {
handle = bounds.find((d) => d.id === handleId); handle = bounds.find((d) => d.id === handleId);
} }
if (typeof handle === 'undefined') { return typeof handle === 'undefined' ? null : handle;
return null;
}
return handle;
} }
interface EdgePositions { interface EdgePositions {
@@ -107,18 +102,13 @@ export const getEdgePositions = (
targetPosition: Position targetPosition: Position
): EdgePositions => { ): EdgePositions => {
const sourceHandlePos = getHandlePosition(sourcePosition, sourceNode, sourceHandle); const sourceHandlePos = getHandlePosition(sourcePosition, sourceNode, sourceHandle);
const sourceX = sourceNode.__rf.position.x + sourceHandlePos.x;
const sourceY = sourceNode.__rf.position.y + sourceHandlePos.y;
const targetHandlePos = getHandlePosition(targetPosition, targetNode, targetHandle); const targetHandlePos = getHandlePosition(targetPosition, targetNode, targetHandle);
const targetX = targetNode.__rf.position.x + targetHandlePos.x;
const targetY = targetNode.__rf.position.y + targetHandlePos.y;
return { return {
sourceX, sourceX: sourceHandlePos.x,
sourceY, sourceY: sourceHandlePos.y,
targetX, targetX: targetHandlePos.x,
targetY, targetY: targetHandlePos.y,
}; };
}; };
@@ -165,7 +155,7 @@ type SourceTargetNode = {
targetNode: Node | null; targetNode: Node | null;
}; };
export const getSourceTargetNode = (edge: Edge, nodes: Node[]): SourceTargetNode => { export const getSourceTargetNodes = (edge: Edge, nodes: Node[]): SourceTargetNode => {
return nodes.reduce( return nodes.reduce(
(res, node) => { (res, node) => {
if (node.id === edge.source) { if (node.id === edge.source) {