Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
monorepo
/
venus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit b87d41ba
authored
Nov 15, 2024
by
Netfan
Committed by
GitHub
Nov 15, 2024
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix: adjust `useWatermark` logic (#4896)
1 parent
788a29a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
11 deletions
packages/effects/hooks/src/use-watermark.ts
playground/src/views/demos/features/watermark/index.vue
packages/effects/hooks/src/use-watermark.ts
View file @
b87d41b
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
:
[
...
...
@@ -45,7 +48,7 @@ export function useWatermark() {
...
options
,
};
watermark
.
value
=
new
Watermark
(
cachedOptions
.
value
);
updatePreferences
({
app
:
{
watermark
:
true
}
});
await
watermark
.
value
?.
create
();
}
...
...
@@ -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
)
,
};
}
playground/src/views/demos/features/watermark/index.vue
View file @
b87d41b
...
...
@@ -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
({
...
...
@@ -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
,
...
...
@@ -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>
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment