Files
otunes/apps/web/lib/move-queue-block.ts
T
2026-11-16 22:18:26 -05:00

46 lines
1.2 KiB
TypeScript

export function getContiguousSelectedIndices(
items: { selectionId: string }[],
selectedIds: Set<string>,
dragIndex: number
): number[] {
const selectedIndices = items
.map((item, index) => (selectedIds.has(item.selectionId) ? index : -1))
.filter((index) => index >= 0)
if (selectedIndices.length > 1 && selectedIndices.includes(dragIndex)) {
return selectedIndices
}
return [dragIndex]
}
export function moveQueueBlock<T>(
queue: T[],
fromIndices: number[],
targetIndex: number
): T[] {
const sorted = [...fromIndices].sort((a, b) => a - b)
if (sorted.length === 0) {
return queue
}
const block = sorted.map((index) => queue[index])
const remaining = queue.filter((_, index) => !sorted.includes(index))
const minSelected = sorted[0]
const maxSelected = sorted[sorted.length - 1]
if (targetIndex >= minSelected && targetIndex <= maxSelected) {
return queue
}
const removedBefore = sorted.filter((index) => index < targetIndex).length
const insertAt = Math.max(
0,
Math.min(targetIndex - removedBefore, remaining.length)
)
const next = [...remaining]
next.splice(insertAt, 0, ...block)
return next
}