Add move item functionality to Settings.
This commit is contained in:
parent
d4400c5c31
commit
e591db7bcf
|
@ -59,6 +59,32 @@ export class Settings {
|
|||
});
|
||||
}
|
||||
|
||||
public async moveQueueItem(
|
||||
id: number,
|
||||
direction: Queue.MoveDirection,
|
||||
): Promise<void> {
|
||||
const targetItem = this.queue.find((item) => item.id === id);
|
||||
if (targetItem === undefined) {
|
||||
throw new Error(`Failed to move item with ID: ${id}`);
|
||||
}
|
||||
|
||||
const previousIndex = targetItem.sortIndex;
|
||||
if (direction === 'down') {
|
||||
targetItem.sortIndex += 1;
|
||||
} else if (direction === 'up') {
|
||||
targetItem.sortIndex -= 1;
|
||||
}
|
||||
|
||||
const existingItem = this.queue.find(
|
||||
(item) => item.sortIndex === targetItem.sortIndex,
|
||||
);
|
||||
if (existingItem !== undefined) {
|
||||
existingItem.sortIndex = previousIndex;
|
||||
}
|
||||
|
||||
await this.save();
|
||||
}
|
||||
|
||||
public newQueueItemId(): number {
|
||||
const item = this.queue.sort((a, b) => b.id - a.id)[0];
|
||||
return item === undefined ? 1 : item.id + 1;
|
||||
|
|
|
@ -26,5 +26,7 @@ declare global {
|
|||
text: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
type MoveDirection = 'up' | 'down';
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue