1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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 RadioButtonTool: React.FC<Props> = ({
- title,
- isActive,
- onClick,
- }: Props) => {
- const [{ scale, viewport }, dispatch] = useStore();
- const { addAnnots } = useActions(dispatch);
- const addRadioButton = (
- 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: '1',
- position: {
- top: coordinate.y,
- left: coordinate.x,
- bottom: coordinate.y + 50,
- right: coordinate.x + 50,
- },
- },
- };
- const newRadioButton = appendUserIdAndDate(
- parseAnnotationObject(annotData, viewport.height, scale),
- );
- addAnnots([newRadioButton]);
- };
- const handleClick = useCallback(
- (event: MouseEvent | TouchEvent): void => {
- const pageEle = (event.target as HTMLElement).parentNode as HTMLElement;
- if (pageEle.hasAttribute('data-page-num')) {
- addRadioButton(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="radio-button" style={{ marginRight: '10px' }} />
- {title}
- </Button>
- </BtnWrapper>
- );
- };
- export default RadioButtonTool;
|