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) {
92
92
group: ' menu-items' ,
93
93
animation: 150 ,
94
94
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
+ },
95
100
onEnd : (evt ) => {
96
101
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);
98
112
}
99
113
}
100
114
});
101
115
}
102
116
}
103
117
},
104
118
105
- saveReorder ( evt ) {
119
+ saveReorderFromOrder ( newOrder ) {
106
120
this .loading = true ;
107
121
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 ) => {
113
124
return {
114
125
id: itemId,
115
126
position: index,
You can’t perform that action at this time.
0 commit comments