NativeDragSource.ts
1.54 KB
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
import type { DragDropMonitor } from 'dnd-core'
import type { NativeItemConfig } from './nativeTypesConfig.js'
export class NativeDragSource {
public item: any
private config: NativeItemConfig
public constructor(config: NativeItemConfig) {
this.config = config
this.item = {}
this.initializeExposedProperties()
}
private initializeExposedProperties() {
Object.keys(this.config.exposeProperties).forEach((property) => {
Object.defineProperty(this.item, property, {
configurable: true, // This is needed to allow redefining it later
enumerable: true,
get() {
// eslint-disable-next-line no-console
console.warn(
`Browser doesn't allow reading "${property}" until the drop event.`,
)
return null
},
})
})
}
public loadDataTransfer(dataTransfer: DataTransfer | null | undefined): void {
if (dataTransfer) {
const newProperties: PropertyDescriptorMap = {}
Object.keys(this.config.exposeProperties).forEach((property) => {
const propertyFn = this.config.exposeProperties[property]
if (propertyFn != null) {
newProperties[property] = {
value: propertyFn(dataTransfer, this.config.matchesTypes),
configurable: true,
enumerable: true,
}
}
})
Object.defineProperties(this.item, newProperties)
}
}
public canDrag(): boolean {
return true
}
public beginDrag(): any {
return this.item
}
public isDragging(monitor: DragDropMonitor, handle: string): boolean {
return handle === monitor.getSourceId()
}
public endDrag(): void {
// empty
}
}