All files / src/utils getPortPosition.ts

94.74% Statements 18/19
40% Branches 4/10
100% Functions 2/2
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47      1x 1x   1x           52905x 52905x 52905x   52905x 52905x 52905x 52905x   52905x     1x               21174x   21166x       63497x   21166x       21166x    
import { ICanvasNode, ICanvasPort, IGraphProps, TSetData } from "../components";
 
import { IGraphConfig } from "../contexts";
import { getNodeConfig } from "./getNodeConfig";
import { getTransformedPosition } from "./transformMatrix";
 
export const getPortPosition = (
  node: ICanvasNode,
  port: ICanvasPort,
  graphConfig: IGraphConfig,
  transformMatrix?: [number, number, number, number, number, number]
): { x: number; y: number } => {
  const nodeConfig = getNodeConfig(node, graphConfig);
  const width = nodeConfig.getWidth(node);
  const height = nodeConfig.getHeight(node);
 
  const xOffset = port.position ? port.position[0] * width : width * 0.5;
  const x = node.x + xOffset;
  const yOffset = port.position ? port.position[1] * height : height;
  const y = node.y + yOffset;
 
  return getTransformedPosition(x, y, transformMatrix);
};
 
export const getPortPositionByPortId = (
  node: ICanvasNode,
  portId: string,
  graphConfig: IGraphConfig,
  graphProps: IGraphProps,
  setData: TSetData,
  transformMatrix?: [number, number, number, number, number, number] // matrix is for the svg has pan/zoom
): { x: number; y: number } => {
  const nodeConfig = getNodeConfig(node, graphConfig);
 
  const ports = nodeConfig.getPorts
    ? nodeConfig.getPorts({ model: node, graphProps, setData })
    : node.ports || [];
 
  const port = ports.filter(p => p.id === portId)[0];
 
  Iif (!port) {
    throw new Error(`invalid port id ${portId}`);
  }
 
  return getPortPosition(node, port, graphConfig, transformMatrix);
};