Fix items not being movable when the next sort index wasn't plus or minus 1.

This commit is contained in:
Bauke 2022-10-25 14:50:50 +02:00
parent 9696b78728
commit ce7fbfe349
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 26 additions and 10 deletions

View File

@ -68,19 +68,13 @@ export class Settings {
throw new Error(`Failed to move item with ID: ${id}`);
}
const previousIndex = targetItem.sortIndex;
let targetIndex = previousIndex;
if (direction === 'down') {
targetIndex += 1;
} else if (direction === 'up') {
targetIndex -= 1;
}
const currentIndex = targetItem.sortIndex;
const targetIndex = this.nextQueueItemSortIndex(currentIndex, direction);
const existingItem = this.queue.find(
(item) => item.sortIndex === targetIndex,
);
if (existingItem !== undefined) {
existingItem.sortIndex = previousIndex;
if (existingItem !== undefined && targetIndex !== undefined) {
existingItem.sortIndex = currentIndex;
targetItem.sortIndex = targetIndex;
await this.save();
}
@ -95,6 +89,28 @@ export class Settings {
return this.queue.sort((a, b) => a.sortIndex - b.sortIndex)[0];
}
public nextQueueItemSortIndex(
currentIndex: number,
direction: Queue.MoveDirection,
): number | undefined {
this.queue.sort((a, b) => {
return direction === 'up'
? b.sortIndex - a.sortIndex
: a.sortIndex - b.sortIndex;
});
let foundCurrent = false;
for (const item of this.queue) {
if (foundCurrent) {
return item.sortIndex;
}
if (item.sortIndex === currentIndex) {
foundCurrent = true;
}
}
}
public async removeQueueItem(id: number): Promise<void> {
const itemIndex = this.queue.findIndex((item) => item.id === id);
if (itemIndex === -1) {