Implemented new set playlist position mode

This commit is contained in:
patben8 committed 2026-10-15 10:48:14 +02:00
1 parent 8230722105
commit 7b2d6de411
6 files changed
+129 -26

No files matched your search

+6 -3
View File
@@ -996,9 +996,12 @@ Two independent apps that talk over HTTP:
column chooser uses those; "Add to" lists
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
moves the selection as ONE block so its first track lands at #N —
allowed while sorted/filtered, unlike drag, because it reorders the FULL
entry list. While the table is sorted the dialog asks the block's Order:
"As sorted" (default; the menu's ids arrive in VIEW order, so the sort is
written into the playlist and survives clearing it) or playlist order;
"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`
+14 -8
View File
@@ -898,11 +898,16 @@ export default function App() {
// TrackTable retargets the selection to a right-clicked row outside it
// before calling this, but that state update has not landed yet — so the
// same rule is applied here to decide what the menu acts on.
const openMenu = (track: Track, x: number, y: number, selectable: boolean) =>
// same rule is applied here to decide what the menu acts on. The ids come
// in the order the view shows them (Set Position can keep that order); a
// selected track the view hides follows.
const openMenu = (track: Track, x: number, y: number, selectable: boolean, viewIds: string[]) =>
setMenu({
track,
ids: selectable && selected.has(track.id) ? [...selected] : [track.id],
ids:
selectable && selected.has(track.id)
? [...new Set([...viewIds.filter((id) => selected.has(id)), ...selected])]
: [track.id],
x,
y,
})
@@ -1321,6 +1326,7 @@ export default function App() {
<SetPositionDialog
order={tracks.map((t) => t.id)}
ids={positioning}
sorting={sorting}
onMove={reorderPlaylist}
onClose={() => setPositioning(null)}
/>
@@ -1603,7 +1609,7 @@ export default function App() {
sorting={sorting}
onSortingChange={setSorting}
selection={{ selected, onChange: setSelected }}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)}
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
@@ -1644,7 +1650,7 @@ export default function App() {
selection={{ selected, onChange: setSelected }}
marked={{ ids: heldInFolder, title: 'Already in your collection' }}
dragFrom={{ origin: 'folder' }}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)}
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
onPlay={playTrack}
onEditField={canEdit ? folderEditField : undefined}
@@ -1680,7 +1686,7 @@ export default function App() {
tracks={filtered}
sorting={sorting}
onSortingChange={setSorting}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, false)}
onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, false, viewIds)}
dragFrom={{ origin: 'device' }}
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
onPlay={playTrack}
@@ -1705,7 +1711,7 @@ export default function App() {
sorting={sorting}
onSortingChange={setSorting}
selection={{ selected, onChange: setSelected }}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)}
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
@@ -1747,7 +1753,7 @@ export default function App() {
origin: 'collection',
playlistId: playlistEditable && source.kind === 'playlist' ? source.id : undefined,
}}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onRowContextMenu={(track, x, y, viewIds) => openMenu(track, x, y, true, viewIds)}
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
+58 -6
View File
@@ -1,24 +1,41 @@
import { createPortal } from 'react-dom'
import { useEffect, useRef, useState } from 'react'
import { useEffect, useRef, useState, type ReactNode } from 'react'
import type { SortingState } from '@tanstack/react-table'
import { maxPosition, moveToPosition } from '../lib/playlistOrder'
import { COLUMN_MENU } from '../lib/trackColumns'
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. */
/** The tracks to move, in the order the table shows them. */
ids: string[]
/** The table's sort. While there is one, the block may keep that order
* instead of its playlist order — which writes the sort into the playlist. */
sorting: SortingState
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) {
/** "Key" / "Key descending, then BPM" — the sort as the Order choice names it. */
function describeSort(sorting: SortingState): string {
const label = (id: string) => COLUMN_MENU.find((c) => c.id === id)?.label ?? id
return sorting.map((s) => `${label(s.id)}${s.desc ? ' descending' : ''}`).join(', then ')
}
/** "Set Position…": type where the first selected track should end up, and —
* while the table is sorted — whether the block keeps its playlist order or
* takes the sorted one. */
export function SetPositionDialog({ order, ids, sorting, 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))
// With one track there is no order to choose; unsorted, both are the same.
const canSort = sorting.length > 0 && count > 1
const [mode, setMode] = useState<'playlist' | 'sorted'>('sorted')
const sorted = canSort && mode === 'sorted'
const input = useRef<HTMLInputElement>(null)
useEffect(() => input.current?.select(), [])
@@ -28,7 +45,7 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
const submit = () => {
if (position === null) return
const next = moveToPosition(order, moving, position)
const next = moveToPosition(order, moving, position, sorted ? ids : undefined)
if (next.some((id, i) => id !== order[i])) onMove(next)
onClose()
}
@@ -41,6 +58,26 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
return () => window.removeEventListener('keydown', onKey)
}, [onClose])
const option = (value: 'playlist' | 'sorted', label: string, detail: ReactNode) => (
<label
className={`flex cursor-pointer gap-3 rounded-lg px-3 py-2 ${
mode === value ? 'is-selected' : 'hover:bg-ink-850'
}`}
>
<input
type="radio"
name="position-order"
checked={mode === value}
onChange={() => setMode(value)}
className="mt-0.5 accent-accent"
/>
<span className="min-w-0 flex-1">
<span className="block text-text">{label}</span>
<span className="block text-xs text-faint">{detail}</span>
</span>
</label>
)
const span = (p: number) => (count > 1 ? `#${p}–${p + count - 1}` : `#${p}`)
return createPortal(
@@ -79,9 +116,24 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
{position === null
? 'Type a position.'
: count > 1
? `Will occupy ${span(position)}`
? `Will occupy ${span(position)}${canSort ? '' : ', in the order they have in the playlist.'}`
: `Now ${span(current)} · will be ${span(position)}`}
</div>
{canSort && (
<div role="radiogroup" aria-label="Order" className="space-y-1">
<div className="text-xs uppercase tracking-wide text-faint">Order</div>
{option(
'sorted',
`As sorted: ${describeSort(sorting)}`,
'In the order shown here. The playlist keeps this order when you clear the sort.',
)}
{option(
'playlist',
'Playlist order',
'In the order they have in the playlist now, whatever the sort shows.',
)}
</div>
)}
</div>
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
<button
+3 -2
View File
@@ -77,7 +77,8 @@ interface Props {
dragFrom?: { origin: TrackOrigin; playlistId?: string }
/** Delete/Backspace removes the selected tracks from this view. */
onRemove?: (ids: string[]) => void
onRowContextMenu?: (track: Track, x: number, y: number) => void
/** `viewIds` is every row's id in the order shown (sorted, filtered). */
onRowContextMenu?: (track: Track, x: number, y: number, viewIds: string[]) => void
/** Right-click on the header row (the column chooser). */
onHeaderContextMenu?: (x: number, y: number) => void
onPlay?: (track: Track) => void
@@ -591,7 +592,7 @@ export function TrackTable({
// Right-clicking outside the selection retargets it, so the
// menu always acts on what is highlighted.
if (selection && !isSelected) selectOnly(vr.index)
onRowContextMenu(row.original, e.clientX, e.clientY)
onRowContextMenu(row.original, e.clientX, e.clientY, rows.map((r) => r.original.id))
}}
>
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
+19
View File
@@ -27,6 +27,25 @@ describe('moveToPosition', () => {
})
})
describe('moveToPosition with a view order', () => {
const sorted = (ids: string[], position: number, view: string[]) =>
moveToPosition(order, new Set(ids), position, view).join('')
it('moves the block in the order the view shows it', () => {
expect(sorted(['B', 'E', 'F'], 1, ['F', 'B', 'E'])).toBe('FBEACDG')
expect(sorted(['B', 'E', 'F'], 3, ['E', 'F', 'B'])).toBe('ACEFBDG')
})
it('ignores view ids that are not moving', () => {
expect(sorted(['B', 'D'], 1, ['G', 'D', 'A', 'B'])).toBe('DBACEFG')
})
it('keeps a twice-listed track together, and appends a track the view lacks', () => {
expect(moveToPosition(['A', 'B', 'A', 'C'], new Set(['A', 'C']), 1, ['C', 'A']).join('')).toBe('CAAB')
expect(sorted(['B', 'C'], 1, ['C'])).toBe('CBADEFG')
})
})
describe('maxPosition', () => {
it('leaves room for the block behind its first track', () => {
expect(maxPosition(order, new Set(['B']))).toBe(7)
+29 -7
View File
@@ -1,10 +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))
/** A playlist's entry order with `moving` lifted out as one block 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. The block keeps the
* tracks' PLAYLIST order, or with `blockOrder` (the ids as a sorted view
* shows them) that order instead — so a sort can be written into the
* playlist. A track listed twice in the playlist moves with both entries,
* since a selection is by track id; in `blockOrder` they stay together. */
export function moveToPosition(
order: string[],
moving: ReadonlySet<string>,
position: number,
blockOrder?: readonly string[],
): string[] {
const block = blockOrder ? orderBlock(order, moving, blockOrder) : 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)]
@@ -15,3 +22,18 @@ export function moveToPosition(order: string[], moving: ReadonlySet<string>, pos
export function maxPosition(order: string[], moving: ReadonlySet<string>): number {
return order.length - order.filter((id) => moving.has(id)).length + 1
}
/** The moving entries in `blockOrder`'s order, each track's repeats together.
* A moving track `blockOrder` lacks (none should) follows in playlist order. */
function orderBlock(order: string[], moving: ReadonlySet<string>, blockOrder: readonly string[]): string[] {
const counts = new Map<string, number>()
for (const id of order) if (moving.has(id)) counts.set(id, (counts.get(id) ?? 0) + 1)
const block: string[] = []
for (const id of [...blockOrder, ...order]) {
const n = counts.get(id)
if (!n) continue
for (let i = 0; i < n; i++) block.push(id)
counts.delete(id)
}
return block
}