mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented new set playlist position mode
This commit is contained in:
1 parent
8230722105
commit
7b2d6de411
6 files changed
+129
-26
No files matched your search
@@ -996,9 +996,12 @@ Two independent apps that talk over HTTP:
|
|||||||
column chooser uses those; "Add to" lists
|
column chooser uses those; "Add to" lists
|
||||||
playlists + exports and acts on the whole selection; in an editable
|
playlists + exports and acts on the whole selection; in an editable
|
||||||
playlist "Set Position…" (`SetPositionDialog`, `lib/playlistOrder.ts`)
|
playlist "Set Position…" (`SetPositionDialog`, `lib/playlistOrder.ts`)
|
||||||
moves the selection as ONE block in playlist order so its first track
|
moves the selection as ONE block so its first track lands at #N —
|
||||||
lands at #N — allowed while sorted/filtered, unlike drag, because it
|
allowed while sorted/filtered, unlike drag, because it reorders the FULL
|
||||||
reorders the FULL entry list; "Remove ▸" holds
|
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,
|
From this playlist / From this export / Grids / Hotcues / From collection,
|
||||||
each behind a `ConfirmDialog` — as is the playlist Delete key. Enter
|
each behind a `ConfirmDialog` — as is the playlist Delete key. Enter
|
||||||
confirms unless a button has focus, so Tab-to-Cancel + Enter cancels) + `EditTagsDialog`
|
confirms unless a button has focus, so Tab-to-Cancel + Enter cancels) + `EditTagsDialog`
|
||||||
|
|||||||
+14
-8
@@ -898,11 +898,16 @@ export default function App() {
|
|||||||
|
|
||||||
// TrackTable retargets the selection to a right-clicked row outside it
|
// TrackTable retargets the selection to a right-clicked row outside it
|
||||||
// before calling this, but that state update has not landed yet — so the
|
// 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.
|
// same rule is applied here to decide what the menu acts on. The ids come
|
||||||
const openMenu = (track: Track, x: number, y: number, selectable: boolean) =>
|
// 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({
|
setMenu({
|
||||||
track,
|
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,
|
x,
|
||||||
y,
|
y,
|
||||||
})
|
})
|
||||||
@@ -1321,6 +1326,7 @@ export default function App() {
|
|||||||
<SetPositionDialog
|
<SetPositionDialog
|
||||||
order={tracks.map((t) => t.id)}
|
order={tracks.map((t) => t.id)}
|
||||||
ids={positioning}
|
ids={positioning}
|
||||||
|
sorting={sorting}
|
||||||
onMove={reorderPlaylist}
|
onMove={reorderPlaylist}
|
||||||
onClose={() => setPositioning(null)}
|
onClose={() => setPositioning(null)}
|
||||||
/>
|
/>
|
||||||
@@ -1603,7 +1609,7 @@ export default function App() {
|
|||||||
sorting={sorting}
|
sorting={sorting}
|
||||||
onSortingChange={setSorting}
|
onSortingChange={setSorting}
|
||||||
selection={{ selected, onChange: setSelected }}
|
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 })}
|
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
onEditField={canEdit ? editField : undefined}
|
onEditField={canEdit ? editField : undefined}
|
||||||
@@ -1644,7 +1650,7 @@ export default function App() {
|
|||||||
selection={{ selected, onChange: setSelected }}
|
selection={{ selected, onChange: setSelected }}
|
||||||
marked={{ ids: heldInFolder, title: 'Already in your collection' }}
|
marked={{ ids: heldInFolder, title: 'Already in your collection' }}
|
||||||
dragFrom={{ origin: 'folder' }}
|
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 })}
|
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
onEditField={canEdit ? folderEditField : undefined}
|
onEditField={canEdit ? folderEditField : undefined}
|
||||||
@@ -1680,7 +1686,7 @@ export default function App() {
|
|||||||
tracks={filtered}
|
tracks={filtered}
|
||||||
sorting={sorting}
|
sorting={sorting}
|
||||||
onSortingChange={setSorting}
|
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' }}
|
dragFrom={{ origin: 'device' }}
|
||||||
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
@@ -1705,7 +1711,7 @@ export default function App() {
|
|||||||
sorting={sorting}
|
sorting={sorting}
|
||||||
onSortingChange={setSorting}
|
onSortingChange={setSorting}
|
||||||
selection={{ selected, onChange: setSelected }}
|
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 })}
|
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
onEditField={canEdit ? editField : undefined}
|
onEditField={canEdit ? editField : undefined}
|
||||||
@@ -1747,7 +1753,7 @@ export default function App() {
|
|||||||
origin: 'collection',
|
origin: 'collection',
|
||||||
playlistId: playlistEditable && source.kind === 'playlist' ? source.id : undefined,
|
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 })}
|
onHeaderContextMenu={(x, y) => setHeaderMenu({ x, y })}
|
||||||
onPlay={playTrack}
|
onPlay={playTrack}
|
||||||
onEditField={canEdit ? editField : undefined}
|
onEditField={canEdit ? editField : undefined}
|
||||||
|
|||||||
@@ -1,24 +1,41 @@
|
|||||||
import { createPortal } from 'react-dom'
|
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 { maxPosition, moveToPosition } from '../lib/playlistOrder'
|
||||||
|
import { COLUMN_MENU } from '../lib/trackColumns'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
/** The playlist's full entry order — never the sorted or filtered view, so
|
/** The playlist's full entry order — never the sorted or filtered view, so
|
||||||
* the move is safe whatever the table is showing. */
|
* the move is safe whatever the table is showing. */
|
||||||
order: string[]
|
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[]
|
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
|
onMove: (order: string[]) => void
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
/** "Set Position…": type where the first selected track should end up. */
|
/** "Key" / "Key descending, then BPM" — the sort as the Order choice names it. */
|
||||||
export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
|
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 moving = new Set(ids)
|
||||||
const count = order.filter((id) => moving.has(id)).length
|
const count = order.filter((id) => moving.has(id)).length
|
||||||
const max = maxPosition(order, moving)
|
const max = maxPosition(order, moving)
|
||||||
const current = order.findIndex((id) => moving.has(id)) + 1
|
const current = order.findIndex((id) => moving.has(id)) + 1
|
||||||
const [value, setValue] = useState(String(current))
|
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)
|
const input = useRef<HTMLInputElement>(null)
|
||||||
|
|
||||||
useEffect(() => input.current?.select(), [])
|
useEffect(() => input.current?.select(), [])
|
||||||
@@ -28,7 +45,7 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
|
|||||||
|
|
||||||
const submit = () => {
|
const submit = () => {
|
||||||
if (position === null) return
|
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)
|
if (next.some((id, i) => id !== order[i])) onMove(next)
|
||||||
onClose()
|
onClose()
|
||||||
}
|
}
|
||||||
@@ -41,6 +58,26 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
|
|||||||
return () => window.removeEventListener('keydown', onKey)
|
return () => window.removeEventListener('keydown', onKey)
|
||||||
}, [onClose])
|
}, [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}`)
|
const span = (p: number) => (count > 1 ? `#${p}–${p + count - 1}` : `#${p}`)
|
||||||
|
|
||||||
return createPortal(
|
return createPortal(
|
||||||
@@ -79,9 +116,24 @@ export function SetPositionDialog({ order, ids, onMove, onClose }: Props) {
|
|||||||
{position === null
|
{position === null
|
||||||
? 'Type a position.'
|
? 'Type a position.'
|
||||||
: count > 1
|
: 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)}`}
|
: `Now ${span(current)} · will be ${span(position)}`}
|
||||||
</div>
|
</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>
|
||||||
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
|
<div className="flex items-center justify-end gap-2 border-t border-line px-5 py-3">
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -77,7 +77,8 @@ interface Props {
|
|||||||
dragFrom?: { origin: TrackOrigin; playlistId?: string }
|
dragFrom?: { origin: TrackOrigin; playlistId?: string }
|
||||||
/** Delete/Backspace removes the selected tracks from this view. */
|
/** Delete/Backspace removes the selected tracks from this view. */
|
||||||
onRemove?: (ids: string[]) => void
|
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). */
|
/** Right-click on the header row (the column chooser). */
|
||||||
onHeaderContextMenu?: (x: number, y: number) => void
|
onHeaderContextMenu?: (x: number, y: number) => void
|
||||||
onPlay?: (track: Track) => void
|
onPlay?: (track: Track) => void
|
||||||
@@ -591,7 +592,7 @@ export function TrackTable({
|
|||||||
// Right-clicking outside the selection retargets it, so the
|
// Right-clicking outside the selection retargets it, so the
|
||||||
// menu always acts on what is highlighted.
|
// menu always acts on what is highlighted.
|
||||||
if (selection && !isSelected) selectOnly(vr.index)
|
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">
|
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
|
||||||
|
|||||||
@@ -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', () => {
|
describe('maxPosition', () => {
|
||||||
it('leaves room for the block behind its first track', () => {
|
it('leaves room for the block behind its first track', () => {
|
||||||
expect(maxPosition(order, new Set(['B']))).toBe(7)
|
expect(maxPosition(order, new Set(['B']))).toBe(7)
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
/** A playlist's entry order with `moving` lifted out as one block, kept in
|
/** A playlist's entry order with `moving` lifted out as one block and put
|
||||||
* their PLAYLIST order, and put back so the block's first track lands at the
|
* back so the block's first track lands at the 1-based `position` of the
|
||||||
* 1-based `position` of the result. A position past the end puts the block
|
* result. A position past the end puts the block last. The block keeps the
|
||||||
* last. A track listed twice in the playlist moves with both entries, since a
|
* tracks' PLAYLIST order, or with `blockOrder` (the ids as a sorted view
|
||||||
* selection is by track id. */
|
* shows them) that order instead — so a sort can be written into the
|
||||||
export function moveToPosition(order: string[], moving: ReadonlySet<string>, position: number): string[] {
|
* playlist. A track listed twice in the playlist moves with both entries,
|
||||||
const block = order.filter((id) => moving.has(id))
|
* 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 rest = order.filter((id) => !moving.has(id))
|
||||||
const at = Math.min(Math.max(Math.floor(position), 1) - 1, rest.length)
|
const at = Math.min(Math.max(Math.floor(position), 1) - 1, rest.length)
|
||||||
return [...rest.slice(0, at), ...block, ...rest.slice(at)]
|
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 {
|
export function maxPosition(order: string[], moving: ReadonlySet<string>): number {
|
||||||
return order.length - order.filter((id) => moving.has(id)).length + 1
|
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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user