ShapeTool.tsx 7.2 KB

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