tdesign-react
Version:
TDesign Component for React
282 lines (275 loc) • 12.7 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js';
import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import classNames from 'classnames';
import useConfig from '../hooks/useConfig.js';
import useMutationObservable from '../hooks/useMutationObserver.js';
import { watermarkDefaultProps } from './defaultProps.js';
import { getStyleStr } from './utils.js';
import useDefaultProps from '../hooks/useDefaultProps.js';
import useVariables from '../hooks/useVariables.js';
import '../_chunks/dep-eca3a3de.js';
import '../_chunks/dep-026a4c6b.js';
import '../config-provider/ConfigContext.js';
import 'lodash-es';
import '../locale/zh_CN.js';
import '../_chunks/dep-e29214cb.js';
import 'dayjs';
import '../_chunks/dep-3c9ab31a.js';
import '../hooks/useLatest.js';
import '../_chunks/dep-55732ade.js';
import '../_chunks/dep-3a09424a.js';
function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function generateBase64Url(_ref, onFinish) {
var width = _ref.width,
height = _ref.height,
gapX = _ref.gapX,
gapY = _ref.gapY,
offsetLeft = _ref.offsetLeft,
offsetTop = _ref.offsetTop,
rotate = _ref.rotate,
alpha = _ref.alpha,
watermarkContent = _ref.watermarkContent,
lineSpace = _ref.lineSpace,
_ref$fontColor = _ref.fontColor,
fontColor = _ref$fontColor === void 0 ? "rgba(0,0,0,0.1)" : _ref$fontColor;
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
if (!ctx) {
console.warn("\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301Canvas, \u65E0\u6CD5\u7ED8\u5236\u6C34\u5370");
onFinish("");
return;
}
var ratio = window.devicePixelRatio || 1;
var canvasWidth = (gapX + width) * ratio;
var canvasHeight = (gapY + height) * ratio;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.width = "".concat(gapX + width, "px");
canvas.style.height = "".concat(gapY + height, "px");
ctx.translate(offsetLeft * ratio, offsetTop * ratio);
ctx.rotate(Math.PI / 180 * Number(rotate));
ctx.globalAlpha = alpha;
var markWidth = width * ratio;
var markHeight = height * ratio;
ctx.fillStyle = "transparent";
ctx.fillRect(0, 0, markWidth, markHeight);
var contents = Array.isArray(watermarkContent) ? watermarkContent : [_objectSpread$1({}, watermarkContent)];
var top = 0;
contents.forEach(function (item) {
if (item.url) {
var url = item.url,
_item$isGrayscale = item.isGrayscale,
isGrayscale = _item$isGrayscale === void 0 ? false : _item$isGrayscale;
item.top = top;
top += height;
var img = new Image();
img.crossOrigin = "anonymous";
img.referrerPolicy = "no-referrer";
img.src = url;
img.onload = function () {
ctx.drawImage(img, 0, item.top * ratio, width * ratio, height * ratio);
if (isGrayscale) {
var imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
var pixels = imgData.data;
for (var i = 0; i < pixels.length; i += 4) {
var lightness = (pixels[i] + pixels[i + 1] + pixels[i + 2]) / 3;
pixels[i] = lightness;
pixels[i + 1] = lightness;
pixels[i + 2] = lightness;
}
ctx.putImageData(imgData, 0, 0);
}
onFinish(canvas.toDataURL());
};
} else if (item.text) {
var text = item.text,
_item$fontSize = item.fontSize,
fontSize = _item$fontSize === void 0 ? 16 : _item$fontSize,
_item$fontFamily = item.fontFamily,
fontFamily = _item$fontFamily === void 0 ? void 0 : _item$fontFamily,
_item$fontWeight = item.fontWeight,
fontWeight = _item$fontWeight === void 0 ? "normal" : _item$fontWeight;
var fillStyle = (item === null || item === void 0 ? void 0 : item.fontColor) || fontColor;
item.top = top;
top += lineSpace;
var markSize = Number(fontSize) * ratio;
ctx.font = "normal normal ".concat(fontWeight, " ").concat(markSize, "px/").concat(markHeight, "px ").concat(fontFamily);
ctx.textAlign = "start";
ctx.textBaseline = "top";
ctx.fillStyle = fillStyle;
ctx.fillText(text, 0, item.top * ratio);
}
});
onFinish(canvas.toDataURL());
}
function randomMovingStyle() {
var align = Math.floor(Math.random() * 4);
var p1 = Math.floor(Math.random() * 70) + 30;
var leftTopLimit = 0;
var bottomLimit = 95;
var rightLimit = 90;
var keyframesStyle = "\n @keyframes watermark {\n 0% {left: ".concat(align === 1 ? rightLimit : align === 3 ? leftTopLimit : p1, "%; top: ").concat(align === 0 ? leftTopLimit : align === 2 ? bottomLimit : p1, "%;}\n 25% {left: ").concat(align === 0 ? rightLimit : align === 2 ? leftTopLimit : 100 - p1, "%; top: ").concat(align === 1 ? bottomLimit : align === 3 ? leftTopLimit : p1, "%;}\n 50% {left: ").concat(align === 1 ? leftTopLimit : align === 3 ? rightLimit : 100 - p1, "%; top: ").concat(align === 0 ? bottomLimit : align === 2 ? leftTopLimit : 100 - p1, "%; transform: translateX(-100%);}\n 75% {left: ").concat(align === 0 ? leftTopLimit : align === 2 ? rightLimit : p1, "%; top: ").concat(align === 1 ? leftTopLimit : align === 3 ? bottomLimit : 100 - p1, "%;}\n 100% {left: ").concat(align === 1 ? rightLimit : align === 3 ? leftTopLimit : p1, "%; top: ").concat(align === 0 ? leftTopLimit : align === 2 ? bottomLimit : p1, "%;}\n }\n ");
return keyframesStyle;
}
var injectStyle = function injectStyle(style) {
var styleElement = document.createElement("style");
var styleSheet = null;
document.head.appendChild(styleElement);
styleSheet = styleElement.sheet;
styleSheet.insertRule(style, styleSheet.cssRules.length);
};
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var Watermark = function Watermark(originalProps) {
var props = useDefaultProps(originalProps, watermarkDefaultProps);
var alpha = props.alpha,
_props$x = props.x,
x = _props$x === void 0 ? 200 : _props$x,
_props$y = props.y,
y = _props$y === void 0 ? 210 : _props$y,
_props$width = props.width,
width = _props$width === void 0 ? 120 : _props$width,
_props$height = props.height,
height = _props$height === void 0 ? 60 : _props$height,
tempRotate = props.rotate,
_props$zIndex = props.zIndex,
zIndex = _props$zIndex === void 0 ? 10 : _props$zIndex,
lineSpace = props.lineSpace,
isRepeat = props.isRepeat,
removable = props.removable,
movable = props.movable,
moveInterval = props.moveInterval,
_props$offset = props.offset,
offset = _props$offset === void 0 ? [] : _props$offset,
content = props.content,
children = props.children,
watermarkContent = props.watermarkContent,
className = props.className,
_props$style = props.style,
style = _props$style === void 0 ? {} : _props$style;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
var gapX = x;
var gapY = y;
var rotate = tempRotate;
if (movable) {
gapX = 0;
gapY = 0;
rotate = 0;
}
var clsName = "".concat(classPrefix, "-watermark");
var _useState = useState(""),
_useState2 = _slicedToArray(_useState, 2),
base64Url = _useState2[0],
setBase64Url = _useState2[1];
var styleStr = useRef("");
var watermarkRef = useRef(null);
var watermarkImgRef = useRef(null);
var stopObservation = useRef(false);
var offsetLeft = offset[0] || gapX / 2;
var offsetTop = offset[1] || gapY / 2;
var _useVariables = useVariables({
fontColor: "--td-text-color-watermark"
}),
fontColor = _useVariables.fontColor;
useEffect(function () {
generateBase64Url({
width: width,
height: height,
rotate: rotate,
lineSpace: lineSpace,
alpha: alpha,
gapX: gapX,
gapY: gapY,
watermarkContent: watermarkContent,
offsetLeft: offsetLeft,
offsetTop: offsetTop,
fontColor: fontColor
}, function (url) {
setBase64Url(url);
});
}, [width, height, rotate, zIndex, lineSpace, alpha, offsetLeft, offsetTop, gapX, gapY, watermarkContent, fontColor]);
useEffect(function () {
styleStr.current = getStyleStr(_objectSpread({
zIndex: zIndex,
position: "absolute",
left: 0,
right: 0,
top: 0,
bottom: 0,
width: movable ? "".concat(width, "px") : "100%",
height: movable ? "".concat(height, "px") : "100%",
backgroundSize: "".concat(gapX + width, "px"),
pointerEvents: "none",
backgroundRepeat: movable ? "no-repeat" : isRepeat ? "repeat" : "no-repeat",
backgroundImage: "url('".concat(base64Url, "')"),
animation: movable ? "watermark infinite ".concat(moveInterval * 4 / 60, "s") : "none"
}, style));
}, [zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, height]);
var renderWatermark = useCallback(function () {
var _watermarkImgRef$curr, _watermarkImgRef$curr2, _watermarkRef$current;
stopObservation.current = true;
(_watermarkImgRef$curr = watermarkImgRef.current) === null || _watermarkImgRef$curr === void 0 || (_watermarkImgRef$curr2 = _watermarkImgRef$curr.remove) === null || _watermarkImgRef$curr2 === void 0 || _watermarkImgRef$curr2.call(_watermarkImgRef$curr);
watermarkImgRef.current = void 0;
watermarkImgRef.current = document.createElement("div");
watermarkImgRef.current.setAttribute("style", styleStr.current);
(_watermarkRef$current = watermarkRef.current) === null || _watermarkRef$current === void 0 || _watermarkRef$current.append(watermarkImgRef.current);
setTimeout(function () {
stopObservation.current = false;
});
}, []);
useEffect(function () {
renderWatermark();
}, [renderWatermark, zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, className]);
useMutationObservable(watermarkRef.current, function (mutations) {
if (stopObservation.current) return;
if (removable) return;
mutations.forEach(function (mutation) {
if (mutation.type === "childList") {
var removeNodes = mutation.removedNodes;
removeNodes.forEach(function (node) {
var element = node;
if (element === watermarkImgRef.current) {
renderWatermark();
}
});
}
if (mutation.target === watermarkImgRef.current) {
renderWatermark();
}
});
});
var parent = useRef(null);
useEffect(function () {
parent.current = watermarkRef.current.parentElement;
var keyframesStyle = randomMovingStyle();
injectStyle(keyframesStyle);
}, []);
useMutationObservable(typeof document !== "undefined" ? document.body : null, function (mutations) {
if (removable) return;
mutations.forEach(function (mutation) {
if (mutation.type === "childList") {
var removeNodes = mutation.removedNodes;
removeNodes.forEach(function (node) {
var element = node;
if (element === watermarkRef.current) {
parent.current.appendChild(element);
}
});
}
});
});
return /* @__PURE__ */React.createElement("div", {
className: classNames([clsName, className]),
ref: watermarkRef
}, children || content);
};
export { Watermark as default };
//# sourceMappingURL=Watermark.js.map