index.tsx 2.1 KB

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