@@ -87,10 +87,10 @@ export class DragRef<T = any> {
87
87
private _pickupPositionOnPage : Point ;
88
88
89
89
/**
90
- * Reference to the element that comes after the draggable in the DOM, at the time
91
- * it was picked up. Used for restoring its initial position when it's dropped .
90
+ * Anchor node used to save the place in the DOM where the element was
91
+ * picked up so that it can be restored at the end of the drag sequence .
92
92
*/
93
- private _nextSibling : Node | null ;
93
+ private _anchor : Comment ;
94
94
95
95
/**
96
96
* CSS `transform` applied to the element when it isn't being dragged. We need a
@@ -374,9 +374,10 @@ export class DragRef<T = any> {
374
374
if ( this . isDragging ( ) ) {
375
375
// Since we move out the element to the end of the body while it's being
376
376
// dragged, we have to make sure that it's removed if it gets destroyed.
377
- removeElement ( this . _rootElement ) ;
377
+ removeNode ( this . _rootElement ) ;
378
378
}
379
379
380
+ removeNode ( this . _anchor ) ;
380
381
this . _destroyPreview ( ) ;
381
382
this . _destroyPlaceholder ( ) ;
382
383
this . _dragDropRegistry . removeDragItem ( this ) ;
@@ -394,7 +395,7 @@ export class DragRef<T = any> {
394
395
this . _dropContainer = undefined ;
395
396
this . _resizeSubscription . unsubscribe ( ) ;
396
397
this . _boundaryElement = this . _rootElement = this . _placeholderTemplate =
397
- this . _previewTemplate = this . _nextSibling = null ! ;
398
+ this . _previewTemplate = this . _anchor = null ! ;
398
399
}
399
400
400
401
/** Checks whether the element is currently being dragged. */
@@ -481,7 +482,7 @@ export class DragRef<T = any> {
481
482
/** Destroys the preview element and its ViewRef. */
482
483
private _destroyPreview ( ) {
483
484
if ( this . _preview ) {
484
- removeElement ( this . _preview ) ;
485
+ removeNode ( this . _preview ) ;
485
486
}
486
487
487
488
if ( this . _previewRef ) {
@@ -494,7 +495,7 @@ export class DragRef<T = any> {
494
495
/** Destroys the placeholder element and its ViewRef. */
495
496
private _destroyPlaceholder ( ) {
496
497
if ( this . _placeholder ) {
497
- removeElement ( this . _placeholder ) ;
498
+ removeNode ( this . _placeholder ) ;
498
499
}
499
500
500
501
if ( this . _placeholderRef ) {
@@ -675,19 +676,19 @@ export class DragRef<T = any> {
675
676
676
677
if ( this . _dropContainer ) {
677
678
const element = this . _rootElement ;
678
-
679
- // Grab the `nextSibling` before the preview and placeholder
680
- // have been created so we don't get the preview by accident.
681
- this . _nextSibling = element . nextSibling ;
682
-
679
+ const parent = element . parentNode ! ;
683
680
const preview = this . _preview = this . _createPreviewElement ( ) ;
684
681
const placeholder = this . _placeholder = this . _createPlaceholderElement ( ) ;
682
+ const anchor = this . _anchor = this . _anchor || this . _document . createComment ( '' ) ;
683
+
684
+ // Insert an anchor node so that we can restore the element's position in the DOM.
685
+ parent . insertBefore ( anchor , element ) ;
685
686
686
687
// We move the element out at the end of the body and we make it hidden, because keeping it in
687
688
// place will throw off the consumer's `:last-child` selectors. We can't remove the element
688
689
// from the DOM completely, because iOS will stop firing all subsequent events in the chain.
689
690
element . style . display = 'none' ;
690
- this . _document . body . appendChild ( element . parentNode ! . replaceChild ( placeholder , element ) ) ;
691
+ this . _document . body . appendChild ( parent . replaceChild ( placeholder , element ) ) ;
691
692
getPreviewInsertionPoint ( this . _document ) . appendChild ( preview ) ;
692
693
this . _dropContainer . start ( ) ;
693
694
}
@@ -770,12 +771,7 @@ export class DragRef<T = any> {
770
771
// can throw off `NgFor` which does smart diffing and re-creates elements only when necessary,
771
772
// while moving the existing elements in all other cases.
772
773
this . _rootElement . style . display = '' ;
773
-
774
- if ( this . _nextSibling ) {
775
- this . _nextSibling . parentNode ! . insertBefore ( this . _rootElement , this . _nextSibling ) ;
776
- } else {
777
- coerceElement ( this . _initialContainer . element ) . appendChild ( this . _rootElement ) ;
778
- }
774
+ this . _anchor . parentNode ! . replaceChild ( this . _rootElement , this . _anchor ) ;
779
775
780
776
this . _destroyPreview ( ) ;
781
777
this . _destroyPlaceholder ( ) ;
@@ -1239,12 +1235,12 @@ function clamp(value: number, min: number, max: number) {
1239
1235
}
1240
1236
1241
1237
/**
1242
- * Helper to remove an element from the DOM and to do all the necessary null checks.
1243
- * @param element Element to be removed.
1238
+ * Helper to remove a node from the DOM and to do all the necessary null checks.
1239
+ * @param node Node to be removed.
1244
1240
*/
1245
- function removeElement ( element : HTMLElement | null ) {
1246
- if ( element && element . parentNode ) {
1247
- element . parentNode . removeChild ( element ) ;
1241
+ function removeNode ( node : Node | null ) {
1242
+ if ( node && node . parentNode ) {
1243
+ node . parentNode . removeChild ( node ) ;
1248
1244
}
1249
1245
}
1250
1246
0 commit comments