index.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react';
  2. import {
  3. AnnotationElementPropsType, PositionType, CoordType,
  4. } from '../../constants/type';
  5. import Icon from '../Icon';
  6. import OuterRect from '../OuterRect';
  7. import { AnnotationContainer } from '../../global/otherStyled';
  8. import { rectCalc } from '../../helpers/position';
  9. import { TextArea, TextAreaContainer, TrashCan } from './styled';
  10. const StickyNote: React.SFC<AnnotationElementPropsType> = ({
  11. viewport,
  12. scale,
  13. obj_attr: {
  14. position,
  15. content,
  16. },
  17. onEdit,
  18. isEdit,
  19. onBlur,
  20. onUpdate,
  21. onDelete,
  22. }: AnnotationElementPropsType) => {
  23. const annotRect = rectCalc(position as PositionType, viewport.height, scale);
  24. const handleClick = (event: React.MouseEvent): void => {
  25. event.preventDefault();
  26. onEdit();
  27. };
  28. const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>): void => {
  29. const textValue = event.currentTarget.value;
  30. onUpdate({
  31. content: textValue,
  32. });
  33. };
  34. const handleMove = (moveCoord: CoordType): void => {
  35. const top = (viewport.height - moveCoord.top) / scale;
  36. const left = moveCoord.left / scale;
  37. onUpdate({
  38. position: {
  39. top,
  40. left,
  41. bottom: top,
  42. right: left,
  43. },
  44. });
  45. };
  46. return (
  47. <>
  48. <AnnotationContainer
  49. top={`${annotRect.top}px`}
  50. left={`${annotRect.left}px`}
  51. width="auto"
  52. height="auto"
  53. >
  54. <Icon
  55. glyph="sticky-note-2"
  56. style={{ width: '30px' }}
  57. onClick={handleClick}
  58. />
  59. </AnnotationContainer>
  60. {
  61. isEdit ? (
  62. <TextAreaContainer
  63. top={`${annotRect.top + 36}px`}
  64. left={`${annotRect.left - 106}px`}
  65. >
  66. <TextArea
  67. autoFocus
  68. defaultValue={content}
  69. onChange={handleChange}
  70. />
  71. <TrashCan>
  72. <Icon
  73. glyph="trash-2"
  74. onClick={onDelete}
  75. />
  76. </TrashCan>
  77. </TextAreaContainer>
  78. ) : null
  79. }
  80. {
  81. isEdit ? (
  82. <OuterRect
  83. top={annotRect.top}
  84. left={annotRect.left}
  85. width={25}
  86. height={25}
  87. onMove={handleMove}
  88. onClick={onBlur}
  89. />
  90. ) : null
  91. }
  92. </>
  93. );
  94. };
  95. export default StickyNote;