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

10.87% Statements 5/46
0% Branches 0/54
0% Functions 0/10
10.87% Lines 5/46

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170              207x   207x 207x     207x                         207x                                                                                                                                                                                                                                                                                              
/*
 * Copyright 2021 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 type { DiagramEngine } from '@projectstorm/react-diagrams-core'
import { Utils } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { DefaultLinkFactory } from './DefaultLinkFactory'
import type { DefaultLinkModel } from './DefaultLinkModel'
import { Event, DiagramDrag } from '../Constants'
 
export interface DefaultLinkSegmentWidgetProps {
  path: string
  link: DefaultLinkModel
  selected: boolean
  forwardRef: React.RefObject<SVGPathElement>
  factory: DefaultLinkFactory
  diagramEngine: DiagramEngine
  onSelection: (selected: boolean) => void
  extras: Record<string, any>
}
 
export function DefaultLinkSegmentWidget(props: DefaultLinkSegmentWidgetProps): JSX.Element {
  const { onSelection, link } = props
  //NOTE: isDragOver is used to set pointerEvents="none" to plus button on dragOver event
  const [isDragOver, setDragOver] = React.useState(false)
  const allowAdd = link.getOptions().allowAdd ?? true
  const prevColorRef = React.useRef('')
  const color = link.getOptions().color
  React.useEffect(() => {
    prevColorRef.current = color || ''
  })
  const Bottom = React.cloneElement(
    props.factory.generateLinkSegment(
      props.link,
      props.selected || props.link.isSelected(),
      props.path,
      allowAdd,
      prevColorRef.current
    ),
    {
      ref: props.forwardRef
    }
  )
 
  const BeforePath = React.cloneElement(Bottom, {
    stroke: prevColorRef.current,
    className: ''
  })
 
  const onMouseLeave = React.useCallback(() => {
    onSelection(false)
  }, [onSelection])
 
  const onMouseEnter = React.useCallback(() => {
    onSelection(true)
  }, [onSelection])
 
  const onContextMenu = React.useCallback(
    event => {
      if (!link.isLocked()) {
        event?.preventDefault()
        link.remove()
      }
    },
    [link]
  )
 
  const onClick = React.useCallback(() => {
    allowAdd && link.fireEvent({}, Event.AddLinkClicked)
  }, [link, allowAdd])
 
  const pathRef = React.useRef<SVGPathElement>()
 
  const [point, setPoint] = React.useState({ x: 0, y: 0 })
 
  React.useEffect(() => {
    if (pathRef.current) {
      const totalLength = (pathRef.current.getTotalLength?.() || 0) * 0.5
      const position = pathRef.current.getPointAtLength?.(totalLength) || { x: 0, y: 0 }
      setPoint({ x: position.x, y: position.y })
    }
  }, [pathRef])
 
  const Top = React.cloneElement(Bottom, {
    strokeLinecap: 'round',
    onMouseLeave,
    onMouseEnter,
    ...props.extras,
    ref: pathRef,
    'data-linkid': props.link.getID(),
    strokeOpacity: 0, // props.selected ? 0.1 : 0,
    strokeWidth: 20,
    fill: 'none',
    onContextMenu,
    onDragOver: (event: React.DragEvent<HTMLDivElement>) => {
      setDragOver(true)
      if (event.dataTransfer.types.indexOf(DiagramDrag.AllowDropOnLink) !== -1) {
        if (allowAdd) {
          onSelection(true)
          event.preventDefault()
        }
      }
    },
    onDragLeave: (event: React.DragEvent<HTMLDivElement>) => {
      setDragOver(false)
      if (event.dataTransfer.types.indexOf(DiagramDrag.AllowDropOnLink) !== -1) {
        if (allowAdd) {
          onSelection(false)
        }
      }
    },
    onDrop: (event: React.DragEvent<HTMLDivElement>) => {
      if (event.dataTransfer.types.indexOf(DiagramDrag.AllowDropOnLink) !== -1) {
        if (allowAdd) {
          const dropData: { id: string; identifier: string } = JSON.parse(
            event.dataTransfer.getData(DiagramDrag.NodeDrag)
          )
          link.fireEvent({ node: dropData }, Event.DropLinkEvent)
        }
      }
    }
  })
 
  return (
    <g>
      {link.getOptions().color !== prevColorRef.current && BeforePath}
      {Bottom}
      {Top}
      {allowAdd && (
        <>
          <circle
            fill={props.link.getOptions().selectedColor}
            r={10}
            cx={point.x}
            pointerEvents={isDragOver ? 'none' : 'all'}
            onMouseLeave={onMouseLeave}
            onMouseEnter={onMouseEnter}
            cy={point.y}
            opacity={props.selected ? 1 : 0}
            onClick={onClick}
          />
          <line
            stroke={Utils.getRealCSSColor(Color.WHITE)}
            x1={point.x - 5}
            y1={point.y}
            x2={point.x + 5}
            y2={point.y}
            strokeWidth="1"
            opacity={props.selected ? 1 : 0}
          ></line>
          <line
            stroke={Utils.getRealCSSColor(Color.WHITE)}
            x1={point.x}
            y1={point.y - 5}
            x2={point.x}
            y2={point.y + 5}
            strokeWidth="1"
            opacity={props.selected ? 1 : 0}
          ></line>
        </>
      )}
    </g>
  )
}