import { copyFile, moveFile } from '~/lib/api/warrens'; import type { DirectoryEntry } from '~/shared/types'; export function joinPaths(path: string, ...other: string[]): string { for (const p of other) { if (!path.endsWith('/')) { path += '/'; } path += trim(p, '/'); } return path; } export function getParentPath(path: string): string { const sliceEnd = Math.max(1, path.lastIndexOf('/')); return path.slice(0, sliceEnd); } export function onDirectoryEntryDrop( entry: DirectoryEntry, isParent: boolean = false ): (e: DragEvent) => void { return async (e: DragEvent) => { const warrenStore = useWarrenStore(); if (e.dataTransfer == null || warrenStore.current == null) { return; } if (entry.fileType !== 'directory') { return; } const fileName = e.dataTransfer.getData('application/warren'); if (entry.name === fileName) { return; } const currentPath = joinPaths(warrenStore.current.path, fileName); let targetPath: string; if (isParent) { targetPath = joinPaths( getParentPath(warrenStore.current.path), fileName ); } else { targetPath = joinPaths( warrenStore.current.path, entry.name, fileName ); } await moveFile(warrenStore.current.warrenId, currentPath, targetPath); }; } export async function pasteFile( current: { warrenId: string; path: string; name: string }, target: { warrenId: string; path: string; name: string } ): Promise { if (current.warrenId !== target.warrenId) { throw new Error('Cross-warren copies are not supported yet'); } const { success } = await copyFile( current.warrenId, joinPaths(current.path, current.name), joinPaths(target.path, target.name) ); return success; }