UNPKG

tntd

Version:

tntd是基于 TNT Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品。

217 lines (215 loc) 8.36 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.Watermark = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /** * 返回当前显示设备的物理像素分辨率与CSS像素分辨率之比 * * @param context * @see api 有些废弃了,其实类型 CanvasRenderingContext2D */ var getPixelRatio = function getPixelRatio(context) { if (!context) { return 1; } var backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || 1; return (window.devicePixelRatio || 1) / backingStore; }; var Watermark = exports.Watermark = function Watermark(props) { var children = props.children, style = props.style, className = props.className, markStyle = props.markStyle, markClassName = props.markClassName, _props$zIndex = props.zIndex, zIndex = _props$zIndex === void 0 ? 9 : _props$zIndex, _props$gapX = props.gapX, gapX = _props$gapX === void 0 ? 212 : _props$gapX, _props$gapY = props.gapY, gapY = _props$gapY === void 0 ? 222 : _props$gapY, _props$width = props.width, width = _props$width === void 0 ? 120 : _props$width, _props$height = props.height, height = _props$height === void 0 ? 64 : _props$height, _props$rotate = props.rotate, rotate = _props$rotate === void 0 ? -22 : _props$rotate, image = props.image, content = props.content, offsetLeft = props.offsetLeft, offsetTop = props.offsetTop, _props$fontStyle = props.fontStyle, fontStyle = _props$fontStyle === void 0 ? 'normal' : _props$fontStyle, _props$textAlign = props.textAlign, textAlign = _props$textAlign === void 0 ? 'center' : _props$textAlign, _props$fontWeight = props.fontWeight, fontWeight = _props$fontWeight === void 0 ? 'normal' : _props$fontWeight, _props$fontColor = props.fontColor, fontColor = _props$fontColor === void 0 ? 'rgba(0,0,0,.15)' : _props$fontColor, _props$fontSize = props.fontSize, fontSize = _props$fontSize === void 0 ? 14 : _props$fontSize, _props$fontFamily = props.fontFamily, fontFamily = _props$fontFamily === void 0 ? 'sans-serif' : _props$fontFamily, _props$prefixCls = props.prefixCls, prefixCls = _props$prefixCls === void 0 ? 'tntd-watermark' : _props$prefixCls; var wrapperCls = (0, _classnames["default"])(["".concat(prefixCls, "-wrapper"), className]); var watermarkCls = (0, _classnames["default"])([prefixCls, markClassName]); var _useState = (0, _react.useState)(''), _useState2 = _slicedToArray(_useState, 2), base64Url = _useState2[0], setBase64Url = _useState2[1]; (0, _react.useEffect)(function () { var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); var ratio = getPixelRatio(ctx); var canvasWidth = "".concat((gapX + width) * ratio, "px"); var canvasHeight = "".concat((gapY + height) * ratio, "px"); var canvasOffsetLeft = offsetLeft || gapX / 2; var canvasOffsetTop = offsetTop || gapY / 2; canvas.setAttribute('width', canvasWidth); canvas.setAttribute('height', canvasHeight); if (ctx) { // 旋转字符 rotate ctx.translate(canvasOffsetLeft * ratio, canvasOffsetTop * ratio); ctx.rotate(Math.PI / 180 * Number(rotate)); var markWidth = width * ratio; var markHeight = height * ratio; if (image) { var img = new Image(); img.crossOrigin = 'anonymous'; img.referrerPolicy = 'no-referrer'; img.src = image; img.onload = function () { ctx.drawImage(img, 0, 0, markWidth, markHeight); setBase64Url(canvas.toDataURL()); }; } else if (content) { var markSize = Number(fontSize) * ratio; ctx.font = "".concat(fontStyle, " normal ").concat(fontWeight, " ").concat(markSize, "px/").concat(markHeight, "px ").concat(fontFamily); ctx.fillStyle = fontColor; ctx.textAlign = textAlign; if (Array.isArray(content)) { content === null || content === void 0 ? void 0 : content.forEach(function (item, index) { return ctx.fillText(item, 0, index * markSize); }); } else { ctx.fillText(content, 0, 0); } setBase64Url(canvas.toDataURL()); } } else { // eslint-disable-next-line no-console console.error('当前环境不支持Canvas'); } }, [gapX, gapY, offsetLeft, offsetTop, rotate, fontStyle, textAlign, fontWeight, width, height, fontFamily, fontColor, image, content, fontSize]); return _react["default"].createElement("div", { style: Object.assign({ position: 'relative' }, style), className: wrapperCls }, children, _react["default"].createElement("div", { className: watermarkCls, style: Object.assign(Object.assign({ zIndex: zIndex, position: 'absolute', left: 0, top: 0, width: '100%', height: '100%', backgroundSize: "".concat(gapX + width, "px"), pointerEvents: 'none', backgroundRepeat: 'repeat' }, base64Url ? { backgroundImage: "url('".concat(base64Url, "')") } : {}), markStyle) })); }; var _default = exports["default"] = Watermark;