UNPKG

5.27 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __export = (target, all) => {
6 for (var name2 in all)
7 __defProp(target, name2, { get: all[name2], enumerable: true });
8};
9var __copyProps = (to, from, except, desc) => {
10 if (from && typeof from === "object" || typeof from === "function") {
11 for (let key of __getOwnPropNames(from))
12 if (!__hasOwnProp.call(to, key) && key !== except)
13 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14 }
15 return to;
16};
17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18var stdin_exports = {};
19__export(stdin_exports, {
20 default: () => stdin_default,
21 watermarkProps: () => watermarkProps
22});
23module.exports = __toCommonJS(stdin_exports);
24var import_vue = require("vue");
25var import_utils = require("../utils");
26const [name, bem] = (0, import_utils.createNamespace)("watermark");
27const watermarkProps = {
28 gapX: (0, import_utils.makeNumberProp)(0),
29 gapY: (0, import_utils.makeNumberProp)(0),
30 image: String,
31 width: (0, import_utils.makeNumberProp)(100),
32 height: (0, import_utils.makeNumberProp)(100),
33 rotate: (0, import_utils.makeNumericProp)(-22),
34 zIndex: import_utils.numericProp,
35 content: String,
36 opacity: import_utils.numericProp,
37 fullPage: import_utils.truthProp,
38 textColor: (0, import_utils.makeStringProp)("#dcdee0")
39};
40var stdin_default = (0, import_vue.defineComponent)({
41 name,
42 props: watermarkProps,
43 setup(props, {
44 slots
45 }) {
46 const svgElRef = (0, import_vue.ref)();
47 const watermarkUrl = (0, import_vue.ref)("");
48 const imageBase64 = (0, import_vue.ref)("");
49 const renderWatermark = () => {
50 const rotateStyle = {
51 transformOrigin: "center",
52 transform: `rotate(${props.rotate}deg)`
53 };
54 const svgInner = () => {
55 if (props.image && !slots.content) {
56 return (0, import_vue.createVNode)("image", {
57 "href": imageBase64.value,
58 "xlink:href": imageBase64.value,
59 "x": "0",
60 "y": "0",
61 "width": props.width,
62 "height": props.height,
63 "style": rotateStyle
64 }, null);
65 }
66 return (0, import_vue.createVNode)("foreignObject", {
67 "x": "0",
68 "y": "0",
69 "width": props.width,
70 "height": props.height
71 }, [(0, import_vue.createVNode)("div", {
72 "xmlns": "http://www.w3.org/1999/xhtml",
73 "style": rotateStyle
74 }, [slots.content ? slots.content() : (0, import_vue.createVNode)("span", {
75 "style": {
76 color: props.textColor
77 }
78 }, [props.content])])]);
79 };
80 const svgWidth = props.width + props.gapX;
81 const svgHeight = props.height + props.gapY;
82 return (0, import_vue.createVNode)("svg", {
83 "viewBox": `0 0 ${svgWidth} ${svgHeight}`,
84 "width": svgWidth,
85 "height": svgHeight,
86 "xmlns": "http://www.w3.org/2000/svg",
87 "xmlns:xlink": "http://www.w3.org/1999/xlink",
88 "style": {
89 padding: `0 ${props.gapX}px ${props.gapY}px 0`,
90 opacity: props.opacity
91 }
92 }, [svgInner()]);
93 };
94 const makeImageToBase64 = (url) => {
95 const canvas = document.createElement("canvas");
96 const image = new Image();
97 image.crossOrigin = "anonymous";
98 image.referrerPolicy = "no-referrer";
99 image.onload = () => {
100 canvas.width = image.naturalWidth;
101 canvas.height = image.naturalHeight;
102 const ctx = canvas.getContext("2d");
103 ctx == null ? void 0 : ctx.drawImage(image, 0, 0);
104 imageBase64.value = canvas.toDataURL();
105 };
106 image.src = url;
107 };
108 const makeSvgToBlobUrl = (svgStr) => {
109 const svgBlob = new Blob([svgStr], {
110 type: "image/svg+xml"
111 });
112 return URL.createObjectURL(svgBlob);
113 };
114 (0, import_vue.watchEffect)(() => {
115 if (props.image) {
116 makeImageToBase64(props.image);
117 }
118 });
119 (0, import_vue.watch)(() => [imageBase64.value, props.content, props.textColor, props.height, props.width, props.rotate, props.gapX, props.gapY], () => {
120 (0, import_vue.nextTick)(() => {
121 if (svgElRef.value) {
122 if (watermarkUrl.value) {
123 URL.revokeObjectURL(watermarkUrl.value);
124 }
125 watermarkUrl.value = makeSvgToBlobUrl(svgElRef.value.innerHTML);
126 }
127 });
128 }, {
129 immediate: true
130 });
131 (0, import_vue.onUnmounted)(() => {
132 if (watermarkUrl.value) {
133 URL.revokeObjectURL(watermarkUrl.value);
134 }
135 });
136 return () => {
137 const style = (0, import_utils.extend)({
138 backgroundImage: `url(${watermarkUrl.value})`
139 }, (0, import_utils.getZIndexStyle)(props.zIndex));
140 return (0, import_vue.createVNode)("div", {
141 "class": bem({
142 full: props.fullPage
143 }),
144 "style": style
145 }, [(0, import_vue.createVNode)("div", {
146 "class": bem("wrapper"),
147 "ref": svgElRef
148 }, [renderWatermark()])]);
149 };
150 }
151});