feat: 添加重复名称检查功能,防止在同一目录下创建同名文件或目录,优化用户体验

This commit is contained in:
zhanggy committed 2025-10-28 19:14:19 +08:00
1 parent 5ca31a5348
commit ac5488a21d
3 files changed
+149 -189

No files matched your search

+148 -28
View File
@@ -537,6 +537,30 @@ const confirmAddDirectory = () => {
fullPath = parentPath + newDirectoryPath.value
}
// Check for duplicate names in the same parent directory
if (parentDirectoryForNewItems.value) {
// In the parent directory, check if a node with the same name already exists
const parentNode = findNodeInTreeByName(treeStructure.value, parentDirectoryForNewItems.value);
if (parentNode && parentNode.children) {
const duplicateExists = parentNode.children.some(child =>
child.name === newDirectoryPath.value
);
if (duplicateExists) {
showSnackbar('当前目录下已存在同名节点!', 'error');
return;
}
}
} else {
// Check at root level for duplicates
const duplicateExists = treeStructure.value.some(node =>
node.name === newDirectoryPath.value
);
if (duplicateExists) {
showSnackbar('根目录下已存在同名节点!', 'error');
return;
}
}
const newId = `dir-${Date.now()}-${Math.random()}`
// Check if we're editing an existing directory
@@ -663,7 +687,31 @@ const selectAndAddFile = async () => {
};
treeStructure.value = updateNodeInTree(treeStructure.value, currentEditingItem.value.path, updatedNode)
} else {
// Check for duplicates only when creating a new file
// Check for duplicate names in the same parent directory
if (parentDirectoryForNewItems.value) {
// In the parent directory, check if a node with the same name already exists
const parentNode = findNodeInTreeByName(treeStructure.value, parentDirectoryForNewItems.value);
if (parentNode && parentNode.children) {
const duplicateExists = parentNode.children.some(child =>
child.name === newFilePath.value
);
if (duplicateExists) {
showSnackbar('当前目录下已存在同名节点!', 'error');
return;
}
}
} else {
// Check at root level for duplicates
const duplicateExists = treeStructure.value.some(node =>
node.name === newFilePath.value
);
if (duplicateExists) {
showSnackbar('根目录下已存在同名节点!', 'error');
return;
}
}
// Also check for duplicates using the full path (for backward compatibility)
const hasDuplicate = checkForDuplicateInTree(treeStructure.value, fullPath);
if (hasDuplicate) {
showSnackbar('该文件路径已存在!')
@@ -681,24 +729,47 @@ const selectAndAddFile = async () => {
const relativePath = foundFile.replace(selectedSourcePath.value, '').substring(1) // Remove leading slash
const fullTargetPath = fullPath ? `${fullPath}/${relativePath}` : relativePath
// Check for duplicates
// Check for duplicates by full path
const hasDuplicateInTree = checkForDuplicateInTree(treeStructure.value, fullTargetPath);
if (!hasDuplicateInTree) {
const newFileNode: TreeNode = {
id: `file-${Date.now()}-${Math.random()}`,
name: fullTargetPath.split('/').pop() || fullTargetPath,
path: fullTargetPath,
type: 'file',
sourceFilePath: foundFile
}
// Add to tree structure
const fileName = fullTargetPath.split('/').pop() || fullTargetPath;
// Additionally, check for duplicate names in the same parent directory
let duplicateNameExists = false;
if (parentDirectoryForNewItems.value) {
// Add as child to the parent directory
treeStructure.value = addChildToNode(treeStructure.value, parentDirectoryForNewItems.value, newFileNode)
// In the parent directory, check if a node with the same name already exists
const parentNode = findNodeInTreeByName(treeStructure.value, parentDirectoryForNewItems.value);
if (parentNode && parentNode.children) {
duplicateNameExists = parentNode.children.some(child =>
child.name === fileName
);
}
} else {
// Add as root node
treeStructure.value.push(newFileNode)
// Check at root level for duplicates
duplicateNameExists = treeStructure.value.some(node =>
node.name === fileName
);
}
if (duplicateNameExists) {
showSnackbar(`当前目录下已存在同名节点:${fileName}`, 'error');
} else {
const newFileNode: TreeNode = {
id: `file-${Date.now()}-${Math.random()}`,
name: fileName,
path: fullTargetPath,
type: 'file',
sourceFilePath: foundFile
}
// Add to tree structure
if (parentDirectoryForNewItems.value) {
// Add as child to the parent directory
treeStructure.value = addChildToNode(treeStructure.value, parentDirectoryForNewItems.value, newFileNode)
} else {
// Add as root node
treeStructure.value.push(newFileNode)
}
}
}
}
@@ -722,21 +793,42 @@ const selectAndAddFile = async () => {
// If we have a selected file, just add it
if (selectedSourcePath.value) {
const newFileNode: TreeNode = {
id: newId,
name: newFilePath.value,
path: fullPath,
type: 'file',
sourceFilePath: selectedSourcePath.value
}
// Add to tree structure
// Check for duplicate names in the same parent directory
let duplicateNameExists = false;
if (parentDirectoryForNewItems.value) {
// Add as child to the parent directory
treeStructure.value = addChildToNode(treeStructure.value, parentDirectoryForNewItems.value, newFileNode)
// In the parent directory, check if a node with the same name already exists
const parentNode = findNodeInTreeByName(treeStructure.value, parentDirectoryForNewItems.value);
if (parentNode && parentNode.children) {
duplicateNameExists = parentNode.children.some(child =>
child.name === newFilePath.value
);
}
} else {
// Add as root node
treeStructure.value.push(newFileNode)
// Check at root level for duplicates
duplicateNameExists = treeStructure.value.some(node =>
node.name === newFilePath.value
);
}
if (duplicateNameExists) {
showSnackbar('当前目录下已存在同名节点!', 'error');
} else {
const newFileNode: TreeNode = {
id: newId,
name: newFilePath.value,
path: fullPath,
type: 'file',
sourceFilePath: selectedSourcePath.value
}
// Add to tree structure
if (parentDirectoryForNewItems.value) {
// Add as child to the parent directory
treeStructure.value = addChildToNode(treeStructure.value, parentDirectoryForNewItems.value, newFileNode)
} else {
// Add as root node
treeStructure.value.push(newFileNode)
}
}
// Reset selection state
@@ -875,6 +967,34 @@ const findNodeInTree = (nodes: TreeNode[], nodeId: string): TreeNode | null => {
return null;
}
// 通过节点名称查找节点的辅助函数
const findNodeInTreeByName = (nodes: TreeNode[], nodeName: string): TreeNode | null => {
for (const node of nodes) {
if (node.name === nodeName) {
return node;
}
if (node.children) {
const found = findNodeInTreeByName(node.children, nodeName);
if (found) {
return found;
}
}
}
return null;
}
// 在指定节点中通过名称查找子节点的辅助函数
const findChildNodeByName = (parentNode: TreeNode, childName: string): TreeNode | null => {
if (parentNode.children) {
for (const child of parentNode.children) {
if (child.name === childName) {
return child;
}
}
}
return null;
}
// Show dialog to edit directory
const showEditDirectoryDialog = (directory: TreeNode) => {
// Set up for editing an existing directory