1
- import type UI5Element from "../../UI5Element.js" ;
2
1
import type MovePlacement from "../../types/MovePlacement.js" ;
3
2
import MultipleDragGhostCss from "../../generated/css/MultipleDragGhost.css.js" ;
4
3
@@ -10,55 +9,16 @@ import {
10
9
11
10
const MIN_MULTI_DRAG_COUNT = 2 ;
12
11
13
- let customDragElementPromise : Promise < HTMLElement > | null = null ;
14
12
let draggedElement : HTMLElement | null = null ;
15
- let globalHandlersAttached = false ;
16
- const subscribers = new Set < UI5Element > ( ) ;
17
- const selfManagedDragAreas = new Set < HTMLElement | ShadowRoot > ( ) ;
18
13
19
- const ondragstart = ( e : DragEvent ) => {
20
- if ( ! e . dataTransfer || ! ( e . target instanceof HTMLElement ) ) {
21
- return ;
22
- }
23
-
24
- if ( ! selfManagedDragAreas . has ( e . target ) ) {
25
- draggedElement = e . target ;
26
- }
27
-
28
- handleMultipleDrag ( e ) ;
29
- } ;
30
-
31
- const handleMultipleDrag = async ( e : DragEvent ) => {
32
- if ( ! customDragElementPromise || ! e . dataTransfer ) {
33
- return ;
34
- }
35
- const dragElement = await customDragElementPromise ;
36
- // Add to document body temporarily
37
- document . body . appendChild ( dragElement ) ;
38
-
39
- e . dataTransfer . setDragImage ( dragElement , 0 , 0 ) ;
40
-
41
- // Clean up the temporary element after the drag operation starts
42
- requestAnimationFrame ( ( ) => {
43
- dragElement . remove ( ) ;
44
- } ) ;
45
- } ;
46
-
47
- const ondragend = ( ) => {
48
- draggedElement = null ;
49
- customDragElementPromise = null ;
14
+ const setDraggedElement = ( element : HTMLElement | null ) => {
15
+ draggedElement = element ;
50
16
} ;
51
17
52
- const ondrop = ( ) => {
18
+ const clearDraggedElement = ( ) => {
53
19
draggedElement = null ;
54
- customDragElementPromise = null ;
55
20
} ;
56
21
57
- const setDraggedElement = ( element : HTMLElement | null ) => {
58
- draggedElement = element ;
59
- } ;
60
- type SetDraggedElementFunction = typeof setDraggedElement ;
61
-
62
22
const getDraggedElement = ( ) => {
63
23
return draggedElement ;
64
24
} ;
@@ -86,58 +46,27 @@ const createDefaultMultiDragElement = async (count: number): Promise<HTMLElement
86
46
* @param {DragEvent } e - The drag event that triggered the operation.
87
47
* @public
88
48
*/
89
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
90
- const startMultipleDrag = ( count : number , e : DragEvent ) : void => {
49
+ const startMultipleDrag = async ( count : number , e : DragEvent ) => {
91
50
if ( count < MIN_MULTI_DRAG_COUNT ) {
92
51
console . warn ( `Cannot start multiple drag with count ${ count } . Minimum is ${ MIN_MULTI_DRAG_COUNT } .` ) ; // eslint-disable-line
93
52
return ;
94
53
}
95
54
96
- customDragElementPromise = createDefaultMultiDragElement ( count ) ;
97
- } ;
98
-
99
- const attachGlobalHandlers = ( ) => {
100
- if ( globalHandlersAttached ) {
55
+ if ( ! e . dataTransfer ) {
101
56
return ;
102
57
}
103
58
104
- document . body . addEventListener ( "dragstart" , ondragstart ) ;
105
- document . body . addEventListener ( "dragend" , ondragend ) ;
106
- document . body . addEventListener ( "drop" , ondrop ) ;
107
- globalHandlersAttached = true ;
108
- } ;
109
-
110
- const detachGlobalHandlers = ( ) => {
111
- document . body . removeEventListener ( "dragstart" , ondragstart ) ;
112
- document . body . removeEventListener ( "dragend" , ondragend ) ;
113
- document . body . removeEventListener ( "drop" , ondrop ) ;
114
- globalHandlersAttached = false ;
115
- } ;
116
-
117
- const subscribe = ( subscriber : UI5Element ) => {
118
- subscribers . add ( subscriber ) ;
119
-
120
- if ( ! globalHandlersAttached ) {
121
- attachGlobalHandlers ( ) ;
122
- }
123
- } ;
59
+ const customDragElement = await createDefaultMultiDragElement ( count ) ;
124
60
125
- const unsubscribe = ( subscriber : UI5Element ) => {
126
- subscribers . delete ( subscriber ) ;
127
-
128
- if ( subscribers . size === 0 && globalHandlersAttached ) {
129
- detachGlobalHandlers ( ) ;
130
- }
131
- } ;
132
-
133
- const addSelfManagedArea = ( area : HTMLElement | ShadowRoot ) => {
134
- selfManagedDragAreas . add ( area ) ;
61
+ // Add to document body temporarily
62
+ document . body . appendChild ( customDragElement ) ;
135
63
136
- return setDraggedElement ;
137
- } ;
64
+ e . dataTransfer . setDragImage ( customDragElement , 0 , 0 ) ;
138
65
139
- const removeSelfManagedArea = ( area : HTMLElement | ShadowRoot ) => {
140
- selfManagedDragAreas . delete ( area ) ;
66
+ // Clean up the temporary element after the drag operation starts
67
+ requestAnimationFrame ( ( ) => {
68
+ customDragElement . remove ( ) ;
69
+ } ) ;
141
70
} ;
142
71
143
72
type DragAndDropSettings = {
@@ -163,10 +92,8 @@ type MoveEventDetail = {
163
92
} ;
164
93
165
94
const DragRegistry = {
166
- subscribe,
167
- unsubscribe,
168
- addSelfManagedArea,
169
- removeSelfManagedArea,
95
+ setDraggedElement,
96
+ clearDraggedElement,
170
97
getDraggedElement,
171
98
startMultipleDrag,
172
99
} ;
@@ -175,8 +102,8 @@ export default DragRegistry;
175
102
export {
176
103
startMultipleDrag ,
177
104
} ;
105
+
178
106
export type {
179
- SetDraggedElementFunction ,
180
107
DragAndDropSettings ,
181
108
MoveEventDetail ,
182
109
} ;
0 commit comments