Fix row/column detection, add comments and remove old code

This commit is contained in:
Andrew Kingston 2022-10-08 15:18:05 +01:00
parent e9dfc9ad9f
commit 6f73ecdc4c
1 changed files with 17 additions and 95 deletions

View File

@ -76,35 +76,12 @@
}, 0) }, 0)
} }
let lastX
let lastY
let lastTime
const processEvent = (mouseX, mouseY) => { const processEvent = (mouseX, mouseY) => {
if (!dropInfo) { if (!dropInfo) {
return null return null
} }
let { id, parent, node, acceptsChildren, empty } = dropInfo let { id, parent, node, acceptsChildren, empty } = dropInfo
// Debounce by 10px difference
// if (lastX != null && lastY != null) {
// const delta = Math.abs(mouseY - lastY) + Math.abs(mouseX - lastX)
// if (delta < 10) {
// console.log("delta fail")
// return
// }
// }
// lastX = mouseX
// lastY = mouseY
// Debounce by time
// if (Date.now() - (lastTime || 0) < 100) {
// console.log("time fail")
// return
// }
// lastTime = Date.now()
// If we're over something that does not accept children then we go up a // If we're over something that does not accept children then we go up a
// level and consider the mouse position relative to the parent // level and consider the mouse position relative to the parent
if (!acceptsChildren) { if (!acceptsChildren) {
@ -114,7 +91,7 @@
} }
// We're now hovering over something which does accept children. // We're now hovering over something which does accept children.
// If it is empty, just go inside it // If it is empty, just go inside it.
if (empty) { if (empty) {
placeholderInfo = { placeholderInfo = {
parent: id, parent: id,
@ -143,13 +120,15 @@
}) })
// Calculate the variance between each set of positions on the children // Calculate the variance between each set of positions on the children
const variances = Object.keys(childCoords[0]).map(key => { const variances = Object.keys(childCoords[0])
const coords = childCoords.map(x => x[key]) .filter(x => x !== "placeholder")
return { .map(key => {
variance: variance(coords), const coords = childCoords.map(x => x[key])
side: key, return {
} variance: variance(coords),
}) side: key,
}
})
// Sort by variance. The lowest variance position indicates whether we are // Sort by variance. The lowest variance position indicates whether we are
// in a row or column layout // in a row or column layout
@ -157,76 +136,29 @@
return a.variance < b.variance ? -1 : 1 return a.variance < b.variance ? -1 : 1
}) })
const column = ["centerX", "left", "right"].includes(variances[0].side) const column = ["centerX", "left", "right"].includes(variances[0].side)
console.log(column ? "COL" : "ROW")
/** SYMMETRICAL BREAKPOINTS **/ // Calculate breakpoints between child components so we can determine the
// let breakpoints = [] // index to drop the component in.
// for (let i = 0; i < childCoords.length - 1; i++) { // We want to ignore the placeholder from this calculation as it should not
// const child1 = childCoords[i] // be considered a real child of the parent.
// const child2 = childCoords[i + 1]
// let breakpoint
// if (column) {
// const top = Math.min(child1.top, child2.top)
// const bottom = Math.max(child1.bottom, child2.bottom)
// breakpoint = (top + bottom) / 2
// } else {
// const left = Math.min(child1.left, child2.left)
// const right = Math.max(child1.right, child2.right)
// breakpoint = (left + right) / 2
// }
// breakpoints.push(breakpoint)
// }
/** CENTER BREAKPOINTS **/
let breakpoints = childCoords let breakpoints = childCoords
.filter(x => !x.placeholder) .filter(x => !x.placeholder)
.map(x => { .map(x => {
return column ? x.centerY : x.centerX return column ? x.centerY : x.centerX
}) })
/** NEXT EDGE BREAKPOINTS **/
// let breakpoints = []
// for (let i = 0; i < childCoords.length; i++) {
// let breakpoint
// if (column) {
// if (mouseY > childCoords[i].top && mouseY < childCoords[i].bottom) {
// // Inside this container
// if (childCoords[i + 1]) {
// breakpoint = childCoords[i + 1].top
// } else {
// breakpoint = childCoords[i].top
// }
// } else {
// breakpoint =
// mouseY < childCoords[i].bottom
// ? childCoords[i].top
// : childCoords[i].bottom
// }
// } else {
// breakpoint =
// mouseX < childCoords[i].left
// ? childCoords[i].left
// : childCoords[i].right
// }
// breakpoints.push(breakpoint)
// }
// Determine the index to drop the component in // Determine the index to drop the component in
const mousePosition = column ? mouseY : mouseX const mousePosition = column ? mouseY : mouseX
let idx = 0 let idx = 0
while (idx < breakpoints.length && breakpoints[idx] < mousePosition) { while (idx < breakpoints.length && breakpoints[idx] < mousePosition) {
idx++ idx++
} }
// console.log(mousePosition, breakpoints.map(Math.round), idx)
placeholderInfo = { placeholderInfo = {
parent: id, parent: id,
index: idx, index: idx,
} }
} }
const throttledProcessEvent = Utils.throttle(processEvent, 250) const throttledProcessEvent = Utils.throttle(processEvent, 200)
const handleEvent = e => { const handleEvent = e => {
e.preventDefault() e.preventDefault()
@ -235,7 +167,6 @@
// Callback when on top of a component // Callback when on top of a component
const onDragOver = e => { const onDragOver = e => {
// Skip if we aren't validly dragging currently
if (!dragInfo || !dropInfo) { if (!dragInfo || !dropInfo) {
return return
} }
@ -244,20 +175,12 @@
// 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 (!dragInfo) {
if (!dragInfo || !e.target.closest) {
return return
} }
const component = e.target.closest(".component:not(.block)") const component = e.target?.closest?.(".component:not(.block)")
if (component && component.classList.contains("droppable")) { if (component && component.classList.contains("droppable")) {
// Do nothing if this is the same target
if (component.dataset.id === dropInfo?.target) {
return
}
// Precompute and store some info to avoid recalculating everything in
// dragOver
dropInfo = { dropInfo = {
id: component.dataset.id, id: component.dataset.id,
parent: component.dataset.parent, parent: component.dataset.parent,
@ -265,7 +188,6 @@
empty: component.classList.contains("empty"), empty: component.classList.contains("empty"),
acceptsChildren: component.classList.contains("parent"), acceptsChildren: component.classList.contains("parent"),
} }
handleEvent(e) handleEvent(e)
} }
} }