diff --git a/source/settings/settings.ts b/source/settings/settings.ts index 39fa720..c6e1769 100644 --- a/source/settings/settings.ts +++ b/source/settings/settings.ts @@ -59,6 +59,32 @@ export class Settings { }); } + public async moveQueueItem( + id: number, + direction: Queue.MoveDirection, + ): Promise { + 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; diff --git a/source/types.d.ts b/source/types.d.ts index bbd8aa6..9d8f154 100644 --- a/source/types.d.ts +++ b/source/types.d.ts @@ -26,5 +26,7 @@ declare global { text: string; url: string; }; + + type MoveDirection = 'up' | 'down'; } }