mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented better track multiselect
This commit is contained in:
1 parent
6c123e2597
commit
07e85cfdc9
13 files changed
+155
-78
No files matched your search
@@ -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()}
|
||||
|
||||
@@ -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()}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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} />}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user