-
Notifications
You must be signed in to change notification settings - Fork 240
/
DataTransfer.ts
172 lines (141 loc) Β· 4.12 KB
/
DataTransfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {createFileList} from './FileList'
// DataTransfer is not implemented in jsdom.
// DataTransfer with FileList is being created by the browser on certain events.
class DataTransferItemStub implements DataTransferItem {
readonly kind: DataTransferItem['kind']
readonly type: DataTransferItem['type']
private file: File | null = null
private data: string | undefined = undefined
constructor(data: string, type: string)
constructor(file: File)
constructor(dataOrFile: string | File, type?: string) {
if (typeof dataOrFile === 'string') {
this.kind = 'string'
this.type = String(type)
this.data = dataOrFile
} else {
this.kind = 'file'
this.type = dataOrFile.type
this.file = dataOrFile
}
}
getAsFile() {
return this.file
}
getAsString(callback: (data: string) => unknown) {
if (typeof this.data === 'string') {
callback(this.data)
}
}
/* istanbul ignore next */
webkitGetAsEntry(): never {
throw new Error('not implemented')
}
}
class DataTransferItemListStub
extends Array<DataTransferItem>
implements DataTransferItemList
{
add(data: string, type: string): DataTransferItem
add(file: File): DataTransferItem
add(...args: never[]) {
const item = new DataTransferItemStub(args[0], args[1])
this.push(item)
return item
}
clear() {
this.splice(0, this.length)
}
remove(index: number) {
this.splice(index, 1)
}
}
function normalizeFormat(format: string) {
return format.toLowerCase()
}
function getTypeMatcher(type: string, exact: boolean) {
const normalizedType = normalizeFormat(type)
const [group, sub] = normalizedType.split('/')
const isGroup = !sub || sub === '*'
return (item: DataTransferItem) => {
const normalizedItemType = normalizeFormat(item.type)
return exact
? normalizedItemType === (isGroup ? group : normalizedType)
: isGroup
? normalizedItemType.startsWith(`${group}/`)
: normalizedItemType === group
}
}
function createDataTransferStub(window: Window & typeof globalThis) {
return new (class DataTransferStub implements DataTransfer {
getData(format: string) {
const match =
this.items.find(getTypeMatcher(format, true)) ??
this.items.find(getTypeMatcher(format, false))
let text = ''
match?.getAsString(t => {
text = t
})
return text
}
setData(format: string, data: string) {
const matchIndex = this.items.findIndex(getTypeMatcher(format, true))
const item = new DataTransferItemStub(data, format) as DataTransferItem
if (matchIndex >= 0) {
this.items.splice(matchIndex, 1, item)
} else {
this.items.push(item)
}
}
clearData(format?: string) {
if (format) {
const matchIndex = this.items.findIndex(getTypeMatcher(format, true))
if (matchIndex >= 0) {
this.items.remove(matchIndex)
}
} else {
this.items.clear()
}
}
dropEffect: DataTransfer['dropEffect'] = 'none'
effectAllowed: DataTransfer['effectAllowed'] = 'uninitialized'
readonly items = new DataTransferItemListStub()
readonly files = createFileList(window, [])
get types() {
const t = []
if (this.files.length) {
t.push('Files')
}
this.items.forEach(i => t.push(i.type))
Object.freeze(t)
return t
}
/* istanbul ignore next */
setDragImage() {}
})()
}
export function createDataTransfer(
window: Window & typeof globalThis,
files: File[] = [],
): DataTransfer {
// Use real DataTransfer if available
const dt =
typeof window.DataTransfer === 'undefined'
? createDataTransferStub(window)
: /* istanbul ignore next */ new window.DataTransfer()
Object.defineProperty(dt, 'files', {get: () => createFileList(window, files)})
return dt
}
export function getBlobFromDataTransferItem(
window: Window & typeof globalThis,
item: DataTransferItem,
) {
if (item.kind === 'file') {
return item.getAsFile() as File
}
let data: string = ''
item.getAsString(s => {
data = s
})
return new window.Blob([data], {type: item.type})
}