2023-08-21 20:15:49 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
2023-01-10 10:56:54 +08:00
|
|
|
import MutateObserver from '@rc-component/mutate-observer';
|
2023-01-20 11:03:50 +08:00
|
|
|
import classNames from 'classnames';
|
2022-12-08 18:06:36 +08:00
|
|
|
|
2023-08-21 20:15:49 +08:00
|
|
|
import theme from '../theme';
|
|
|
|
import useClips, { FontGap } from './useClips';
|
|
|
|
import { getPixelRatio, getStyleStr, reRendering } from './utils';
|
2022-12-08 18:06:36 +08:00
|
|
|
|
|
|
|
export interface WatermarkProps {
|
|
|
|
zIndex?: number;
|
|
|
|
rotate?: number;
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
image?: string;
|
|
|
|
content?: string | string[];
|
|
|
|
font?: {
|
|
|
|
color?: string;
|
|
|
|
fontSize?: number | string;
|
|
|
|
fontWeight?: 'normal' | 'light' | 'weight' | number;
|
|
|
|
fontStyle?: 'none' | 'normal' | 'italic' | 'oblique';
|
|
|
|
fontFamily?: string;
|
|
|
|
};
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-12-08 18:06:36 +08:00
|
|
|
gap?: [number, number];
|
|
|
|
offset?: [number, number];
|
|
|
|
children?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Watermark: React.FC<WatermarkProps> = (props) => {
|
|
|
|
const {
|
|
|
|
/**
|
|
|
|
* The antd content layer zIndex is basically below 10
|
|
|
|
* https://github.com/ant-design/ant-design/blob/6192403b2ce517c017f9e58a32d58774921c10cd/components/style/themes/default.less#L335
|
|
|
|
*/
|
|
|
|
zIndex = 9,
|
|
|
|
rotate = -22,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
image,
|
|
|
|
content,
|
|
|
|
font = {},
|
|
|
|
style,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-12-13 15:47:57 +08:00
|
|
|
gap = [100, 100],
|
2022-12-08 18:06:36 +08:00
|
|
|
offset,
|
|
|
|
children,
|
|
|
|
} = props;
|
2023-07-24 16:05:40 +08:00
|
|
|
const { token } = theme.useToken();
|
2022-12-08 18:06:36 +08:00
|
|
|
const {
|
2023-07-24 16:05:40 +08:00
|
|
|
color = token.colorFill,
|
|
|
|
fontSize = token.fontSizeLG,
|
2022-12-08 18:06:36 +08:00
|
|
|
fontWeight = 'normal',
|
|
|
|
fontStyle = 'normal',
|
|
|
|
fontFamily = 'sans-serif',
|
|
|
|
} = font;
|
|
|
|
|
|
|
|
const [gapX, gapY] = gap;
|
|
|
|
const gapXCenter = gapX / 2;
|
|
|
|
const gapYCenter = gapY / 2;
|
|
|
|
const offsetLeft = offset?.[0] ?? gapXCenter;
|
|
|
|
const offsetTop = offset?.[1] ?? gapYCenter;
|
|
|
|
|
2022-12-13 15:47:57 +08:00
|
|
|
const getMarkStyle = () => {
|
2022-12-08 18:06:36 +08:00
|
|
|
const markStyle: React.CSSProperties = {
|
|
|
|
zIndex,
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
pointerEvents: 'none',
|
|
|
|
backgroundRepeat: 'repeat',
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Calculate the style of the offset */
|
|
|
|
let positionLeft = offsetLeft - gapXCenter;
|
|
|
|
let positionTop = offsetTop - gapYCenter;
|
|
|
|
if (positionLeft > 0) {
|
|
|
|
markStyle.left = `${positionLeft}px`;
|
|
|
|
markStyle.width = `calc(100% - ${positionLeft}px)`;
|
|
|
|
positionLeft = 0;
|
|
|
|
}
|
|
|
|
if (positionTop > 0) {
|
|
|
|
markStyle.top = `${positionTop}px`;
|
|
|
|
markStyle.height = `calc(100% - ${positionTop}px)`;
|
|
|
|
positionTop = 0;
|
|
|
|
}
|
|
|
|
markStyle.backgroundPosition = `${positionLeft}px ${positionTop}px`;
|
|
|
|
|
|
|
|
return markStyle;
|
|
|
|
};
|
|
|
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const watermarkRef = useRef<HTMLDivElement>();
|
2023-01-10 10:56:54 +08:00
|
|
|
const stopObservation = useRef(false);
|
2022-12-08 18:06:36 +08:00
|
|
|
|
|
|
|
const destroyWatermark = () => {
|
|
|
|
if (watermarkRef.current) {
|
|
|
|
watermarkRef.current.remove();
|
|
|
|
watermarkRef.current = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const appendWatermark = (base64Url: string, markWidth: number) => {
|
|
|
|
if (containerRef.current && watermarkRef.current) {
|
2023-01-10 10:56:54 +08:00
|
|
|
stopObservation.current = true;
|
2022-12-08 18:06:36 +08:00
|
|
|
watermarkRef.current.setAttribute(
|
|
|
|
'style',
|
|
|
|
getStyleStr({
|
2022-12-13 15:47:57 +08:00
|
|
|
...getMarkStyle(),
|
2022-12-08 18:06:36 +08:00
|
|
|
backgroundImage: `url('${base64Url}')`,
|
2023-08-21 20:15:49 +08:00
|
|
|
backgroundSize: `${Math.floor(markWidth)}px`,
|
2022-12-08 18:06:36 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
containerRef.current?.append(watermarkRef.current);
|
2023-01-10 10:56:54 +08:00
|
|
|
// Delayed execution
|
|
|
|
setTimeout(() => {
|
|
|
|
stopObservation.current = false;
|
2022-12-08 18:06:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width and height of the watermark. The default values are as follows
|
|
|
|
* Image: [120, 64]; Content: It's calculated by content;
|
|
|
|
*/
|
2022-12-13 15:47:57 +08:00
|
|
|
const getMarkSize = (ctx: CanvasRenderingContext2D) => {
|
2022-12-08 18:06:36 +08:00
|
|
|
let defaultWidth = 120;
|
|
|
|
let defaultHeight = 64;
|
|
|
|
if (!image && ctx.measureText) {
|
|
|
|
ctx.font = `${Number(fontSize)}px ${fontFamily}`;
|
|
|
|
const contents = Array.isArray(content) ? content : [content];
|
2023-08-21 20:15:49 +08:00
|
|
|
const sizes = contents.map((item) => {
|
|
|
|
const metrics = ctx.measureText(item!);
|
|
|
|
|
|
|
|
return [metrics.width, metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent];
|
|
|
|
});
|
|
|
|
defaultWidth = Math.ceil(Math.max(...sizes.map((size) => size[0])));
|
|
|
|
defaultHeight =
|
|
|
|
Math.ceil(Math.max(...sizes.map((size) => size[1]))) * contents.length +
|
|
|
|
(contents.length - 1) * FontGap;
|
2022-12-08 18:06:36 +08:00
|
|
|
}
|
2022-12-09 18:23:59 +08:00
|
|
|
return [width ?? defaultWidth, height ?? defaultHeight] as const;
|
2022-12-08 18:06:36 +08:00
|
|
|
};
|
|
|
|
|
2023-08-21 20:15:49 +08:00
|
|
|
const getClips = useClips();
|
2023-02-28 11:20:15 +08:00
|
|
|
|
2022-12-08 18:06:36 +08:00
|
|
|
const renderWatermark = () => {
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
if (!watermarkRef.current) {
|
|
|
|
watermarkRef.current = document.createElement('div');
|
|
|
|
}
|
|
|
|
|
2022-12-13 15:47:57 +08:00
|
|
|
const ratio = getPixelRatio();
|
2022-12-08 18:06:36 +08:00
|
|
|
const [markWidth, markHeight] = getMarkSize(ctx);
|
2022-12-13 15:47:57 +08:00
|
|
|
|
2023-08-21 20:15:49 +08:00
|
|
|
const drawCanvas = (
|
|
|
|
drawContent?: NonNullable<WatermarkProps['content']> | HTMLImageElement,
|
|
|
|
) => {
|
|
|
|
const [textClips, clipWidth] = getClips(
|
|
|
|
drawContent || '',
|
|
|
|
rotate,
|
|
|
|
ratio,
|
|
|
|
markWidth,
|
|
|
|
markHeight,
|
|
|
|
{
|
|
|
|
color,
|
|
|
|
fontSize,
|
|
|
|
fontStyle,
|
|
|
|
fontWeight,
|
|
|
|
fontFamily,
|
|
|
|
},
|
|
|
|
gapX,
|
|
|
|
gapY,
|
|
|
|
);
|
2022-12-13 15:47:57 +08:00
|
|
|
|
2023-08-21 20:15:49 +08:00
|
|
|
appendWatermark(textClips, clipWidth);
|
|
|
|
};
|
2022-12-08 18:06:36 +08:00
|
|
|
|
|
|
|
if (image) {
|
|
|
|
const img = new Image();
|
|
|
|
img.onload = () => {
|
2023-08-21 20:15:49 +08:00
|
|
|
drawCanvas(img);
|
|
|
|
};
|
|
|
|
img.onerror = () => {
|
|
|
|
drawCanvas(content);
|
2022-12-08 18:06:36 +08:00
|
|
|
};
|
|
|
|
img.crossOrigin = 'anonymous';
|
|
|
|
img.referrerPolicy = 'no-referrer';
|
|
|
|
img.src = image;
|
|
|
|
} else {
|
2023-08-21 20:15:49 +08:00
|
|
|
drawCanvas(content);
|
2022-12-08 18:06:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-10 10:56:54 +08:00
|
|
|
const onMutate = (mutations: MutationRecord[]) => {
|
|
|
|
if (stopObservation.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mutations.forEach((mutation) => {
|
|
|
|
if (reRendering(mutation, watermarkRef.current)) {
|
|
|
|
destroyWatermark();
|
|
|
|
renderWatermark();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-08 22:32:37 +08:00
|
|
|
useEffect(renderWatermark, [
|
2022-12-08 18:06:36 +08:00
|
|
|
rotate,
|
|
|
|
zIndex,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
image,
|
|
|
|
content,
|
|
|
|
color,
|
|
|
|
fontSize,
|
|
|
|
fontWeight,
|
|
|
|
fontStyle,
|
|
|
|
fontFamily,
|
|
|
|
gapX,
|
|
|
|
gapY,
|
|
|
|
offsetLeft,
|
|
|
|
offsetTop,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return (
|
2023-01-10 10:56:54 +08:00
|
|
|
<MutateObserver onMutate={onMutate}>
|
2023-01-20 11:03:50 +08:00
|
|
|
<div
|
|
|
|
ref={containerRef}
|
|
|
|
className={classNames(className, rootClassName)}
|
|
|
|
style={{ position: 'relative', ...style }}
|
|
|
|
>
|
2023-01-10 10:56:54 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</MutateObserver>
|
2022-12-08 18:06:36 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Watermark.displayName = 'Watermark';
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:06:36 +08:00
|
|
|
export default Watermark;
|