All files SvgArrow.js

86.84% Statements 33/38
72% Branches 18/25
100% Functions 6/6
86.84% Lines 33/38

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        5x           4x   1x                         5x   5x 5x   5x       3x 2x         1x 1x                   3x 2x         1x 1x                   3x 3x   3x 3x   3x               2x                   1x   1x 1x   1x               1x             1x                 1x   1x   1x                                               2x         2x   2x                        
import React from 'react';
import PropTypes from 'prop-types';
 
function computeEndingArrowDirectionVector(endingAnchor) {
  switch (endingAnchor) {
    case 'left':
      return { arrowX: -1, arrowY: 0 };
    case 'right':
      return { arrowX: 1, arrowY: 0 };
    case 'top':
      return { arrowX: 0, arrowY: -1 };
    case 'bottom':
      return { arrowX: 0, arrowY: 1 };
    default:
      return { arrowX: 0, arrowY: 0 };
  }
}
 
export function computeEndingPointAccordingToArrow(
  xEnd,
  yEnd,
  arrowLength,
  strokeWidth,
  endingAnchor,
) {
  const { arrowX, arrowY } = computeEndingArrowDirectionVector(endingAnchor);
 
  const xe = xEnd + arrowX * arrowLength * strokeWidth / 2;
  const ye = yEnd + arrowY * arrowLength * strokeWidth / 2;
 
  return { xe, ye };
}
 
export function computeStartingAnchorPosition(xs, ys, xe, ye, startingAnchor) {
  if (startingAnchor === 'top' || startingAnchor === 'bottom') {
    return {
      xa1: xs,
      ya1: ys + (ye - ys) / 2,
    };
  }
  Eif (startingAnchor === 'left' || startingAnchor === 'right') {
    return {
      xa1: xs + (xe - xs) / 2,
      ya1: ys,
    };
  }
 
  return { xa1: xs, ya1: ys };
}
 
export function computeEndingAnchorPosition(xs, ys, xe, ye, endingAnchor) {
  if (endingAnchor === 'top' || endingAnchor === 'bottom') {
    return {
      xa2: xe,
      ya2: ye - (ye - ys) / 2,
    };
  }
  Eif (endingAnchor === 'left' || endingAnchor === 'right') {
    return {
      xa2: xe - (xe - xs) / 2,
      ya2: ye,
    };
  }
 
  return { xa2: xe, ya2: ye };
}
 
export function computeLabelDimensions(xs, ys, xe, ye) {
  const wl = Math.abs(xe - xs);
  const hl = Math.abs(ye - ys);
 
  const xl = xe > xs ? xs : xe;
  const yl = ye > ys ? ys : ye;
 
  return {
    xl,
    yl,
    wl,
    hl,
  };
}
 
const SvgArrow = ({
  startingPoint,
  startingAnchor,
  endingPoint,
  endingAnchor,
  strokeColor,
  arrowLength,
  strokeWidth,
  arrowLabel,
}) => {
  const actualArrowLength = arrowLength * 2;
 
  const xs = startingPoint.x;
  const ys = startingPoint.y;
 
  const { xe, ye } = computeEndingPointAccordingToArrow(
    endingPoint.x,
    endingPoint.y,
    actualArrowLength,
    strokeWidth,
    endingAnchor,
  );
 
  const { xa1, ya1 } = computeStartingAnchorPosition(
    xs,
    ys,
    xe,
    ye,
    startingAnchor,
  );
  const { xa2, ya2 } = computeEndingAnchorPosition(
    xs,
    ys,
    xe,
    ye,
    endingAnchor,
  );
 
  const pathString =
    `M${xs},${ys} ` + `C${xa1},${ya1} ${xa2},${ya2} ` + `${xe},${ye}`;
 
  const { xl, yl, wl, hl } = computeLabelDimensions(xs, ys, xe, ye);
 
  return (
    <g>
      <path
        d={pathString}
        style={{ fill: 'none', stroke: strokeColor, strokeWidth }}
        markerEnd={`url(${location.href}#arrow)`}
      />
      <foreignObject x={xl} y={yl} width={wl} height={hl}>
        <div
          style={{
            width: wl,
            height: hl,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          <div>{arrowLabel}</div>
        </div>
      </foreignObject>
    </g>
  );
};
 
const pointPropType = PropTypes.shape({
  x: PropTypes.number,
  y: PropTypes.number,
});
 
const anchorType = PropTypes.oneOf(['top', 'bottom', 'left', 'right']);
 
SvgArrow.propTypes = {
  startingPoint: pointPropType,
  startingAnchor: anchorType,
  endingPoint: pointPropType,
  endingAnchor: anchorType,
  strokeColor: PropTypes.string,
  arrowLength: PropTypes.number,
  strokeWidth: PropTypes.number,
  arrowLabel: PropTypes.node,
};
 
export default SvgArrow;