Skip to content

Commit 8a99f57

Browse files
committed
feat(notify): [notify] enhance notification function, add type definition and option interface, optimize code structure
1 parent 87c5286 commit 8a99f57

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

packages/vue/src/notify/index.ts

+56-25
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,71 @@ import NotifyConstructor from './src/pc.vue'
1515
import '@opentiny/vue-theme/notify/index.less'
1616
import { IconSuccess, iconError, IconPrompt, iconWarningTriangle } from '@opentiny/vue-icon'
1717

18+
type NotifyType = 'warning' | 'error' | 'info' | 'success'
19+
type NotifyPosition = 'top-right' | 'bottom-right' | 'top-left' | 'bottom-left'
20+
21+
interface NotifyOptions {
22+
type?: NotifyType
23+
duration?: number
24+
position?: NotifyPosition
25+
statusIcon?: any
26+
offset?: number
27+
verticalOffset?: number
28+
debounceDelay?: number
29+
onClose?: (instance: NotifyInstance) => void
30+
[key: string]: any
31+
}
32+
33+
interface NotifyState {
34+
position: NotifyPosition
35+
verticalOffset: number
36+
visible: boolean
37+
verticalProperty: string
38+
}
39+
40+
interface NotifyInstance {
41+
id: string
42+
$el: HTMLElement
43+
dom: HTMLElement
44+
state: NotifyState
45+
position: NotifyPosition
46+
getZindex: () => number
47+
close: (id: string) => void
48+
}
49+
1850
let seed = 1
19-
let instances = []
51+
let instances: NotifyInstance[] = []
2052

21-
const IconMap = {
53+
const IconMap: Record<NotifyType, any> = {
2254
warning: iconWarningTriangle(),
2355
error: iconError(),
2456
info: IconPrompt(),
2557
success: IconSuccess()
2658
}
2759

28-
const durationMap = {
60+
const durationMap: Record<NotifyType, number> = {
2961
info: 5000,
3062
success: 5000,
3163
warning: 10000,
3264
error: 10000
3365
}
3466

35-
const positionList = ['top-right', 'bottom-right', 'top-left', 'bottom-left']
67+
const positionList: NotifyPosition[] = ['top-right', 'bottom-right', 'top-left', 'bottom-left']
3668

37-
const debounce = (fn, debounceDelay) => {
38-
let timer = null
69+
const debounce = (fn: (options: NotifyOptions) => NotifyInstance, debounceDelay: number) => {
70+
let timer: NodeJS.Timeout | null = null
3971

40-
return async function () {
72+
return async function (this: any, ...args: any[]) {
4173
if (timer) {
4274
clearTimeout(timer)
4375
}
4476

45-
let instance = null
77+
let instance: NotifyInstance | null = null
4678

47-
await new Promise((resolve) => {
79+
await new Promise<void>((resolve) => {
4880
timer = setTimeout(() => {
49-
// eslint-disable-next-line prefer-rest-params
50-
instance = fn.apply(this, arguments)
81+
instance = fn.apply(this, args)
5182
timer = null
52-
5383
resolve()
5484
}, debounceDelay)
5585
})
@@ -58,18 +88,18 @@ const debounce = (fn, debounceDelay) => {
5888
}
5989
}
6090

61-
const notify = (options) => {
62-
if (!~Object.keys(IconMap).indexOf(options.type)) {
91+
const notify = (options: NotifyOptions): NotifyInstance => {
92+
if (!Object.keys(IconMap).includes(options.type as NotifyType)) {
6393
options.type = 'info'
6494
}
6595

66-
options.duration = options.duration ? options.duration : durationMap[options.type]
67-
options.position = !~positionList.indexOf(options.position) ? 'bottom-right' : options.position
68-
!options.statusIcon && options.type && (options.statusIcon = IconMap[options.type])
96+
options.duration = options.duration ? options.duration : durationMap[options.type as NotifyType]
97+
options.position = !positionList.includes(options.position as NotifyPosition) ? 'bottom-right' : options.position
98+
!options.statusIcon && options.type && (options.statusIcon = IconMap[options.type as NotifyType])
6999

70100
const id = 'notify_' + seed++
71101
const userOnClose = options.onClose
72-
const position = options.position
102+
const position = options.position as NotifyPosition
73103

74104
options.onClose = function () {
75105
Notify.close(id, userOnClose)
@@ -79,7 +109,7 @@ const notify = (options) => {
79109
el: document.createElement('div'),
80110
propsData: options,
81111
component: NotifyConstructor
82-
})
112+
}) as NotifyInstance
83113
instance.id = id
84114
document.body.appendChild(instance.$el)
85115

@@ -95,7 +125,7 @@ const notify = (options) => {
95125
instances.push(instance)
96126

97127
instance.dom = instance.$el
98-
instance.dom.style.zIndex = instance.getZindex()
128+
instance.dom.style.zIndex = instance.getZindex().toString()
99129
instance.state.verticalOffset = verticalOffset
100130
instance.state.visible = true
101131

@@ -106,7 +136,7 @@ const notify = (options) => {
106136
return instance
107137
}
108138

109-
const Notify = (options) => {
139+
const Notify = (options: NotifyOptions): NotifyInstance | ((options: NotifyOptions) => Promise<NotifyInstance>) => {
110140
let { debounceDelay } = options
111141

112142
if (debounceDelay) {
@@ -115,10 +145,11 @@ const Notify = (options) => {
115145
return notify(options)
116146
}
117147
}
118-
Notify.close = function (id, userOnClose) {
148+
149+
Notify.close = function (id: string, userOnClose?: (instance: NotifyInstance) => void): void {
119150
let index = -1
120151
let len = instances.length
121-
let instance
152+
let instance: NotifyInstance | undefined
122153

123154
for (let i = 0; i < len; i++) {
124155
let tmp = instances[i]
@@ -135,7 +166,7 @@ Notify.close = function (id, userOnClose) {
135166

136167
typeof userOnClose === 'function' && userOnClose(instance)
137168
let lastHeight = instance.$el.offsetHeight
138-
instance.$el.parentNode.removeChild(instance.$el)
169+
instance.$el.parentNode?.removeChild(instance.$el)
139170
instances.splice(index, 1)
140171

141172
if (len <= 1) {
@@ -161,7 +192,7 @@ Notify.close = function (id, userOnClose) {
161192
})
162193
}
163194

164-
Notify.closeAll = function () {
195+
Notify.closeAll = function (): void {
165196
let copys = instances.slice(0)
166197

167198
copys = copys.reverse()

0 commit comments

Comments
 (0)