Favorite: computeNewPosition fixed (#2134)
This commit is contained in:
@ -104,7 +104,7 @@ export const useFavorites = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const computeNewPosition = (destIndex: number) => {
|
const computeNewPosition = (destIndex: number, sourceIndex: number) => {
|
||||||
if (destIndex === 0) {
|
if (destIndex === 0) {
|
||||||
return favorites[destIndex].position / 2;
|
return favorites[destIndex].position / 2;
|
||||||
}
|
}
|
||||||
@ -112,6 +112,13 @@ export const useFavorites = () => {
|
|||||||
if (destIndex === favorites.length - 1) {
|
if (destIndex === favorites.length - 1) {
|
||||||
return favorites[destIndex].position + 1;
|
return favorites[destIndex].position + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sourceIndex < destIndex) {
|
||||||
|
return (
|
||||||
|
(favorites[destIndex + 1].position + favorites[destIndex].position) / 2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
(favorites[destIndex - 1].position + favorites[destIndex].position) / 2
|
(favorites[destIndex - 1].position + favorites[destIndex].position) / 2
|
||||||
);
|
);
|
||||||
@ -121,7 +128,10 @@ export const useFavorites = () => {
|
|||||||
if (!result.destination || !favorites) {
|
if (!result.destination || !favorites) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const newPosition = computeNewPosition(result.destination.index);
|
const newPosition = computeNewPosition(
|
||||||
|
result.destination.index,
|
||||||
|
result.source.index,
|
||||||
|
);
|
||||||
|
|
||||||
const reorderFavorites = Array.from(favorites);
|
const reorderFavorites = Array.from(favorites);
|
||||||
const [removed] = reorderFavorites.splice(result.source.index, 1);
|
const [removed] = reorderFavorites.splice(result.source.index, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user