Skip to content

Commit a1e967d

Browse files
committed
refactor: watermark type
1 parent 6058ca5 commit a1e967d

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

components/watermark/index.tsx

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue';
1+
import type { ExtractPropTypes, CSSProperties } from 'vue';
22
import { computed, defineComponent, onBeforeUnmount, onMounted, shallowRef, watch } from 'vue';
33
import { getStyleStr, getPixelRatio, rotateWatermark, reRendering } from './utils';
4-
import { withInstall } from '../_util/type';
4+
import { arrayType, objectType, someType, withInstall } from '../_util/type';
55
import { useMutationObserver } from '../_util/hooks/_vueuse/useMutationObserver';
6+
import { initDefaultProps } from '../_util/props-util';
67

78
/**
89
* Base size of the canvas, 1 for parallel layout and 2 for alternate layout
@@ -24,32 +25,22 @@ export const watermarkProps = () => ({
2425
width: Number,
2526
height: Number,
2627
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>(),
3830
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]>(),
4733
});
4834
export type WatermarkProps = Partial<ExtractPropTypes<ReturnType<typeof watermarkProps>>>;
4935
const Watermark = defineComponent({
5036
name: 'AWatermark',
5137
inheritAttrs: false,
52-
props: watermarkProps(),
38+
props: initDefaultProps(watermarkProps(), {
39+
zIndex: 9,
40+
rotate: -22,
41+
font: {},
42+
gap: [100, 100],
43+
}),
5344
setup(props, { slots, attrs }) {
5445
const containerRef = shallowRef<HTMLDivElement>();
5546
const watermarkRef = shallowRef<HTMLDivElement>();
@@ -65,7 +56,7 @@ const Watermark = defineComponent({
6556
const fontStyle = computed(() => props.font?.fontStyle ?? 'normal');
6657
const fontFamily = computed(() => props.font?.fontFamily ?? 'sans-serif');
6758
const color = computed(() => props.font?.color ?? 'rgba(0, 0, 0, 0.15)');
68-
const getMarkStyle = () => {
59+
const markStyle = computed(() => {
6960
const markStyle: CSSProperties = {
7061
zIndex: props.zIndex ?? 9,
7162
position: 'absolute',
@@ -93,7 +84,7 @@ const Watermark = defineComponent({
9384
markStyle.backgroundPosition = `${positionLeft}px ${positionTop}px`;
9485

9586
return markStyle;
96-
};
87+
});
9788
const destroyWatermark = () => {
9889
if (watermarkRef.value) {
9990
watermarkRef.value.remove();
@@ -107,7 +98,7 @@ const Watermark = defineComponent({
10798
watermarkRef.value.setAttribute(
10899
'style',
109100
getStyleStr({
110-
...getMarkStyle(),
101+
...markStyle.value,
111102
backgroundImage: `url('${base64Url}')`,
112103
backgroundSize: `${(gapX.value + markWidth) * BaseSize}px`,
113104
}),
@@ -248,9 +239,10 @@ const Watermark = defineComponent({
248239
return () => {
249240
return (
250241
<div
242+
{...attrs}
251243
ref={containerRef}
252244
class={[attrs.class, props.rootClassName]}
253-
style={{ position: 'relative' }}
245+
style={[{ position: 'relative' }, attrs.style as CSSProperties]}
254246
>
255247
{slots.default?.()}
256248
</div>

0 commit comments

Comments
 (0)