index.tsx 2.1 KB

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