Skip to content

Commit 343ed56

Browse files
committed
fix: improvise drag-drop by looking for elements with same size
1 parent e9df8b2 commit 343ed56

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,48 @@ function RemoteFunctions(config = {}) {
987987
}
988988
}
989989

990+
/**
991+
* this function is for finding the best target element on where to drop the dragged element
992+
* for ex: div > image...here both the div and image are of the exact same size, then when user is dragging some
993+
* other element, then almost everytime they want to drop it before/after the div and not like div>newEle+img
994+
* @param {Element} target - The current target element
995+
* @returns {Element|null} - The outermost parent with all edges aligned, or null
996+
*/
997+
function _findBestParentTarget(target) {
998+
if (!target) {
999+
return null;
1000+
}
1001+
1002+
const tolerance = 1; // 1px is considered same
1003+
let bestParent = null;
1004+
let currentElement = target;
1005+
let parent = currentElement.parentElement;
1006+
1007+
while (parent) {
1008+
if (parent.hasAttribute("data-brackets-id") && isElementEditable(parent)) {
1009+
const currentRect = currentElement.getBoundingClientRect();
1010+
const parentRect = parent.getBoundingClientRect();
1011+
1012+
// check if all the edges are same
1013+
const topAligned = Math.abs(currentRect.top - parentRect.top) <= tolerance;
1014+
const bottomAligned = Math.abs(currentRect.bottom - parentRect.bottom) <= tolerance;
1015+
const leftAligned = Math.abs(currentRect.left - parentRect.left) <= tolerance;
1016+
const rightAligned = Math.abs(currentRect.right - parentRect.right) <= tolerance;
1017+
1018+
if (topAligned && bottomAligned && leftAligned && rightAligned) {
1019+
// all edges match, we prefer the parent element
1020+
bestParent = parent;
1021+
currentElement = parent;
1022+
} else {
1023+
break;
1024+
}
1025+
}
1026+
parent = parent.parentElement;
1027+
}
1028+
1029+
return bestParent;
1030+
}
1031+
9901032
/**
9911033
* Find the nearest valid drop target when direct elementFromPoint fails
9921034
* @param {number} clientX - x coordinate
@@ -1065,6 +1107,12 @@ function RemoteFunctions(config = {}) {
10651107
}
10661108
}
10671109

1110+
// Check if we should prefer a parent when all edges are aligned
1111+
const bestParent = _findBestParentTarget(target);
1112+
if (bestParent) {
1113+
target = bestParent;
1114+
}
1115+
10681116
// Store original styles before modifying them
10691117
if (target._originalDragBackgroundColor === undefined) {
10701118
target._originalDragBackgroundColor = target.style.backgroundColor;
@@ -1136,6 +1184,12 @@ function RemoteFunctions(config = {}) {
11361184
return;
11371185
}
11381186

1187+
// Check if we should prefer a parent when all edges are aligned
1188+
const bestParent = _findBestParentTarget(target);
1189+
if (bestParent) {
1190+
target = bestParent;
1191+
}
1192+
11391193
// Determine drop position based on container layout and cursor position
11401194
const indicatorType = _getIndicatorType(target);
11411195
const dropZone = _getDropZone(

0 commit comments

Comments
 (0)