Annotation.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 [{ 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. />
  76. </div>
  77. );
  78. };
  79. export default Annotation;