index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React from 'react';
  2. import OuterRect from '../OuterRect';
  3. import {
  4. AnnotationElementPropsType, PositionType, CoordType,
  5. } from '../../constants/type';
  6. import { rectCalc, parsePositionForBackend } from '../../helpers/position';
  7. import { AnnotationContainer } from '../../global/otherStyled';
  8. import { TextWrapper, Input } from './styled';
  9. const FreeText: React.SFC<AnnotationElementPropsType> = ({
  10. obj_type,
  11. obj_attr: {
  12. content,
  13. position,
  14. fontname,
  15. fontsize,
  16. textcolor,
  17. },
  18. scale,
  19. viewport,
  20. isCollapse,
  21. isEdit,
  22. onEdit,
  23. onUpdate,
  24. onBlur,
  25. }: AnnotationElementPropsType) => {
  26. const annotRect = rectCalc(position as PositionType, viewport.height, scale);
  27. const handleChange = (event: React.FormEvent<HTMLInputElement>): void => {
  28. const textValue = event.currentTarget.value;
  29. const fontWidth = textValue.length * (fontsize || 1);
  30. const newPosition = { ...position as PositionType };
  31. newPosition.right = newPosition.left + fontWidth;
  32. onUpdate({
  33. content: textValue,
  34. position: newPosition,
  35. });
  36. };
  37. const handleScaleOrMove = ({
  38. top, left, width = 0, 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(obj_type, newPosition, viewport.height, scale),
  49. });
  50. };
  51. const styles = {
  52. fontFamily: fontname,
  53. fontSize: fontsize ? fontsize * scale : 0,
  54. color: textcolor,
  55. };
  56. return (
  57. <div>
  58. <AnnotationContainer
  59. top={`${annotRect.top}px`}
  60. left={`${annotRect.left}px`}
  61. width={`${annotRect.width + 2}px`}
  62. height={`${annotRect.height + 4}px`}
  63. >
  64. {
  65. isEdit ? (
  66. <Input
  67. defaultValue={content}
  68. onChange={handleChange}
  69. autoFocus
  70. onBlur={onBlur}
  71. style={styles}
  72. />
  73. ) : (
  74. <TextWrapper
  75. style={styles}
  76. >
  77. {content}
  78. </TextWrapper>
  79. )
  80. }
  81. </AnnotationContainer>
  82. {
  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. </div>
  96. );
  97. };
  98. export default FreeText;