index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { useEffect, useRef } from 'react';
  2. import OuterRect from '../OuterRect';
  3. import { rectCalc, parsePositionForBackend } from '../../helpers/position';
  4. import { AnnotationContainer } from '../../global/otherStyled';
  5. import { TextWrapper, Input } from './styled';
  6. const FreeText: React.FC<AnnotationElementPropsType> = ({
  7. id,
  8. obj_type,
  9. obj_attr: { content, position, fontname, fontsize, textcolor },
  10. scale,
  11. viewport,
  12. isCollapse,
  13. isEdit,
  14. onEdit,
  15. onUpdate,
  16. onBlur,
  17. }: AnnotationElementPropsType) => {
  18. const inputRef = useRef<HTMLInputElement>(null);
  19. const annotRect = rectCalc(position as PositionType, viewport.height, scale);
  20. const handleChange = (event: React.FormEvent<HTMLInputElement>): void => {
  21. const textValue = event.currentTarget.value;
  22. const fontWidth = textValue.length * ((fontsize as number) * 0.9 || 1);
  23. const newPosition = { ...(position as PositionType) };
  24. newPosition.right = newPosition.left + fontWidth;
  25. onUpdate({
  26. content: textValue,
  27. position: newPosition,
  28. });
  29. };
  30. const handleScaleOrMove = ({
  31. top,
  32. left,
  33. width = 0,
  34. height = 0,
  35. }: CoordType): void => {
  36. const newPosition = {
  37. top,
  38. left,
  39. bottom: top + (height || annotRect.height),
  40. right: left + (width || annotRect.width),
  41. };
  42. onUpdate({
  43. fontsize: (height || annotRect.height) / scale,
  44. position: parsePositionForBackend(
  45. obj_type,
  46. newPosition,
  47. viewport.height,
  48. scale,
  49. ),
  50. });
  51. };
  52. const styles = {
  53. fontFamily: fontname,
  54. fontSize: fontsize ? fontsize * scale : 0,
  55. color: textcolor,
  56. };
  57. useEffect(() => {
  58. if (isEdit) {
  59. setTimeout(() => {
  60. if (inputRef.current) inputRef.current.focus();
  61. }, 100);
  62. }
  63. }, [isEdit]);
  64. return (
  65. <>
  66. <AnnotationContainer
  67. id={id}
  68. top={`${annotRect.top}px`}
  69. left={`${annotRect.left}px`}
  70. width={`${annotRect.width + 2}px`}
  71. height={`${annotRect.height + 8}px`}
  72. >
  73. {isEdit ? (
  74. <Input
  75. ref={inputRef}
  76. defaultValue={content}
  77. onChange={handleChange}
  78. onBlur={onBlur}
  79. style={styles}
  80. />
  81. ) : (
  82. <TextWrapper style={styles}>{content}</TextWrapper>
  83. )}
  84. </AnnotationContainer>
  85. {!isCollapse ? (
  86. <OuterRect
  87. top={annotRect.top}
  88. left={annotRect.left}
  89. width={annotRect.width}
  90. height={annotRect.height}
  91. onMove={handleScaleOrMove}
  92. onScale={handleScaleOrMove}
  93. onDoubleClick={onEdit}
  94. />
  95. ) : (
  96. ''
  97. )}
  98. </>
  99. );
  100. };
  101. export default FreeText;