@@ -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 ) {
@@ -672,19 +673,19 @@ export class DragRef<T = any> {
672
673
673
674
if ( this . _dropContainer ) {
674
675
const element = this . _rootElement ;
675
-
676
- // Grab the `nextSibling` before the preview and placeholder
677
- // have been created so we don't get the preview by accident.
678
- this . _nextSibling = element . nextSibling ;
679
-
676
+ const parent = element . parentNode ! ;
680
677
const preview = this . _preview = this . _createPreviewElement ( ) ;
681
678
const placeholder = this . _placeholder = this . _createPlaceholderElement ( ) ;
679
+ const anchor = this . _anchor = this . _anchor || this . _document . createComment ( '' ) ;
680
+
681
+ // Insert an anchor node so that we can restore the element's position in the DOM.
682
+ parent . insertBefore ( anchor , element ) ;
682
683
683
684
// We move the element out at the end of the body and we make it hidden, because keeping it in
684
685
// place will throw off the consumer's `:last-child` selectors. We can't remove the element
685
686
// from the DOM completely, because iOS will stop firing all subsequent events in the chain.
686
687
element . style . display = 'none' ;
687
- this . _document . body . appendChild ( element . parentNode ! . replaceChild ( placeholder , element ) ) ;
688
+ this . _document . body . appendChild ( parent . replaceChild ( placeholder , element ) ) ;
688
689
getPreviewInsertionPoint ( this . _document ) . appendChild ( preview ) ;
689
690
this . _dropContainer . start ( ) ;
690
691
}
@@ -767,12 +768,7 @@ export class DragRef<T = any> {
767
768
// can throw off `NgFor` which does smart diffing and re-creates elements only when necessary,
768
769
// while moving the existing elements in all other cases.
769
770
this . _rootElement . style . display = '' ;
770
-
771
- if ( this . _nextSibling ) {
772
- this . _nextSibling . parentNode ! . insertBefore ( this . _rootElement , this . _nextSibling ) ;
773
- } else {
774
- coerceElement ( this . _initialContainer . element ) . appendChild ( this . _rootElement ) ;
775
- }
771
+ this . _anchor . parentNode ! . replaceChild ( this . _rootElement , this . _anchor ) ;
776
772
777
773
this . _destroyPreview ( ) ;
778
774
this . _destroyPlaceholder ( ) ;
@@ -1236,12 +1232,12 @@ function clamp(value: number, min: number, max: number) {
1236
1232
}
1237
1233
1238
1234
/**
1239
- * Helper to remove an element from the DOM and to do all the necessary null checks.
1240
- * @param element Element to be removed.
1235
+ * Helper to remove a node from the DOM and to do all the necessary null checks.
1236
+ * @param node Node to be removed.
1241
1237
*/
1242
- function removeElement ( element : HTMLElement | null ) {
1243
- if ( element && element . parentNode ) {
1244
- element . parentNode . removeChild ( element ) ;
1238
+ function removeNode ( node : Node | null ) {
1239
+ if ( node && node . parentNode ) {
1240
+ node . parentNode . removeChild ( node ) ;
1245
1241
}
1246
1242
}
1247
1243
0 commit comments