Reduce DND snap to edge threshold to prevent unwanted target spamming on the edges of the parent component

This commit is contained in:
Andrew Kingston 2021-09-20 16:11:42 +01:00
parent 495c20f851
commit dc3df10c58
1 changed files with 14 additions and 30 deletions

View File

@ -63,37 +63,21 @@
const { droppableInside, bounds } = dropInfo const { droppableInside, bounds } = dropInfo
const { top, height } = bounds const { top, height } = bounds
const mouseY = e.clientY const mouseY = e.clientY
const elTop = top const snapFactor = droppableInside ? 0.33 : 0.5
const elBottom = top + height const snapLimit = Math.min(40, height * snapFactor)
const edgeLimits = [
// Determine which edge we're nearest as this is needed for potentially Math.round(top + snapLimit),
// any drop mode Math.round(top + height - snapLimit),
let nearestEdge
if (Math.abs(elTop - mouseY) < Math.abs(elBottom - mouseY)) {
nearestEdge = "above"
} else {
nearestEdge = "below"
}
// If not available to drop inside, just check whether we are closer
// to the top or bottom
if (!droppableInside) {
dropInfo.mode = nearestEdge
}
// Otherwise determine whether the user wants to drop inside or at
// either edge
else {
const edgeLimit = Math.min(40, height * 0.33)
const insideLimit = [
Math.round(top + edgeLimit),
Math.round(top + height - edgeLimit),
] ]
if (mouseY >= insideLimit[0] && mouseY <= insideLimit[1]) {
if (mouseY <= edgeLimits[0]) {
dropInfo.mode = "above"
} else if (mouseY >= edgeLimits[1]) {
dropInfo.mode = "below"
} else if (droppableInside) {
dropInfo.mode = "inside" dropInfo.mode = "inside"
} else { } else {
dropInfo.mode = nearestEdge dropInfo.mode = null
}
} }
} }
@ -147,7 +131,7 @@
// Callback when dropping a drag on top of some component // Callback when dropping a drag on top of some component
const onDrop = e => { const onDrop = e => {
e.preventDefault() e.preventDefault()
if (dropInfo) { if (dropInfo?.mode) {
builderStore.actions.moveComponent( builderStore.actions.moveComponent(
dragInfo.target, dragInfo.target,
dropInfo.target, dropInfo.target,