@@ -93,10 +93,10 @@ export class DragRef<T = any> {
93
93
private _pickupPositionOnPage : Point ;
94
94
95
95
/**
96
- * Reference to the element that comes after the draggable in the DOM, at the time
97
- * it was picked up. Used for restoring its initial position when it's dropped .
96
+ * Anchor node used to save the place in the DOM where the element was
97
+ * picked up so that it can be restored at the end of the drag sequence .
98
98
*/
99
- private _nextSibling : Node | null ;
99
+ private _anchor : Comment ;
100
100
101
101
/**
102
102
* CSS `transform` applied to the element when it isn't being dragged. We need a
@@ -380,9 +380,10 @@ export class DragRef<T = any> {
380
380
if ( this . isDragging ( ) ) {
381
381
// Since we move out the element to the end of the body while it's being
382
382
// dragged, we have to make sure that it's removed if it gets destroyed.
383
- removeElement ( this . _rootElement ) ;
383
+ removeNode ( this . _rootElement ) ;
384
384
}
385
385
386
+ removeNode ( this . _anchor ) ;
386
387
this . _destroyPreview ( ) ;
387
388
this . _destroyPlaceholder ( ) ;
388
389
this . _dragDropRegistry . removeDragItem ( this ) ;
@@ -400,7 +401,7 @@ export class DragRef<T = any> {
400
401
this . _dropContainer = undefined ;
401
402
this . _resizeSubscription . unsubscribe ( ) ;
402
403
this . _boundaryElement = this . _rootElement = this . _placeholderTemplate =
403
- this . _previewTemplate = this . _nextSibling = null ! ;
404
+ this . _previewTemplate = this . _anchor = null ! ;
404
405
}
405
406
406
407
/** Checks whether the element is currently being dragged. */
@@ -487,7 +488,7 @@ export class DragRef<T = any> {
487
488
/** Destroys the preview element and its ViewRef. */
488
489
private _destroyPreview ( ) {
489
490
if ( this . _preview ) {
490
- removeElement ( this . _preview ) ;
491
+ removeNode ( this . _preview ) ;
491
492
}
492
493
493
494
if ( this . _previewRef ) {
@@ -500,7 +501,7 @@ export class DragRef<T = any> {
500
501
/** Destroys the placeholder element and its ViewRef. */
501
502
private _destroyPlaceholder ( ) {
502
503
if ( this . _placeholder ) {
503
- removeElement ( this . _placeholder ) ;
504
+ removeNode ( this . _placeholder ) ;
504
505
}
505
506
506
507
if ( this . _placeholderRef ) {
@@ -681,19 +682,19 @@ export class DragRef<T = any> {
681
682
682
683
if ( this . _dropContainer ) {
683
684
const element = this . _rootElement ;
684
-
685
- // Grab the `nextSibling` before the preview and placeholder
686
- // have been created so we don't get the preview by accident.
687
- this . _nextSibling = element . nextSibling ;
688
-
685
+ const parent = element . parentNode ! ;
689
686
const preview = this . _preview = this . _createPreviewElement ( ) ;
690
687
const placeholder = this . _placeholder = this . _createPlaceholderElement ( ) ;
688
+ const anchor = this . _anchor = this . _anchor || this . _document . createComment ( '' ) ;
689
+
690
+ // Insert an anchor node so that we can restore the element's position in the DOM.
691
+ parent . insertBefore ( anchor , element ) ;
691
692
692
693
// We move the element out at the end of the body and we make it hidden, because keeping it in
693
694
// place will throw off the consumer's `:last-child` selectors. We can't remove the element
694
695
// from the DOM completely, because iOS will stop firing all subsequent events in the chain.
695
696
element . style . display = 'none' ;
696
- this . _document . body . appendChild ( element . parentNode ! . replaceChild ( placeholder , element ) ) ;
697
+ this . _document . body . appendChild ( parent . replaceChild ( placeholder , element ) ) ;
697
698
getPreviewInsertionPoint ( this . _document ) . appendChild ( preview ) ;
698
699
this . _dropContainer . start ( ) ;
699
700
}
@@ -776,12 +777,7 @@ export class DragRef<T = any> {
776
777
// can throw off `NgFor` which does smart diffing and re-creates elements only when necessary,
777
778
// while moving the existing elements in all other cases.
778
779
this . _rootElement . style . display = '' ;
779
-
780
- if ( this . _nextSibling ) {
781
- this . _nextSibling . parentNode ! . insertBefore ( this . _rootElement , this . _nextSibling ) ;
782
- } else {
783
- coerceElement ( this . _initialContainer . element ) . appendChild ( this . _rootElement ) ;
784
- }
780
+ this . _anchor . parentNode ! . replaceChild ( this . _rootElement , this . _anchor ) ;
785
781
786
782
this . _destroyPreview ( ) ;
787
783
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