index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { useEffect, useState, useCallback } from 'react';
  2. import Icon from '../../components/Icon';
  3. import Button from '../../components/Button';
  4. import Typography from '../../components/Typography';
  5. import ExpansionPanel from '../../components/ExpansionPanel';
  6. import Sliders from '../../components/Sliders';
  7. import ColorSelector from '../../components/ColorSelector';
  8. import useActions from '../../actions';
  9. import useStore from '../../store';
  10. import { getAnnotationWithSelection } from '../../helpers/annotation';
  11. import { Group, Item, SliderWrapper } from '../../global/toolStyled';
  12. import data from './data';
  13. type Props = {
  14. isActive: boolean;
  15. onClick: () => void;
  16. };
  17. const Highlight: React.FunctionComponent<Props> = ({
  18. isActive,
  19. onClick,
  20. }: Props) => {
  21. const [color, setColor] = useState('#FCFF36');
  22. const [type, setType] = useState('highlight');
  23. const [opacity, setOpacity] = useState(40);
  24. const [{ scale }, dispatch] = useStore();
  25. const { addAnnotation } = useActions(dispatch);
  26. const handleSlide = (value: number): void => {
  27. setOpacity(value);
  28. };
  29. const selectRange = useCallback((e: MouseEvent): void => {
  30. if (e.target && isActive) {
  31. const textLayer = (e.target as HTMLElement).parentNode as HTMLElement;
  32. if (textLayer && textLayer.getAttribute('data-id') === 'text-layer') {
  33. const newAnnotations = getAnnotationWithSelection({
  34. color, type, opacity, scale,
  35. });
  36. if (newAnnotations && newAnnotations.length) {
  37. addAnnotation(newAnnotations, true);
  38. }
  39. }
  40. }
  41. }, [isActive, color, type, opacity, scale]);
  42. useEffect(() => {
  43. document.addEventListener('mouseup', selectRange);
  44. return (): void => {
  45. document.removeEventListener('mouseup', selectRange);
  46. };
  47. }, [selectRange]);
  48. return (
  49. <ExpansionPanel
  50. label={(
  51. <Button
  52. shouldFitContainer
  53. align="left"
  54. onClick={onClick}
  55. isActive={isActive}
  56. >
  57. <Icon glyph="highlight" style={{ marginRight: '10px' }} />
  58. Highlight Tools
  59. </Button>
  60. )}
  61. isActive={isActive}
  62. >
  63. <Typography variant="subtitle" style={{ marginTop: '8px' }}>Style</Typography>
  64. <Group>
  65. {data.lineType.map(ele => (
  66. <Item
  67. key={ele.key}
  68. selected={type === ele.key}
  69. onClick={(): void => { setType(ele.key); }}
  70. >
  71. <Icon glyph={ele.icon} />
  72. </Item>
  73. ))}
  74. </Group>
  75. <ColorSelector showTitle color={color} onClick={setColor} />
  76. <Typography variant="subtitle" style={{ marginTop: '8px' }}>Opacity</Typography>
  77. <Group>
  78. <SliderWrapper>
  79. <Sliders defaultValue={opacity} onChange={handleSlide} />
  80. </SliderWrapper>
  81. {`${opacity}%`}
  82. </Group>
  83. </ExpansionPanel>
  84. );
  85. };
  86. export default Highlight;