refactor frontend warren move api
This commit is contained in:
@@ -1,6 +1,18 @@
|
||||
import { 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);
|
||||
@@ -27,23 +39,22 @@ export function onDirectoryEntryDrop(
|
||||
return;
|
||||
}
|
||||
|
||||
const currentPath = joinPaths(warrenStore.current.path, fileName);
|
||||
let targetPath: string;
|
||||
|
||||
if (isParent) {
|
||||
targetPath = getParentPath(warrenStore.current.path);
|
||||
targetPath = joinPaths(
|
||||
getParentPath(warrenStore.current.path),
|
||||
fileName
|
||||
);
|
||||
} else {
|
||||
targetPath = warrenStore.current.path;
|
||||
if (!targetPath.endsWith('/')) {
|
||||
targetPath += '/';
|
||||
}
|
||||
targetPath += entry.name;
|
||||
targetPath = joinPaths(
|
||||
warrenStore.current.path,
|
||||
entry.name,
|
||||
fileName
|
||||
);
|
||||
}
|
||||
|
||||
await moveFile(
|
||||
warrenStore.current.warrenId,
|
||||
warrenStore.current.path,
|
||||
fileName,
|
||||
targetPath
|
||||
);
|
||||
await moveFile(warrenStore.current.warrenId, currentPath, targetPath);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,3 +16,14 @@ export function splitOnce(
|
||||
|
||||
return [str.slice(0, index), str.slice(index + 1)];
|
||||
}
|
||||
|
||||
export function trim(str: string, char: string) {
|
||||
let start = 0;
|
||||
let end = str.length;
|
||||
|
||||
while (start < end && str[start] === char) ++start;
|
||||
|
||||
while (end > start && str[end - 1] === char) --end;
|
||||
|
||||
return start > 0 || end < str.length ? str.substring(start, end) : str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user