refactor: watermark type

pull/6320/head
tangjinzhou 2023-02-28 11:01:43 +08:00
parent 6058ca5576
commit a1e967dfc2
1 changed files with 18 additions and 26 deletions

View File

@ -1,8 +1,9 @@
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue'; import type { ExtractPropTypes, CSSProperties } from 'vue';
import { computed, defineComponent, onBeforeUnmount, onMounted, shallowRef, watch } from 'vue'; import { computed, defineComponent, onBeforeUnmount, onMounted, shallowRef, watch } from 'vue';
import { getStyleStr, getPixelRatio, rotateWatermark, reRendering } from './utils'; import { getStyleStr, getPixelRatio, rotateWatermark, reRendering } from './utils';
import { withInstall } from '../_util/type'; import { arrayType, objectType, someType, withInstall } from '../_util/type';
import { useMutationObserver } from '../_util/hooks/_vueuse/useMutationObserver'; import { useMutationObserver } from '../_util/hooks/_vueuse/useMutationObserver';
import { initDefaultProps } from '../_util/props-util';
/** /**
* Base size of the canvas, 1 for parallel layout and 2 for alternate layout * Base size of the canvas, 1 for parallel layout and 2 for alternate layout
@ -24,32 +25,22 @@ export const watermarkProps = () => ({
width: Number, width: Number,
height: Number, height: Number,
image: String, image: String,
content: [String, Array], content: someType<string | string[]>([String, Array]),
font: { font: objectType<WatermarkFontType>(),
type: Object as PropType<WatermarkFontType>,
default: () => ({
fontSize: 16,
color: 'rgba(0, 0, 0, 0.15)',
fontWeight: 'normal',
fontStyle: 'normal',
fontFamily: 'sans-serif',
}),
},
rootClassName: String, rootClassName: String,
gap: { gap: arrayType<[number, number]>(),
type: [Array, Object] as PropType<[number, number]>, offset: arrayType<[number, number]>(),
default: undefined,
},
offset: {
type: [Array, Object] as PropType<[number, number]>,
default: undefined,
},
}); });
export type WatermarkProps = Partial<ExtractPropTypes<ReturnType<typeof watermarkProps>>>; export type WatermarkProps = Partial<ExtractPropTypes<ReturnType<typeof watermarkProps>>>;
const Watermark = defineComponent({ const Watermark = defineComponent({
name: 'AWatermark', name: 'AWatermark',
inheritAttrs: false, inheritAttrs: false,
props: watermarkProps(), props: initDefaultProps(watermarkProps(), {
zIndex: 9,
rotate: -22,
font: {},
gap: [100, 100],
}),
setup(props, { slots, attrs }) { setup(props, { slots, attrs }) {
const containerRef = shallowRef<HTMLDivElement>(); const containerRef = shallowRef<HTMLDivElement>();
const watermarkRef = shallowRef<HTMLDivElement>(); const watermarkRef = shallowRef<HTMLDivElement>();
@ -65,7 +56,7 @@ const Watermark = defineComponent({
const fontStyle = computed(() => props.font?.fontStyle ?? 'normal'); const fontStyle = computed(() => props.font?.fontStyle ?? 'normal');
const fontFamily = computed(() => props.font?.fontFamily ?? 'sans-serif'); const fontFamily = computed(() => props.font?.fontFamily ?? 'sans-serif');
const color = computed(() => props.font?.color ?? 'rgba(0, 0, 0, 0.15)'); const color = computed(() => props.font?.color ?? 'rgba(0, 0, 0, 0.15)');
const getMarkStyle = () => { const markStyle = computed(() => {
const markStyle: CSSProperties = { const markStyle: CSSProperties = {
zIndex: props.zIndex ?? 9, zIndex: props.zIndex ?? 9,
position: 'absolute', position: 'absolute',
@ -93,7 +84,7 @@ const Watermark = defineComponent({
markStyle.backgroundPosition = `${positionLeft}px ${positionTop}px`; markStyle.backgroundPosition = `${positionLeft}px ${positionTop}px`;
return markStyle; return markStyle;
}; });
const destroyWatermark = () => { const destroyWatermark = () => {
if (watermarkRef.value) { if (watermarkRef.value) {
watermarkRef.value.remove(); watermarkRef.value.remove();
@ -107,7 +98,7 @@ const Watermark = defineComponent({
watermarkRef.value.setAttribute( watermarkRef.value.setAttribute(
'style', 'style',
getStyleStr({ getStyleStr({
...getMarkStyle(), ...markStyle.value,
backgroundImage: `url('${base64Url}')`, backgroundImage: `url('${base64Url}')`,
backgroundSize: `${(gapX.value + markWidth) * BaseSize}px`, backgroundSize: `${(gapX.value + markWidth) * BaseSize}px`,
}), }),
@ -248,9 +239,10 @@ const Watermark = defineComponent({
return () => { return () => {
return ( return (
<div <div
{...attrs}
ref={containerRef} ref={containerRef}
class={[attrs.class, props.rootClassName]} class={[attrs.class, props.rootClassName]}
style={{ position: 'relative' }} style={[{ position: 'relative' }, attrs.style as CSSProperties]}
> >
{slots.default?.()} {slots.default?.()}
</div> </div>