1
- import type { PropType , ExtractPropTypes , CSSProperties } from 'vue' ;
1
+ import type { ExtractPropTypes , CSSProperties } from 'vue' ;
2
2
import { computed , defineComponent , onBeforeUnmount , onMounted , shallowRef , watch } from 'vue' ;
3
3
import { getStyleStr , getPixelRatio , rotateWatermark , reRendering } from './utils' ;
4
- import { withInstall } from '../_util/type' ;
4
+ import { arrayType , objectType , someType , withInstall } from '../_util/type' ;
5
5
import { useMutationObserver } from '../_util/hooks/_vueuse/useMutationObserver' ;
6
+ import { initDefaultProps } from '../_util/props-util' ;
6
7
7
8
/**
8
9
* Base size of the canvas, 1 for parallel layout and 2 for alternate layout
@@ -24,32 +25,22 @@ export const watermarkProps = () => ({
24
25
width : Number ,
25
26
height : Number ,
26
27
image : String ,
27
- content : [ String , Array ] ,
28
- font : {
29
- type : Object as PropType < WatermarkFontType > ,
30
- default : ( ) => ( {
31
- fontSize : 16 ,
32
- color : 'rgba(0, 0, 0, 0.15)' ,
33
- fontWeight : 'normal' ,
34
- fontStyle : 'normal' ,
35
- fontFamily : 'sans-serif' ,
36
- } ) ,
37
- } ,
28
+ content : someType < string | string [ ] > ( [ String , Array ] ) ,
29
+ font : objectType < WatermarkFontType > ( ) ,
38
30
rootClassName : String ,
39
- gap : {
40
- type : [ Array , Object ] as PropType < [ number , number ] > ,
41
- default : undefined ,
42
- } ,
43
- offset : {
44
- type : [ Array , Object ] as PropType < [ number , number ] > ,
45
- default : undefined ,
46
- } ,
31
+ gap : arrayType < [ number , number ] > ( ) ,
32
+ offset : arrayType < [ number , number ] > ( ) ,
47
33
} ) ;
48
34
export type WatermarkProps = Partial < ExtractPropTypes < ReturnType < typeof watermarkProps > > > ;
49
35
const Watermark = defineComponent ( {
50
36
name : 'AWatermark' ,
51
37
inheritAttrs : false ,
52
- props : watermarkProps ( ) ,
38
+ props : initDefaultProps ( watermarkProps ( ) , {
39
+ zIndex : 9 ,
40
+ rotate : - 22 ,
41
+ font : { } ,
42
+ gap : [ 100 , 100 ] ,
43
+ } ) ,
53
44
setup ( props , { slots, attrs } ) {
54
45
const containerRef = shallowRef < HTMLDivElement > ( ) ;
55
46
const watermarkRef = shallowRef < HTMLDivElement > ( ) ;
@@ -65,7 +56,7 @@ const Watermark = defineComponent({
65
56
const fontStyle = computed ( ( ) => props . font ?. fontStyle ?? 'normal' ) ;
66
57
const fontFamily = computed ( ( ) => props . font ?. fontFamily ?? 'sans-serif' ) ;
67
58
const color = computed ( ( ) => props . font ?. color ?? 'rgba(0, 0, 0, 0.15)' ) ;
68
- const getMarkStyle = ( ) => {
59
+ const markStyle = computed ( ( ) => {
69
60
const markStyle : CSSProperties = {
70
61
zIndex : props . zIndex ?? 9 ,
71
62
position : 'absolute' ,
@@ -93,7 +84,7 @@ const Watermark = defineComponent({
93
84
markStyle . backgroundPosition = `${ positionLeft } px ${ positionTop } px` ;
94
85
95
86
return markStyle ;
96
- } ;
87
+ } ) ;
97
88
const destroyWatermark = ( ) => {
98
89
if ( watermarkRef . value ) {
99
90
watermarkRef . value . remove ( ) ;
@@ -107,7 +98,7 @@ const Watermark = defineComponent({
107
98
watermarkRef . value . setAttribute (
108
99
'style' ,
109
100
getStyleStr ( {
110
- ...getMarkStyle ( ) ,
101
+ ...markStyle . value ,
111
102
backgroundImage : `url('${ base64Url } ')` ,
112
103
backgroundSize : `${ ( gapX . value + markWidth ) * BaseSize } px` ,
113
104
} ) ,
@@ -248,9 +239,10 @@ const Watermark = defineComponent({
248
239
return ( ) => {
249
240
return (
250
241
< div
242
+ { ...attrs }
251
243
ref = { containerRef }
252
244
class = { [ attrs . class , props . rootClassName ] }
253
- style = { { position : 'relative' } }
245
+ style = { [ { position : 'relative' } , attrs . style as CSSProperties ] }
254
246
>
255
247
{ slots . default ?.( ) }
256
248
</ div >
0 commit comments