@@ -9,36 +9,33 @@ import React, {
99 useLayoutEffect
1010} from 'react'
1111import { useDispatch , useSelector } from 'react-redux'
12- import { DragEnterDirection , getDragEnterDirection } from './util'
12+ import {
13+ DragEnterDirection ,
14+ getDragEnterDirection ,
15+ isEnteringAfter ,
16+ isExactGroup ,
17+ isSameGroup
18+ } from './util'
1319import { changeRef , setDraggedOverGroup } from './dragDropSlice'
1420import styles from './styles.module.scss'
1521import { DropTarget } from './DropTarget'
16- import { getIDIndex , getIDWithoutIndex } from '../../../util/ids'
22+ import { DragDropItemWithTarget } from './DragDropItemWithTarget'
23+ import { DragDropItem } from './DragDropItem'
24+ import { getIDIndex } from '../../../util/ids'
1725import { Any } from '../../../util/objects'
1826import { PlotsState } from '../../../plots/store'
1927import { getStyleProperty } from '../../../util/styles'
2028import { idToNode } from '../../../util/helpers'
2129import { useDeferedDragLeave } from '../../hooks/useDeferedDragLeave'
2230
23- const AFTER_DIRECTIONS = new Set ( [
24- DragEnterDirection . RIGHT ,
25- DragEnterDirection . BOTTOM
26- ] )
27-
2831const orderIdxTune = ( direction : DragEnterDirection , isAfter : boolean ) => {
29- if ( AFTER_DIRECTIONS . has ( direction ) ) {
32+ if ( isEnteringAfter ( direction ) ) {
3033 return isAfter ? 0 : 1
3134 }
3235
3336 return isAfter ? - 1 : 0
3437}
3538
36- export const isSameGroup = ( group1 ?: string , group2 ?: string ) =>
37- getIDWithoutIndex ( group1 ) === getIDWithoutIndex ( group2 )
38-
39- const isExactGroup = ( group1 ?: string , group1Alt ?: string , group2 ?: string ) =>
40- group1 === group2 || group1Alt === group2
41-
4239const setStyle = ( elem : HTMLElement , style : CSSProperties , reset ?: boolean ) => {
4340 for ( const [ rule , value ] of Object . entries ( style ) ) {
4441 elem . style [ getStyleProperty ( rule ) ] = reset ? '' : value
@@ -199,7 +196,6 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
199196 const dragged = newOrder [ draggedIndex ]
200197 newOrder . splice ( draggedIndex , 1 )
201198 newOrder . splice ( droppedIndex , 0 , dragged )
202-
203199 setOrder ( newOrder )
204200 dispatch ( changeRef ( undefined ) )
205201
@@ -266,26 +262,6 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
266262 deferedDragLeave ( )
267263 }
268264
269- const buildItem = ( id : string , draggable : JSX . Element ) => (
270- < draggable . type
271- key = { draggable . key }
272- // eslint-disable-next-line @typescript-eslint/no-explicit-any
273- ref = { ( draggable as any ) . ref }
274- { ...draggable . props }
275- onDragStart = { handleDragStart }
276- onDragEnd = { cleanup }
277- onDragOver = { handleDragOver }
278- onDragEnter = { handleDragEnter }
279- onDrop = { handleOnDrop }
280- onDragLeave = { handleDragLeave }
281- draggable = { ! disabledDropIds . includes ( id ) }
282- style = {
283- ( ! shouldShowOnDrag && id === draggedId && { display : 'none' } ) ||
284- draggable . props . style
285- }
286- />
287- )
288-
289265 const getDropTargetClassNames = ( isEnteringRight : boolean ) =>
290266 shouldShowOnDrag
291267 ? cx ( styles . dropTargetWhenShowingOnDrag , {
@@ -300,7 +276,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
300276 wrapper : JSX . Element
301277 ) => (
302278 < DropTarget
303- key = " drop-target"
279+ key = { ` drop-target- ${ id } ` }
304280 onDragOver = { handleDragOver }
305281 onDragLeave = { handleDragLeave }
306282 onDrop = { handleOnDrop }
@@ -312,50 +288,63 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
312288 </ DropTarget >
313289 )
314290
315- const createItemWithDropTarget = ( id : string , item : JSX . Element ) => {
316- const isEnteringAfter = AFTER_DIRECTIONS . has ( direction )
317- const target = isExactGroup ( draggedOverGroup , draggedRef ?. group , group )
318- ? getTarget (
319- id ,
320- isEnteringAfter ,
321- shouldShowOnDrag ? < div /> : < item . type />
322- )
323- : null
324-
325- const itemWithTag = shouldShowOnDrag ? (
326- < div key = "item" { ...item . props } />
327- ) : (
328- item
329- )
330- const block = isEnteringAfter
331- ? [ itemWithTag , target ]
332- : [ target , itemWithTag ]
333-
334- return shouldShowOnDrag ? (
335- < item . type key = { item . key } className = { styles . newBlockWhenShowingOnDrag } >
336- { block }
337- </ item . type >
338- ) : (
339- block
340- )
341- }
291+ const wrappedItems = items
292+ . map ( draggable => {
293+ const id = draggable . props . id
294+ const isDraggedOver =
295+ id === draggedOverId && ( hoveringSomething || ! parentDraggedOver )
296+
297+ const item = (
298+ < DragDropItem
299+ key = { draggable . key }
300+ cleanup = { cleanup }
301+ disabledDropIds = { disabledDropIds }
302+ draggable = { draggable }
303+ id = { id }
304+ onDragEnter = { handleDragEnter }
305+ onDragLeave = { handleDragLeave }
306+ onDragOver = { handleDragOver }
307+ onDragStart = { handleDragStart }
308+ onDrop = { handleOnDrop }
309+ draggedId = { draggedId }
310+ shouldShowOnDrag = { shouldShowOnDrag }
311+ isDiv = { isDraggedOver && shouldShowOnDrag }
312+ />
313+ )
342314
343- const wrappedItems = items . flatMap ( draggable => {
344- const id = draggable ?. props ?. id
345- const item = id && buildItem ( id , draggable )
315+ if ( isDraggedOver ) {
316+ const isAfter = isEnteringAfter ( direction )
317+ const target = isExactGroup ( draggedOverGroup , draggedRef ?. group , group )
318+ ? getTarget (
319+ id ,
320+ isAfter ,
321+ shouldShowOnDrag ? < div /> : < draggable . type />
322+ )
323+ : null
324+
325+ return (
326+ < DragDropItemWithTarget
327+ key = { draggable . key }
328+ isAfter = { isAfter }
329+ dropTarget = { target }
330+ shouldShowOnDrag = { shouldShowOnDrag }
331+ draggable = { draggable }
332+ >
333+ { item }
334+ </ DragDropItemWithTarget >
335+ )
336+ }
346337
347- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
348- return id === draggedOverId && ( hoveringSomething || ! parentDraggedOver )
349- ? createItemWithDropTarget ( id , item )
350- : item
351- } )
338+ return item
339+ } )
340+ . filter ( Boolean ) as JSX . Element [ ]
352341
353342 if (
354343 isSameGroup ( draggedRef ?. group , group ) &&
355344 ! hoveringSomething &&
356345 parentDraggedOver
357346 ) {
358- const lastItem = wrappedItems [ wrappedItems . length - 1 ]
347+ const lastItem = items [ items . length - 1 ]
359348 wrappedItems . push ( getTarget ( lastItem . props . id , false , < lastItem . type /> ) )
360349 }
361350
0 commit comments