All files index.tsx

92.3% Statements 12/13
100% Branches 3/3
75% Functions 3/4
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                          2x 2x       2x 2x 2x   2x 1x                               1x                                       2x                       2x 2x               1x      
import React, { CSSProperties, ForwardedRef, forwardRef, useEffect, useState } from 'react';
import WatermarkJS, { WatermarkOptions } from '@uiw/watermark.js';
 
export interface WatermarkProps
  extends Omit<React.AllHTMLAttributes<HTMLDivElement>, 'width' | 'height' | 'content'>,
    WatermarkOptions {
  prefixCls?: string;
  /** watermark style */
  markStyle?: React.CSSProperties;
  /** watermark class name */
  markClassName?: string;
  /** watermark text content */
  text?: string;
}
 
function FancyWatermark(props: WatermarkProps, ref: ForwardedRef<HTMLDivElement>) {
  const {
    prefixCls = 'w-watermark',
    text,
    className,
    markClassName,
    markStyle,
    content,
    rotate,
    image,
    gapX = 212,
    gapY,
    width = 120,
    height,
    offsetLeft,
    offsetTop,
    fontSize,
    fontFamily,
    fontWeight,
    fontColor,
    fontStyle,
    ...other
  } = props;
  const style: CSSProperties = {
    ...props.style,
    position: 'relative',
  };
  const wrapperCls = [`${prefixCls}-wrapper`, className].filter(Boolean).join(' ');
  const waterMakrCls = [prefixCls, markClassName].filter(Boolean).join(' ');
  const [base64Url, setBase64Url] = useState('');
 
  useEffect(() => {
    const water = new WatermarkJS({
      content,
      rotate,
      image,
      gapX,
      gapY,
      width,
      height,
      offsetLeft,
      offsetTop,
      fontSize,
      fontFamily,
      fontWeight,
      fontColor,
      fontStyle,
    });
    water
      .create()
      .then((base64String) => setBase64Url(base64String))
      .catch(() => {});
  }, [
    content,
    rotate,
    image,
    gapX,
    gapY,
    width,
    height,
    offsetLeft,
    offsetTop,
    fontSize,
    fontFamily,
    fontWeight,
    fontColor,
    fontStyle,
  ]);
  const watermarkStyle: CSSProperties = {
    position: 'absolute',
    top: 0,
    left: 0,
    zIndex: 9,
    width: '100%',
    height: '100%',
    backgroundSize: `${gapX + width}px`,
    backgroundRepeat: 'repeat',
    ...markStyle,
    pointerEvents: 'none',
  };
  watermarkStyle.backgroundImage = `url(${base64Url})`;
  return (
    <div ref={ref} {...other} className={wrapperCls} style={style}>
      {props.children}
      <div style={watermarkStyle} className={waterMakrCls}></div>
    </div>
  );
}
 
const Watermark = forwardRef<HTMLDivElement, WatermarkProps>(FancyWatermark);
 
export default Watermark;