Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/stores/workspaceStore.ts
Comment thread
Huynhthuongg marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function updateChildrenPaths(children: FileNode[], oldPath: string, newPath: str
return children.map((child) => {
const updatedChild: FileNode = {
...child,
path: child.path.replace(oldPath, newPath),
path: child.path.startsWith(oldPath) ? newPath + child.path.slice(oldPath.length) : child.path,
Comment thread
Huynhthuongg marked this conversation as resolved.
};
if (updatedChild.children) {
updatedChild.children = updateChildrenPaths(updatedChild.children, oldPath, newPath);
Expand All @@ -201,6 +201,20 @@ function updateChildrenPaths(children: FileNode[], oldPath: string, newPath: str
});
}

function deepCloneFileNode(node: FileNode, newParentPath: string): FileNode {
const cloned: FileNode = {
...node,
id: genId('file'),
path: `${newParentPath}/${node.name}`,
};
if (node.children) {
cloned.children = node.children.map((child) =>
deepCloneFileNode(child, cloned.path)
);
}
return cloned;
}

function addChildToFolder(
nodes: FileNode[],
parentPath: string,
Expand Down Expand Up @@ -492,6 +506,11 @@ export const useWorkspaceStore = create<WorkspaceState>((set, get) => ({
name: dupName,
path: `${parentPath}/${dupName}`,
};
if (node.children) {
newNode.children = node.children.map((child) =>
deepCloneFileNode(child, newNode.path)
);
}
if (!parentPath) {
return { files: [...s.files, newNode] };
}
Expand Down