All files / src/built-in withDefaultPortsPosition.ts

95.45% Statements 21/22
50% Branches 2/4
100% Functions 4/4
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      1x 14x     31748x 31748x   31748x       95236x 95236x 31748x 31748x 31748x 31748x 31748x   31748x 31814x           31748x 63422x           31748x        
import { ICanvasPort } from "../components";
import { INodeConfig, INodeDrawArgs } from "../contexts";
 
export const withDefaultPortsPosition = (base: INodeConfig): INodeConfig => {
  return {
    ...base,
    getPorts: (args: INodeDrawArgs): ICanvasPort[] => {
      const node = args.model;
      const rawPorts = base.getPorts ? base.getPorts(args) : node.ports;
 
      Iif (!rawPorts) {
        return [];
      }
 
      const inputPorts = rawPorts.filter(p => !p.isInputDisabled);
      const outputPorts = rawPorts.filter(p => p.isInputDisabled);
      const inputPortsCount = inputPorts.length;
      const outputPortsCount = outputPorts.length;
      const inputPortsInterval = 1 / (inputPortsCount + 1);
      const outputPortsInterval = 1 / (outputPortsCount + 1);
      const res: ICanvasPort[] = [];
 
      for (let i = 0; i < inputPortsCount; i++) {
        res.push({
          ...inputPorts[i],
          position: [(i + 1) * inputPortsInterval, 0]
        });
      }
 
      for (let i = 0; i < outputPortsCount; i++) {
        res.push({
          ...outputPorts[i],
          position: [(i + 1) * outputPortsInterval, 1]
        });
      }
 
      return res;
    }
  };
};