修复显示bug和项目回读bug
This commit is contained in:
@@ -168,50 +168,48 @@ class ConfigPanel {
|
||||
}
|
||||
// === 数据持久化方法 ===
|
||||
/**
|
||||
* 保存所有数据到项目路径
|
||||
* 保存当前项目数据到项目路径
|
||||
*/
|
||||
async saveAllData() {
|
||||
async saveCurrentProjectData() {
|
||||
try {
|
||||
console.log('开始保存数据...');
|
||||
console.log('开始保存当前项目数据...');
|
||||
console.log('当前项目ID:', this.currentProjectId);
|
||||
console.log('项目路径映射:', Array.from(this.projectPaths.entries()));
|
||||
// 找到当前项目的路径
|
||||
let projectPath;
|
||||
// 首先尝试使用当前项目ID
|
||||
if (this.currentProjectId) {
|
||||
projectPath = this.projectPaths.get(this.currentProjectId);
|
||||
}
|
||||
// 如果没有找到,尝试使用第一个项目
|
||||
if (!projectPath && this.projects.length > 0) {
|
||||
projectPath = this.projectPaths.get(this.projects[0].id);
|
||||
if (!this.currentProjectId) {
|
||||
console.log('未找到当前项目ID,跳过保存数据');
|
||||
vscode.window.showWarningMessage('未找到当前项目,数据将不会保存');
|
||||
return;
|
||||
}
|
||||
const projectPath = this.projectPaths.get(this.currentProjectId);
|
||||
if (!projectPath) {
|
||||
console.log('未找到项目路径,跳过保存数据');
|
||||
vscode.window.showWarningMessage('未找到项目存储路径,数据将不会保存');
|
||||
return;
|
||||
}
|
||||
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
|
||||
// 只保存与当前项目相关的数据
|
||||
const currentProjectAircrafts = this.aircrafts.filter(a => a.projectId === this.currentProjectId);
|
||||
const currentAircraftIds = currentProjectAircrafts.map(a => a.id);
|
||||
const currentProjectContainers = this.containers.filter(c => currentAircraftIds.includes(c.aircraftId));
|
||||
const currentContainerIds = currentProjectContainers.map(c => c.id);
|
||||
const currentProjectConfigs = this.configs.filter(cfg => currentContainerIds.includes(cfg.containerId));
|
||||
const data = {
|
||||
projects: this.projects,
|
||||
aircrafts: this.aircrafts,
|
||||
containers: this.containers,
|
||||
configs: this.configs,
|
||||
projectPaths: Object.fromEntries(this.projectPaths)
|
||||
projects: this.projects.filter(p => p.id === this.currentProjectId),
|
||||
aircrafts: currentProjectAircrafts,
|
||||
containers: currentProjectContainers,
|
||||
configs: currentProjectConfigs
|
||||
};
|
||||
console.log('要保存的数据:', {
|
||||
projects: this.projects.length,
|
||||
aircrafts: this.aircrafts.length,
|
||||
containers: this.containers.length,
|
||||
configs: this.configs.length,
|
||||
projectPaths: this.projectPaths.size
|
||||
console.log('要保存的当前项目数据:', {
|
||||
projects: data.projects.length,
|
||||
aircrafts: data.aircrafts.length,
|
||||
containers: data.containers.length,
|
||||
configs: data.configs.length
|
||||
});
|
||||
const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2));
|
||||
await vscode.workspace.fs.writeFile(dataUri, uint8Array);
|
||||
console.log('项目数据已保存到:', dataUri.fsPath);
|
||||
vscode.window.showInformationMessage('项目数据已保存');
|
||||
console.log('当前项目数据已保存到:', dataUri.fsPath);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('保存项目数据时出错:', error);
|
||||
console.error('保存当前项目数据时出错:', error);
|
||||
vscode.window.showErrorMessage(`保存项目数据失败: ${error}`);
|
||||
}
|
||||
}
|
||||
@@ -239,7 +237,6 @@ class ConfigPanel {
|
||||
this.aircrafts = [];
|
||||
this.containers = [];
|
||||
this.configs = [];
|
||||
this.projectPaths = new Map();
|
||||
// 验证数据格式并加载
|
||||
if (data.projects && Array.isArray(data.projects)) {
|
||||
this.projects = data.projects;
|
||||
@@ -253,19 +250,16 @@ class ConfigPanel {
|
||||
if (data.configs && Array.isArray(data.configs)) {
|
||||
this.configs = data.configs;
|
||||
}
|
||||
if (data.projectPaths && typeof data.projectPaths === 'object') {
|
||||
this.projectPaths = new Map(Object.entries(data.projectPaths));
|
||||
}
|
||||
console.log('加载后的数据状态:', {
|
||||
projects: this.projects.length,
|
||||
aircrafts: this.aircrafts.length,
|
||||
containers: this.containers.length,
|
||||
configs: this.configs.length,
|
||||
projectPaths: this.projectPaths.size
|
||||
configs: this.configs.length
|
||||
});
|
||||
// 设置当前项目为第一个项目(如果有的话)
|
||||
if (this.projects.length > 0) {
|
||||
this.currentProjectId = this.projects[0].id;
|
||||
this.projectPaths.set(this.currentProjectId, projectPath);
|
||||
this.currentView = 'aircrafts';
|
||||
}
|
||||
vscode.window.showInformationMessage(`项目数据已从 ${projectPath} 加载`);
|
||||
@@ -344,7 +338,7 @@ class ConfigPanel {
|
||||
this.projectPaths.set(projectId, selectedPath);
|
||||
vscode.window.showInformationMessage(`项目存储位置已设置: ${selectedPath}`);
|
||||
// 保存初始数据
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
return selectedPath;
|
||||
}
|
||||
}
|
||||
@@ -368,7 +362,7 @@ class ConfigPanel {
|
||||
this.projectPaths.set(projectId, pathInput);
|
||||
vscode.window.showInformationMessage(`项目存储位置已创建: ${pathInput}`);
|
||||
// 保存初始数据
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
return pathInput;
|
||||
}
|
||||
catch (error) {
|
||||
@@ -390,7 +384,7 @@ class ConfigPanel {
|
||||
if (project) {
|
||||
project.name = newName;
|
||||
vscode.window.showInformationMessage(`项目名称更新: ${newName}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
@@ -403,7 +397,6 @@ class ConfigPanel {
|
||||
};
|
||||
this.projects.push(newProject);
|
||||
vscode.window.showInformationMessage(`新建项目: ${name}`);
|
||||
await this.saveAllData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 删除项目
|
||||
@@ -424,7 +417,7 @@ class ConfigPanel {
|
||||
// 删除项目路径映射
|
||||
this.projectPaths.delete(projectId);
|
||||
vscode.window.showInformationMessage(`删除项目: ${project.name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 更新飞行器名
|
||||
@@ -433,7 +426,7 @@ class ConfigPanel {
|
||||
if (aircraft) {
|
||||
aircraft.name = newName;
|
||||
vscode.window.showInformationMessage(`飞行器名称更新: ${newName}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
@@ -451,7 +444,7 @@ class ConfigPanel {
|
||||
};
|
||||
this.aircrafts.push(newAircraft);
|
||||
vscode.window.showInformationMessage(`新建飞行器: ${name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 删除飞行器
|
||||
@@ -466,7 +459,7 @@ class ConfigPanel {
|
||||
const containerIds = this.containers.filter(c => c.aircraftId === aircraftId).map(c => c.id);
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
vscode.window.showInformationMessage(`删除飞行器: ${aircraft.name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 更新容器名
|
||||
@@ -475,11 +468,11 @@ class ConfigPanel {
|
||||
if (container) {
|
||||
container.name = newName;
|
||||
vscode.window.showInformationMessage(`容器名称更新: ${newName}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
// 创建新容器 - 修复版本
|
||||
// 创建新容器
|
||||
async createContainer(name) {
|
||||
console.log('创建容器,当前飞行器ID:', this.currentAircraftId);
|
||||
if (!this.currentAircraftId) {
|
||||
@@ -516,7 +509,7 @@ class ConfigPanel {
|
||||
configs: this.configs.length
|
||||
});
|
||||
vscode.window.showInformationMessage(`新建容器: ${name} (包含2个默认配置文件)`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 删除容器
|
||||
@@ -529,7 +522,7 @@ class ConfigPanel {
|
||||
// 删除相关的配置
|
||||
this.configs = this.configs.filter(cfg => cfg.containerId !== containerId);
|
||||
vscode.window.showInformationMessage(`删除容器: ${container.name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 更新配置名
|
||||
@@ -538,7 +531,7 @@ class ConfigPanel {
|
||||
if (config) {
|
||||
config.name = newName;
|
||||
vscode.window.showInformationMessage(`配置名称更新: ${newName}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
@@ -548,7 +541,7 @@ class ConfigPanel {
|
||||
if (config) {
|
||||
config.fileName = fileName;
|
||||
vscode.window.showInformationMessage(`文件名更新: ${fileName}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
@@ -558,13 +551,13 @@ class ConfigPanel {
|
||||
const newConfig = {
|
||||
id: newId,
|
||||
name: name,
|
||||
fileName: name.toLowerCase().replace(/\s+/g, '_') + '.yaml',
|
||||
fileName: name.toLowerCase().replace(/\s+/g, '_'),
|
||||
content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`,
|
||||
containerId: this.currentContainerId
|
||||
};
|
||||
this.configs.push(newConfig);
|
||||
vscode.window.showInformationMessage(`新建配置: ${name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
// 删除配置文件
|
||||
@@ -573,7 +566,7 @@ class ConfigPanel {
|
||||
if (config) {
|
||||
this.configs = this.configs.filter(c => c.id !== configId);
|
||||
vscode.window.showInformationMessage(`删除配置: ${config.name}`);
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
this.updateWebview();
|
||||
}
|
||||
}
|
||||
@@ -630,7 +623,7 @@ class ConfigPanel {
|
||||
config.content = content;
|
||||
vscode.window.showInformationMessage(`配置文件已保存: ${fileUri.fsPath}`);
|
||||
// 保存数据到JSON文件
|
||||
await this.saveAllData();
|
||||
await this.saveCurrentProjectData();
|
||||
}
|
||||
catch (error) {
|
||||
vscode.window.showErrorMessage(`保存文件时出错: ${error}`);
|
||||
@@ -670,9 +663,11 @@ class ConfigPanel {
|
||||
});
|
||||
case 'containers':
|
||||
const currentProject = this.projects.find(p => p.id === this.currentProjectId);
|
||||
const currentAircraft = this.aircrafts.find(a => a.id === this.currentAircraftId);
|
||||
const projectContainers = this.containers.filter(c => c.aircraftId === this.currentAircraftId);
|
||||
return this.containerView.render({
|
||||
project: currentProject,
|
||||
aircraft: currentAircraft,
|
||||
containers: projectContainers
|
||||
});
|
||||
case 'configs':
|
||||
|
||||
Reference in New Issue
Block a user