index.tsx 2.7 KB

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