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