Implemented better track multiselect

This commit is contained in:
patben8 committed 2026-09-25 10:27:00 +02:00
1 parent 6c123e2597
commit 07e85cfdc9
13 files changed
+155 -78

No files matched your search

+6 -1
View File
@@ -334,7 +334,12 @@ Two independent apps that talk over HTTP:
library replaces — plus the playlist tree +
create/rename/delete), `SaveBar`, `Toolbar` (search/filters + `ColumnsMenu`),
`TrackTable` (All Tracks — TanStack Table + **virtualized** grid; per-row play
button, configurable columns, inline double-click editing) and `PlaylistTable`
button, configurable columns, inline double-click editing, and **Finder-style
row selection** — click / Cmd-Ctrl+click / Shift+click over the SORTED rows,
↑/↓ (Shift extends), Cmd/Ctrl+A, Esc; shortcuts stand down while an
`aria-modal` dialog or `role="menu"` is open, so new dialogs need
`aria-modal="true"`. Right-click acts on the whole selection; single-track
items are hidden for a multi-selection) and `PlaylistTable`
(playlists — same columns/cells/play/inline-edit via shared `HeaderRow`/
`RowCells`/`PlayButton`, so the two views look identical, PLUS dnd-kit
drag-to-reorder + a remove button, header sorting disabled so the manual order
+28 -30
View File
@@ -50,7 +50,9 @@ export default function App() {
const [selected, setSelected] = useState<Set<string>>(new Set())
const [toast, setToast] = useState<ToastMsg | null>(null)
const [forcePicker, setForcePicker] = useState(false)
const [menu, setMenu] = useState<{ track: Track; x: number; y: number } | null>(null)
// `ids` is what the menu acts on: the whole selection when the clicked row is
// part of it, otherwise just that row.
const [menu, setMenu] = useState<{ track: Track; ids: string[]; x: number; y: number } | null>(null)
const [editing, setEditing] = useState<Track | null>(null)
const [showPaths, setShowPaths] = useState(false)
const [showHistory, setShowHistory] = useState(false)
@@ -394,16 +396,16 @@ export default function App() {
setSelected(new Set())
}
const toggle = (id: string) =>
setSelected((prev) => {
const next = new Set(prev)
next.has(id) ? next.delete(id) : next.add(id)
return next
// 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) =>
setMenu({
track,
ids: selectable && selected.has(track.id) ? [...selected] : [track.id],
x,
y,
})
const toggleAll = () =>
setSelected((prev) =>
prev.size === filtered.length ? new Set() : new Set(filtered.map((t) => t.id)),
)
return (
<CapabilitiesContext.Provider value={capabilities.data!}>
@@ -411,20 +413,26 @@ export default function App() {
<Toast toast={toast} onClose={() => setToast(null)} />
{menu && (
<ContextMenu
// Remount per opening: a right-click on another row while a menu is
// open must not be caught by the old menu's dismiss listener.
key={`${menu.track.id}:${menu.x}:${menu.y}`}
x={menu.x}
y={menu.y}
items={[
{ label: 'Load to Deck', onClick: () => setPrepTrack(menu.track) },
// Single-track actions are hidden, not disabled, for a multi-selection.
...(menu.ids.length === 1
? [{ label: 'Load to Deck', onClick: () => setPrepTrack(menu.track) }]
: []),
// Offering "Edit Tags…" on a read-only library would open a dialog
// whose every save is refused, so it is not offered at all.
...(canEdit
...(canEdit && menu.ids.length === 1
? [{ label: 'Edit Tags…', onClick: () => setEditing(menu.track) }]
: []),
// Adding to an export touches no library data, so it is offered even
// on a read-only one — an export is Konduktor's own curation.
...(exportSets.data ?? []).map((set) => ({
label: `Add to “${set.name}”`,
onClick: () => addToExport(set.id, [menu.track.id]),
onClick: () => addToExport(set.id, menu.ids),
})),
// Only on the export's ROOT view. Inside a referenced playlist there
// is nothing to remove: the reference is live, and per-track removal
@@ -437,7 +445,7 @@ export default function App() {
onClick: () =>
removeFromExport(
source.kind === 'export' ? source.id : source.exportId,
[menu.track.id],
menu.ids,
),
},
]
@@ -597,13 +605,8 @@ export default function App() {
tracks={filtered}
sorting={sorting}
onSortingChange={setSorting}
selection={{
selected,
onToggle: toggle,
onToggleAll: toggleAll,
allSelected: selected.size > 0 && selected.size === filtered.length,
}}
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
selection={{ selected, onChange: setSelected }}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
activeTrackId={prepTrack?.id ?? null}
@@ -636,7 +639,7 @@ export default function App() {
tracks={filtered}
sorting={sorting}
onSortingChange={setSorting}
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, false)}
onPlay={playTrack}
activeTrackId={prepTrack?.id ?? null}
columnVisibility={columnVisibility}
@@ -657,13 +660,8 @@ export default function App() {
tracks={filtered}
sorting={sorting}
onSortingChange={setSorting}
selection={{
selected,
onToggle: toggle,
onToggleAll: toggleAll,
allSelected: selected.size > 0 && selected.size === filtered.length,
}}
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
selection={{ selected, onChange: setSelected }}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, true)}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
activeTrackId={prepTrack?.id ?? null}
@@ -689,7 +687,7 @@ export default function App() {
) : (
<PlaylistTable
tracks={filtered}
onRowContextMenu={(track, x, y) => setMenu({ track, x, y })}
onRowContextMenu={(track, x, y) => openMenu(track, x, y, false)}
onPlay={playTrack}
onEditField={canEdit ? editField : undefined}
activeTrackId={prepTrack?.id ?? null}
+1 -1
View File
@@ -130,7 +130,7 @@ export function AutoCueDialog({ track, slotCount, slotLabel, existing, onClose,
}
return (
<div className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6" onClick={() => !busy && onClose()}>
<div aria-modal="true" className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6" onClick={() => !busy && onClose()}>
<div
className="flex max-h-[85vh] w-full max-w-xl flex-col overflow-hidden rounded-xl border border-line bg-ink-900 shadow-2xl"
onClick={(e) => e.stopPropagation()}
+11 -4
View File
@@ -1,4 +1,4 @@
import { useEffect } from 'react'
import { useEffect, useRef } from 'react'
export interface MenuItem {
label: string
@@ -15,9 +15,15 @@ interface Props {
// Lightweight fixed-position menu. Closes on outside click, scroll, or Escape.
export function ContextMenu({ x, y, items, onClose }: Props) {
// Read through a ref so the listeners are attached ONCE. Callers pass an
// inline `onClose`; if the effect depended on it, a click that re-renders the
// parent (e.g. selecting a row) would detach the listeners before the
// window's click handler ran, and the menu would never close.
const onCloseRef = useRef(onClose)
onCloseRef.current = onClose
useEffect(() => {
const close = () => onClose()
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
const close = () => onCloseRef.current()
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onCloseRef.current()
// Defer attaching outside-dismiss listeners by a tick so the very
// right-click/click that opened the menu can't immediately close it.
const id = setTimeout(() => {
@@ -33,10 +39,11 @@ export function ContextMenu({ x, y, items, onClose }: Props) {
window.removeEventListener('scroll', close, true)
window.removeEventListener('keydown', onKey)
}
}, [onClose])
}, [])
return (
<div
role="menu"
className="fixed z-50 min-w-[160px] overflow-hidden rounded-lg border border-line bg-ink-850 py-1 shadow-2xl"
style={{ top: Math.min(y, window.innerHeight - 100), left: Math.min(x, window.innerWidth - 180) }}
onClick={(e) => e.stopPropagation()}
+1 -1
View File
@@ -97,7 +97,7 @@ export function EditTagsDialog({ track, onClose, onApplied, onError }: Props) {
return (
<div
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
aria-modal="true" className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
onClick={onClose}
>
<div
+1 -1
View File
@@ -67,7 +67,7 @@ export function ExportDialog({ editing, onClose, onSaved, onError }: Props) {
onClose={() => setBrowsing(false)}
/>
)}
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div aria-modal="true" className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div className="w-full max-w-lg rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">
+1 -1
View File
@@ -101,7 +101,7 @@ export function ExportRunDialog({ set, onClose, onDone, onError }: Props) {
const blocked = p?.blocked ?? null
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div aria-modal="true" className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div className="w-full max-w-lg rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">Export “{set.name}”</h2>
+1 -1
View File
@@ -31,7 +31,7 @@ export function FolderPicker({ value, onChange, onClose }: Props) {
}
return (
<div className="fixed inset-0 z-[60] flex items-center justify-center bg-black/60 p-4">
<div aria-modal="true" className="fixed inset-0 z-[60] flex items-center justify-center bg-black/60 p-4">
<div className="flex h-[70vh] w-full max-w-2xl flex-col overflow-hidden rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">Choose a folder</h2>
+1 -1
View File
@@ -80,7 +80,7 @@ export function HistoryPanel({ onClose, onNotify, onError }: Props) {
return (
<div
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
aria-modal="true" className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
onClick={onClose}
>
<div
+1 -1
View File
@@ -153,7 +153,7 @@ export function ImportDialog({ playlistId, deviceLabel, onClose, onDone, onError
onClose={() => setBrowsing(false)}
/>
)}
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div aria-modal="true" className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
<div className="w-full max-w-lg rounded-lg border border-line bg-ink-900 shadow-xl">
<div className="border-b border-line px-5 py-3">
<h2 className="text-sm font-semibold text-text">
@@ -103,7 +103,7 @@ export function PathMappingDialog({ onClose, onNotify, onError }: Props) {
return (
<div
className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
aria-modal="true" className="fixed inset-0 z-40 flex items-center justify-center bg-black/60 p-6"
onClick={onClose}
>
<div
+101 -34
View File
@@ -47,11 +47,13 @@ const ROW_HEIGHT = 44
// every commit → infinite re-render loop. Playlists never sort, so share one array.
const NO_SORTING: SortingState = []
/** Row selection, Finder-style: click selects one row, Cmd/Ctrl+click toggles,
* Shift+click selects a range from the anchor (the last row clicked without
* Shift) in the table's SORTED order. The table owns the anchor; the caller
* owns the set. */
interface Selection {
selected: Set<string>
onToggle: (id: string) => void
onToggleAll: () => void
allSelected: boolean
onChange: (next: Set<string>) => void
}
interface CommonProps {
@@ -194,7 +196,6 @@ function HeaderRow({
lead,
trail,
hasPlay,
selection,
}: {
headers: Header<Track, unknown>[]
sensors: ReturnType<typeof useSensors>
@@ -202,7 +203,6 @@ function HeaderRow({
lead?: boolean // playlist drag-handle spacer
trail?: boolean // playlist remove spacer
hasPlay: boolean
selection?: Selection
}) {
// True while (and just after) a column is being reordered, so the trailing
// click a drag emits is swallowed instead of toggling the sort.
@@ -225,19 +225,7 @@ function HeaderRow({
>
<div className="sticky top-0 z-10 flex border-b border-line bg-ink-850">
{lead && <span className="w-8 shrink-0" />}
{selection ? (
<span className="flex w-10 shrink-0 items-center justify-center">
<input
type="checkbox"
checked={selection.allSelected}
onChange={selection.onToggleAll}
className="accent-accent"
title="Select all"
/>
</span>
) : (
<span className="w-10 shrink-0" />
)}
<span className="w-10 shrink-0" />
{hasPlay && <span className="w-9 shrink-0" />}
<SortableContext
items={headers.map((h) => h.column.id)}
@@ -311,15 +299,96 @@ export function TrackTable({
onColumnOrderChange(arrayMove(columnOrder, from, to))
}
// Range selection runs from the anchor; arrow keys move from the lead (the
// row most recently clicked or arrowed to). Both are ids, not indices, so a
// re-sort keeps them on the same track.
const anchorRef = useRef<string | null>(null)
const leadRef = useRef<string | null>(null)
const indexOf = (id: string | null) => (id == null ? -1 : rows.findIndex((r) => r.original.id === id))
const rangeIds = (a: number, b: number) =>
rows.slice(Math.min(a, b), Math.max(a, b) + 1).map((r) => r.original.id)
const selectOnly = (index: number) => {
const id = rows[index].original.id
anchorRef.current = leadRef.current = id
selection?.onChange(new Set([id]))
}
const clickRow = (index: number, e: { shiftKey: boolean; metaKey: boolean; ctrlKey: boolean }) => {
if (!selection) return
const id = rows[index].original.id
const toggle = e.metaKey || e.ctrlKey
const anchor = indexOf(anchorRef.current)
if (e.shiftKey && anchor >= 0) {
const range = rangeIds(anchor, index)
selection.onChange(toggle ? new Set([...selection.selected, ...range]) : new Set(range))
leadRef.current = id
} else if (toggle) {
const next = new Set(selection.selected)
next.has(id) ? next.delete(id) : next.add(id)
anchorRef.current = leadRef.current = id
selection.onChange(next)
} else {
selectOnly(index)
}
}
// Keyboard: ↑/↓ move the selection (Shift extends it), Cmd/Ctrl+A selects
// every visible row, Esc clears. Read through a ref so the listener is
// attached once. ←/→, Space, C and digits belong to the deck (PrepStrip).
const keysRef = useRef<(e: KeyboardEvent) => void>(() => {})
keysRef.current = (e) => {
if (!selection || rows.length === 0) return
const t = e.target as HTMLElement | null
if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.tagName === 'SELECT' || t.isContentEditable)) return
// A dialog or menu on top owns the keyboard (and its own Esc).
if (document.querySelector('[aria-modal="true"], [role="menu"]')) return
if ((e.metaKey || e.ctrlKey) && !e.shiftKey && !e.altKey && e.code === 'KeyA') {
e.preventDefault()
selection.onChange(new Set(rows.map((r) => r.original.id)))
return
}
if (e.metaKey || e.ctrlKey || e.altKey) return
if (e.key === 'Escape') {
if (selection.selected.size > 0) selection.onChange(new Set())
return
}
if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return
e.preventDefault()
const lead = indexOf(leadRef.current)
const step = e.key === 'ArrowDown' ? 1 : -1
const next = lead < 0 ? 0 : Math.max(0, Math.min(rows.length - 1, lead + step))
const anchor = indexOf(anchorRef.current)
if (e.shiftKey && anchor >= 0) {
leadRef.current = rows[next].original.id
selection.onChange(new Set(rangeIds(anchor, next)))
} else {
selectOnly(next)
}
virtualizer.scrollToIndex(next)
}
useEffect(() => {
const onKey = (e: KeyboardEvent) => keysRef.current(e)
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [])
return (
<div ref={parentRef} className="h-full overflow-auto">
<div
ref={parentRef}
className="h-full overflow-auto"
onClick={(e) => {
// A click in the empty space below the rows clears the selection.
const t = e.target as HTMLElement
if (selection && !t.closest('[data-row]') && !t.closest('.sticky')) selection.onChange(new Set())
}}
>
<div style={{ width: totalWidth, minWidth: '100%' }}>
<HeaderRow
headers={headers}
sensors={sensors}
onColumnDragEnd={onColumnDragEnd}
hasPlay={hasPlay}
selection={selection}
/>
<div style={{ height: virtualizer.getTotalSize(), position: 'relative' }}>
{virtualRows.map((vr) => {
@@ -329,30 +398,28 @@ export function TrackTable({
return (
<div
key={row.id}
data-row
className={`group absolute left-0 flex items-center border-b border-ink-850 text-sm ${
isSelected ? 'bg-accent-soft/50' : 'hover:bg-ink-850'
}`}
style={{ top: 0, transform: `translateY(${vr.start}px)`, height: vr.size, width: '100%' }}
onMouseDown={(e) => {
// Shift/Cmd-click would otherwise also select page text.
if (selection && (e.shiftKey || e.metaKey || e.ctrlKey)) e.preventDefault()
}}
onClick={(e) => selection && clickRow(vr.index, e)}
onContextMenu={(e) => {
if (!onRowContextMenu) return
e.preventDefault()
// 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)
}}
>
{selection ? (
<span className="flex w-10 shrink-0 items-center justify-center">
<input
type="checkbox"
checked={isSelected}
onChange={() => selection.onToggle(row.original.id)}
className="accent-accent"
/>
</span>
) : (
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
{vr.index + 1}
</span>
)}
<span className="w-10 shrink-0 pr-2 text-right text-xs tabular-nums text-faint">
{vr.index + 1}
</span>
{hasPlay && (
<span className="flex w-9 shrink-0 items-center justify-center">
{onPlay && <PlayButton track={row.original} isActive={isActive} onPlay={onPlay} />}
+1 -1
View File
@@ -148,7 +148,7 @@ export function UpdateCheck() {
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-6"
aria-modal="true" className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-6"
onClick={close}
>
<div