index.tsx 2.7 KB

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