WatermarkTool.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useState, useEffect } from 'react';
  2. import queryString from 'query-string';
  3. import dayjs from 'dayjs';
  4. import { useTranslation } from 'react-i18next';
  5. import { WatermarkType, AnnotationType } from '../constants/type';
  6. import Icon from '../components/Icon';
  7. import Button from '../components/Button';
  8. import Drawer from '../components/Drawer';
  9. import WatermarkOption from '../components/WatermarkOption';
  10. import useActions from '../actions';
  11. import useStore from '../store';
  12. import { BtnWrapper } from '../global/toolStyled';
  13. const WatermarkTool: React.FC = () => {
  14. const { t } = useTranslation('sidebar');
  15. const [isActive, setActive] = useState(false);
  16. const [{
  17. totalPage,
  18. watermark,
  19. annotations,
  20. }, dispatch] = useStore();
  21. const {
  22. setSidebar,
  23. updateWatermark,
  24. addAnnots,
  25. updateAnnots,
  26. } = useActions(dispatch);
  27. const setDataState = (obj: WatermarkType): void => {
  28. updateWatermark(obj);
  29. };
  30. const removeWatermarkInAnnots = (): void => {
  31. const index = annotations.findIndex((ele: AnnotationType) => ele.obj_type === 'watermark');
  32. if (index >= 0) {
  33. annotations.splice(index, 1);
  34. updateAnnots(annotations);
  35. }
  36. };
  37. const handleClick = (): void => {
  38. setActive(!isActive);
  39. setSidebar(!isActive ? 'watermark' : '');
  40. };
  41. const handleSave = (): void => {
  42. const { type, imagepath = '', ...rest } = watermark;
  43. const imagePathMatch = imagepath.match(/image\/(.*);base64/) || [];
  44. const imageTypeMatch = imagepath.match(/base64,(.*)/) || [];
  45. const watermarkData = {
  46. obj_type: 'watermark',
  47. obj_attr: {
  48. ...rest,
  49. type,
  50. pages: `0-${totalPage}`,
  51. opacity: watermark.opacity,
  52. original_image_name: imagePathMatch && `temp.${imagePathMatch[1]}`,
  53. image_data: imageTypeMatch && imageTypeMatch[1],
  54. },
  55. };
  56. removeWatermarkInAnnots();
  57. addAnnots([watermarkData], true);
  58. };
  59. const handleDelete = (): void => {
  60. removeWatermarkInAnnots();
  61. updateWatermark({
  62. ...watermark,
  63. text: '',
  64. imagepath: '',
  65. });
  66. };
  67. useEffect(() => {
  68. const parsed = queryString.parse(window.location.search);
  69. if (parsed.watermark) {
  70. const datetime = dayjs().format('YYYY-MM-DD HH:mm:ss');
  71. setDataState({
  72. text: `${parsed.watermark} @ ${datetime}`,
  73. });
  74. }
  75. }, []);
  76. return (
  77. <>
  78. <BtnWrapper>
  79. <Button shouldFitContainer align="left" onClick={handleClick}>
  80. <Icon glyph="watermark" style={{ marginRight: '10px' }} />
  81. {t('watermark')}
  82. </Button>
  83. </BtnWrapper>
  84. <Drawer open={isActive} anchor="left" zIndex={4}>
  85. <WatermarkOption
  86. isActive={isActive}
  87. onClick={handleClick}
  88. onSave={handleSave}
  89. onDelete={handleDelete}
  90. setDataState={setDataState}
  91. {...watermark}
  92. />
  93. </Drawer>
  94. </>
  95. );
  96. };
  97. export default WatermarkTool;