directory back up (parent) button + drag entry into parent to move
This commit is contained in:
49
frontend/utils/files.ts
Normal file
49
frontend/utils/files.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { moveFile } from '~/lib/api/warrens';
|
||||
import type { DirectoryEntry } from '~/shared/types';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
let targetPath: string;
|
||||
|
||||
if (isParent) {
|
||||
targetPath = getParentPath(warrenStore.current.path);
|
||||
} else {
|
||||
targetPath = warrenStore.current.path;
|
||||
if (!targetPath.endsWith('/')) {
|
||||
targetPath += '/';
|
||||
}
|
||||
targetPath += entry.name;
|
||||
}
|
||||
|
||||
await moveFile(
|
||||
warrenStore.current.warrenId,
|
||||
warrenStore.current.path,
|
||||
fileName,
|
||||
targetPath
|
||||
);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user