diff --git a/packages/uhk-web/src/app/components/macro/list/macro-list.component.html b/packages/uhk-web/src/app/components/macro/list/macro-list.component.html index 8cff2e4f7d1..e549788d319 100644 --- a/packages/uhk-web/src/app/components/macro/list/macro-list.component.html +++ b/packages/uhk-web/src/app/components/macro/list/macro-list.component.html @@ -5,7 +5,7 @@ [dragAndDrop]="macro.macroActions" [dragAndDropHandle]="dragAndDropHandle" (dragAndDropChange)="macroActionReordered($event)"> - >(ElementRef); @@ -68,6 +69,7 @@ export class DragAndDropDirective implements OnDestroy { this.offsetX = event.clientX - rect.left; this.offsetY = event.clientY - rect.top; this.initialRect = rect; + this.initialOrder = Array.from(this.elementRef.nativeElement.children) as HTMLElement[]; window.addEventListener('pointermove', this.onPointerMove); window.addEventListener('pointerup', this.onPointerUp); @@ -110,6 +112,7 @@ export class DragAndDropDirective implements OnDestroy { } const reordered = this.dragging ? this.getReorderedModel() : undefined; + this.restoreDomOrder(); this.cleanup(); if (reordered) { @@ -117,6 +120,24 @@ export class DragAndDropDirective implements OnDestroy { } }; + /** + * Puts the list items back into their pre-drag DOM order. + * + * The reordering is shown live by moving the actual DOM nodes, but that leaves the DOM out of + * sync with the view order Angular tracks for the bound model. Restoring the original order + * before emitting lets Angular re-render the reordered model with its own, consistent view + * order instead of duplicating or misplacing items. + */ + private restoreDomOrder(): void { + const container = this.elementRef.nativeElement; + + for (const child of this.initialOrder) { + container.appendChild(child); + } + + this.initialOrder = []; + } + private beginDrag(): void { this.dragging = true; this.item.classList.add(TRANSIT_CLASS); @@ -309,5 +330,6 @@ export class DragAndDropDirective implements OnDestroy { this.mirror = undefined; this.pointerId = -1; this.initialRect = undefined; + this.initialOrder = []; } } \ No newline at end of file