ShapeTool.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 (pdfViewer) {
  138. if (isActive) {
  139. subscribeEvent();
  140. } else {
  141. unsubscribeEvent();
  142. }
  143. }
  144. return (): void => {
  145. if (pdfViewer) {
  146. unsubscribeEvent();
  147. }
  148. };
  149. }, [isActive, handleMouseDown, handleMouseUp]);
  150. useEffect(() => {
  151. if (cursorPosition.x && cursorPosition.y) {
  152. setEndPoint({ x: cursorPosition.x, y: cursorPosition.y });
  153. }
  154. }, [cursorPosition]);
  155. const checkSvgChild = (type: string): string => {
  156. switch (type) {
  157. case 'square':
  158. return 'rect';
  159. case 'circle':
  160. return 'ellipse';
  161. default:
  162. return 'line';
  163. }
  164. };
  165. useEffect(() => {
  166. const pageEle = document.getElementById(`page_${data.page}`);
  167. const canvas = pageEle?.getElementsByClassName('canvas')[0] as HTMLElement;
  168. if (endPoint.x && endPoint.y) {
  169. if (shapeEle) {
  170. const { top, left, right, bottom } = convertPosition(
  171. ANNOTATION_TYPE[data.shape],
  172. startPoint.x,
  173. startPoint.y,
  174. endPoint.x,
  175. endPoint.y,
  176. ) as PositionType;
  177. if (data.shape === 'square') {
  178. shapeEle.setAttribute('x', `${left}`);
  179. shapeEle.setAttribute('y', `${top}`);
  180. shapeEle.setAttribute('width', `${right - left}`);
  181. shapeEle.setAttribute('height', `${bottom - top}`);
  182. } else if (data.shape === 'circle') {
  183. const xRadius = (right - left) / 2;
  184. const yRadius = (bottom - top) / 2;
  185. shapeEle.setAttribute('cx', `${left + xRadius}`);
  186. shapeEle.setAttribute('cy', `${top + yRadius}`);
  187. shapeEle.setAttribute('rx', `${xRadius}`);
  188. shapeEle.setAttribute('ry', `${yRadius}`);
  189. } else {
  190. shapeEle.setAttribute('x1', `${startPoint.x}`);
  191. shapeEle.setAttribute('y1', `${startPoint.y}`);
  192. shapeEle.setAttribute('x2', `${endPoint.x}`);
  193. shapeEle.setAttribute('y2', `${endPoint.y}`);
  194. }
  195. } else {
  196. shapeEle = document.createElementNS(
  197. 'http://www.w3.org/2000/svg',
  198. checkSvgChild(data.shape),
  199. );
  200. if (
  201. data.type !== 'fill' ||
  202. data.shape === 'line' ||
  203. data.shape === 'arrow'
  204. ) {
  205. shapeEle.setAttribute('fill', 'none');
  206. shapeEle.setAttribute('stroke', data.color);
  207. shapeEle.setAttribute('stroke-width', `${data.width * scale}`);
  208. shapeEle.setAttribute('stroke-opacity', `${data.opacity * 0.01}`);
  209. } else {
  210. shapeEle.setAttribute('fill', data.color);
  211. shapeEle.setAttribute('fill-opacity', `${data.opacity * 0.01}`);
  212. }
  213. canvas.appendChild(shapeEle);
  214. }
  215. } else if (canvas && shapeEle) {
  216. canvas.removeChild(shapeEle);
  217. shapeEle = null;
  218. }
  219. }, [startPoint, endPoint, data, scale]);
  220. const Label = (
  221. <Button
  222. shouldFitContainer
  223. align="left"
  224. onClick={onClick}
  225. isActive={isActive}
  226. >
  227. <Icon glyph="shape" style={{ marginRight: '10px' }} />
  228. {title}
  229. </Button>
  230. );
  231. return (
  232. <ExpansionPanel isActive={isActive} label={Label}>
  233. <ShapeOption {...data} setDataState={setDataState} />
  234. </ExpansionPanel>
  235. );
  236. };
  237. export default Shape;