ShapeTool.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Shape: 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): 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:
  104. data.shape === 'line' ||
  105. data.shape === 'arrow' ||
  106. data.type === 'border'
  107. ? data.color
  108. : undefined,
  109. fcolor: data.type === 'fill' ? data.color : undefined,
  110. transparency: data.opacity,
  111. ftransparency: data.type === 'fill' ? data.opacity : undefined,
  112. bdwidth: data.width,
  113. is_arrow: data.shape === 'arrow',
  114. },
  115. };
  116. const shapeAnnotation = appendUserIdAndDate(
  117. parseAnnotationObject(annoteData, viewport.height, scale),
  118. );
  119. addAnnots([shapeAnnotation]);
  120. setRef(null);
  121. setStartPoint({ x: 0, y: 0 });
  122. setEndPoint({ x: 0, y: 0 });
  123. }
  124. }, [startPoint, endPoint, viewport, data, scale]);
  125. const subscribeEvent = (): void => {
  126. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  127. pdfViewer.addEventListener('mousedown', handleMouseDown);
  128. pdfViewer.addEventListener('mouseup', handleMouseUp);
  129. };
  130. const unsubscribeEvent = (): void => {
  131. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  132. pdfViewer.removeEventListener('mousedown', handleMouseDown);
  133. pdfViewer.removeEventListener('mouseup', handleMouseUp);
  134. };
  135. useEffect(() => {
  136. const pdfViewer = document.getElementById('pdf_viewer') as HTMLDivElement;
  137. if (isActive && pdfViewer) {
  138. subscribeEvent();
  139. }
  140. return (): void => {
  141. if (pdfViewer) {
  142. unsubscribeEvent();
  143. }
  144. };
  145. }, [isActive, handleMouseDown, handleMouseUp]);
  146. useEffect(() => {
  147. if (cursorPosition.x && cursorPosition.y) {
  148. setEndPoint({ x: cursorPosition.x, y: cursorPosition.y });
  149. }
  150. }, [cursorPosition]);
  151. const checkSvgChild = (type: string): string => {
  152. switch (type) {
  153. case 'square':
  154. return 'rect';
  155. case 'circle':
  156. return 'ellipse';
  157. default:
  158. return 'line';
  159. }
  160. };
  161. useEffect(() => {
  162. const pageEle = document.getElementById(`page_${data.page}`);
  163. const canvas = pageEle?.getElementsByClassName('canvas')[0] as HTMLElement;
  164. if (endPoint.x && endPoint.y) {
  165. canvas.style.display = 'block';
  166. if (shapeEle) {
  167. const { top, left, right, bottom } = convertPosition(
  168. ANNOTATION_TYPE[data.shape],
  169. startPoint.x,
  170. startPoint.y,
  171. endPoint.x,
  172. endPoint.y,
  173. ) as PositionType;
  174. if (data.shape === 'square') {
  175. shapeEle.setAttribute('x', `${left}`);
  176. shapeEle.setAttribute('y', `${top}`);
  177. shapeEle.setAttribute('width', `${right - left}`);
  178. shapeEle.setAttribute('height', `${bottom - top}`);
  179. } else if (data.shape === 'circle') {
  180. const xRadius = (right - left) / 2;
  181. const yRadius = (bottom - top) / 2;
  182. shapeEle.setAttribute('cx', `${left + xRadius}`);
  183. shapeEle.setAttribute('cy', `${top + yRadius}`);
  184. shapeEle.setAttribute('rx', `${xRadius}`);
  185. shapeEle.setAttribute('ry', `${yRadius}`);
  186. } else {
  187. shapeEle.setAttribute('x1', `${startPoint.x}`);
  188. shapeEle.setAttribute('y1', `${startPoint.y}`);
  189. shapeEle.setAttribute('x2', `${endPoint.x}`);
  190. shapeEle.setAttribute('y2', `${endPoint.y}`);
  191. }
  192. } else {
  193. shapeEle = document.createElementNS(
  194. 'http://www.w3.org/2000/svg',
  195. checkSvgChild(data.shape),
  196. );
  197. if (
  198. data.type !== 'fill' ||
  199. data.shape === 'line' ||
  200. data.shape === 'arrow'
  201. ) {
  202. shapeEle.setAttribute('fill', 'none');
  203. shapeEle.setAttribute('stroke', data.color);
  204. shapeEle.setAttribute('stroke-width', `${data.width * scale}`);
  205. shapeEle.setAttribute('stroke-opacity', `${data.opacity * 0.01}`);
  206. } else {
  207. shapeEle.setAttribute('fill', data.color);
  208. shapeEle.setAttribute('fill-opacity', `${data.opacity * 0.01}`);
  209. }
  210. canvas.appendChild(shapeEle);
  211. }
  212. } else if (canvas && shapeEle) {
  213. canvas.style.display = 'none';
  214. canvas.removeChild(shapeEle);
  215. shapeEle = null;
  216. }
  217. }, [startPoint, endPoint, data, scale]);
  218. const Label = (
  219. <Button
  220. shouldFitContainer
  221. align="left"
  222. onClick={onClick}
  223. isActive={isActive}
  224. >
  225. <Icon glyph="shape" style={{ marginRight: '10px' }} />
  226. {title}
  227. </Button>
  228. );
  229. return (
  230. <ExpansionPanel isActive={isActive} label={Label}>
  231. <ShapeOption {...data} setDataState={setDataState} />
  232. </ExpansionPanel>
  233. );
  234. };
  235. export default Shape;