修改了configview页面的命名逻辑
This commit is contained in:
@@ -48,6 +48,22 @@ class ProjectService {
|
||||
const randomPart = `${Date.now().toString(36)}${Math.random().toString(36).substring(2, 8)}`;
|
||||
return `${prefix}${randomPart}`;
|
||||
}
|
||||
/**
|
||||
* [新增] 文件名/文件夹名净化:只允许中文、字母、数字、-、_
|
||||
*/
|
||||
sanitizeFileName(name) {
|
||||
// 允许中文、字母、数字、-、_
|
||||
let fileName = name.trim().toLowerCase();
|
||||
// 1. 替换所有空格为下划线
|
||||
fileName = fileName.replace(/\s+/g, '_');
|
||||
// 2. 移除所有不被允许的特殊字符(只保留中文、字母、数字、-、_)
|
||||
fileName = fileName.replace(/[^\u4e00-\u9fa5a-z0-9_-]/g, '');
|
||||
// 3. 确保不为空
|
||||
if (fileName.length === 0) {
|
||||
return 'config_file';
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
// =============== 项目相关方法 ===============
|
||||
getProjects() {
|
||||
return this.projects;
|
||||
@@ -294,20 +310,26 @@ class ProjectService {
|
||||
}
|
||||
async createConfig(name, containerId) {
|
||||
const newId = this.generateUniqueId('cfg', this.configs);
|
||||
// 使用新增的 sanitizeFileName 方法来处理文件名
|
||||
const newFileName = this.sanitizeFileName(name);
|
||||
const newConfig = {
|
||||
id: newId,
|
||||
name: name,
|
||||
fileName: name.toLowerCase().replace(/\s+/g, '_'),
|
||||
fileName: newFileName,
|
||||
containerId: containerId
|
||||
};
|
||||
this.configs.push(newConfig);
|
||||
await this.ensureContainerDirectoryExists(containerId);
|
||||
return newId;
|
||||
}
|
||||
updateConfigName(configId, newName) {
|
||||
/**
|
||||
* [修改] 同时更新配置显示名称和文件名称
|
||||
*/
|
||||
updateConfigName(configId, newName, newFileName) {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (config) {
|
||||
config.name = newName;
|
||||
config.fileName = newFileName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -319,6 +341,37 @@ class ProjectService {
|
||||
this.configs = this.configs.filter(c => c.id !== configId);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* [新增] 重命名磁盘上的配置文件
|
||||
*/
|
||||
async renameConfigFileOnDisk(configId, newFileName) {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (!config)
|
||||
return false;
|
||||
const oldFilePath = this.getConfigFilePath(configId);
|
||||
// 为了计算新路径,临时使用新的文件名
|
||||
const oldFileName = config.fileName;
|
||||
config.fileName = newFileName;
|
||||
const newFilePath = this.getConfigFilePath(configId);
|
||||
// 恢复旧的文件名,因为 ProjectService.updateConfigName 会在 ConfigPanel 中调用
|
||||
config.fileName = oldFileName;
|
||||
if (!oldFilePath || !newFilePath) {
|
||||
return false;
|
||||
}
|
||||
if (!fs.existsSync(oldFilePath)) {
|
||||
console.warn(`旧配置文件不存在,跳过磁盘重命名: ${oldFilePath}`);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await fs.promises.rename(oldFilePath, newFilePath);
|
||||
console.log(`✅ 已重命名配置文件: ${oldFilePath} -> ${newFilePath}`);
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`重命名配置文件失败: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
// =============== 模块文件夹相关方法 ===============
|
||||
getModuleFoldersByContainer(containerId) {
|
||||
return this.moduleFolders.filter(folder => folder.containerId === containerId);
|
||||
@@ -346,14 +399,17 @@ class ProjectService {
|
||||
this.moduleFolders = this.moduleFolders.filter(f => f.id !== folderId);
|
||||
return true;
|
||||
}
|
||||
renameModuleFolder(folderId, newName) {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder)
|
||||
return false;
|
||||
const oldName = folder.localPath.split('/').pop() || '';
|
||||
folder.localPath = folder.localPath.replace(/\/[^/]+$/, '/' + newName);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* [删除] 此方法已被 ConfigPanel 中的 updateModuleFolder 替换,以支持同步磁盘操作。
|
||||
*/
|
||||
// renameModuleFolder(folderId: string, newName: string): boolean {
|
||||
// const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
// if (!folder) return false;
|
||||
//
|
||||
// const oldName = folder.localPath.split('/').pop() || '';
|
||||
// folder.localPath = folder.localPath.replace(/\/[^/]+$/, '/' + newName);
|
||||
// return true;
|
||||
// }
|
||||
// =============== 文件系统操作 (新增/修改) ===============
|
||||
/**
|
||||
* 获取项目目录重命名所需的旧路径和新路径(新增)
|
||||
@@ -633,6 +689,7 @@ class ProjectService {
|
||||
const pathParts = folder.localPath.split('/').filter(part => part);
|
||||
if (pathParts.length < 4)
|
||||
return null;
|
||||
// 路径的最后一部分是文件夹的名称
|
||||
const folderName = pathParts[pathParts.length - 1];
|
||||
return path.join(projectPath, aircraft.name, container.name, folderName);
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user