All files / modules/70-pipeline/components/Diagram/link DefaultLinkWidget.tsx

48% Statements 24/50
19.05% Branches 4/21
30% Functions 3/10
51.06% Lines 24/47

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              207x 207x     207x 207x 207x                       207x                               207x                     207x 46x 46x   46x 46x 46x         46x 46x 32x       46x 46x   46x                                               46x 46x   46x 46x 46x                                                                                    
/*
 * Copyright 2020 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import { DiagramEngine, LinkWidget, PointModel } from '@projectstorm/react-diagrams-core'
import type { MouseEvent } from 'react'
import type { DefaultLinkModel } from './DefaultLinkModel'
import { DefaultLinkPointWidget } from './DefaultLinkPointWidget'
import { DefaultLinkSegmentWidget } from './DefaultLinkSegmentWidget'
import css from './Default.module.scss'
 
export interface DefaultLinkProps {
  link: DefaultLinkModel
  diagramEngine: DiagramEngine
  pointAdded?: (point: PointModel, event: MouseEvent) => any
}
 
export interface DefaultLinkState {
  selected: boolean
}
 
const addPointToLink = (event: MouseEvent, index: number, props: DefaultLinkProps): void => {
  if (
    !event.shiftKey &&
    !props.link.isLocked() &&
    props.link.getPoints().length - 1 <= props.diagramEngine.getMaxNumberPointsPerLink()
  ) {
    const point = new PointModel({
      link: props.link,
      position: props.diagramEngine.getRelativeMousePoint(event)
    })
    props.link.addPoint(point, index)
    event.persist()
    event.stopPropagation()
  }
}
 
const generatePoint = (point: PointModel, props: DefaultLinkProps): JSX.Element => {
  return (
    <DefaultLinkPointWidget
      key={point.getID()}
      point={point as any}
      colorSelected={props.link.getOptions().selectedColor || ''}
      color={props.link.getOptions().color}
    />
  )
}
 
export function DefaultLinkWidget(props: DefaultLinkProps): React.ReactElement | null {
  const [state, setState] = React.useState<DefaultLinkState>({ selected: false })
  const refPaths: React.RefObject<SVGPathElement>[] = []
 
  React.useEffect(() => {
    const renderedPaths: SVGPathElement[] = []
    refPaths.forEach(ref => {
      if (ref.current !== null) {
        renderedPaths.push(ref.current)
      }
    })
    props.link.setRenderedPaths(renderedPaths)
    return () => {
      props.link.setRenderedPaths([])
    }
  }, [refPaths, props.link])
 
  const { diagramEngine, link } = props
  const { selected } = state
 
  const generateLink = React.useCallback(
    (path: string, extraProps: any, id: string | number): JSX.Element => {
      const ref = React.createRef<SVGPathElement>()
      refPaths.push(ref)
      return (
        <DefaultLinkSegmentWidget
          key={`link-${id}`}
          path={path}
          selected={selected}
          diagramEngine={diagramEngine}
          factory={diagramEngine.getFactoryForLink(link)}
          link={link}
          forwardRef={ref}
          onSelection={(select: boolean) => {
            setState(prevState => ({ ...prevState, selected: select }))
          }}
          extras={extraProps}
        />
      )
    },
    [diagramEngine, link, refPaths, selected]
  )
 
  //ensure id is present for all points on the path
  const points = props.link.getPoints()
  const paths = []
 
  Eif (points.length === 2) {
    Eif (points[1].getPosition().x === 0 && points[1].getPosition().y === 0) {
      return null
    }
    paths.push(generateLink(props.link.getSVGPath() || '', {}, '0'))
 
    // draw the link as dangeling
    if (props.link.getTargetPort() == null) {
      paths.push(generatePoint(points[1], props))
    }
  } else {
    //draw the multiple anchors and complex line instead
    for (let j = 0; j < points.length - 1; j++) {
      paths.push(
        generateLink(
          LinkWidget.generateLinePath(points[j], points[j + 1]),
          {
            'data-linkid': props.link.getID(),
            'data-point': j,
            onMouseDown: (event: MouseEvent) => {
              addPointToLink(event, j + 1, props)
            }
          },
          j
        )
      )
    }
 
    //render the circles
    for (let i = 1; i < points.length - 1; i++) {
      paths.push(generatePoint(points[i], props))
    }
 
    if (props.link.getTargetPort() == null) {
      paths.push(generatePoint(points[points.length - 1], props))
    }
  }
 
  return (
    <g className={css.link} data-default-link-test={props.link.getOptions().testName}>
      {paths}
    </g>
  )
}