12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import React, { useEffect, useCallback } from 'react';
- import { ANNOTATION_TYPE } from '../constants';
- import Button from '../components/Button';
- import Icon from '../components/Icon';
- import { getAbsoluteCoordinate } from '../helpers/position';
- import {
- parseAnnotationObject,
- appendUserIdAndDate,
- } from '../helpers/annotation';
- import useActions from '../actions';
- import useStore from '../store';
- import { BtnWrapper } from '../global/toolStyled';
- type Props = {
- title: string;
- isActive: boolean;
- onClick: () => void;
- };
- const CheckboxTool: React.FC<Props> = ({ title, isActive, onClick }: Props) => {
- const [{ scale, viewport }, dispatch] = useStore();
- const { addAnnots } = useActions(dispatch);
- const addCheckbox = (
- pageEle: HTMLElement,
- event: MouseEvent | TouchEvent,
- ): void => {
- const pageNum = pageEle.getAttribute('data-page-num') || 0;
- const coordinate = getAbsoluteCoordinate(pageEle, event);
- const annotData = {
- obj_type: ANNOTATION_TYPE.checkbox,
- obj_attr: {
- page: pageNum as number,
- style: '0',
- position: {
- top: coordinate.y,
- left: coordinate.x,
- bottom: coordinate.y + 50,
- right: coordinate.x + 50,
- },
- },
- };
- const newCheckbox = appendUserIdAndDate(
- parseAnnotationObject(annotData, viewport.height, scale),
- );
- addAnnots([newCheckbox]);
- };
- const handleClick = useCallback(
- (event: MouseEvent | TouchEvent): void => {
- const pageEle = (event.target as HTMLElement).parentNode as HTMLElement;
- if (pageEle.hasAttribute('data-page-num')) {
- addCheckbox(pageEle, event);
- }
- },
- [viewport, scale],
- );
- useEffect(() => {
- if (isActive) {
- document.addEventListener('click', handleClick);
- }
- return () => {
- document.removeEventListener('click', handleClick);
- };
- }, [isActive]);
- return (
- <BtnWrapper>
- <Button
- shouldFitContainer
- align="left"
- isActive={isActive}
- onClick={onClick}
- >
- <Icon glyph="checkbox" style={{ marginRight: '10px' }} />
- {title}
- </Button>
- </BtnWrapper>
- );
- };
- export default CheckboxTool;
|