@@ -72,4 +72,15 @@ export const scriptsApi = {
|
|||||||
deleteFolder: async (path: string): Promise<void> => {
|
deleteFolder: async (path: string): Promise<void> => {
|
||||||
await apiClient.delete(`/scripts/folder`, { data: { path } });
|
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.`);
|
alert(`"${value}" already exists.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const newFiles = renameNode(
|
|
||||||
files!,
|
const oldPath = dialog.node.path || dialog.node.name;
|
||||||
dialog.node.path || dialog.node.name,
|
const newPath = parentPath ? `${parentPath}/${value}` : value;
|
||||||
value,
|
|
||||||
);
|
// Сохраняем раскрытые папки
|
||||||
if (newFiles) {
|
const savedExpandedFolders = new Set(get().expandedFolders);
|
||||||
set({ files: newFiles });
|
|
||||||
|
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 });
|
set({ dialog: null });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user