Annotation.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* eslint-disable @typescript-eslint/camelcase */
  2. import React, { useState } from 'react';
  3. import { AnnotationType } from '../constants/type';
  4. import AnnotationComp from '../components/Annotation';
  5. import useCursorPosition from '../hooks/useCursorPosition';
  6. import useStore from '../store';
  7. import useActions from '../actions';
  8. type Props = AnnotationType & {
  9. index: number;
  10. scale: number;
  11. };
  12. const Annotation: React.FunctionComponent<Props> = ({
  13. obj_type,
  14. obj_attr,
  15. index,
  16. scale,
  17. }: Props) => {
  18. const [isCollapse, setCollapse] = useState(true);
  19. const [isCovered, setMouseOver] = useState(false);
  20. const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
  21. const [cursorPosition, ref] = useCursorPosition();
  22. const [{ viewport, annotations }, dispatch] = useStore();
  23. const { updateAnnotation } = useActions(dispatch);
  24. const handleClick = (): void => {
  25. if (isCollapse) {
  26. setCollapse(false);
  27. setMousePosition({
  28. x: cursorPosition.x || 0,
  29. y: cursorPosition.y || 0,
  30. });
  31. }
  32. };
  33. const handleBlur = (): void => {
  34. setCollapse(true);
  35. };
  36. const handleMouseOver = (): void => {
  37. setMouseOver(true);
  38. };
  39. const handleMouseOut = (): void => {
  40. setMouseOver(false);
  41. };
  42. const handleUpdate = (data: any): void => {
  43. if (data.color) {
  44. annotations[index].obj_attr.bdcolor = data.color;
  45. }
  46. if (data.opacity) {
  47. annotations[index].obj_attr.transparency = data.opacity * 0.01;
  48. }
  49. updateAnnotation(annotations);
  50. };
  51. const handleDelete = (): void => {
  52. annotations.splice(index, 1);
  53. updateAnnotation(annotations);
  54. };
  55. return (
  56. <div
  57. ref={ref}
  58. tabIndex={0}
  59. role="button"
  60. onFocus={(): void => {}}
  61. onBlur={handleBlur}
  62. onMouseDown={handleClick}
  63. onMouseOver={handleMouseOver}
  64. onMouseOut={handleMouseOut}
  65. >
  66. <AnnotationComp
  67. obj_type={obj_type}
  68. obj_attr={obj_attr}
  69. scale={scale}
  70. isCollapse={isCollapse}
  71. isCovered={isCovered}
  72. mousePosition={mousePosition}
  73. onUpdate={handleUpdate}
  74. onDelete={handleDelete}
  75. viewport={viewport}
  76. />
  77. </div>
  78. );
  79. };
  80. export default Annotation;