0
0

修复显示bug和项目回读bug

This commit is contained in:
xubing
2025-11-21 16:07:48 +08:00
parent 6d3d020f8a
commit 925024bce1
17 changed files with 193 additions and 243 deletions

View File

@@ -36,7 +36,6 @@ interface ProjectData {
aircrafts: Aircraft[];
containers: Container[];
configs: Config[];
projectPaths: { [key: string]: string };
}
export class ConfigPanel {
@@ -105,66 +104,66 @@ export class ConfigPanel {
}
private setupMessageListener() {
this.panel.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case 'openExistingProject':
await this.openExistingProject();
break;
case 'configureProject':
const selectedPath = await this.selectProjectPath(data.projectId, data.projectName);
if (selectedPath) {
this.panel.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case 'openExistingProject':
await this.openExistingProject();
break;
case 'configureProject':
const selectedPath = await this.selectProjectPath(data.projectId, data.projectName);
if (selectedPath) {
this.currentView = 'aircrafts';
this.currentProjectId = data.projectId;
this.updateWebview();
}
break;
case 'openProject':
// 已配置的项目直接打开
this.currentView = 'aircrafts';
this.currentProjectId = data.projectId;
this.updateWebview();
}
break;
case 'openProject':
// 已配置的项目直接打开
this.currentView = 'aircrafts';
this.currentProjectId = data.projectId;
this.updateWebview();
break;
case 'openAircraftConfig':
this.currentView = 'containers';
this.currentProjectId = data.projectId;
this.currentAircraftId = data.aircraftId;
this.updateWebview();
break;
case 'openContainerConfig':
this.currentView = 'configs';
this.currentContainerId = data.containerId;
this.updateWebview();
break;
// 修复返回按钮的消息处理
case 'goBackToProjects':
this.currentView = 'projects';
// 清空当前选择的ID
this.currentProjectId = '';
this.currentAircraftId = '';
this.currentContainerId = '';
this.updateWebview();
break;
case 'goBackToAircrafts':
this.currentView = 'aircrafts';
// 保持 currentProjectId清空其他ID
this.currentAircraftId = '';
this.currentContainerId = '';
this.updateWebview();
break;
case 'goBackToContainers':
this.currentView = 'containers';
// 保持 currentProjectId 和 currentAircraftId清空 currentContainerId
this.currentContainerId = '';
this.updateWebview();
break;
break;
case 'openAircraftConfig':
this.currentView = 'containers';
this.currentProjectId = data.projectId;
this.currentAircraftId = data.aircraftId;
this.updateWebview();
break;
case 'openContainerConfig':
this.currentView = 'configs';
this.currentContainerId = data.containerId;
this.updateWebview();
break;
// 修复返回按钮的消息处理
case 'goBackToProjects':
this.currentView = 'projects';
// 清空当前选择的ID
this.currentProjectId = '';
this.currentAircraftId = '';
this.currentContainerId = '';
this.updateWebview();
break;
case 'goBackToAircrafts':
this.currentView = 'aircrafts';
// 保持 currentProjectId清空其他ID
this.currentAircraftId = '';
this.currentContainerId = '';
this.updateWebview();
break;
case 'goBackToContainers':
this.currentView = 'containers';
// 保持 currentProjectId 和 currentAircraftId清空 currentContainerId
this.currentContainerId = '';
this.updateWebview();
break;
case 'updateProjectName':
await this.updateProjectName(data.projectId, data.name);
break;
@@ -251,27 +250,20 @@ export class ConfigPanel {
// === 数据持久化方法 ===
/**
* 保存所有数据到项目路径
* 保存当前项目数据到项目路径
*/
private async saveAllData(): Promise<void> {
private async saveCurrentProjectData(): Promise<void> {
try {
console.log('开始保存数据...');
console.log('开始保存当前项目数据...');
console.log('当前项目ID:', this.currentProjectId);
console.log('项目路径映射:', Array.from(this.projectPaths.entries()));
// 找到当前项目的路径
let projectPath: string | undefined;
// 首先尝试使用当前项目ID
if (this.currentProjectId) {
projectPath = this.projectPaths.get(this.currentProjectId);
if (!this.currentProjectId) {
console.log('未找到当前项目ID跳过保存数据');
vscode.window.showWarningMessage('未找到当前项目,数据将不会保存');
return;
}
// 如果没有找到,尝试使用第一个项目
if (!projectPath && this.projects.length > 0) {
projectPath = this.projectPaths.get(this.projects[0].id);
}
const projectPath = this.projectPaths.get(this.currentProjectId);
if (!projectPath) {
console.log('未找到项目路径,跳过保存数据');
vscode.window.showWarningMessage('未找到项目存储路径,数据将不会保存');
@@ -280,29 +272,33 @@ export class ConfigPanel {
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: ProjectData = {
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}`);
}
}
@@ -334,7 +330,6 @@ export class ConfigPanel {
this.aircrafts = [];
this.containers = [];
this.configs = [];
this.projectPaths = new Map();
// 验证数据格式并加载
if (data.projects && Array.isArray(data.projects)) {
@@ -349,21 +344,18 @@ export 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';
}
@@ -458,7 +450,7 @@ export class ConfigPanel {
vscode.window.showInformationMessage(`项目存储位置已设置: ${selectedPath}`);
// 保存初始数据
await this.saveAllData();
await this.saveCurrentProjectData();
return selectedPath;
}
@@ -485,7 +477,7 @@ export class ConfigPanel {
vscode.window.showInformationMessage(`项目存储位置已创建: ${pathInput}`);
// 保存初始数据
await this.saveAllData();
await this.saveCurrentProjectData();
return pathInput;
} catch (error) {
@@ -508,7 +500,7 @@ export class ConfigPanel {
if (project) {
project.name = newName;
vscode.window.showInformationMessage(`项目名称更新: ${newName}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
}
@@ -523,7 +515,6 @@ export class ConfigPanel {
this.projects.push(newProject);
vscode.window.showInformationMessage(`新建项目: ${name}`);
await this.saveAllData();
this.updateWebview();
}
@@ -546,7 +537,7 @@ export class ConfigPanel {
this.projectPaths.delete(projectId);
vscode.window.showInformationMessage(`删除项目: ${project.name}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
@@ -556,7 +547,7 @@ export class ConfigPanel {
if (aircraft) {
aircraft.name = newName;
vscode.window.showInformationMessage(`飞行器名称更新: ${newName}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
}
@@ -577,7 +568,7 @@ export class ConfigPanel {
this.aircrafts.push(newAircraft);
vscode.window.showInformationMessage(`新建飞行器: ${name}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
@@ -594,7 +585,7 @@ export class ConfigPanel {
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
vscode.window.showInformationMessage(`删除飞行器: ${aircraft.name}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
@@ -604,12 +595,12 @@ export class ConfigPanel {
if (container) {
container.name = newName;
vscode.window.showInformationMessage(`容器名称更新: ${newName}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
}
// 创建新容器 - 修复版本
// 创建新容器
private async createContainer(name: string) {
console.log('创建容器当前飞行器ID:', this.currentAircraftId);
@@ -654,7 +645,7 @@ export class ConfigPanel {
});
vscode.window.showInformationMessage(`新建容器: ${name} (包含2个默认配置文件)`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
@@ -670,7 +661,7 @@ export class ConfigPanel {
this.configs = this.configs.filter(cfg => cfg.containerId !== containerId);
vscode.window.showInformationMessage(`删除容器: ${container.name}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
@@ -680,7 +671,7 @@ export class ConfigPanel {
if (config) {
config.name = newName;
vscode.window.showInformationMessage(`配置名称更新: ${newName}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
}
@@ -691,7 +682,7 @@ export class ConfigPanel {
if (config) {
config.fileName = fileName;
vscode.window.showInformationMessage(`文件名更新: ${fileName}`);
await this.saveAllData();
await this.saveCurrentProjectData();
this.updateWebview();
}
}
@@ -702,14 +693,14 @@ export class ConfigPanel {
const newConfig: Config = {
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();
}
@@ -719,7 +710,7 @@ export 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();
}
}
@@ -786,7 +777,7 @@ export class ConfigPanel {
vscode.window.showInformationMessage(`配置文件已保存: ${fileUri.fsPath}`);
// 保存数据到JSON文件
await this.saveAllData();
await this.saveCurrentProjectData();
} catch (error) {
vscode.window.showErrorMessage(`保存文件时出错: ${error}`);
}
@@ -827,10 +818,12 @@ export 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':