-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclipboarditem.ts
47 lines (41 loc) · 1.44 KB
/
clipboarditem.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
type ClipboardItems = Record<string, string | Blob | PromiseLike<string | Blob>>
const records = new WeakMap<ClipboardItem, ClipboardItems>()
const presentationStyles = new WeakMap<ClipboardItem, PresentationStyle>()
export class ClipboardItem {
constructor(items: ClipboardItems, options: ClipboardItemOptions | undefined = {}) {
if (Object.keys(items).length === 0) throw new TypeError('Empty dictionary argument')
records.set(this, items)
presentationStyles.set(this, options.presentationStyle || 'unspecified')
}
get presentationStyle(): PresentationStyle {
return presentationStyles.get(this) || 'unspecified'
}
get types() {
return Object.freeze(Object.keys(records.get(this) || {}))
}
async getType(type: string): Promise<Blob> {
const record = records.get(this)
if (record && type in record) {
const item = await record[type]!
if (typeof item === 'string') return new Blob([item], {type})
return item
}
throw new DOMException("Failed to execute 'getType' on 'ClipboardItem': The type was not found", 'NotFoundError')
}
}
export function isSupported(): boolean {
try {
new globalThis.ClipboardItem({'text/plain': Promise.resolve('')})
return true
} catch {
return false
}
}
export function isPolyfilled(): boolean {
return globalThis.ClipboardItem === ClipboardItem
}
export function apply(): void {
if (!isSupported()) {
globalThis.ClipboardItem = ClipboardItem
}
}