1
1
import Notification from './Notification.svelte' ;
2
- import type { SvelteComponent } from 'svelte' ;
2
+ import type { ComponentConstructorOptions , SvelteComponent } from 'svelte' ;
3
3
4
4
type NotifyPlacement = 'right-top' | 'left-top' | 'right-bottom' | 'left-bottom' | 'center' ;
5
5
type NotifyType = 'info' | 'warning' | 'error' | 'success' ;
6
+ type Extend = {
7
+ __notify_index : number ;
8
+ __notify_placement : NotifyPlacement ;
9
+ index : number ;
10
+ show : boolean ;
11
+ } ;
12
+ type NotifyComponent = SvelteComponent < NotifyOptions & Extend > ;
6
13
export interface NotifyOptions {
7
14
customClass ?: string ;
8
15
close ?: boolean ;
9
16
title ?: string ;
10
17
type ?: NotifyType ;
11
18
placement ?: NotifyPlacement ;
12
- target ?: HTMLElement ;
19
+ target ?: Element ;
13
20
onClose ?: ( ) => void ;
14
21
slotTitle ?: string | SvelteComponent ; // svelte sfc or html sting
15
22
slot ?: string | SvelteComponent ; // svelte sfc or html sting
@@ -28,11 +35,11 @@ const defaultNotifyOptions: NotifyOptions = {
28
35
} ;
29
36
30
37
const notifyMap = {
31
- 'right-top' : [ ] ,
32
- center : [ ] ,
33
- 'left-bottom' : [ ] ,
34
- 'right-bottom' : [ ] ,
35
- 'left-top' : [ ]
38
+ 'right-top' : [ ] as NotifyComponent [ ] ,
39
+ center : [ ] as NotifyComponent [ ] ,
40
+ 'left-bottom' : [ ] as NotifyComponent [ ] ,
41
+ 'right-bottom' : [ ] as NotifyComponent [ ] ,
42
+ 'left-top' : [ ] as NotifyComponent [ ]
36
43
} ;
37
44
38
45
const resolveNotifyOptions = ( options : NotifyOptions ) => {
@@ -65,7 +72,7 @@ function mountNotify(
65
72
evt : Record < string , any > ,
66
73
context : Map < string , string | SvelteComponent >
67
74
) {
68
- const notifyArray = notifyMap [ options . placement ] ;
75
+ const notifyArray = notifyMap [ options . placement || 'right-top' ] ;
69
76
let index = 0 ;
70
77
if ( ! notifyArray ) {
71
78
console . error (
@@ -77,13 +84,17 @@ function mountNotify(
77
84
78
85
index = notifyArray . length ;
79
86
const NotificationInst = new Notification ( {
80
- target : options . target ,
87
+ target : options . target ! ,
81
88
props : {
82
89
...options ,
83
90
show : false ,
84
91
index,
85
92
onClose : async ( ) => {
86
- await unmountNotify ( options . placement , NotificationInst , 0 ) ;
93
+ await unmountNotify (
94
+ options . placement || 'right-top' ,
95
+ NotificationInst as unknown as NotifyComponent ,
96
+ 0
97
+ ) ;
87
98
evt . onClose && evt . onClose ( ) ;
88
99
}
89
100
} ,
@@ -94,26 +105,30 @@ function mountNotify(
94
105
95
106
NotificationInst . $set ( { show : true } ) ;
96
107
// auto close
97
- autoUnmountNotify ( options , NotificationInst ) ;
108
+ autoUnmountNotify ( options , NotificationInst as unknown as NotifyComponent ) ;
98
109
// cache NotificationInst
99
- notifyArray . push ( NotificationInst ) ;
110
+ notifyArray . push ( NotificationInst as unknown as NotifyComponent ) ;
100
111
101
112
return NotificationInst ;
102
113
}
103
114
104
- async function autoUnmountNotify ( options : NotifyOptions , inst ) {
115
+ async function autoUnmountNotify ( options : NotifyOptions , inst : NotifyComponent ) {
105
116
if ( options . autoClose ) {
106
- await durationUnmountNotify ( options . placement , inst , options . duration ) ;
117
+ await durationUnmountNotify ( options . placement || 'right-top' , inst , options . duration || 0 ) ;
107
118
}
108
119
}
109
120
110
- async function durationUnmountNotify ( placement : NotifyPlacement , inst , duration : number ) {
121
+ async function durationUnmountNotify (
122
+ placement : NotifyPlacement ,
123
+ inst : NotifyComponent ,
124
+ duration : number
125
+ ) {
111
126
setTimeout ( ( ) => {
112
127
unmountNotify ( placement , inst , 300 ) ;
113
128
} , duration ) ;
114
129
}
115
130
116
- async function unmountNotify ( placement : NotifyPlacement , inst , duration : number ) {
131
+ async function unmountNotify ( placement : NotifyPlacement , inst : NotifyComponent , duration : number ) {
117
132
inst . $set ( { show : false } ) ;
118
133
setTimeout ( ( ) => {
119
134
notifyMap [ placement ] . splice ( inst . __notify_index , 1 ) ;
@@ -123,7 +138,7 @@ async function unmountNotify(placement: NotifyPlacement, inst, duration: number)
123
138
}
124
139
125
140
function updatedNotifyByIndex ( placement : NotifyPlacement ) {
126
- notifyMap [ placement ] . forEach ( ( inst , index ) => {
141
+ notifyMap [ placement ] . forEach ( ( inst : NotifyComponent , index ) => {
127
142
inst . $set ( { index } ) ;
128
143
inst . __notify_index = index ;
129
144
} ) ;
@@ -158,19 +173,19 @@ NotifyFn.success = (options: NotifyOptions = {}) => {
158
173
return mountNotify ( finalOptions , evt , context ) ;
159
174
} ;
160
175
161
- NotifyFn . clear = async ( inst ) => {
176
+ NotifyFn . clear = async ( inst : NotifyComponent ) => {
162
177
await unmountNotify ( inst . __notify_placment , inst , 300 ) ;
163
178
} ;
164
179
165
180
NotifyFn . clearAll = ( ) => {
166
181
Object . keys ( notifyMap ) . forEach ( ( instArr ) => {
167
- notifyMap [ instArr ] . forEach ( ( inst ) => {
182
+ notifyMap [ instArr as NotifyPlacement ] . forEach ( ( inst : NotifyComponent ) => {
168
183
unmountNotify ( inst . __notify_placment , inst , 0 ) ;
169
184
} ) ;
170
185
} ) ;
171
186
} ;
172
187
173
- NotifyFn . update = async ( inst , options : NotifyOptions = { } ) => {
188
+ NotifyFn . update = async ( inst : NotifyComponent , options : NotifyOptions = { } ) => {
174
189
inst . $set ( { ...options } ) ;
175
190
} ;
176
191
0 commit comments