@@ -74,63 +74,65 @@ function getTypeMatcher(type: string, exact: boolean) {
74
74
}
75
75
}
76
76
77
- class DataTransferStub implements DataTransfer {
78
- getData ( format : string ) {
79
- const match =
80
- this . items . find ( getTypeMatcher ( format , true ) ) ??
81
- this . items . find ( getTypeMatcher ( format , false ) )
82
-
83
- let text = ''
84
- match ?. getAsString ( t => {
85
- text = t
86
- } )
87
-
88
- return text
89
- }
90
-
91
- setData ( format : string , data : string ) {
92
- const matchIndex = this . items . findIndex ( getTypeMatcher ( format , true ) )
93
-
94
- const item = new DataTransferItemStub ( data , format ) as DataTransferItem
95
- if ( matchIndex >= 0 ) {
96
- this . items . splice ( matchIndex , 1 , item )
97
- } else {
98
- this . items . push ( item )
77
+ function createDataTransferStub ( window : Window & typeof globalThis ) {
78
+ return new ( class DataTransferStub implements DataTransfer {
79
+ getData ( format : string ) {
80
+ const match =
81
+ this . items . find ( getTypeMatcher ( format , true ) ) ??
82
+ this . items . find ( getTypeMatcher ( format , false ) )
83
+
84
+ let text = ''
85
+ match ?. getAsString ( t => {
86
+ text = t
87
+ } )
88
+
89
+ return text
99
90
}
100
- }
101
91
102
- clearData ( format ?: string ) {
103
- if ( format ) {
92
+ setData ( format : string , data : string ) {
104
93
const matchIndex = this . items . findIndex ( getTypeMatcher ( format , true ) )
105
94
95
+ const item = new DataTransferItemStub ( data , format ) as DataTransferItem
106
96
if ( matchIndex >= 0 ) {
107
- this . items . remove ( matchIndex )
97
+ this . items . splice ( matchIndex , 1 , item )
98
+ } else {
99
+ this . items . push ( item )
108
100
}
109
- } else {
110
- this . items . clear ( )
111
101
}
112
- }
113
-
114
- dropEffect : DataTransfer [ 'dropEffect' ] = 'none'
115
- effectAllowed : DataTransfer [ 'effectAllowed' ] = 'uninitialized'
116
102
117
- readonly items = new DataTransferItemListStub ( )
118
- readonly files = createFileList ( [ ] )
103
+ clearData ( format ?: string ) {
104
+ if ( format ) {
105
+ const matchIndex = this . items . findIndex ( getTypeMatcher ( format , true ) )
119
106
120
- get types ( ) {
121
- const t = [ ]
122
- if ( this . files . length ) {
123
- t . push ( 'Files' )
107
+ if ( matchIndex >= 0 ) {
108
+ this . items . remove ( matchIndex )
109
+ }
110
+ } else {
111
+ this . items . clear ( )
112
+ }
124
113
}
125
- this . items . forEach ( i => t . push ( i . type ) )
126
114
127
- Object . freeze ( t )
115
+ dropEffect : DataTransfer [ 'dropEffect' ] = 'none'
116
+ effectAllowed : DataTransfer [ 'effectAllowed' ] = 'uninitialized'
128
117
129
- return t
130
- }
118
+ readonly items = new DataTransferItemListStub ( )
119
+ readonly files = createFileList ( window , [ ] )
131
120
132
- /* istanbul ignore next */
133
- setDragImage ( ) { }
121
+ get types ( ) {
122
+ const t = [ ]
123
+ if ( this . files . length ) {
124
+ t . push ( 'Files' )
125
+ }
126
+ this . items . forEach ( i => t . push ( i . type ) )
127
+
128
+ Object . freeze ( t )
129
+
130
+ return t
131
+ }
132
+
133
+ /* istanbul ignore next */
134
+ setDragImage ( ) { }
135
+ } ) ( )
134
136
}
135
137
136
138
export function createDataTransfer (
@@ -140,10 +142,10 @@ export function createDataTransfer(
140
142
// Use real DataTransfer if available
141
143
const dt =
142
144
typeof window . DataTransfer === 'undefined'
143
- ? ( new DataTransferStub ( ) as DataTransfer )
145
+ ? createDataTransferStub ( window )
144
146
: /* istanbul ignore next */ new window . DataTransfer ( )
145
147
146
- Object . defineProperty ( dt , 'files' , { get : ( ) => createFileList ( files ) } )
148
+ Object . defineProperty ( dt , 'files' , { get : ( ) => createFileList ( window , files ) } )
147
149
148
150
return dt
149
151
}
0 commit comments