Track position in playlist can now be typed

This commit is contained in:
patben8 committed 2026-10-05 21:01:25 +02:00
1 parent 0fac84cb73
commit c8b86100f0
5 files changed
+180 -1

No files matched your search

+16
View File
@@ -30,6 +30,7 @@ import { ImportDialog } from './components/ImportDialog'
import { AddFilesDialog, type AddTarget } from './components/AddFilesDialog'
import { ConvertStemsDialog } from './components/ConvertStemsDialog'
import { StemReportDialog } from './components/StemReportDialog'
import { SetPositionDialog } from './components/SetPositionDialog'
import { Icon } from './lib/icons'
import { usePrefsWriter } from './lib/prefs'
@@ -91,6 +92,8 @@ export default function App() {
// Convert to Stems: the dialog's selection, a finished run's Details, and a
// running engine download (which outlives the dialog — decided).
const [stemDialog, setStemDialog] = useState<string[] | null>(null)
// Set Position…: the tracks being moved within the open playlist.
const [positioning, setPositioning] = useState<string[] | null>(null)
const [stemReport, setStemReport] = useState<StemBatchResult | null>(null)
const [engineJob, setEngineJob] = useState<string | null>(null)
const [batchCancelling, setBatchCancelling] = useState(false)
@@ -1062,6 +1065,11 @@ export default function App() {
: viewingDevice
? []
: [{ label: 'Add to', submenu: addToItems(menu.ids) }]),
// Unlike drag-reorder this stays on while sorted or filtered: it
// moves within the playlist's FULL order, which the # column shows.
...(playlistEditable
? [{ label: 'Set Position…', onClick: () => setPositioning(menu.ids) }]
: []),
...(() => {
const items = removeItems(menu.ids)
return items.length ? [{ label: 'Remove', submenu: items }] : []
@@ -1103,6 +1111,14 @@ export default function App() {
/>
)}
{stemReport && <StemReportDialog result={stemReport} onClose={() => setStemReport(null)} />}
{positioning && (
<SetPositionDialog
order={tracks.map((t) => t.id)}
ids={positioning}
onMove={reorderPlaylist}
onClose={() => setPositioning(null)}
/>
)}
{editing && (
<EditTagsDialog
track={editing}
@@ -0,0 +1,106 @@
import { createPortal } from 'react-dom'
import { useEffect, useRef, useState } from 'react'
import { maxPosition, moveToPosition } from '../lib/playlistOrder'
interface Props {
/** The playlist's full entry order — never the sorted or filtered view, so
* the move is safe whatever the table is showing. */
order: string[]
/** The tracks to move; they keep their playlist order as one block. */
ids: string[]
onMove: (order: string[]) => void
onClose: () => void
}
/** "Set Position…": type where the first selected track should end up. */
export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
const moving = new Set(ids)
const count = order.filter((id) => moving.has(id)).length
const max = maxPosition(order, moving)
const current = order.findIndex((id) => moving.has(id)) + 1
const [value, setValue] = useState(String(current))
const input = useRef<HTMLInputElement>(null)
useEffect(() => input.current?.select(), [])
const typed = Number.parseInt(value, 10)
const position = Number.isFinite(typed) ? Math.min(Math.max(typed, 1), max) : null
const submit = () => {
if (position === null) return
const next = moveToPosition(order, moving, position)
if (next.some((id, i) => id !== order[i])) onMove(next)
onClose()
}
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose()
}
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [onClose])
const span = (p: number) => (count > 1 ? `#${p}–${p + count - 1}` : `#${p}`)
return createPortal(
<div
aria-modal="true"
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-[3px] p-6"
onClick={onClose}
>
<form
className="w-full max-w-sm overflow-hidden glass-overlay"
onClick={(e) => e.stopPropagation()}
onSubmit={(e) => {
e.preventDefault()
submit()
}}
>
<div className="border-b border-line px-5 py-4 text-[15px] font-semibold tracking-tight">
Set position
</div>
<div className="space-y-3 px-5 py-4 text-sm text-muted">
<label className="flex items-center gap-2">
<span>{count > 1 ? `Move ${count} tracks to position` : 'Move to position'}</span>
<input
ref={input}
type="number"
inputMode="numeric"
min={1}
max={max}
value={value}
onChange={(e) => setValue(e.target.value)}
className="w-20 rounded-lg well px-2.5 py-1.5 text-sm tabular-nums text-text outline-none focus:ring-1 focus:ring-accent"
/>
<span>of {max}</span>
</label>
<div className="tabular-nums">
{position === null
? 'Type a position.'
: count > 1
? `Will occupy ${span(position)}`
: `Now ${span(current)} · will be ${span(position)}`}
</div>
</div>
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
<button
type="button"
onClick={onClose}
className="btn-glass rounded-full px-4 py-1.5 text-sm text-muted hover:text-text"
>
Cancel
</button>
<button
type="submit"
disabled={position === null}
className="btn-primary rounded-full px-4 py-1.5 text-sm font-semibold disabled:opacity-50"
>
Move
</button>
</div>
</form>
</div>,
document.body,
)
}
+36
View File
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest'
import { maxPosition, moveToPosition } from './playlistOrder'
const order = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
const move = (ids: string[], position: number) => moveToPosition(order, new Set(ids), position).join('')
describe('moveToPosition', () => {
it('puts the track AT the position, moving down or up', () => {
expect(move(['B'], 5)).toBe('ACDEBFG')
expect(move(['F'], 2)).toBe('AFBCDEG')
expect(move(['A'], 1)).toBe('ABCDEFG')
})
it('moves a selection as one block in playlist order', () => {
expect(move(['F', 'B'], 2)).toBe('ABFCDEG')
expect(move(['G', 'A', 'D'], 3)).toBe('BCADGEF')
})
it('clamps past either end', () => {
expect(move(['B', 'C'], 99)).toBe('ADEFGBC')
expect(move(['C'], 0)).toBe('CABDEFG')
})
it('moves every entry of a track listed twice', () => {
expect(moveToPosition(['A', 'B', 'A', 'C'], new Set(['A']), 2).join('')).toBe('BAAC')
})
})
describe('maxPosition', () => {
it('leaves room for the block behind its first track', () => {
expect(maxPosition(order, new Set(['B']))).toBe(7)
expect(maxPosition(order, new Set(['B', 'C', 'D']))).toBe(5)
expect(move(['B', 'C', 'D'], 5)).toBe('AEFGBCD')
})
})
+17
View File
@@ -0,0 +1,17 @@
/** A playlist's entry order with `moving` lifted out as one block, kept in
* their PLAYLIST order, and put back so the block's first track lands at the
* 1-based `position` of the result. A position past the end puts the block
* last. A track listed twice in the playlist moves with both entries, since a
* selection is by track id. */
export function moveToPosition(order: string[], moving: ReadonlySet<string>, position: number): string[] {
const block = order.filter((id) => moving.has(id))
const rest = order.filter((id) => !moving.has(id))
const at = Math.min(Math.max(Math.floor(position), 1) - 1, rest.length)
return [...rest.slice(0, at), ...block, ...rest.slice(at)]
}
/** The highest position the block's first track can take: the block's own
* length is what sits behind it. */
export function maxPosition(order: string[], moving: ReadonlySet<string>): number {
return order.length - order.filter((id) => moving.has(id)).length + 1
}