Implemented drag to import

This commit is contained in:
patben8 committed 2026-10-13 17:42:24 +02:00
1 parent 87cbd3720e
commit a65ef959b2
14 files changed
+807 -114

No files matched your search

+85 -36
View File
@@ -18,6 +18,11 @@ import { Icon } from './icons'
* events for its own file drop on Windows, so HTML5 DnD silently dies in the
* packaged app.
*
* Files dragged in from Finder / Explorer ride the same store as a `files`
* payload: Tauri reports that drag (with real paths) instead of the webview,
* and `lib/fileDrop.ts` feeds it in through `nativeDragStart` / `Move` / `End`,
* so the same targets light up and the same ghost names what a drop would do.
*
* The state lives in a module-level store read through useSyncExternalStore
* with narrow selectors, so a pointer move re-renders only the target under it
* (and the ghost) — never the table.
@@ -39,6 +44,12 @@ export type DragPayload =
source: object
}
| { kind: 'node'; node: PlaylistNode; parentId: string | null }
/** Files and folders from the OS, as absolute paths. Not yet tracks: a
* target hands them to the drop resolver, which reads what is in them. */
| { kind: 'files'; paths: string[] }
/** What a playlist or export row takes: track rows, or files from the OS. */
export type TracksOrFiles = Exclude<DragPayload, { kind: 'node' }>
export interface DropPoint {
x: number
@@ -117,44 +128,26 @@ function evaluate() {
let detach: (() => void) | null = null
/** Start a drag. Call it once the press has moved past the source's threshold. */
export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt: boolean }) {
/** End the drag in progress, dropping on the target under the pointer if `drop`. */
function finish(drop: boolean) {
const { payload: p, target, hint, x, y, alt } = state
detach?.()
if (!drop || !p || !target) return
const spec = targets.get(target)
try {
spec?.current.onDrop(p, hint, { x, y, alt })
} catch (err) {
console.error('drop failed', err)
}
}
/** Publish a new drag and start edge auto-scroll. Returns what undoes both. */
function open(payload: DragPayload, at: { x: number; y: number; alt: boolean }): () => void {
detach?.()
set({ ...IDLE, payload, ...at })
document.documentElement.classList.add('is-dragging')
window.getSelection()?.removeAllRanges()
const onMove = (e: PointerEvent) => {
// One notification per move: evaluate() publishes the position with the hint.
state = { ...state, x: e.clientX, y: e.clientY, alt: e.altKey }
evaluate()
}
const finish = (drop: boolean) => {
const { payload: p, target, hint, x, y, alt } = state
detach?.()
if (!drop || !p || !target) return
const spec = targets.get(target)
try {
spec?.current.onDrop(p, hint, { x, y, alt })
} catch (err) {
console.error('drop failed', err)
}
}
const onUp = () => finish(true)
const onCancel = () => finish(false)
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape' && e.type === 'keydown') {
// Before every other Esc handler: cancelling the drag is all Esc does.
e.preventDefault()
e.stopImmediatePropagation()
finish(false)
return
}
if (e.key === 'Alt' && state.alt !== (e.type === 'keydown')) {
set({ alt: e.type === 'keydown' })
evaluate()
}
}
// 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). The edge is
// thinner than a row and must be HELD for a moment first: otherwise the
@@ -187,6 +180,37 @@ export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt:
}
raf = requestAnimationFrame(autoScroll)
return () => {
cancelAnimationFrame(raf)
document.documentElement.classList.remove('is-dragging')
set(IDLE)
}
}
/** Start a drag. Call it once the press has moved past the source's threshold. */
export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt: boolean }) {
const close = open(payload, at)
const onMove = (e: PointerEvent) => {
// One notification per move: evaluate() publishes the position with the hint.
state = { ...state, x: e.clientX, y: e.clientY, alt: e.altKey }
evaluate()
}
const onUp = () => finish(true)
const onCancel = () => finish(false)
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape' && e.type === 'keydown') {
// Before every other Esc handler: cancelling the drag is all Esc does.
e.preventDefault()
e.stopImmediatePropagation()
finish(false)
return
}
if (e.key === 'Alt' && state.alt !== (e.type === 'keydown')) {
set({ alt: e.type === 'keydown' })
evaluate()
}
}
window.addEventListener('pointermove', onMove)
window.addEventListener('pointerup', onUp)
window.addEventListener('pointercancel', onCancel)
@@ -195,14 +219,12 @@ export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt:
window.addEventListener('blur', onCancel)
detach = () => {
detach = null
cancelAnimationFrame(raf)
window.removeEventListener('pointermove', onMove)
window.removeEventListener('pointerup', onUp)
window.removeEventListener('pointercancel', onCancel)
window.removeEventListener('keydown', onKey, true)
window.removeEventListener('keyup', onKey, true)
window.removeEventListener('blur', onCancel)
document.documentElement.classList.remove('is-dragging')
// The pointerup that ended the drag is followed by a click on whatever
// is under it; that click is part of the drop, not a click of its own.
const swallow = (e: MouseEvent) => {
@@ -211,11 +233,34 @@ export function beginDrag(payload: DragPayload, at: { x: number; y: number; alt:
}
window.addEventListener('click', swallow, true)
setTimeout(() => window.removeEventListener('click', swallow, true), 0)
set(IDLE)
close()
}
evaluate()
}
/** A drag the OS owns (files from Finder / Explorer): it enters the window… */
export function nativeDragStart(payload: DragPayload, at: { x: number; y: number }) {
const close = open(payload, { ...at, alt: false })
detach = () => {
detach = null
close()
}
evaluate()
}
/** …moves over it (window coordinates, CSS pixels)… */
export function nativeDragMove(at: { x: number; y: number }) {
if (!state.payload) return
state = { ...state, ...at }
evaluate()
}
/** …and is dropped (or leaves the window: `drop` false). */
export function nativeDragEnd(drop: boolean, at?: { x: number; y: number }) {
if (drop && at) nativeDragMove(at)
finish(drop)
}
/** A pointerdown handler that starts a drag once the press moves 6 px. A plain
* click never moves that far, so the element keeps its click; a press on a
* field inside it (a rename input) never drags. `make` is asked at the moment
@@ -273,6 +318,10 @@ export function useDropTarget<H>(spec: DropSpec<H>): [(el: HTMLElement | null) =
function describe(p: DragPayload): { icon: 'music' | 'playlist' | 'folder' | 'smart'; text: string } {
if (p.kind === 'node')
return { icon: p.node.kind === 'folder' ? 'folder' : p.node.kind === 'smart' ? 'smart' : 'playlist', text: p.node.name }
if (p.kind === 'files')
return p.paths.length === 1
? { icon: 'music', text: p.paths[0].split(/[\\/]/).filter(Boolean).pop() ?? p.paths[0] }
: { icon: 'music', text: `${p.paths.length} items` }
if (p.ids.length === 1) {
const t = p.tracks[0]
return { icon: 'music', text: t ? [t.artist, t.title].filter(Boolean).join(' – ') || 'Untitled' : '1 track' }
+84
View File
@@ -0,0 +1,84 @@
import { useEffect, useRef } from 'react'
import { nativeDragEnd, nativeDragMove, nativeDragStart } from './drag'
/**
* Files dragged in from Finder / Explorer, fed into the window's ONE drag
* store (`lib/drag.tsx`) as a `files` payload, so the targets that take track
* rows — playlists, exports, the deck — take files too, and anywhere else is
* "add to the collection" (App's own target).
*
* Tauri only: the shell's webview hands the OS drag to Tauri, which reports it
* with real paths — what adding a file IN PLACE needs. A plain browser gives a
* page `File` objects without paths, so in a browser a file drop is only
* CAUGHT — left alone, the browser navigates to the dropped file and the app
* is gone — and `onBrowserDrop` says why nothing happened.
*
* `enabled` false (no library open yet) and an open dialog both refuse the
* drag at its start, before anything is highlighted.
*/
export function useNativeFileDrop(enabled: boolean, onBrowserDrop: () => void) {
const enabledRef = useRef(enabled)
enabledRef.current = enabled
const onBrowserDropRef = useRef(onBrowserDrop)
onBrowserDropRef.current = onBrowserDrop
useEffect(() => {
if (!('__TAURI_INTERNALS__' in window)) {
// Only drags carrying files: the app's own drags are pointer events,
// and a text drag into a field must keep working.
const hasFiles = (e: DragEvent) => !!e.dataTransfer?.types.includes('Files')
const over = (e: DragEvent) => {
if (hasFiles(e)) e.preventDefault()
}
const drop = (e: DragEvent) => {
if (!hasFiles(e)) return
e.preventDefault()
onBrowserDropRef.current()
}
window.addEventListener('dragover', over)
window.addEventListener('drop', drop)
return () => {
window.removeEventListener('dragover', over)
window.removeEventListener('drop', drop)
}
}
let unlisten: (() => void) | undefined
let cancelled = false
let active = false
void (async () => {
const { getCurrentWebview } = await import('@tauri-apps/api/webview')
const off = await getCurrentWebview().onDragDropEvent(({ payload }) => {
// Tauri reports PHYSICAL pixels; the page works in CSS pixels.
const scale = window.devicePixelRatio || 1
const at = 'position' in payload ? { x: payload.position.x / scale, y: payload.position.y / scale } : null
switch (payload.type) {
case 'enter':
active =
enabledRef.current &&
payload.paths.length > 0 &&
!document.querySelector('[aria-modal="true"]')
if (active && at) nativeDragStart({ kind: 'files', paths: payload.paths }, at)
break
case 'over':
if (active && at) nativeDragMove(at)
break
case 'drop':
if (active) nativeDragEnd(true, at ?? undefined)
active = false
break
case 'leave':
if (active) nativeDragEnd(false)
active = false
break
}
})
if (cancelled) off()
else unlisten = off
})()
return () => {
cancelled = true
unlisten?.()
if (active) nativeDragEnd(false)
}
}, [])
}