HighlightTools.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useEffect, useState, useCallback } from 'react';
  2. import Icon from '../components/Icon';
  3. import Button from '../components/Button';
  4. import ExpansionPanel from '../components/ExpansionPanel';
  5. import HighlightOption from '../components/HighlightOption';
  6. import useActions from '../actions';
  7. import useStore from '../store';
  8. import { getMarkupWithSelection } from '../helpers/markup';
  9. type Props = {
  10. title: string;
  11. isActive: boolean;
  12. onClick: () => void;
  13. };
  14. const HighlightTools: React.FC<Props> = ({
  15. title,
  16. isActive,
  17. onClick,
  18. }: Props) => {
  19. const [data, setData] = useState({
  20. type: 'highlight',
  21. color: '#FCFF36',
  22. opacity: 40,
  23. });
  24. const [{ scale }, dispatch] = useStore();
  25. const { addAnnots } = useActions(dispatch);
  26. const setDataState = (obj: OptionPropsType): void => {
  27. setData(prev => ({
  28. ...prev,
  29. ...obj,
  30. }));
  31. };
  32. const selectRange = useCallback(
  33. (e: MouseEvent | TouchEvent): void => {
  34. if (e.target && isActive) {
  35. const textLayer = (e.target as HTMLElement).parentNode as HTMLElement;
  36. if (textLayer) {
  37. const newMarkup = getMarkupWithSelection({
  38. ...data,
  39. scale,
  40. });
  41. if (newMarkup) {
  42. addAnnots([newMarkup]);
  43. }
  44. }
  45. }
  46. },
  47. [isActive, data, scale]
  48. );
  49. useEffect(() => {
  50. if (isActive) {
  51. document.addEventListener('touchend', selectRange);
  52. document.addEventListener('mouseup', selectRange);
  53. }
  54. return (): void => {
  55. document.removeEventListener('touchend', selectRange);
  56. document.removeEventListener('mouseup', selectRange);
  57. };
  58. }, [isActive, selectRange]);
  59. const Label = (
  60. <Button
  61. shouldFitContainer
  62. align="left"
  63. onClick={onClick}
  64. isActive={isActive}
  65. >
  66. <Icon glyph="highlight" style={{ marginRight: '10px' }} />
  67. {title}
  68. </Button>
  69. );
  70. return (
  71. <ExpansionPanel label={Label} isActive={isActive} showBottomBorder>
  72. <HighlightOption {...data} setDataState={setDataState} />
  73. </ExpansionPanel>
  74. );
  75. };
  76. export default HighlightTools;