File tree Expand file tree Collapse file tree 1 file changed +18
-7
lines changed Expand file tree Collapse file tree 1 file changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -92,24 +92,35 @@ function menuItemsManager(config) {
9292 group: ' menu-items' ,
9393 animation: 150 ,
9494 handle: ' .drag-handle' ,
95+ onStart : (evt ) => {
96+ // Store the original order and DOM state before drag
97+ this .originalOrder = Array .from (container .children ).map (el => this .extractItemId (el));
98+ this .originalElements = Array .from (container .children );
99+ },
95100 onEnd : (evt ) => {
96101 if (evt .oldIndex !== evt .newIndex ) {
97- this .saveReorder (evt);
102+ // Calculate the new order based on the drag operation
103+ const newOrder = [... this .originalOrder ];
104+ const movedItem = newOrder .splice (evt .oldIndex , 1 )[0 ];
105+ newOrder .splice (evt .newIndex , 0 , movedItem);
106+
107+ // Immediately revert DOM to original state to preserve Livewire
108+ container .innerHTML = ' ' ;
109+ this .originalElements .forEach (el => container .appendChild (el));
110+
111+ this .saveReorderFromOrder (newOrder);
98112 }
99113 }
100114 });
101115 }
102116 }
103117 },
104118
105- saveReorder ( evt ) {
119+ saveReorderFromOrder ( newOrder ) {
106120 this .loading = true ;
107121
108- // Get all items in their new order
109- const container = document .getElementById (' menu-items-container' );
110- const items = Array .from (container .children ).map ((element , index ) => {
111- // Extract item ID from the wire:key attribute or data attribute
112- const itemId = this .extractItemId (element);
122+ // Build items array from the calculated new order
123+ const items = newOrder .map ((itemId , index ) => {
113124 return {
114125 id: itemId,
115126 position: index,
You can’t perform that action at this time.
0 commit comments