diff --git a/CLAUDE.md b/CLAUDE.md index f52243a..a1de5e5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -884,7 +884,11 @@ Two independent apps that talk over HTTP: `ContextMenu` (supports `▸` submenus, section headings, separators and checkbox items that toggle without closing — the header row's right-click column chooser uses those; "Add to" lists - playlists + exports and acts on the whole selection; "Remove ▸" holds + playlists + exports and acts on the whole selection; in an editable + playlist "Set Position…" (`SetPositionDialog`, `lib/playlistOrder.ts`) + moves the selection as ONE block in playlist order so its first track + lands at #N — allowed while sorted/filtered, unlike drag, because it + reorders the FULL entry list; "Remove ▸" holds From this playlist / From this export / Grids / Hotcues / From collection, each behind a `ConfirmDialog` — as is the playlist Delete key. Enter confirms unless a button has focus, so Tab-to-Cancel + Enter cancels) + `EditTagsDialog` diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 0ee54a1..c9d5f7b 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -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(null) + // Set Position…: the tracks being moved within the open playlist. + const [positioning, setPositioning] = useState(null) const [stemReport, setStemReport] = useState(null) const [engineJob, setEngineJob] = useState(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 && setStemReport(null)} />} + {positioning && ( + t.id)} + ids={positioning} + onMove={reorderPlaylist} + onClose={() => setPositioning(null)} + /> + )} {editing && ( 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(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( +
+
e.stopPropagation()} + onSubmit={(e) => { + e.preventDefault() + submit() + }} + > +
+ Set position +
+
+ +
+ {position === null + ? 'Type a position.' + : count > 1 + ? `Will occupy ${span(position)}` + : `Now ${span(current)} · will be ${span(position)}`} +
+
+
+ + +
+
+
, + document.body, + ) +} diff --git a/frontend/src/lib/playlistOrder.test.ts b/frontend/src/lib/playlistOrder.test.ts new file mode 100644 index 0000000..2767047 --- /dev/null +++ b/frontend/src/lib/playlistOrder.test.ts @@ -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') + }) +}) diff --git a/frontend/src/lib/playlistOrder.ts b/frontend/src/lib/playlistOrder.ts new file mode 100644 index 0000000..7d2894a --- /dev/null +++ b/frontend/src/lib/playlistOrder.ts @@ -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, 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): number { + return order.length - order.filter((id) => moving.has(id)).length + 1 +}