WatermarkTool.tsx 2.8 KB

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