ShapeTool.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import React, { useEffect, useState, useCallback } from 'react';
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { ANNOTATION_TYPE } from '../constants';
  4. import Button from '../components/Button';
  5. import ExpansionPanel from '../components/ExpansionPanel';
  6. import Icon from '../components/Icon';
  7. import ShapeOption from '../components/ShapeOption';
  8. import { getAbsoluteCoordinate } from '../helpers/position';
  9. import {
  10. parseAnnotationObject,
  11. appendUserIdAndDate,
  12. } from '../helpers/annotation';
  13. import { switchPdfViewerScrollState } from '../helpers/pdf';
  14. import useCursorPosition from '../hooks/useCursorPosition';
  15. import useActions from '../actions';
  16. import useStore from '../store';
  17. type Props = {
  18. title: string;
  19. isActive: boolean;
  20. onClick: () => void;
  21. };
  22. let shapeEle: SVGElement | null = null;
  23. const ShapeTool: React.FC<Props> = ({ title, isActive, onClick }: Props) => {
  24. const [startPoint, setStartPoint] = useState({ x: 0, y: 0 });
  25. const [endPoint, setEndPoint] = useState({ x: 0, y: 0 });
  26. const [data, setData] = useState({
  27. page: 0,
  28. shape: 'square',
  29. type: 'fill',
  30. color: '#FBB705',
  31. opacity: 35,
  32. width: 0,
  33. });
  34. const [cursorPosition, setRef] = useCursorPosition(30);
  35. const [{ viewport, scale }, dispatch] = useStore();
  36. const { addAnnots } = useActions(dispatch);
  37. const setDataState = (obj: OptionPropsType): void => {
  38. setData((prev) => ({
  39. ...prev,
  40. ...obj,
  41. }));
  42. };
  43. const convertPosition = (
  44. type: string,
  45. x1: number,
  46. y1: number,
  47. x2: number,
  48. y2: number,
  49. ): PositionType | LinePositionType => {
  50. switch (type) {
  51. case 'Line':
  52. return {
  53. start: {
  54. x: x1,
  55. y: y1,
  56. },
  57. end: {
  58. x: x2,
  59. y: y2,
  60. },
  61. };
  62. default:
  63. return {
  64. top: y2 > y1 ? y1 : y2,
  65. left: x2 > x1 ? x1 : x2,
  66. right: x2 > x1 ? x2 : x1,
  67. bottom: y2 > y1 ? y2 : y1,
  68. };
  69. }
  70. };
  71. const handleMouseDown = (event: MouseEvent | TouchEvent): void => {
  72. switchPdfViewerScrollState('hidden');
  73. const pageEle = (event.target as HTMLElement).parentNode as HTMLElement;
  74. if (pageEle.hasAttribute('data-page-num')) {
  75. setRef(pageEle);
  76. const pageNum = pageEle.getAttribute('data-page-num') || '0';
  77. const coordinate = getAbsoluteCoordinate(pageEle, event);
  78. setData((current) => ({
  79. ...current,
  80. page: parseInt(pageNum, 10),
  81. }));
  82. setStartPoint(coordinate);
  83. }
  84. };
  85. const handleMouseUp = useCallback((): void => {
  86. switchPdfViewerScrollState('scroll');
  87. const shapeType = ANNOTATION_TYPE[data.shape];
  88. const id = uuidv4();
  89. if (startPoint.x !== endPoint.x && endPoint.y) {
  90. const position = convertPosition(
  91. shapeType,
  92. startPoint.x,
  93. startPoint.y,
  94. endPoint.x,
  95. endPoint.y,
  96. );
  97. const annoteData = {
  98. id,
  99. obj_type: shapeType,
  100. obj_attr: {
  101. page: data.page as number,
  102. position,
  103. bdcolor: data.color,
  104. fcolor: data.type === 'fill' ? data.color : undefined,
  105. transparency: data.opacity,
  106. ftransparency: data.type === 'fill' ? data.opacity : undefined,
  107. bdwidth: data.width,
  108. is_arrow: data.shape === 'arrow',
  109. },
  110. };
  111. const shapeAnnotation = appendUserIdAndDate(
  112. parseAnnotationObject(annoteData, viewport.height, scale),
  113. );
  114. addAnnots([shapeAnnotation]);
  115. setRef(null);
  116. setStartPoint({ x: 0, y: 0 });
  117. setEndPoint({ x: 0, y: 0 });
  118. }
  119. }, [startPoint, endPoint, viewport, data, scale]);
  120. const subscribeEvent = (): void => {
  121. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  122. pdfViewer.addEventListener('mousedown', handleMouseDown);
  123. pdfViewer.addEventListener('mouseup', handleMouseUp);
  124. pdfViewer.addEventListener('touchstart', handleMouseDown);
  125. pdfViewer.addEventListener('touchend', handleMouseUp);
  126. };
  127. const unsubscribeEvent = (): void => {
  128. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  129. pdfViewer.removeEventListener('mousedown', handleMouseDown);
  130. pdfViewer.removeEventListener('mouseup', handleMouseUp);
  131. pdfViewer.removeEventListener('touchstart', handleMouseDown);
  132. pdfViewer.removeEventListener('touchend', handleMouseUp);
  133. };
  134. useEffect(() => {
  135. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  136. if (isActive && pdfViewer) {
  137. subscribeEvent();
  138. }
  139. return (): void => {
  140. if (pdfViewer) {
  141. unsubscribeEvent();
  142. }
  143. };
  144. }, [isActive, handleMouseUp]);
  145. useEffect(() => {
  146. if (cursorPosition.x && cursorPosition.y) {
  147. setEndPoint({ x: cursorPosition.x, y: cursorPosition.y });
  148. }
  149. }, [cursorPosition]);
  150. const checkSvgChild = (type: string) => {
  151. switch (type) {
  152. case 'square':
  153. return 'rect';
  154. case 'circle':
  155. return 'ellipse';
  156. default:
  157. return 'line';
  158. }
  159. };
  160. useEffect(() => {
  161. const pageEle = document.getElementById(`page_${data.page}`);
  162. const canvas = pageEle?.getElementsByClassName('canvas')[0] as HTMLElement;
  163. if (endPoint.x && endPoint.y) {
  164. canvas.style.display = 'block';
  165. if (shapeEle) {
  166. const { top, left, right, bottom } = convertPosition(
  167. ANNOTATION_TYPE[data.shape],
  168. startPoint.x,
  169. startPoint.y,
  170. endPoint.x,
  171. endPoint.y,
  172. ) as PositionType;
  173. if (data.shape === 'square') {
  174. shapeEle.setAttribute('x', `${left}`);
  175. shapeEle.setAttribute('y', `${top}`);
  176. shapeEle.setAttribute('width', `${right - left}`);
  177. shapeEle.setAttribute('height', `${bottom - top}`);
  178. } else if (data.shape === 'circle') {
  179. const xRadius = (right - left) / 2;
  180. const yRadius = (bottom - top) / 2;
  181. shapeEle.setAttribute('cx', `${left + xRadius}`);
  182. shapeEle.setAttribute('cy', `${top + yRadius}`);
  183. shapeEle.setAttribute('rx', `${xRadius}`);
  184. shapeEle.setAttribute('ry', `${yRadius}`);
  185. } else {
  186. const actualWidth = data.width * scale;
  187. shapeEle.setAttribute('x1', `${startPoint.x + actualWidth}`);
  188. shapeEle.setAttribute('y1', `${startPoint.y + actualWidth}`);
  189. shapeEle.setAttribute('x2', `${endPoint.x + actualWidth}`);
  190. shapeEle.setAttribute('y2', `${endPoint.y + actualWidth}`);
  191. }
  192. } else {
  193. shapeEle = document.createElementNS(
  194. 'http://www.w3.org/2000/svg',
  195. checkSvgChild(data.shape),
  196. );
  197. shapeEle.setAttribute('fill', data.color);
  198. shapeEle.setAttribute('fill-opacity', `${data.opacity * 0.01}`);
  199. shapeEle.setAttribute('stroke', data.color);
  200. shapeEle.setAttribute('stroke-width', `${data.width * scale}`);
  201. shapeEle.setAttribute('stroke-opacity', `${data.opacity * 0.01}`);
  202. canvas.appendChild(shapeEle);
  203. }
  204. } else if (canvas && shapeEle) {
  205. canvas.style.display = 'none';
  206. canvas.removeChild(shapeEle);
  207. shapeEle = null;
  208. }
  209. }, [startPoint, endPoint, data, scale]);
  210. const Label = (
  211. <Button
  212. shouldFitContainer
  213. align="left"
  214. onClick={onClick}
  215. isActive={isActive}
  216. >
  217. <Icon glyph="shape" style={{ marginRight: '10px' }} />
  218. {title}
  219. </Button>
  220. );
  221. return (
  222. <ExpansionPanel isActive={isActive} label={Label}>
  223. <ShapeOption {...data} setDataState={setDataState} />
  224. </ExpansionPanel>
  225. );
  226. };
  227. export default ShapeTool;