blob: 2167a6abb808a1e8259084b656569e728bf31d99 (
plain)
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
|
/**
* @typedef {Object} ClipboardContent
* @property {('file'|'text'|'none')} kind content type (file or text)
* @property {File[] | FileList | undefined} files files array
* @property {String | undefined} text text content
*/
/**
* Fetches the clipboard content from the event.
*
* @param {ClipboardEvent} event native event
* @returns {ClipboardContent}
*/
const fetchClipboardContent = function(event) {
const clipboardData = event.clipboardData
if (!clipboardData) {
return { kind: 'none' }
}
if (clipboardData.files && clipboardData.files.length > 0) {
return { kind: 'file', files: clipboardData.files }
}
if (clipboardData.items && clipboardData.items.length > 0) {
const files = []
for (let i = 0; i < clipboardData.items.length; i++) {
if (clipboardData.items[i].kind === 'file') {
files.push(clipboardData.items[i].getAsFile())
}
}
if (files.length > 0) {
return { kind: 'file', files }
}
}
const text = clipboardData.getData('text/plain')
return { kind: 'text', text }
}
export {
fetchClipboardContent,
}
|