Skip to content

Commit 8e47517

Browse files
committed
fix: drag-drop causing elements to disappear when target is inside source
1 parent 069342b commit 8e47517

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ function RemoteFunctions(config) {
10281028
_dragStartChores(this.element);
10291029
_clearDropMarkers();
10301030
window._currentDraggedElement = this.element;
1031-
1031+
dismissMoreOptionsBox();
10321032
// Add drag image styling
10331033
event.dataTransfer.effectAllowed = "move";
10341034
});

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,27 @@ define(function (require, exports, module) {
343343
}
344344
}
345345

346+
/**
347+
* This function is to make sure that the target element doesn't lie completely within the source element
348+
* because if that is the case then it means that the drag-drop was not performed correctly
349+
*
350+
* @param {Object} source - start/end pos of the source element
351+
* @param {Object} target - start/end pos of the target element
352+
* @returns {Boolean} true if target is fully inside source, false otherwise
353+
*/
354+
function _targetInsideSource(source, target) {
355+
if (
356+
(source.from.line < target.from.line ||
357+
(source.from.line === target.from.line && source.from.ch <= target.from.ch)) &&
358+
(source.to.line > target.to.line ||
359+
(source.to.line === target.to.line && source.to.ch >= target.to.ch))
360+
) {
361+
return true;
362+
}
363+
364+
return false;
365+
}
366+
346367
/**
347368
* This function is responsible for moving an element from one position to another in the source code
348369
* it is called when there is drag-drop in the live preview
@@ -379,6 +400,12 @@ define(function (require, exports, module) {
379400
to: targetRange.endPos
380401
};
381402

403+
// make sure that the target is not within the source
404+
// this would otherwise remove both source and target, breaking the document
405+
if (_targetInsideSource(sourceRangeObj, targetRangeObj)) {
406+
return;
407+
}
408+
382409
const sourceText = editor.getTextBetween(sourceRangeObj.from, sourceRangeObj.to);
383410
let targetIndent = editor.getTextBetween({ line: targetRangeObj.from.line, ch: 0 }, targetRangeObj.from);
384411
if(targetIndent && targetIndent.trim() !== "") { // because indentation should hold no text

0 commit comments

Comments
 (0)