@@ -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