@@ -62,6 +62,11 @@ export function initializeDragContainer(
6262
6363 if ( element ) {
6464 const tabsContainer = queryClosest ( e . target , ".vuuDragContainer" , true ) ;
65+ const gridLayoutItem = queryClosest (
66+ tabsContainer ,
67+ ".vuuGridLayoutItem" ,
68+ true ,
69+ ) ;
6570 const tabIndex = getDataIndex ( element ) ;
6671 const label = getDataLabel ( element ) ;
6772 const isSelectedTab =
@@ -70,44 +75,57 @@ export function initializeDragContainer(
7075 const { gridLayoutItemId } = element . dataset ;
7176 e . stopPropagation ( ) ;
7277 dragContext . beginDrag ( e , {
73- id : gridLayoutItemId ?? "" ,
7478 element,
7579 isSelectedTab,
76- label,
80+ tab : { id : gridLayoutItemId ?? "" , label } ,
7781 tabIndex,
78- tabsId : tabsContainer . id ,
82+ tabsId : gridLayoutItem . id ,
7983 type : "tabbed-component" ,
8084 } ) ;
8185
8286 requestAnimationFrame ( ( ) => {
8387 spaceMan . dragStart ( tabIndex ) ;
84- const gridLayoutItem = queryClosest (
85- tabsContainer ,
86- ".vuuGridLayoutItem" ,
87- ) ;
88- if ( gridLayoutItem ) {
89- dragContext . detachTab ( gridLayoutItem . id , tabIndex , label ) ;
90- }
88+ dragContext . detachTab ( gridLayoutItem . id , tabIndex , label ) ;
9189 } ) ;
9290 }
9391 } ;
9492
9593 const onDragEnter = ( e : DragEvent ) => {
96- // console.log(`[drag-drop-listeners] onDragEnter`);
97- // don't think we need this check. DragEnter wouldn't be firing
98- // if we weren't in a drop zone
99- e . stopPropagation ( ) ;
10094 // we should really mark drop targets
10195 const dropTarget = getDraggableEl ( e . target ) ;
96+ console . log ( `[drag-drop-listeners] onDragEnter` , {
97+ target : e . target ,
98+ dropTarget,
99+ } ) ;
100+ // We always revent default here, that way useAsDropItem will know that another drag handler
101+ // is responsible for this area
102+ e . preventDefault ( ) ;
102103 const { dragSource, x, y } = dragContext ;
103- if ( dropTarget && sourceIsTabbedComponent ( dragSource ) ) {
104+ if ( dropTarget ) {
105+ console . log (
106+ `[drag-drop-listeners] onDragEnter ${ dropTarget . className } preventDefault` ,
107+ ) ;
104108 const indexOfDropTarget = getDataIndex ( dropTarget ) ;
109+ if ( sourceIsTabbedComponent ( dragSource ) ) {
110+ if (
111+ indexOfDropTarget !== - 1 &&
112+ ( indexOfDropTarget !== dragSource ?. tabIndex ||
113+ isRemoteContainer ( dragSource . element , dropTarget ) )
114+ ) {
115+ const direction =
116+ orientation === "horizontal"
117+ ? e . clientX > x
118+ ? "fwd"
119+ : "bwd"
120+ : e . clientY > y
121+ ? "fwd"
122+ : "bwd" ;
123+
124+ spaceMan . dragEnter ( indexOfDropTarget , direction ) ;
125+ }
126+ } else {
127+ console . log ( `draging a componnet over a tabstrip` ) ;
105128
106- if (
107- indexOfDropTarget !== - 1 &&
108- ( indexOfDropTarget !== dragSource ?. tabIndex ||
109- isRemoteContainer ( dragSource . element , dropTarget ) )
110- ) {
111129 const direction =
112130 orientation === "horizontal"
113131 ? e . clientX > x
@@ -129,35 +147,55 @@ export function initializeDragContainer(
129147 } ;
130148
131149 const onDragLeave = ( e : DragEvent ) => {
132- // TODO include container #id in query
133- const container = queryClosest ( e . relatedTarget , ".vuuDragContainer" ) ;
150+ console . log ( `[drag-drop-listeners] onDragleave ${ spaceMan . id } ` ) ;
151+ // Have we dragged the draggable item right out of the parent drag container
152+ const container = queryClosest ( e . relatedTarget , `#${ spaceMan . id } ` ) ;
134153 if ( container === null ) {
135154 spaceMan . leaveDragContainer ( ) ;
136155 }
137156 } ;
138157
139158 const onDrop = async ( e : DragEvent ) => {
140- const { clientX, clientY } = e ;
141- e . stopPropagation ( ) ;
142- // console.log(
143- // `[drag-drop-listeners] onDrop toIndex: ${spaceMan.toIndex}
144- // ${dropPosition.position} targetTabId ${dropPosition.target}`,
145- // );
146- // important we capture this before calling spaceMan.drop
147- const { dropPosition } = spaceMan ;
148- await spaceMan . drop ( clientX , clientY ) ;
149- dragContext . drop ( {
150- toId : dragId ,
151- tabsId : dragId ,
152- dropPosition,
153- } ) ;
159+ if ( e . defaultPrevented ) {
160+ console . log ( `[drag-drop-listeners] onDrop ${ dragId } - already handled` ) ;
161+ spaceMan . cleanup ( ) ;
162+ } else {
163+ const { clientX, clientY } = e ;
164+ e . stopPropagation ( ) ;
154165
155- if ( dragContext . dragSource ) {
156- focusDroppedTab ( dragId , dragContext . dragSource . label ) ;
166+ // important we capture this before calling spaceMan.drop
167+ const { dropPosition } = spaceMan ;
168+
169+ console . log (
170+ `[drag-drop-listeners] onDrop #${ dragId } ${ dropPosition ?. position } targetTabId ${ dropPosition ?. target } ` ,
171+ ) ;
172+ if ( dropPosition ) {
173+ await spaceMan . drop ( clientX , clientY ) ;
174+ dragContext . drop ( {
175+ toId : dragId ,
176+ tabsId : dragId ,
177+ dropPosition,
178+ } ) ;
179+
180+ if ( dragContext . dragSource ) {
181+ focusDroppedTab ( dragId , dragContext . dragSource . label ) ;
182+ }
183+ } else {
184+ console . log ( `[drag-drop-listeners] onDrop, drop is elsewhere ` ) ;
185+ spaceMan . cleanup ( ) ;
186+ }
157187 }
158188 } ;
159189 const onDragEnd = ( ) => {
160- console . log ( "[drag-drop-listeners] onDragEnd" ) ;
190+ console . log ( `[drag-drop-listeners#${ dragId } ] onDragEnd` , {
191+ dragSource : dragContext . dragSource ,
192+ } ) ;
193+ if (
194+ sourceIsTabbedComponent ( dragContext . dragSource ) &&
195+ dragContext . dragSource . tabsId === dragId
196+ ) {
197+ console . log ( `[drag-drop-listeners#${ dragId } ] do we need to cleanup` ) ;
198+ }
161199 if ( ! dragContext . dropped ) {
162200 spaceMan . dragEnd ( ) ;
163201 }
@@ -180,17 +218,17 @@ export function initializeDragContainer(
180218 containerEl ?. addEventListener ( "dragenter" , onDragEnter ) ;
181219 containerEl ?. addEventListener ( "dragleave" , onDragLeave ) ;
182220 containerEl ?. addEventListener ( "dragover" , onDragOver ) ;
183- containerEl ? .addEventListener ( "drop" , onDrop ) ;
184- containerEl ? .addEventListener ( "dragend" , onDragEnd ) ;
221+ document . body . addEventListener ( "drop" , onDrop ) ;
222+ document . body . addEventListener ( "dragend" , onDragEnd ) ;
185223
186224 function cleanUp ( ) {
187225 containerEl ?. removeEventListener ( "mousedown" , onMouseDown ) ;
188226 containerEl ?. removeEventListener ( "dragstart" , onDragStart ) ;
189227 containerEl ?. removeEventListener ( "dragenter" , onDragEnter ) ;
190228 containerEl ?. removeEventListener ( "dragleave" , onDragLeave ) ;
191229 containerEl ?. removeEventListener ( "dragover" , onDragOver ) ;
192- containerEl ? .removeEventListener ( "drop" , onDrop ) ;
193- containerEl ? .removeEventListener ( "dragend" , onDragEnd ) ;
230+ document . body . removeEventListener ( "drop" , onDrop ) ;
231+ document . body . removeEventListener ( "dragend" , onDragEnd ) ;
194232 }
195233
196234 return cleanUp ;
0 commit comments