Skip to content

Commit

Permalink
fix: adjust useWatermark logic (#4896)
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetfan authored Nov 15, 2024
1 parent 788a29a commit b87d41b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
25 changes: 18 additions & 7 deletions packages/effects/hooks/src/use-watermark.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Watermark, WatermarkOptions } from 'watermark-js-plus';

import { nextTick, onUnmounted, ref } from 'vue';
import { nextTick, onUnmounted, readonly, ref } from 'vue';

import { updatePreferences } from '@vben/preferences';

const watermark = ref<Watermark>();
const unmountedHooked = ref<boolean>(false);
const cachedOptions = ref<Partial<WatermarkOptions>>({
advancedStyle: {
colorStops: [
Expand Down Expand Up @@ -45,7 +48,7 @@ export function useWatermark() {
...options,
};
watermark.value = new Watermark(cachedOptions.value);

updatePreferences({ app: { watermark: true } });
await watermark.value?.create();
}

Expand All @@ -62,16 +65,24 @@ export function useWatermark() {
}

function destroyWatermark() {
watermark.value?.destroy();
if (watermark.value) {
watermark.value.destroy();
watermark.value = undefined;
}
updatePreferences({ app: { watermark: false } });
}

onUnmounted(() => {
destroyWatermark();
});
// 只在第一次调用时注册卸载钩子,防止重复注册以致于在路由切换时销毁了水印
if (!unmountedHooked.value) {
unmountedHooked.value = true;
onUnmounted(() => {
destroyWatermark();
});
}

return {
destroyWatermark,
updateWatermark,
watermark,
watermark: readonly(watermark),
};
}
28 changes: 24 additions & 4 deletions playground/src/views/demos/features/watermark/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { useWatermark } from '@vben/hooks';
import { Button, Card } from 'ant-design-vue';
const { destroyWatermark, updateWatermark } = useWatermark();
const { destroyWatermark, updateWatermark, watermark } = useWatermark();
async function recreateWaterMark() {
destroyWatermark();
await updateWatermark({});
}
async function createWaterMark() {
await updateWatermark({
Expand All @@ -21,7 +26,7 @@ async function createWaterMark() {
],
type: 'linear',
},
content: 'hello my watermark',
content: `hello my watermark\n${new Date().toLocaleString()}`,
globalAlpha: 0.5,
gridLayoutOptions: {
cols: 2,
Expand Down Expand Up @@ -57,10 +62,25 @@ async function createWaterMark() {
</template>

<Card title="使用">
<Button class="mr-2" type="primary" @click="createWaterMark()">
<Button
:disabled="!!watermark"
class="mr-2"
type="primary"
@click="recreateWaterMark"
>
创建水印
</Button>
<Button danger @click="destroyWatermark">移除水印</Button>
<Button
:disabled="!watermark"
class="mr-2"
type="primary"
@click="createWaterMark"
>
更新水印
</Button>
<Button :disabled="!watermark" danger @click="destroyWatermark">
移除水印
</Button>
</Card>
</Page>
</template>

0 comments on commit b87d41b

Please sign in to comment.