0
0

新增加了项目管理页面

This commit is contained in:
xubing
2025-11-18 14:03:22 +08:00
parent 64a411e463
commit 53e9307157
12 changed files with 974 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigPanel = void 0;
// src/panels/ConfigPanel.ts
const vscode = require("vscode");
const ProjectManagementView_1 = require("./views/ProjectManagementView");
const ProjectListView_1 = require("./views/ProjectListView");
const AircraftConfigView_1 = require("./views/AircraftConfigView");
const ContainerConfigView_1 = require("./views/ContainerConfigView");
@@ -21,14 +22,14 @@ class ConfigPanel {
ConfigPanel.currentPanel = new ConfigPanel(panel, extensionUri);
}
constructor(panel, extensionUri) {
this.currentView = 'projects';
this.currentView = 'management';
this.currentProjectId = '';
this.currentAircraftId = '';
this.currentContainerId = '';
// 数据存储
this.projects = [
{ id: 'p1', name: '飞行器1' },
{ id: 'p2', name: '飞行器2' }
{ id: 'p1', name: '项目1' },
{ id: 'p2', name: '项目2' }
];
this.aircrafts = [
{ id: 'a1', name: '飞行器配置1', projectId: 'p1' },
@@ -45,9 +46,12 @@ class ConfigPanel {
{ id: 'cfg3', name: '配置1', fileName: 'docker-compose.yml', content: '# 配置1内容', containerId: 'c2' },
{ id: 'cfg4', name: '配置1', fileName: 'config.yaml', content: '# 配置1内容', containerId: 'c3' }
];
// 项目存储路径映射
this.projectPaths = new Map();
this.panel = panel;
this.extensionUri = extensionUri;
// 初始化各个视图
this.projectManagementView = new ProjectManagementView_1.ProjectManagementView(extensionUri);
this.projectListView = new ProjectListView_1.ProjectListView(extensionUri);
this.aircraftConfigView = new AircraftConfigView_1.AircraftConfigView(extensionUri);
this.containerConfigView = new ContainerConfigView_1.ContainerConfigView(extensionUri);
@@ -60,6 +64,20 @@ class ConfigPanel {
setupMessageListener() {
this.panel.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case 'configureProject':
const selectedPath = await this.selectProjectPath(data.projectId, data.projectName);
if (selectedPath) {
this.currentView = 'projects';
this.currentProjectId = data.projectId;
this.updateWebview();
}
break;
case 'openProject':
// 已配置的项目直接打开
this.currentView = 'projects';
this.currentProjectId = data.projectId;
this.updateWebview();
break;
case 'openAircraftConfig':
this.currentView = 'aircrafts';
this.currentProjectId = data.projectId;
@@ -75,6 +93,10 @@ class ConfigPanel {
this.currentContainerId = data.containerId;
this.updateWebview();
break;
case 'goBackToManagement':
this.currentView = 'management';
this.updateWebview();
break;
case 'goBackToProjects':
this.currentView = 'projects';
this.updateWebview();
@@ -98,11 +120,14 @@ class ConfigPanel {
case 'updateConfigName':
this.updateConfigName(data.configId, data.name);
break;
case 'updateConfigFileName':
await this.updateConfigFileName(data.configId, data.fileName);
break;
case 'createConfig':
this.createConfig(data.name);
break;
case 'saveConfigFile':
this.saveConfigFile(data.configId, data.content);
await this.saveConfigFileToDisk(data.configId, data.content);
break;
case 'loadConfigFile':
this.loadConfigFile(data.configId);
@@ -113,12 +138,83 @@ class ConfigPanel {
case 'deleteContainer':
this.deleteContainer(data.containerId);
break;
case 'deleteConfig': // 新增:删除配置文件的处理
case 'deleteConfig':
this.deleteConfig(data.configId);
break;
}
});
}
// === 项目路径选择 ===
async selectProjectPath(projectId, projectName) {
try {
// 提供两种方式:选择现有路径或输入新路径
const choice = await vscode.window.showQuickPick([
{
label: '$(folder) 选择现有文件夹',
description: '从文件系统中选择已存在的文件夹',
value: 'select'
},
{
label: '$(new-folder) 创建新文件夹',
description: '输入新文件夹路径(将自动创建)',
value: 'create'
}
], {
placeHolder: '选择项目存储方式'
});
if (!choice) {
return null;
}
if (choice.value === 'select') {
// 选择现有路径
const result = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: `选择 ${projectName} 的存储位置`,
title: `为项目 "${projectName}" 选择存储文件夹`
});
if (result && result.length > 0) {
const selectedPath = result[0].fsPath;
this.projectPaths.set(projectId, selectedPath);
vscode.window.showInformationMessage(`项目存储位置已设置: ${selectedPath}`);
return selectedPath;
}
}
else {
// 创建新路径
const pathInput = await vscode.window.showInputBox({
prompt: '请输入项目存储路径(绝对路径)',
placeHolder: `/path/to/your/project/${projectName}`,
validateInput: (value) => {
if (!value) {
return '路径不能为空';
}
return null;
}
});
if (pathInput) {
try {
// 尝试创建目录
const dirUri = vscode.Uri.file(pathInput);
await vscode.workspace.fs.createDirectory(dirUri);
this.projectPaths.set(projectId, pathInput);
vscode.window.showInformationMessage(`项目存储位置已创建: ${pathInput}`);
return pathInput;
}
catch (error) {
vscode.window.showErrorMessage(`创建目录失败: ${error}`);
return null;
}
}
}
return null;
}
catch (error) {
vscode.window.showErrorMessage(`选择存储路径时出错: ${error}`);
return null;
}
}
// === 项目相关方法 ===
updateProjectName(projectId, newName) {
const project = this.projects.find(p => p.id === projectId);
@@ -157,6 +253,8 @@ class ConfigPanel {
// 删除相关的配置
const containerIds = this.containers.filter(c => aircraftIds.includes(c.aircraftId)).map(c => c.id);
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
// 删除项目路径映射
this.projectPaths.delete(projectId);
vscode.window.showInformationMessage(`删除项目: ${projectId}`);
this.updateWebview();
}
@@ -220,6 +318,15 @@ class ConfigPanel {
this.updateWebview();
}
}
async updateConfigFileName(configId, fileName) {
const config = this.configs.find(c => c.id === configId);
if (config) {
config.fileName = fileName;
vscode.window.showInformationMessage(`文件名更新: ${fileName}`);
this.updateWebview();
}
}
// 创建配置文件
createConfig(name) {
const newId = 'cfg' + (this.configs.length + 1);
const newConfig = {
@@ -233,7 +340,7 @@ class ConfigPanel {
vscode.window.showInformationMessage(`新建配置: ${name}`);
this.updateWebview();
}
// 新增:删除配置文件方法
// 删除配置文件
deleteConfig(configId) {
const config = this.configs.find(c => c.id === configId);
if (config) {
@@ -242,13 +349,62 @@ class ConfigPanel {
this.updateWebview();
}
}
saveConfigFile(configId, content) {
const config = this.configs.find(c => c.id === configId);
if (config) {
config.content = content;
vscode.window.showInformationMessage(`配置文件已保存: ${config.name}`);
// 保存配置文件到磁盘
async saveConfigFileToDisk(configId, content) {
try {
const config = this.configs.find(c => c.id === configId);
if (!config) {
vscode.window.showErrorMessage('未找到配置文件');
return;
}
const container = this.containers.find(c => c.id === config.containerId);
if (!container) {
vscode.window.showErrorMessage('未找到容器');
return;
}
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
if (!aircraft) {
vscode.window.showErrorMessage('未找到飞行器');
return;
}
const project = this.projects.find(p => p.id === aircraft.projectId);
if (!project) {
vscode.window.showErrorMessage('未找到项目');
return;
}
const projectPath = this.projectPaths.get(aircraft.projectId);
if (!projectPath) {
vscode.window.showErrorMessage('未设置项目存储路径,请先配置项目');
return;
}
// 构建文件路径:项目路径/飞行器名/容器名/文件名
const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
const containerDir = vscode.Uri.joinPath(aircraftDir, container.name);
const fileUri = vscode.Uri.joinPath(containerDir, config.fileName);
// 确保飞行器目录存在
try {
await vscode.workspace.fs.createDirectory(aircraftDir);
}
catch (error) {
// 目录可能已存在,忽略错误
}
// 确保容器目录存在
try {
await vscode.workspace.fs.createDirectory(containerDir);
}
catch (error) {
// 目录可能已存在,忽略错误
}
// 写入文件
const uint8Array = new TextEncoder().encode(content);
await vscode.workspace.fs.writeFile(fileUri, uint8Array);
vscode.window.showInformationMessage(`配置文件已保存: ${fileUri.fsPath}`);
}
catch (error) {
vscode.window.showErrorMessage(`保存文件时出错: ${error}`);
}
}
// 加载配置文件
loadConfigFile(configId) {
const config = this.configs.find(c => c.id === configId);
if (config) {
@@ -264,11 +420,17 @@ class ConfigPanel {
});
}
}
// 更新视图
updateWebview() {
this.panel.webview.html = this.getWebviewContent();
}
getWebviewContent() {
switch (this.currentView) {
case 'management':
return this.projectManagementView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
case 'projects':
return this.projectListView.render({
projects: this.projects
@@ -292,8 +454,9 @@ class ConfigPanel {
configs: containerConfigs
});
default:
return this.projectListView.render({
projects: this.projects
return this.projectManagementView.render({
projects: this.projects,
projectPaths: this.projectPaths
});
}
}