StickyNoteTools.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React, { useEffect, useCallback } from 'react';
  2. import { ANNOTATION_TYPE } from '../constants';
  3. import Button from '../components/Button';
  4. import Icon from '../components/Icon';
  5. import { getAbsoluteCoordinate } from '../helpers/position';
  6. import {
  7. parseAnnotationObject,
  8. appendUserIdAndDate,
  9. } from '../helpers/annotation';
  10. import { BtnWrapper } from '../global/toolStyled';
  11. import useActions from '../actions';
  12. import useStore from '../store';
  13. type Props = {
  14. title: string;
  15. isActive: boolean;
  16. onClick: () => void;
  17. };
  18. const StickyNoteTools: React.FC<Props> = ({
  19. title,
  20. isActive,
  21. onClick,
  22. }: Props) => {
  23. const [{ viewport, scale }, dispatch] = useStore();
  24. const { addAnnots } = useActions(dispatch);
  25. const addStickyNote = (
  26. pageEle: HTMLElement,
  27. event: MouseEvent | TouchEvent
  28. ): void => {
  29. const pageNum = pageEle.getAttribute('data-page-num') || 0;
  30. const coordinate = getAbsoluteCoordinate(pageEle, event);
  31. const annotData = {
  32. obj_type: ANNOTATION_TYPE.text,
  33. obj_attr: {
  34. page: pageNum as number,
  35. position: {
  36. top: coordinate.y,
  37. left: coordinate.x,
  38. bottom: coordinate.y + 20,
  39. right: coordinate.x + 20,
  40. },
  41. content: '',
  42. },
  43. };
  44. const stickyNote = appendUserIdAndDate(
  45. parseAnnotationObject(annotData, viewport.height, scale)
  46. );
  47. addAnnots([stickyNote]);
  48. };
  49. const handleMouseDown = useCallback(
  50. (event: MouseEvent | TouchEvent): void => {
  51. // event.preventDefault();
  52. const pageEle = (event.target as HTMLElement).parentNode as HTMLElement;
  53. if (pageEle.hasAttribute('data-page-num')) {
  54. addStickyNote(pageEle, event);
  55. }
  56. },
  57. [viewport, scale]
  58. );
  59. useEffect(() => {
  60. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  61. if (isActive && pdfViewer) {
  62. window.addEventListener('touchstart', handleMouseDown);
  63. window.addEventListener('mousedown', handleMouseDown);
  64. }
  65. return (): void => {
  66. if (pdfViewer) {
  67. window.removeEventListener('touchstart', handleMouseDown);
  68. window.removeEventListener('mousedown', handleMouseDown);
  69. }
  70. };
  71. }, [isActive, handleMouseDown]);
  72. return (
  73. <BtnWrapper>
  74. <Button
  75. shouldFitContainer
  76. align="left"
  77. onClick={onClick}
  78. isActive={isActive}
  79. >
  80. <Icon glyph="sticky-note" style={{ marginRight: '10px' }} />
  81. {title}
  82. </Button>
  83. </BtnWrapper>
  84. );
  85. };
  86. export default StickyNoteTools;