index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React, { useEffect, useState } from 'react';
  2. import OuterRect from '../OuterRect';
  3. import SvgShapeElement from '../SvgShapeElement';
  4. import { rectCalc, parsePositionForBackend } from '../../helpers/position';
  5. import { AnnotationContainer } from '../../global/otherStyled';
  6. const Shape: React.FC<AnnotationElementPropsType> = ({
  7. id,
  8. obj_type,
  9. obj_attr: {
  10. bdcolor = '',
  11. fcolor = '',
  12. bdwidth = 0,
  13. transparency = 0,
  14. ftransparency = 0,
  15. position,
  16. },
  17. scale,
  18. viewport,
  19. isCollapse,
  20. onUpdate,
  21. }: AnnotationElementPropsType) => {
  22. const [moving, setMoving] = useState(false);
  23. const [localePos, setLocalePos] = useState({
  24. top: 0,
  25. left: 0,
  26. width: 0,
  27. height: 0,
  28. });
  29. const actualbdwidth = bdwidth * scale;
  30. const handleScaleOrMove = ({
  31. top,
  32. left,
  33. width = 0,
  34. height = 0,
  35. }: CoordType): void => {
  36. setMoving(true);
  37. const newPosition = {
  38. top,
  39. left,
  40. height,
  41. width,
  42. };
  43. setLocalePos(newPosition);
  44. };
  45. const handleMouseUp = () => {
  46. setMoving(false);
  47. };
  48. useEffect(() => {
  49. const rect = rectCalc(position as PositionType, viewport.height, scale);
  50. setLocalePos({
  51. top: rect.top - actualbdwidth / 2,
  52. left: rect.left - actualbdwidth / 2,
  53. width: rect.width + actualbdwidth,
  54. height: rect.height + actualbdwidth,
  55. });
  56. }, [viewport, scale]);
  57. useEffect(() => {
  58. if (!moving && localePos.width) {
  59. const newPosition = {
  60. top: localePos.top,
  61. left: localePos.left,
  62. right: localePos.left + localePos.width,
  63. bottom: localePos.top + localePos.height,
  64. };
  65. onUpdate({
  66. position: parsePositionForBackend(
  67. obj_type,
  68. newPosition as PositionType,
  69. viewport.height,
  70. scale,
  71. ),
  72. });
  73. }
  74. }, [moving, localePos]);
  75. return (
  76. <>
  77. <AnnotationContainer
  78. id={id}
  79. top={`${localePos.top}px`}
  80. left={`${localePos.left}px`}
  81. width={`${localePos.width}px`}
  82. height={`${localePos.height}px`}
  83. >
  84. <SvgShapeElement
  85. shape={obj_type}
  86. width={localePos.width}
  87. height={localePos.height}
  88. bdcolor={bdcolor}
  89. transparency={transparency}
  90. bdwidth={actualbdwidth}
  91. fcolor={fcolor}
  92. ftransparency={ftransparency}
  93. />
  94. </AnnotationContainer>
  95. {!isCollapse ? (
  96. <OuterRect
  97. top={localePos.top}
  98. left={localePos.left}
  99. width={localePos.width}
  100. height={localePos.height}
  101. onMove={handleScaleOrMove}
  102. onScale={handleScaleOrMove}
  103. onMouseUp={handleMouseUp}
  104. />
  105. ) : (
  106. ''
  107. )}
  108. </>
  109. );
  110. };
  111. export default Shape;