|
| 1 | +/* eslint-disable no-nested-ternary */ |
| 2 | +import React, { useCallback, useState, useEffect, useRef } from 'react'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import generateBase64Url from '../_common/js/watermark/generateBase64Url'; |
| 5 | +import randomMovingStyle from '../_common/js/watermark/randomMovingStyle'; |
| 6 | +import injectStyle from '../_common/js/utils/injectStyle'; |
| 7 | +import { StyledProps } from '../common'; |
| 8 | +import useConfig from '../hooks/useConfig'; |
| 9 | +import useMutationObserver from '../hooks/useMutationObserver'; |
| 10 | +import { TdWatermarkProps } from './type'; |
| 11 | +import { watermarkDefaultProps } from './defaultProps'; |
| 12 | +import { getStyleStr } from './utils'; |
| 13 | +import useDefaultProps from '../hooks/useDefaultProps'; |
| 14 | +import useVariables from '../hooks/useVariables'; |
| 15 | +import parseTNode from '../_util/parseTNode'; |
| 16 | + |
| 17 | +export interface WatermarkProps extends TdWatermarkProps, StyledProps {} |
| 18 | + |
| 19 | +const Watermark: React.FC<WatermarkProps> = (originalProps) => { |
| 20 | + const props = useDefaultProps<WatermarkProps>(originalProps, watermarkDefaultProps); |
| 21 | + const { |
| 22 | + alpha, |
| 23 | + x = 200, |
| 24 | + y = 210, |
| 25 | + width = 120, |
| 26 | + height = 60, |
| 27 | + rotate: tempRotate, |
| 28 | + zIndex = 10, |
| 29 | + lineSpace, |
| 30 | + isRepeat, |
| 31 | + removable, |
| 32 | + movable, |
| 33 | + moveInterval, |
| 34 | + offset = [], |
| 35 | + content, |
| 36 | + children, |
| 37 | + watermarkContent, |
| 38 | + layout, |
| 39 | + className, |
| 40 | + style = {}, |
| 41 | + } = props; |
| 42 | + |
| 43 | + const { classPrefix } = useConfig(); |
| 44 | + |
| 45 | + let gapX = x; |
| 46 | + let gapY = y; |
| 47 | + let rotate = tempRotate; |
| 48 | + if (movable) { |
| 49 | + gapX = 0; |
| 50 | + gapY = 0; |
| 51 | + rotate = 0; |
| 52 | + } |
| 53 | + const clsName = `${classPrefix}-watermark`; |
| 54 | + const [base64Url, setBase64Url] = useState(''); |
| 55 | + const styleStr = useRef(''); |
| 56 | + const watermarkRef = useRef<HTMLDivElement>(null); |
| 57 | + const watermarkImgRef = useRef<HTMLDivElement>(null); |
| 58 | + const stopObservation = useRef(false); |
| 59 | + const offsetLeft = offset[0] || gapX / 2; |
| 60 | + const offsetTop = offset[1] || gapY / 2; |
| 61 | + const backgroundSize = useRef(`${gapX + width}px`); |
| 62 | + |
| 63 | + const { fontColor } = useVariables({ |
| 64 | + fontColor: '--td-text-color-watermark', |
| 65 | + }); |
| 66 | + |
| 67 | + // 水印节点 - 背景base64 |
| 68 | + useEffect(() => { |
| 69 | + generateBase64Url( |
| 70 | + { |
| 71 | + width, |
| 72 | + height, |
| 73 | + rotate, |
| 74 | + lineSpace, |
| 75 | + alpha, |
| 76 | + gapX, |
| 77 | + gapY, |
| 78 | + watermarkContent, |
| 79 | + offsetLeft, |
| 80 | + offsetTop, |
| 81 | + fontColor, |
| 82 | + layout, |
| 83 | + }, |
| 84 | + (url, { width }) => { |
| 85 | + backgroundSize.current = width ? `${width}px` : null; |
| 86 | + setBase64Url(url); |
| 87 | + }, |
| 88 | + ); |
| 89 | + }, [ |
| 90 | + width, |
| 91 | + height, |
| 92 | + rotate, |
| 93 | + zIndex, |
| 94 | + lineSpace, |
| 95 | + alpha, |
| 96 | + offsetLeft, |
| 97 | + offsetTop, |
| 98 | + gapX, |
| 99 | + gapY, |
| 100 | + watermarkContent, |
| 101 | + fontColor, |
| 102 | + layout, |
| 103 | + ]); |
| 104 | + |
| 105 | + // 水印节点 - styleStr |
| 106 | + useEffect(() => { |
| 107 | + styleStr.current = getStyleStr({ |
| 108 | + zIndex, |
| 109 | + position: 'absolute', |
| 110 | + left: 0, |
| 111 | + right: 0, |
| 112 | + top: 0, |
| 113 | + bottom: 0, |
| 114 | + width: movable ? `${width}px` : '100%', |
| 115 | + height: movable ? `${height}px` : '100%', |
| 116 | + backgroundSize: backgroundSize.current || `${gapX + width}px`, |
| 117 | + pointerEvents: 'none', |
| 118 | + backgroundRepeat: movable ? 'no-repeat' : isRepeat ? 'repeat' : 'no-repeat', |
| 119 | + backgroundImage: `url('${base64Url}')`, |
| 120 | + animation: movable ? `watermark infinite ${(moveInterval * 4) / 60}s` : 'none', |
| 121 | + ...style, |
| 122 | + }); |
| 123 | + }, [zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, height, backgroundSize]); |
| 124 | + |
| 125 | + // 水印节点 - 渲染 |
| 126 | + const renderWatermark = useCallback(() => { |
| 127 | + // 停止监听 |
| 128 | + stopObservation.current = true; |
| 129 | + // 删除之前 |
| 130 | + watermarkImgRef.current?.remove?.(); |
| 131 | + watermarkImgRef.current = undefined; |
| 132 | + // 创建新的 |
| 133 | + watermarkImgRef.current = document.createElement('div'); |
| 134 | + watermarkImgRef.current.setAttribute('style', styleStr.current); |
| 135 | + watermarkRef.current?.append(watermarkImgRef.current); |
| 136 | + // 继续监听 |
| 137 | + setTimeout(() => { |
| 138 | + stopObservation.current = false; |
| 139 | + }); |
| 140 | + }, []); |
| 141 | + |
| 142 | + // 水印节点 - 初始化渲染 |
| 143 | + useEffect(() => { |
| 144 | + renderWatermark(); |
| 145 | + }, [renderWatermark, zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, className]); |
| 146 | + |
| 147 | + // 水印节点 - 变化时重新渲染 |
| 148 | + useMutationObserver(watermarkRef.current, (mutations) => { |
| 149 | + if (stopObservation.current) return; |
| 150 | + if (removable) return; |
| 151 | + mutations.forEach((mutation) => { |
| 152 | + // 水印节点被删除 |
| 153 | + if (mutation.type === 'childList') { |
| 154 | + const removeNodes = mutation.removedNodes; |
| 155 | + removeNodes.forEach((node) => { |
| 156 | + const element = node as HTMLElement; |
| 157 | + if (element === watermarkImgRef.current) { |
| 158 | + renderWatermark(); |
| 159 | + } |
| 160 | + }); |
| 161 | + } |
| 162 | + // 水印节点其他变化 |
| 163 | + if (mutation.target === watermarkImgRef.current) { |
| 164 | + renderWatermark(); |
| 165 | + } |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + // 组件父节点 - 增加keyframes |
| 170 | + const parent = useRef<HTMLElement>(null); |
| 171 | + useEffect(() => { |
| 172 | + parent.current = watermarkRef.current.parentElement; |
| 173 | + const keyframesStyle = randomMovingStyle(); |
| 174 | + injectStyle(keyframesStyle); |
| 175 | + }, []); |
| 176 | + |
| 177 | + // 水印节点的父节点 - 防删除 |
| 178 | + useMutationObserver(typeof document !== 'undefined' ? document.body : null, (mutations) => { |
| 179 | + if (removable) return; |
| 180 | + mutations.forEach((mutation) => { |
| 181 | + if (mutation.type === 'childList') { |
| 182 | + const removeNodes = mutation.removedNodes; |
| 183 | + removeNodes.forEach((node) => { |
| 184 | + const element = node as HTMLElement; |
| 185 | + if (element === watermarkRef.current) { |
| 186 | + parent.current.appendChild(element); |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + return ( |
| 194 | + <div className={classNames([clsName, className])} ref={watermarkRef}> |
| 195 | + {parseTNode(children || content)} |
| 196 | + </div> |
| 197 | + ); |
| 198 | +}; |
| 199 | + |
| 200 | +export default Watermark; |
0 commit comments