Fix a few possible crashes by dragging in certain ways, and display on screen when an invalid drop target is hovered over

This commit is contained in:
Andrew Kingston 2021-09-20 08:06:01 +01:00
parent 8460744e68
commit 2c7e93423e
1 changed files with 52 additions and 38 deletions

View File

@ -18,6 +18,10 @@
// Callback when initially starting a drag on a draggable component // Callback when initially starting a drag on a draggable component
const onDragStart = e => { const onDragStart = e => {
if (!e.target?.dataset?.componentId) {
return
}
// Update state // Update state
dragTarget = e.target.dataset.componentId dragTarget = e.target.dataset.componentId
builderStore.actions.selectComponent(dragTarget) builderStore.actions.selectComponent(dragTarget)
@ -32,23 +36,29 @@
// Callback when drag stops (whether dropped or not) // Callback when drag stops (whether dropped or not)
const onDragEnd = e => { const onDragEnd = e => {
// Reset state and styles
dropTarget = null
dropInfo = null
builderStore.actions.setDragging(false)
// Reset opacity style // Reset opacity style
if (dragTarget) {
const child = getDOMNodeForComponent(e.target) const child = getDOMNodeForComponent(e.target)
if (child) { if (child) {
child.style.opacity = "" child.style.opacity = ""
} }
} }
// Reset state and styles
dragTarget = null
dropTarget = null
dropInfo = null
builderStore.actions.setDragging(false)
}
// Callback when on top of a component // Callback when on top of a component
const onDragOver = e => { const onDragOver = e => {
e.preventDefault() // Skip if we aren't validly dragging currently
if (!dragTarget || !dropInfo) {
return
}
if (dropInfo) { e.preventDefault()
const { droppableInside, bounds } = dropInfo const { droppableInside, bounds } = dropInfo
const { top, height } = bounds const { top, height } = bounds
const mouseY = e.clientY const mouseY = e.clientY
@ -85,10 +95,14 @@
} }
} }
} }
}
// Callback when entering a potential drop target // Callback when entering a potential drop target
const onDragEnter = e => { const onDragEnter = e => {
// Skip if we aren't validly dragging currently
if (!dragTarget) {
return
}
const element = e.target.closest("[data-type='component']") const element = e.target.closest("[data-type='component']")
if ( if (
element && element &&