All files / react-archer/src SvgArrow.js

84.38% Statements 27/32
66.67% Branches 14/21
100% Functions 5/5
84.38% Lines 27/32

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        5x           4x   1x                         5x   5x 5x   5x       3x 2x         1x 1x                   3x 2x         1x 1x                 1x                 1x   1x 1x   1x               1x             1x                 1x   1x                 1x         1x   1x                      
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 };
}
 
const SvgArrow = ({
  startingPoint,
  startingAnchor,
  endingPoint,
  endingAnchor,
  strokeColor,
  arrowLength,
  strokeWidth,
}) => {
  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}`;
 
  return (
    <path
      d={pathString}
      style={{ fill: 'none', stroke: strokeColor, strokeWidth }}
      markerEnd="url(#arrow)"
    />
  );
};
 
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,
};
 
export default SvgArrow;