Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions webview/src/shared/components/dragDrop/DragDropContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,42 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
(state: PlotsState) => state.dragAndDrop
)
const draggedOverIdTimeout = useRef<number>(0)
const pickedUp = useRef<boolean>(false)
const dispatch = useDispatch()

const cleanup = useCallback(() => {
immediateDragLeave()
setDraggedOverId('')
setDraggedId('')
setDirection(defaultDragEnterDirection)
if (pickedUp.current) {
immediateDragLeave()
setDraggedOverId('')
setDraggedId('')
setDirection(defaultDragEnterDirection)
pickedUp.current = false
dispatch(changeRef(undefined))
}
}, [
setDraggedOverId,
setDirection,
defaultDragEnterDirection,
immediateDragLeave
immediateDragLeave,
dispatch
])

useEffect(() => {
const onMove = (e: MouseEvent) => {
if (!e.buttons && pickedUp.current) {
cleanup()
}
}
document.addEventListener('mousemove', onMove)
return () => {
clearTimeout(draggedOverIdTimeout.current)
document.removeEventListener('mousemove', onMove)
}
}, [])
}, [cleanup])

useEffect(() => {
cleanup()
}, [order, cleanup])
}, [cleanup])

useLayoutEffect(() => {
onLayoutChange?.()
Expand All @@ -142,6 +155,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
}

const handleDragStart = (e: DragEvent<HTMLElement>) => {
pickedUp.current = true
const { id } = e.currentTarget
const idx = order.indexOf(id)
let toIdx = shouldShowOnDrag ? idx : idx + 1
Expand Down Expand Up @@ -189,6 +203,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
dispatch(changeRef(undefined))

onDrop?.(oldDraggedId, draggedRef?.group || '', group, droppedIndex)
cleanup()
}

const handleOnDrop = (e: DragEvent<HTMLElement>) => {
Expand All @@ -201,7 +216,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
}, 0)
const dragged = draggedRef.itemId
if (dragged === draggedOverId) {
dispatch(changeRef(undefined))
cleanup()
return
}
const isNew = !order.includes(dragged)
Expand Down Expand Up @@ -246,11 +261,6 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
}
}

const handleDragEnd = () => {
dispatch(changeRef(undefined))
cleanup()
}

const handleDragLeave = () => {
deferedDragLeave()
}
Expand All @@ -262,7 +272,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
ref={(draggable as any).ref}
{...draggable.props}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onDragEnd={cleanup}
onDragOver={handleDragOver}
onDragEnter={handleDragEnter}
onDrop={handleOnDrop}
Expand Down