mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-11-30 19:19:26 +08:00
7f134954b9
* fix(watermark): can be hidden through the Hide Element feature in the browser * chore: add emphasized style * chore: update snapshot
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import * as React from 'react';
|
|
|
|
import { getStyleStr } from './utils';
|
|
|
|
/**
|
|
* Base size of the canvas, 1 for parallel layout and 2 for alternate layout
|
|
* Only alternate layout is currently supported
|
|
*/
|
|
export const BaseSize = 2;
|
|
export const FontGap = 3;
|
|
|
|
// Prevent external hidden elements from adding accent styles
|
|
const EmphasizedStyles = {
|
|
visibility: 'visible !important',
|
|
};
|
|
|
|
export type AppendWatermark = (
|
|
base64Url: string,
|
|
markWidth: number,
|
|
container: HTMLElement,
|
|
) => void;
|
|
|
|
export default function useWatermark(
|
|
markStyle: React.CSSProperties,
|
|
): [
|
|
appendWatermark: AppendWatermark,
|
|
removeWatermark: (container: HTMLElement) => void,
|
|
isWatermarkEle: (ele: Node) => boolean,
|
|
] {
|
|
const [watermarkMap] = React.useState(() => new Map<HTMLElement, HTMLDivElement>());
|
|
|
|
const appendWatermark = (base64Url: string, markWidth: number, container: HTMLElement) => {
|
|
if (container) {
|
|
if (!watermarkMap.get(container)) {
|
|
const newWatermarkEle = document.createElement('div');
|
|
watermarkMap.set(container, newWatermarkEle);
|
|
}
|
|
|
|
const watermarkEle = watermarkMap.get(container)!;
|
|
|
|
watermarkEle.setAttribute(
|
|
'style',
|
|
getStyleStr({
|
|
...markStyle,
|
|
backgroundImage: `url('${base64Url}')`,
|
|
backgroundSize: `${Math.floor(markWidth)}px`,
|
|
...(EmphasizedStyles as React.CSSProperties),
|
|
}),
|
|
);
|
|
// Prevents using the browser `Hide Element` to hide watermarks
|
|
watermarkEle.removeAttribute('class');
|
|
|
|
container.append(watermarkEle);
|
|
}
|
|
};
|
|
|
|
const removeWatermark = (container: HTMLElement) => {
|
|
const watermarkEle = watermarkMap.get(container);
|
|
|
|
if (watermarkEle && container) {
|
|
container.removeChild(watermarkEle);
|
|
}
|
|
|
|
watermarkMap.delete(container);
|
|
};
|
|
|
|
const isWatermarkEle = (ele: any) => Array.from(watermarkMap.values()).includes(ele);
|
|
|
|
return [appendWatermark, removeWatermark, isWatermarkEle];
|
|
}
|