@@ -72,4 +72,15 @@ export const scriptsApi = {
|
||||
deleteFolder: async (path: string): Promise<void> => {
|
||||
await apiClient.delete(`/scripts/folder`, { data: { path } });
|
||||
},
|
||||
|
||||
rename: async (payload: {
|
||||
old_path: string;
|
||||
new_path: string;
|
||||
}): Promise<{ path: string }> => {
|
||||
const res = await apiClient.post<{ path: string }>(
|
||||
"/scripts/rename",
|
||||
payload,
|
||||
);
|
||||
return res.data;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -476,14 +476,45 @@ export const useIDEStore = create<IDEState>((set, get) => ({
|
||||
alert(`"${value}" already exists.`);
|
||||
return;
|
||||
}
|
||||
const newFiles = renameNode(
|
||||
files!,
|
||||
dialog.node.path || dialog.node.name,
|
||||
value,
|
||||
);
|
||||
if (newFiles) {
|
||||
set({ files: newFiles });
|
||||
|
||||
const oldPath = dialog.node.path || dialog.node.name;
|
||||
const newPath = parentPath ? `${parentPath}/${value}` : value;
|
||||
|
||||
// Сохраняем раскрытые папки
|
||||
const savedExpandedFolders = new Set(get().expandedFolders);
|
||||
|
||||
try {
|
||||
await scriptsApi.rename({ old_path: oldPath, new_path: newPath });
|
||||
await get().fetchTree();
|
||||
|
||||
// Восстанавливаем раскрытые папки
|
||||
set({ expandedFolders: savedExpandedFolders });
|
||||
|
||||
// Раскрываем родительскую цепочку
|
||||
const allParentPaths: string[] = [];
|
||||
let current = parentPath;
|
||||
while (current) {
|
||||
allParentPaths.push(current);
|
||||
const parts = current.split("/");
|
||||
parts.pop();
|
||||
current = parts.join("/");
|
||||
}
|
||||
autoExpandPaths(new Set(allParentPaths));
|
||||
|
||||
// Если переименованный файл был открыт — обновим его в openFiles
|
||||
const { openFiles, activeFile } = get();
|
||||
const updatedOpenFiles = openFiles.map((f) =>
|
||||
f.path === oldPath ? { ...f, name: value, path: newPath } : f,
|
||||
);
|
||||
set({ openFiles: updatedOpenFiles });
|
||||
|
||||
if (activeFile?.path === oldPath) {
|
||||
set({ activeFile: { ...activeFile, name: value, path: newPath } });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to rename:", e);
|
||||
}
|
||||
|
||||
set({ dialog: null });
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user