Add move item functionality to Settings.

This commit is contained in:
Bauke 2022-10-25 12:28:37 +02:00
parent d4400c5c31
commit e591db7bcf
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 28 additions and 0 deletions

View File

@ -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;

2
source/types.d.ts vendored
View File

@ -26,5 +26,7 @@ declare global {
text: string;
url: string;
};
type MoveDirection = 'up' | 'down';
}
}