Commit 3b0077dc73772a460684b5ae5807c5bf511bd757
Committed by
GitHub
1 parent
3de5b53b
fix: 修复水印重复叠加问题 (#2622)
Co-authored-by: whong <whif@foxmail.com>
Showing
1 changed file
with
6 additions
and
0 deletions
src/hooks/web/useWatermark.ts
@@ -4,10 +4,15 @@ import { addResizeListener, removeResizeListener } from '/@/utils/event'; | @@ -4,10 +4,15 @@ import { addResizeListener, removeResizeListener } from '/@/utils/event'; | ||
4 | import { isDef } from '/@/utils/is'; | 4 | import { isDef } from '/@/utils/is'; |
5 | 5 | ||
6 | const domSymbol = Symbol('watermark-dom'); | 6 | const domSymbol = Symbol('watermark-dom'); |
7 | +const sourceMap = new WeakMap<HTMLElement, {}>(); | ||
7 | 8 | ||
8 | export function useWatermark( | 9 | export function useWatermark( |
9 | appendEl: Ref<HTMLElement | null> = ref(document.body) as Ref<HTMLElement>, | 10 | appendEl: Ref<HTMLElement | null> = ref(document.body) as Ref<HTMLElement>, |
10 | ) { | 11 | ) { |
12 | + const appendElRaw = unref(appendEl); | ||
13 | + if (appendElRaw && sourceMap.has(appendElRaw)) { | ||
14 | + return sourceMap.get(appendElRaw); | ||
15 | + } | ||
11 | const func = useRafThrottle(function () { | 16 | const func = useRafThrottle(function () { |
12 | const el = unref(appendEl); | 17 | const el = unref(appendEl); |
13 | if (!el) return; | 18 | if (!el) return; |
@@ -82,6 +87,7 @@ export function useWatermark( | @@ -82,6 +87,7 @@ export function useWatermark( | ||
82 | const { clientHeight: height, clientWidth: width } = el; | 87 | const { clientHeight: height, clientWidth: width } = el; |
83 | updateWatermark({ str, width, height }); | 88 | updateWatermark({ str, width, height }); |
84 | el.appendChild(div); | 89 | el.appendChild(div); |
90 | + sourceMap.set(el, { setWatermark, clear }); | ||
85 | return id; | 91 | return id; |
86 | }; | 92 | }; |
87 | 93 |