mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented library rearrangement
This commit is contained in:
1 parent
89c7dade25
commit
7ba71981c8
3 files changed
+149
-15
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { api, type PlaylistKind, type PlaylistNode } from '../api'
|
import { api, type PlaylistKind, type PlaylistNode } from '../api'
|
||||||
import { useCaps } from '../lib/capabilities'
|
import { useCaps } from '../lib/capabilities'
|
||||||
@@ -12,8 +12,8 @@ import { ContextMenu, type MenuItem } from './ContextMenu'
|
|||||||
import { Icon, type IconName } from '../lib/icons'
|
import { Icon, type IconName } from '../lib/icons'
|
||||||
import { PlatformIcon } from '../lib/platformIcons'
|
import { PlatformIcon } from '../lib/platformIcons'
|
||||||
import { addTracksToPlaylist } from '../lib/addTracks'
|
import { addTracksToPlaylist } from '../lib/addTracks'
|
||||||
import { usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag'
|
import { useDragPayload, usePressToDrag, useDropTarget, type DragPayload } from '../lib/drag'
|
||||||
import { playlistIdsUnder } from '../lib/playlistTree'
|
import { nodeIdsUnder, playlistIdsUnder } from '../lib/playlistTree'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Which view the main table is showing.
|
* Which view the main table is showing.
|
||||||
@@ -108,8 +108,22 @@ interface RowActions {
|
|||||||
/** Tracks dropped on a playlist; `alt` (Option) moves them out of the
|
/** Tracks dropped on a playlist; `alt` (Option) moves them out of the
|
||||||
* playlist they came from. */
|
* playlist they came from. */
|
||||||
onDropTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>, alt: boolean) => void
|
onDropTracks: (node: PlaylistNode, p: Extract<DragPayload, { kind: 'tracks' }>, alt: boolean) => void
|
||||||
|
/** Whether a node may be dragged elsewhere in the tree. */
|
||||||
|
canMove: (node: PlaylistNode) => boolean
|
||||||
|
/** A node dropped in the tree: `index` among `parentId`'s children,
|
||||||
|
* counted without it (what `move_playlist` takes). */
|
||||||
|
onMoveNode: (node: PlaylistNode, parentId: string | null, index: number, into: PlaylistNode | null) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Where a dragged node would land, relative to the row under the pointer:
|
||||||
|
* `before`/`after` it, `into` it (a folder; appended), or `first` inside it —
|
||||||
|
* the lower edge of an EXPANDED folder, where the line sits above its first
|
||||||
|
* child. `tracks` is a track drop on a playlist; `peek` is tracks held over a
|
||||||
|
* collapsed folder, which opens it and drops nothing. */
|
||||||
|
type RowHint =
|
||||||
|
| { zone: 'tracks' | 'peek' }
|
||||||
|
| { zone: 'before' | 'after' | 'into' | 'first'; parentId: string | null; index: number }
|
||||||
|
|
||||||
/** The inline name field for a node being created. Enter or clicking away
|
/** The inline name field for a node being created. Enter or clicking away
|
||||||
* commits a non-empty name; Esc (or an empty name) creates nothing. */
|
* commits a non-empty name; Esc (or an empty name) creates nothing. */
|
||||||
function DraftRow({
|
function DraftRow({
|
||||||
@@ -160,6 +174,7 @@ function DraftRow({
|
|||||||
function NodeRow({
|
function NodeRow({
|
||||||
node,
|
node,
|
||||||
parentId,
|
parentId,
|
||||||
|
siblings,
|
||||||
depth,
|
depth,
|
||||||
source,
|
source,
|
||||||
onSelect,
|
onSelect,
|
||||||
@@ -172,6 +187,8 @@ function NodeRow({
|
|||||||
node: PlaylistNode
|
node: PlaylistNode
|
||||||
/** The folder this node sits in; null = the top level. */
|
/** The folder this node sits in; null = the top level. */
|
||||||
parentId: string | null
|
parentId: string | null
|
||||||
|
/** That folder's children, this node included, in order. */
|
||||||
|
siblings: PlaylistNode[]
|
||||||
depth: number
|
depth: number
|
||||||
source: Source
|
source: Source
|
||||||
onSelect: (s: Source) => void
|
onSelect: (s: Source) => void
|
||||||
@@ -203,26 +220,90 @@ function NodeRow({
|
|||||||
const startDrag = usePressToDrag(() => (renaming ? null : { kind: 'node', node, parentId }))
|
const startDrag = usePressToDrag(() => (renaming ? null : { kind: 'node', node, parentId }))
|
||||||
const moves = (p: DragPayload, alt: boolean) =>
|
const moves = (p: DragPayload, alt: boolean) =>
|
||||||
p.kind === 'tracks' && alt && !!p.fromPlaylist && p.fromPlaylist !== node.id
|
p.kind === 'tracks' && alt && !!p.fromPlaylist && p.fromPlaylist !== node.id
|
||||||
const [dropRef, over] = useDropTarget<true>({
|
|
||||||
hint: (p) => (p.kind === 'tracks' && actions.acceptsTracks(node, p) ? true : null),
|
// A node held over this row: which zone, and where that puts it. Never into
|
||||||
label: (p, _h, at) => `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`,
|
// itself or anything nested in it; a drop that would leave it exactly where
|
||||||
onDrop: (p, _h, at) => p.kind === 'tracks' && actions.onDropTracks(node, p, at.alt),
|
// it is shows nothing.
|
||||||
|
const placeNode = (p: Extract<DragPayload, { kind: 'node' }>, y: number, el: HTMLElement): RowHint | null => {
|
||||||
|
if (!actions.canMove(p.node) || nodeIdsUnder(p.node).has(node.id)) return null
|
||||||
|
const r = el.getBoundingClientRect()
|
||||||
|
const f = (y - r.top) / r.height
|
||||||
|
const opened = expanded && node.children.length > 0
|
||||||
|
const zone: 'before' | 'after' | 'into' | 'first' = node.can_contain_children
|
||||||
|
? f < 0.25 ? 'before' : f > 0.75 ? (opened ? 'first' : 'after') : 'into'
|
||||||
|
: f < 0.5 ? 'before' : 'after'
|
||||||
|
let target: string | null
|
||||||
|
let index: number
|
||||||
|
let current: PlaylistNode[]
|
||||||
|
if (zone === 'into' || zone === 'first') {
|
||||||
|
target = node.id
|
||||||
|
current = node.children
|
||||||
|
index = zone === 'first' ? 0 : node.children.filter((c) => c.id !== p.node.id).length
|
||||||
|
} else {
|
||||||
|
target = parentId
|
||||||
|
current = siblings
|
||||||
|
const i = siblings.filter((c) => c.id !== p.node.id).findIndex((c) => c.id === node.id)
|
||||||
|
index = zone === 'before' ? i : i + 1
|
||||||
|
}
|
||||||
|
if (target === p.parentId && current.findIndex((c) => c.id === p.node.id) === index) return null
|
||||||
|
return { zone, parentId: target, index }
|
||||||
|
}
|
||||||
|
const [dropRef, over] = useDropTarget<RowHint>({
|
||||||
|
hint: (p, at, el) => {
|
||||||
|
if (p.kind === 'node') return placeNode(p, at.y, el)
|
||||||
|
if (actions.acceptsTracks(node, p)) return { zone: 'tracks' }
|
||||||
|
return isFolder && !expanded ? { zone: 'peek' } : null
|
||||||
|
},
|
||||||
|
label: (p, h, at) => {
|
||||||
|
if (h.zone === 'tracks') return `${moves(p, at.alt) ? 'Move' : 'Add'} to ${node.name}`
|
||||||
|
if (h.zone === 'peek') return null
|
||||||
|
return h.zone === 'into' || h.zone === 'first' ? `Move into ${node.name}` : `Move ${h.zone} ${node.name}`
|
||||||
|
},
|
||||||
|
onDrop: (p, h, at) => {
|
||||||
|
if (p.kind === 'tracks') {
|
||||||
|
if (h.zone === 'tracks') actions.onDropTracks(node, p, at.alt)
|
||||||
|
} else if ('parentId' in h) {
|
||||||
|
actions.onMoveNode(p.node, h.parentId, h.index, h.zone === 'into' && !expanded ? node : null)
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
// Holding a drag over a collapsed folder opens it, so a playlist nested in
|
||||||
|
// it can be reached without letting go.
|
||||||
|
const opening = over?.zone === 'into' || over?.zone === 'peek'
|
||||||
|
useEffect(() => {
|
||||||
|
if (!opening || expanded) return
|
||||||
|
const t = setTimeout(() => setOpen(true), 600)
|
||||||
|
return () => clearTimeout(t)
|
||||||
|
}, [opening, expanded])
|
||||||
|
const ringed = over?.zone === 'tracks' || over?.zone === 'into'
|
||||||
|
const dragging = useDragPayload()
|
||||||
|
const beingMoved = dragging?.kind === 'node' && dragging.node.id === node.id
|
||||||
|
const line = over?.zone === 'before' || over?.zone === 'after' || over?.zone === 'first' ? over.zone : null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className={beingMoved ? 'opacity-40' : undefined}>
|
||||||
<div
|
<div
|
||||||
ref={dropRef}
|
ref={dropRef}
|
||||||
|
data-node-row
|
||||||
onPointerDown={startDrag}
|
onPointerDown={startDrag}
|
||||||
onContextMenu={(e) => actions.onContextMenu(e, node)}
|
onContextMenu={(e) => actions.onContextMenu(e, node)}
|
||||||
className={`group flex items-center gap-1 rounded-[10px] pr-1 text-sm transition-colors ${
|
className={`group relative flex items-center gap-1 rounded-[10px] pr-1 text-sm transition-colors ${
|
||||||
over
|
ringed
|
||||||
? 'bg-accent-soft/40 text-text ring-1 ring-accent'
|
? 'bg-accent-soft/40 text-text ring-1 ring-accent'
|
||||||
: selected
|
: selected
|
||||||
? 'is-selected font-medium text-text'
|
? 'is-selected font-medium text-text'
|
||||||
: 'text-muted hover:bg-ink-800 hover:text-text'
|
: 'text-muted hover:bg-ink-800 hover:text-text'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
|
{line && (
|
||||||
|
<span
|
||||||
|
aria-hidden
|
||||||
|
className={`pointer-events-none absolute right-1 h-0.5 rounded-full bg-accent ${
|
||||||
|
line === 'before' ? '-top-px' : '-bottom-px'
|
||||||
|
}`}
|
||||||
|
style={{ left: 8 + (line === 'first' ? depth + 1 : depth) * 14 }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (isFolder) setOpen((o) => !o)
|
if (isFolder) setOpen((o) => !o)
|
||||||
@@ -323,6 +404,7 @@ function NodeRow({
|
|||||||
key={c.id}
|
key={c.id}
|
||||||
node={c}
|
node={c}
|
||||||
parentId={node.id}
|
parentId={node.id}
|
||||||
|
siblings={node.children}
|
||||||
depth={depth + 1}
|
depth={depth + 1}
|
||||||
source={source}
|
source={source}
|
||||||
onSelect={onSelect}
|
onSelect={onSelect}
|
||||||
@@ -526,6 +608,17 @@ export function Sidebar({
|
|||||||
// nothing to the collection's playlists.
|
// nothing to the collection's playlists.
|
||||||
acceptsTracks: (node, p) =>
|
acceptsTracks: (node, p) =>
|
||||||
caps.writable && node.can_add_tracks && p.origin === 'collection' && p.fromPlaylist !== node.id,
|
caps.writable && node.can_add_tracks && p.origin === 'collection' && p.fromPlaylist !== node.id,
|
||||||
|
canMove: (node) => caps.writable && node.can_move,
|
||||||
|
onMoveNode: async (node, parentId, index, into) => {
|
||||||
|
try {
|
||||||
|
await api.movePlaylist(node.id, parentId, index)
|
||||||
|
invalidate()
|
||||||
|
// Into a COLLAPSED folder the node vanishes from view, so say where.
|
||||||
|
if (into) onDone(`Moved “${node.name}” into ${into.name}`)
|
||||||
|
} catch (e) {
|
||||||
|
onError((e as Error).message)
|
||||||
|
}
|
||||||
|
},
|
||||||
onDropTracks: async (node, p, alt) => {
|
onDropTracks: async (node, p, alt) => {
|
||||||
try {
|
try {
|
||||||
const msg = await addTracksToPlaylist({
|
const msg = await addTracksToPlaylist({
|
||||||
@@ -542,6 +635,22 @@ export function Sidebar({
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The empty space below the rows: a node dropped there goes to the END of
|
||||||
|
// the top level. Only the space itself — over a row that declined (a folder
|
||||||
|
// into its own subtree), nothing happens rather than this.
|
||||||
|
const roots = playlists ?? []
|
||||||
|
const [rootDropRef, rootOver] = useDropTarget<{ index: number }>({
|
||||||
|
hint: (p, at) => {
|
||||||
|
if (p.kind !== 'node' || !actions.canMove(p.node)) return null
|
||||||
|
if (document.elementFromPoint(at.x, at.y)?.closest('[data-node-row]')) return null
|
||||||
|
const index = roots.filter((n) => n.id !== p.node.id).length
|
||||||
|
if (p.parentId === null && roots.findIndex((n) => n.id === p.node.id) === index) return null
|
||||||
|
return { index }
|
||||||
|
},
|
||||||
|
label: () => 'Move to the top level',
|
||||||
|
onDrop: (p, h) => p.kind === 'node' && actions.onMoveNode(p.node, null, h.index, null),
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className="glass flex h-full w-64 shrink-0 flex-col">
|
<aside className="glass flex h-full w-64 shrink-0 flex-col">
|
||||||
{/* Which library is open — and the way to open a different one. Until
|
{/* Which library is open — and the way to open a different one. Until
|
||||||
@@ -607,6 +716,7 @@ export function Sidebar({
|
|||||||
{/* Right-click here (below the rows) creates at the top level; rows
|
{/* Right-click here (below the rows) creates at the top level; rows
|
||||||
stop the event so they get their own menu. */}
|
stop the event so they get their own menu. */}
|
||||||
<div
|
<div
|
||||||
|
ref={rootDropRef}
|
||||||
data-drag-scroll=""
|
data-drag-scroll=""
|
||||||
className="mt-1 min-h-24 flex-1 overflow-y-auto px-2 pb-4"
|
className="mt-1 min-h-24 flex-1 overflow-y-auto px-2 pb-4"
|
||||||
onContextMenu={(e) => openMenu(e, createItems(null))}
|
onContextMenu={(e) => openMenu(e, createItems(null))}
|
||||||
@@ -618,6 +728,7 @@ export function Sidebar({
|
|||||||
key={n.id}
|
key={n.id}
|
||||||
node={n}
|
node={n}
|
||||||
parentId={null}
|
parentId={null}
|
||||||
|
siblings={playlists}
|
||||||
depth={0}
|
depth={0}
|
||||||
source={source}
|
source={source}
|
||||||
onSelect={onSelect}
|
onSelect={onSelect}
|
||||||
@@ -628,6 +739,7 @@ export function Sidebar({
|
|||||||
actions={actions}
|
actions={actions}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
{rootOver && <div aria-hidden className="mx-2 mt-0.5 h-0.5 rounded-full bg-accent" />}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{menu && <ContextMenu {...menu} onClose={() => setMenu(null)} />}
|
{menu && <ContextMenu {...menu} onClose={() => setMenu(null)} />}
|
||||||
|
|||||||
@@ -154,18 +154,28 @@ export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Hold near the edge of a `data-drag-scroll` element to scroll it. Its
|
// Hold near the edge of a `data-drag-scroll` element to scroll it. Its
|
||||||
// value is the height covered at the top (a sticky header).
|
// value is the height covered at the top (a sticky header). The edge is
|
||||||
|
// thinner than a row and must be HELD for a moment first: otherwise the
|
||||||
|
// first or last visible row cannot be dropped on, because the list scrolls
|
||||||
|
// out from under a pointer resting on it.
|
||||||
let raf = 0
|
let raf = 0
|
||||||
|
const dwell = new Map<HTMLElement, number>()
|
||||||
const autoScroll = () => {
|
const autoScroll = () => {
|
||||||
const { x, y } = state
|
const { x, y } = state
|
||||||
let scrolled = false
|
let scrolled = false
|
||||||
document.querySelectorAll<HTMLElement>('[data-drag-scroll]').forEach((el) => {
|
document.querySelectorAll<HTMLElement>('[data-drag-scroll]').forEach((el) => {
|
||||||
const r = el.getBoundingClientRect()
|
const r = el.getBoundingClientRect()
|
||||||
if (x < r.left || x > r.right || y < r.top - 24 || y > r.bottom + 24) return
|
|
||||||
const top = r.top + Number(el.dataset.dragScroll || 0)
|
const top = r.top + Number(el.dataset.dragScroll || 0)
|
||||||
const edge = 40
|
const edge = 20
|
||||||
const dy = y < top + edge ? -Math.ceil((top + edge - y) / 4) : y > r.bottom - edge ? Math.ceil((y - r.bottom + edge) / 4) : 0
|
const inside = x >= r.left && x <= r.right && y >= r.top - 24 && y <= r.bottom + 24
|
||||||
if (!dy) return
|
const dy = !inside ? 0 : y < top + edge ? -Math.ceil((top + edge - y) / 3) : y > r.bottom - edge ? Math.ceil((y - r.bottom + edge) / 3) : 0
|
||||||
|
if (!dy) {
|
||||||
|
dwell.delete(el)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const since = dwell.get(el) ?? performance.now()
|
||||||
|
dwell.set(el, since)
|
||||||
|
if (performance.now() - since < 250) return
|
||||||
const before = el.scrollTop
|
const before = el.scrollTop
|
||||||
el.scrollTop += Math.max(-20, Math.min(20, dy))
|
el.scrollTop += Math.max(-20, Math.min(20, dy))
|
||||||
scrolled ||= el.scrollTop !== before
|
scrolled ||= el.scrollTop !== before
|
||||||
|
|||||||
@@ -8,3 +8,15 @@ export function playlistIdsUnder(node: PlaylistNode): string[] {
|
|||||||
const here = node.kind === 'folder' ? [] : [node.id]
|
const here = node.kind === 'folder' ? [] : [node.id]
|
||||||
return [...here, ...(node.children ?? []).flatMap(playlistIdsUnder)]
|
return [...here, ...(node.children ?? []).flatMap(playlistIdsUnder)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The ids of a node and everything nested in it — where a dragged folder
|
||||||
|
* cannot go. */
|
||||||
|
export function nodeIdsUnder(node: PlaylistNode): Set<string> {
|
||||||
|
const ids = new Set<string>()
|
||||||
|
const walk = (n: PlaylistNode) => {
|
||||||
|
ids.add(n.id)
|
||||||
|
n.children.forEach(walk)
|
||||||
|
}
|
||||||
|
walk(node)
|
||||||
|
return ids
|
||||||
|
}
|
||||||
Reference in new issue
Block a user