useWatermark.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.FontGap = exports.BaseSize = void 0;
  7. exports.default = useWatermark;
  8. var React = _interopRequireWildcard(require("react"));
  9. var _utils = require("./utils");
  10. /**
  11. * Base size of the canvas, 1 for parallel layout and 2 for alternate layout
  12. * Only alternate layout is currently supported
  13. */
  14. const BaseSize = exports.BaseSize = 2;
  15. const FontGap = exports.FontGap = 3;
  16. // Prevent external hidden elements from adding accent styles
  17. const emphasizedStyle = {
  18. visibility: 'visible !important'
  19. };
  20. function useWatermark(markStyle) {
  21. const watermarkMap = React.useRef(new Map());
  22. const appendWatermark = (base64Url, markWidth, container) => {
  23. if (container) {
  24. if (!watermarkMap.current.get(container)) {
  25. const newWatermarkEle = document.createElement('div');
  26. watermarkMap.current.set(container, newWatermarkEle);
  27. }
  28. const watermarkEle = watermarkMap.current.get(container);
  29. watermarkEle.setAttribute('style', (0, _utils.getStyleStr)(Object.assign(Object.assign(Object.assign({}, markStyle), {
  30. backgroundImage: `url('${base64Url}')`,
  31. backgroundSize: `${Math.floor(markWidth)}px`
  32. }), emphasizedStyle)));
  33. // Prevents using the browser `Hide Element` to hide watermarks
  34. watermarkEle.removeAttribute('class');
  35. watermarkEle.removeAttribute('hidden');
  36. if (watermarkEle.parentElement !== container) {
  37. container.append(watermarkEle);
  38. }
  39. }
  40. return watermarkMap.current.get(container);
  41. };
  42. const removeWatermark = container => {
  43. const watermarkEle = watermarkMap.current.get(container);
  44. if (watermarkEle && container) {
  45. container.removeChild(watermarkEle);
  46. }
  47. watermarkMap.current.delete(container);
  48. };
  49. const isWatermarkEle = ele => Array.from(watermarkMap.current.values()).includes(ele);
  50. return [appendWatermark, removeWatermark, isWatermarkEle];
  51. }