Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-vue-admin
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-vue-admin
Commits
a1d956d3
Unverified
提交
a1d956d3
authored
7月 13, 2021
作者:
yanzhuang
提交者:
GitHub
7月 13, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(useWatermark): fix `func` call `createWatermark` call `clear` to resizeEvent removed (#901)
上级
35e13470
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
70 行增加
和
20 行删除
+70
-20
useWatermark.ts
src/hooks/web/useWatermark.ts
+55
-20
domUtils.ts
src/utils/domUtils.ts
+15
-0
没有找到文件。
src/hooks/web/useWatermark.ts
浏览文件 @
a1d956d3
import
{
getCurrentInstance
,
onBeforeUnmount
,
ref
,
Ref
,
unref
}
from
'vue'
;
import
{
getCurrentInstance
,
onBeforeUnmount
,
ref
,
Ref
,
shallowRef
,
unref
}
from
'vue'
;
import
{
useRafThrottle
}
from
'/@/utils/domUtils'
;
import
{
addResizeListener
,
removeResizeListener
}
from
'/@/utils/event'
;
import
{
isDef
}
from
'/@/utils/is'
;
const
domSymbol
=
Symbol
(
'watermark-dom'
);
const
domSymbol
=
Symbol
(
'watermark-dom'
);
export
function
useWatermark
(
export
function
useWatermark
(
appendEl
:
Ref
<
HTMLElement
|
null
>
=
ref
(
document
.
body
)
as
Ref
<
HTMLElement
>
appendEl
:
Ref
<
HTMLElement
|
null
>
=
ref
(
document
.
body
)
as
Ref
<
HTMLElement
>
)
{
)
{
let
func
:
Fn
=
()
=>
{};
const
func
=
useRafThrottle
(
function
()
{
const
el
=
unref
(
appendEl
);
if
(
!
el
)
return
;
const
{
clientHeight
:
height
,
clientWidth
:
width
}
=
el
;
updateWatermark
({
height
,
width
});
});
const
id
=
domSymbol
.
toString
();
const
id
=
domSymbol
.
toString
();
const
watermarkEl
=
shallowRef
<
HTMLElement
>
();
const
clear
=
()
=>
{
const
clear
=
()
=>
{
const
domId
=
document
.
getElementById
(
id
);
const
domId
=
unref
(
watermarkEl
);
if
(
domId
)
{
watermarkEl
.
value
=
undefined
;
const
el
=
unref
(
appendEl
);
const
el
=
unref
(
appendEl
);
el
&&
el
.
removeChild
(
domId
)
;
if
(
!
el
)
return
;
}
domId
&&
el
.
removeChild
(
domId
);
window
.
removeEventListener
(
'resize'
,
func
);
removeResizeListener
(
el
,
func
);
};
};
const
createWatermark
=
(
str
:
string
)
=>
{
clear
();
function
createBase64
(
str
:
string
)
{
const
can
=
document
.
createElement
(
'canvas'
);
const
can
=
document
.
createElement
(
'canvas'
);
can
.
width
=
300
;
const
width
=
300
;
can
.
height
=
240
;
const
height
=
240
;
Object
.
assign
(
can
,
{
width
,
height
});
const
cans
=
can
.
getContext
(
'2d'
);
const
cans
=
can
.
getContext
(
'2d'
);
if
(
cans
)
{
if
(
cans
)
{
...
@@ -29,30 +39,55 @@ export function useWatermark(
...
@@ -29,30 +39,55 @@ export function useWatermark(
cans
.
fillStyle
=
'rgba(0, 0, 0, 0.15)'
;
cans
.
fillStyle
=
'rgba(0, 0, 0, 0.15)'
;
cans
.
textAlign
=
'left'
;
cans
.
textAlign
=
'left'
;
cans
.
textBaseline
=
'middle'
;
cans
.
textBaseline
=
'middle'
;
cans
.
fillText
(
str
,
can
.
width
/
20
,
can
.
height
);
cans
.
fillText
(
str
,
width
/
20
,
height
);
}
return
can
.
toDataURL
(
'image/png'
);
}
}
function
updateWatermark
(
options
:
{
width
?:
number
;
height
?:
number
;
str
?:
string
;
}
=
{}
)
{
const
el
=
unref
(
watermarkEl
);
if
(
!
el
)
return
;
if
(
isDef
(
options
.
width
))
{
el
.
style
.
width
=
`
${
options
.
width
}
px`
;
}
if
(
isDef
(
options
.
height
))
{
el
.
style
.
height
=
`
${
options
.
height
}
px`
;
}
if
(
isDef
(
options
.
str
))
{
el
.
style
.
background
=
`url(
${
createBase64
(
options
.
str
)}
) left top repeat`
;
}
}
const
createWatermark
=
(
str
:
string
)
=>
{
if
(
unref
(
watermarkEl
))
{
updateWatermark
({
str
});
return
id
;
}
const
div
=
document
.
createElement
(
'div'
);
const
div
=
document
.
createElement
(
'div'
);
watermarkEl
.
value
=
div
;
div
.
id
=
id
;
div
.
id
=
id
;
div
.
style
.
pointerEvents
=
'none'
;
div
.
style
.
pointerEvents
=
'none'
;
div
.
style
.
top
=
'0px'
;
div
.
style
.
top
=
'0px'
;
div
.
style
.
left
=
'0px'
;
div
.
style
.
left
=
'0px'
;
div
.
style
.
position
=
'absolute'
;
div
.
style
.
position
=
'absolute'
;
div
.
style
.
zIndex
=
'100000'
;
div
.
style
.
zIndex
=
'100000'
;
div
.
style
.
width
=
document
.
documentElement
.
clientWidth
+
'px'
;
div
.
style
.
height
=
document
.
documentElement
.
clientHeight
+
'px'
;
div
.
style
.
background
=
'url('
+
can
.
toDataURL
(
'image/png'
)
+
') left top repeat'
;
const
el
=
unref
(
appendEl
);
const
el
=
unref
(
appendEl
);
el
&&
el
.
appendChild
(
div
);
if
(
!
el
)
return
id
;
const
{
clientHeight
:
height
,
clientWidth
:
width
}
=
el
;
updateWatermark
({
str
,
width
,
height
});
el
.
appendChild
(
div
);
return
id
;
return
id
;
};
};
function
setWatermark
(
str
:
string
)
{
function
setWatermark
(
str
:
string
)
{
createWatermark
(
str
);
createWatermark
(
str
);
func
=
()
=>
{
addResizeListener
(
document
.
documentElement
,
func
);
createWatermark
(
str
);
};
window
.
addEventListener
(
'resize'
,
func
);
const
instance
=
getCurrentInstance
();
const
instance
=
getCurrentInstance
();
if
(
instance
)
{
if
(
instance
)
{
onBeforeUnmount
(()
=>
{
onBeforeUnmount
(()
=>
{
...
...
src/utils/domUtils.ts
浏览文件 @
a1d956d3
import
type
{
FunctionArgs
}
from
'@vueuse/core'
;
import
{
upperFirst
}
from
'lodash-es'
;
import
{
upperFirst
}
from
'lodash-es'
;
export
interface
ViewportOffsetResult
{
export
interface
ViewportOffsetResult
{
...
@@ -163,3 +164,17 @@ export function once(el: HTMLElement, event: string, fn: EventListener): void {
...
@@ -163,3 +164,17 @@ export function once(el: HTMLElement, event: string, fn: EventListener): void {
};
};
on
(
el
,
event
,
listener
);
on
(
el
,
event
,
listener
);
}
}
export
function
useRafThrottle
<
T
extends
FunctionArgs
>
(
fn
:
T
):
T
{
let
locked
=
false
;
// @ts-ignore
return
function
(...
args
:
any
[])
{
if
(
locked
)
return
;
locked
=
true
;
window
.
requestAnimationFrame
(()
=>
{
// @ts-ignore
fn
.
apply
(
this
,
args
);
locked
=
false
;
});
};
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论