UNPKG

tdesign-react

Version:
389 lines (378 loc) 17.6 kB
/** * tdesign v1.16.2 * (c) 2025 tdesign * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var defineProperty = require('../_chunks/dep-0fe55884.js'); var slicedToArray = require('../_chunks/dep-5cb0d66d.js'); var React = require('react'); var classNames = require('classnames'); var hooks_useConfig = require('../hooks/useConfig.js'); var hooks_useMutationObserver = require('../hooks/useMutationObserver.js'); var watermark_defaultProps = require('./defaultProps.js'); var watermark_utils = require('./utils.js'); var hooks_useDefaultProps = require('../hooks/useDefaultProps.js'); var hooks_useVariables = require('../hooks/useVariables.js'); require('../_chunks/dep-b325182b.js'); require('../_chunks/dep-737b8bd8.js'); require('../config-provider/ConfigContext.js'); require('../_chunks/dep-17dea53e.js'); require('../_chunks/dep-a0cbf081.js'); require('dayjs'); require('../_chunks/dep-481a1ecc.js'); require('../_chunks/dep-474eb386.js'); require('../_chunks/dep-3b256bc0.js'); require('../_chunks/dep-4ce0670e.js'); require('../_chunks/dep-3448f35f.js'); require('../_chunks/dep-b6a3ada9.js'); require('../_chunks/dep-967e785f.js'); require('../_chunks/dep-5c8525ea.js'); require('../_chunks/dep-735cd5b9.js'); require('../_chunks/dep-bdafd287.js'); require('../_chunks/dep-4d300b8f.js'); require('../_chunks/dep-3332ad6f.js'); require('../_chunks/dep-98783318.js'); require('../_chunks/dep-fc884a8e.js'); require('../_chunks/dep-008b21d4.js'); require('../_chunks/dep-9193eded.js'); require('../_chunks/dep-afe817f9.js'); require('../_chunks/dep-0b70c7ec.js'); require('../_chunks/dep-a1e3e59b.js'); require('../_chunks/dep-24ab8f68.js'); require('../_chunks/dep-753b7d52.js'); require('../hooks/useLatest.js'); require('../_chunks/dep-1b7ce2a3.js'); require('../_chunks/dep-625279d1.js'); require('../_chunks/dep-4f4b2eb1.js'); require('../_chunks/dep-e018b522.js'); require('../_chunks/dep-7f4d0687.js'); require('../_chunks/dep-b23d5e45.js'); require('../_chunks/dep-de1ebf3f.js'); require('../_chunks/dep-ff8b8704.js'); require('../_chunks/dep-eb2203ae.js'); require('../_chunks/dep-4fbbd844.js'); require('../_chunks/dep-e0735b5f.js'); require('../_chunks/dep-b304bc94.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var classNames__default = /*#__PURE__*/_interopDefaultLegacy(classNames); 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._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, layout = _ref.layout; var isHexagonal = layout === "hexagonal"; 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 actualBackgroundSize = { width: gapX + width }; var canvasWidth = (gapX + width) * ratio; var canvasHeight = (gapY + height) * ratio; var markWidth = width * ratio; var markHeight = height * ratio; var dislocationRotateX = canvasWidth; var dislocationRotateY = canvasHeight; var dislocationDrawX = (gapX + width) * ratio; var dislocationDrawY = (gapY + height) * ratio; canvas.width = canvasWidth; canvas.height = canvasHeight; canvas.style.width = "".concat(gapX + width, "px"); canvas.style.height = "".concat(gapY + height, "px"); if (isHexagonal) { canvas.style.width = "".concat(canvasWidth * 2, "px"); canvas.style.height = "".concat(canvasHeight * 2, "px"); canvas.width = canvasWidth * 2; canvas.height = canvasHeight * 2; actualBackgroundSize = { width: gapX + width * 2 + width / 2 }; } ctx.translate(offsetLeft * ratio, offsetTop * ratio); ctx.globalAlpha = alpha; ctx.fillStyle = "transparent"; ctx.fillRect(0, 0, markWidth, markHeight); var drawRotate = function drawRotate(ctx2, x, y, rotate2) { ctx2.translate(x, y); ctx2.rotate(Math.PI / 180 * Number(rotate2)); ctx2.translate(-x, -y); }; var drawText = function drawText(ctx2, x, y, markHeight2, text, fontWeight, fontSize, fontFamily, fillStyle) { ctx2.font = "normal normal ".concat(fontWeight, " ").concat(fontSize * ratio, "px/").concat(markHeight2, "px ").concat(fontFamily); ctx2.fillStyle = fillStyle; ctx2.textAlign = "start"; ctx2.textBaseline = "top"; ctx2.fillText(text, x, y); }; var contents = Array.isArray(watermarkContent) ? watermarkContent : [_objectSpread$1({}, watermarkContent)]; var top = 0; var imageLoadCount = 0; var totalImages = 0; contents.forEach(function (item) { item.top = top; if (item.url) { top += height; totalImages += isHexagonal ? 2 : 1; } else if (item.text) { top += lineSpace; } }); var renderWatermarkItem = function renderWatermarkItem(item) { var offsetX = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var offsetY = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; var rotateX = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var rotateY = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0; if (item.url) { var url = item.url, _item$isGrayscale = item.isGrayscale, isGrayscale = _item$isGrayscale === void 0 ? false : _item$isGrayscale; var img = new Image(); img.crossOrigin = "anonymous"; img.referrerPolicy = "no-referrer"; img.src = url; img.onload = function () { var _ctx$save, _ctx$restore; (_ctx$save = ctx.save) === null || _ctx$save === void 0 || _ctx$save.call(ctx); drawRotate(ctx, rotateX, rotateY, rotate); if (isGrayscale) { var tempCanvas = document.createElement("canvas"); var tempCtx = tempCanvas.getContext("2d"); tempCanvas.width = width * ratio; tempCanvas.height = height * ratio; tempCtx.drawImage(img, 0, 0, width * ratio, height * ratio); var imgData = tempCtx.getImageData(0, 0, width * ratio, height * ratio); 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; } tempCtx.putImageData(imgData, 0, 0); ctx.drawImage(tempCanvas, offsetX, offsetY + item.top * ratio, width * ratio, height * ratio); } else { ctx.drawImage(img, offsetX, offsetY + item.top * ratio, width * ratio, height * ratio); } (_ctx$restore = ctx.restore) === null || _ctx$restore === void 0 || _ctx$restore.call(ctx); imageLoadCount += 1; if (imageLoadCount === totalImages) { onFinish(canvas.toDataURL(), actualBackgroundSize); } }; } else if (item.text) { var _ctx$save2, _ctx$restore2; 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 ? "normal" : _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; (_ctx$save2 = ctx.save) === null || _ctx$save2 === void 0 || _ctx$save2.call(ctx); drawRotate(ctx, rotateX, rotateY, rotate); drawText(ctx, offsetX, offsetY + item.top * ratio, markHeight, text, fontWeight, fontSize, fontFamily, fillStyle); (_ctx$restore2 = ctx.restore) === null || _ctx$restore2 === void 0 || _ctx$restore2.call(ctx); } }; contents.forEach(function (item) { renderWatermarkItem(item, 0, 0, 0, 0); }); if (isHexagonal) { contents.forEach(function (item) { renderWatermarkItem(item, dislocationDrawX, dislocationDrawY, dislocationRotateX, dislocationRotateY); }); } if (totalImages === 0) { onFinish(canvas.toDataURL(), actualBackgroundSize); } } 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._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 = hooks_useDefaultProps["default"](originalProps, watermark_defaultProps.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, layout = props.layout, className = props.className, _props$style = props.style, style = _props$style === void 0 ? {} : _props$style; var _useConfig = hooks_useConfig["default"](), 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 = React.useState(""), _useState2 = slicedToArray._slicedToArray(_useState, 2), base64Url = _useState2[0], setBase64Url = _useState2[1]; var styleStr = React.useRef(""); var watermarkRef = React.useRef(null); var watermarkImgRef = React.useRef(null); var stopObservation = React.useRef(false); var offsetLeft = offset[0] || gapX / 2; var offsetTop = offset[1] || gapY / 2; var backgroundSize = React.useRef("".concat(gapX + width, "px")); var _useVariables = hooks_useVariables["default"]({ fontColor: "--td-text-color-watermark" }), fontColor = _useVariables.fontColor; React.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, layout: layout }, function (url, param) { var _ref = param || {}, width2 = _ref.width; backgroundSize.current = width2 ? "".concat(width2, "px") : null; setBase64Url(url); }); }, [width, height, rotate, zIndex, lineSpace, alpha, offsetLeft, offsetTop, gapX, gapY, watermarkContent, fontColor, layout]); React.useEffect(function () { styleStr.current = watermark_utils.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: backgroundSize.current || "".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, backgroundSize]); var renderWatermark = React.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; }); }, []); React.useEffect(function () { renderWatermark(); }, [renderWatermark, zIndex, gapX, width, movable, isRepeat, base64Url, moveInterval, style, className]); hooks_useMutationObserver["default"](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 = React.useRef(null); React.useEffect(function () { parent.current = watermarkRef.current.parentElement; var keyframesStyle = randomMovingStyle(); injectStyle(keyframesStyle); }, []); hooks_useMutationObserver["default"](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__default["default"].createElement("div", { className: classNames__default["default"]([clsName, className]), ref: watermarkRef }, children || content); }; exports["default"] = Watermark; //# sourceMappingURL=Watermark.js.map