index.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react';
  2. import OuterRect from '../OuterRect';
  3. import { rectCalc, parsePositionForBackend } from '../../helpers/position';
  4. import { AnnotationContainer } from '../../global/otherStyled';
  5. import Img from './styled';
  6. const Image: React.FC<AnnotationElementPropsType> = ({
  7. id,
  8. obj_type,
  9. obj_attr: { src = '', position },
  10. scale,
  11. viewport,
  12. isCollapse,
  13. onUpdate,
  14. }: AnnotationElementPropsType) => {
  15. const annotRect = rectCalc(position as PositionType, viewport.height, scale);
  16. const handleScaleOrMove = ({
  17. top,
  18. left,
  19. width = 0,
  20. height = 0,
  21. }: CoordType): void => {
  22. const newPosition = {
  23. top,
  24. left,
  25. bottom: top + (height || annotRect.height),
  26. right: left + (width || annotRect.width),
  27. };
  28. onUpdate({
  29. position: parsePositionForBackend(
  30. obj_type,
  31. newPosition,
  32. viewport.height,
  33. scale,
  34. ),
  35. });
  36. };
  37. return (
  38. <>
  39. <AnnotationContainer
  40. id={id}
  41. top={`${annotRect.top}px`}
  42. left={`${annotRect.left}px`}
  43. width={`${annotRect.width}px`}
  44. height={`${annotRect.height}px`}
  45. >
  46. <Img src={src} alt="" />
  47. </AnnotationContainer>
  48. {!isCollapse ? (
  49. <OuterRect
  50. top={annotRect.top}
  51. left={annotRect.left}
  52. width={annotRect.width}
  53. height={annotRect.height}
  54. onMove={handleScaleOrMove}
  55. onScale={handleScaleOrMove}
  56. />
  57. ) : (
  58. ''
  59. )}
  60. </>
  61. );
  62. };
  63. export default Image;