现在代码上传逻辑存在问题
This commit is contained in:
File diff suppressed because it is too large
Load Diff
325
src/panels/services/GitService.ts
Normal file
325
src/panels/services/GitService.ts
Normal file
@@ -0,0 +1,325 @@
|
||||
import * as fs from 'fs';
|
||||
import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/node';
|
||||
import * as path from 'path';
|
||||
import { exec } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import { GitBranch, GitFileTree } from '../types/CommonTypes';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
/**
|
||||
* Git操作服务
|
||||
*/
|
||||
export class GitService {
|
||||
/**
|
||||
* 获取远程分支列表
|
||||
*/
|
||||
static async fetchBranches(
|
||||
url: string,
|
||||
username?: string,
|
||||
token?: string
|
||||
): Promise<GitBranch[]> {
|
||||
try {
|
||||
const options: any = {
|
||||
http: http,
|
||||
url: url
|
||||
};
|
||||
|
||||
if (username || token) {
|
||||
options.onAuth = () => ({
|
||||
username: username || '',
|
||||
password: token || ''
|
||||
});
|
||||
}
|
||||
|
||||
const refs = await git.listServerRefs(options);
|
||||
|
||||
const branchRefs = refs.filter((ref: any) =>
|
||||
ref.ref.startsWith('refs/heads/') || ref.ref.startsWith('refs/remotes/origin/')
|
||||
);
|
||||
|
||||
const branches: GitBranch[] = branchRefs.map((ref: any) => {
|
||||
let branchName: string;
|
||||
|
||||
if (ref.ref.startsWith('refs/remotes/')) {
|
||||
branchName = ref.ref.replace('refs/remotes/origin/', '');
|
||||
} else {
|
||||
branchName = ref.ref.replace('refs/heads/', '');
|
||||
}
|
||||
|
||||
return {
|
||||
name: branchName,
|
||||
isCurrent: branchName === 'main' || branchName === 'master',
|
||||
selected: false
|
||||
};
|
||||
});
|
||||
|
||||
return branches;
|
||||
} catch (error) {
|
||||
console.error('获取分支失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆仓库
|
||||
*/
|
||||
static async cloneRepository(
|
||||
url: string,
|
||||
localPath: string,
|
||||
branch: string = 'main',
|
||||
onProgress?: (event: any) => void,
|
||||
username?: string,
|
||||
token?: string
|
||||
): Promise<void> {
|
||||
const parentDir = path.dirname(localPath);
|
||||
await fs.promises.mkdir(parentDir, { recursive: true });
|
||||
|
||||
// 检查目录是否已存在且非空(不变)
|
||||
let dirExists = false;
|
||||
try {
|
||||
await fs.promises.access(localPath);
|
||||
dirExists = true;
|
||||
} catch {
|
||||
dirExists = false;
|
||||
}
|
||||
|
||||
if (dirExists) {
|
||||
const dirContents = await fs.promises.readdir(localPath);
|
||||
if (dirContents.length > 0 && dirContents.some(item => item !== '.git')) {
|
||||
throw new Error('目标目录不为空,请清空目录或选择其他路径');
|
||||
}
|
||||
}
|
||||
|
||||
const options: any = {
|
||||
fs,
|
||||
http,
|
||||
dir: localPath,
|
||||
url,
|
||||
singleBranch: true,
|
||||
depth: 1,
|
||||
ref: branch,
|
||||
onProgress
|
||||
};
|
||||
|
||||
if (username || token) {
|
||||
options.onAuth = () => ({
|
||||
username: username || '',
|
||||
password: token || ''
|
||||
});
|
||||
}
|
||||
|
||||
await git.clone(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取最新更改
|
||||
*/
|
||||
static async pullChanges(localPath: string): Promise<void> {
|
||||
await git.pull({
|
||||
fs: fs,
|
||||
http: http,
|
||||
dir: localPath,
|
||||
author: { name: 'DCSP User', email: 'user@dcsp.local' },
|
||||
fastForward: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建分支树结构
|
||||
*/
|
||||
static buildBranchTree(branches: GitBranch[]): any[] {
|
||||
const root: any[] = [];
|
||||
|
||||
branches.forEach(branch => {
|
||||
const parts = branch.name.split('/');
|
||||
let currentLevel = root;
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
const isLeaf = i === parts.length - 1;
|
||||
const fullName = parts.slice(0, i + 1).join('/');
|
||||
|
||||
let node = currentLevel.find((n: any) => n.name === part);
|
||||
|
||||
if (!node) {
|
||||
node = {
|
||||
name: part,
|
||||
fullName: fullName,
|
||||
isLeaf: isLeaf,
|
||||
children: [],
|
||||
level: i,
|
||||
expanded: true
|
||||
};
|
||||
currentLevel.push(node);
|
||||
}
|
||||
|
||||
if (isLeaf) {
|
||||
node.branch = branch;
|
||||
}
|
||||
|
||||
currentLevel = node.children;
|
||||
}
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化Git仓库
|
||||
*/
|
||||
static async initRepository(localPath: string, branchName: string): Promise<void> {
|
||||
await execAsync(`git init && git checkout -b "${branchName}"`, { cwd: localPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加远程仓库
|
||||
*/
|
||||
static async addRemote(
|
||||
localPath: string,
|
||||
repoUrl: string,
|
||||
username?: string,
|
||||
token?: string
|
||||
): Promise<void> {
|
||||
let finalUrl = repoUrl;
|
||||
|
||||
if (username && token) {
|
||||
const u = encodeURIComponent(username);
|
||||
const t = encodeURIComponent(token);
|
||||
finalUrl = repoUrl.replace('://', `://${u}:${t}@`);
|
||||
}
|
||||
|
||||
await execAsync(`git remote add origin "${finalUrl}"`, { cwd: localPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交初始文件
|
||||
*/
|
||||
static async commitInitialFiles(localPath: string): Promise<void> {
|
||||
try {
|
||||
await execAsync('git add .', { cwd: localPath });
|
||||
await execAsync(
|
||||
`git commit -m "Initial commit from DCSP - ${new Date().toLocaleString()}"`,
|
||||
{ cwd: localPath }
|
||||
);
|
||||
} catch (error: any) {
|
||||
if (error.stderr?.includes('nothing to commit')) {
|
||||
console.log('没有需要提交的更改');
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送到远程仓库
|
||||
*/
|
||||
static async pushToRemote(localPath: string, branchName: string): Promise<void> {
|
||||
await execAsync(`git push -u origin "${branchName}" --force`, { cwd: localPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Git命令提交并推送
|
||||
*/
|
||||
static async commitAndPush(localPath: string): Promise<void> {
|
||||
await execAsync('git add .', { cwd: localPath });
|
||||
await execAsync(
|
||||
`git commit -m "Auto commit from DCSP - ${new Date().toLocaleString()}"`,
|
||||
{ cwd: localPath }
|
||||
);
|
||||
await execAsync('git push', { cwd: localPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有未提交的更改
|
||||
*/
|
||||
static async hasUncommittedChanges(localPath: string): Promise<boolean> {
|
||||
try {
|
||||
const { stdout } = await execAsync('git status --porcelain', { cwd: localPath });
|
||||
return !!stdout.trim();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送到指定仓库URL
|
||||
*/
|
||||
static async pushToRepoUrl(
|
||||
localPath: string,
|
||||
repoUrl: string,
|
||||
branchName: string,
|
||||
username?: string,
|
||||
token?: string
|
||||
): Promise<void> {
|
||||
let finalUrl = repoUrl;
|
||||
|
||||
if (username && token) {
|
||||
const u = encodeURIComponent(username);
|
||||
const t = encodeURIComponent(token);
|
||||
finalUrl = repoUrl.replace('://', `://${u}:${t}@`);
|
||||
}
|
||||
|
||||
const commands = [
|
||||
'git fetch --unshallow || true', // 防止没有 shallow 时直接失败
|
||||
`git checkout -B "${branchName}"`,
|
||||
`git push -u "${finalUrl}" "${branchName}" --force`
|
||||
];
|
||||
|
||||
await execAsync(commands.join(' && '), { cwd: localPath });
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成模块文件夹名称
|
||||
*/
|
||||
static generateModuleFolderName(url: string, branch: string): { displayName: string; folderName: string } {
|
||||
const repoName = url.split('/').pop()?.replace('.git', '') || 'unknown-repo';
|
||||
const branchSafeName = branch.replace(/[^a-zA-Z0-9-_]/g, '-');
|
||||
|
||||
return {
|
||||
displayName: repoName,
|
||||
folderName: branchSafeName
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建文件树
|
||||
*/
|
||||
static async buildFileTree(dir: string, relativePath: string = ''): Promise<GitFileTree[]> {
|
||||
try {
|
||||
const files = await fs.promises.readdir(dir);
|
||||
const tree: GitFileTree[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
if (file.startsWith('.') && file !== '.git') continue;
|
||||
if (file === '.dcsp-data.json') continue;
|
||||
|
||||
const filePath = path.join(dir, file);
|
||||
const stats = await fs.promises.stat(filePath);
|
||||
const currentRelativePath = path.join(relativePath, file);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
const children = await GitService.buildFileTree(filePath, currentRelativePath);
|
||||
tree.push({
|
||||
name: file,
|
||||
type: 'folder',
|
||||
path: currentRelativePath,
|
||||
children: children
|
||||
});
|
||||
} else {
|
||||
tree.push({
|
||||
name: file,
|
||||
type: 'file',
|
||||
path: currentRelativePath
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return tree;
|
||||
} catch (error) {
|
||||
console.error('构建文件树失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
484
src/panels/services/ProjectService.ts
Normal file
484
src/panels/services/ProjectService.ts
Normal file
@@ -0,0 +1,484 @@
|
||||
// src/panels/services/ProjectService.ts
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import {
|
||||
Project,
|
||||
Aircraft,
|
||||
Container,
|
||||
Config,
|
||||
ModuleFolder
|
||||
} from '../types/CommonTypes';
|
||||
import { StorageService } from './StorageService';
|
||||
|
||||
/**
|
||||
* 项目数据管理服务
|
||||
*/
|
||||
export class ProjectService {
|
||||
private projects: Project[] = [];
|
||||
private aircrafts: Aircraft[] = [];
|
||||
private containers: Container[] = [];
|
||||
private configs: Config[] = [];
|
||||
private moduleFolders: ModuleFolder[] = [];
|
||||
private projectPaths: Map<string, string> = new Map();
|
||||
|
||||
/**
|
||||
* 生成唯一ID
|
||||
*/
|
||||
generateUniqueId(prefix: string, _existingItems: any[] = []): string {
|
||||
const randomPart = `${Date.now().toString(36)}${Math.random().toString(36).substring(2, 8)}`;
|
||||
return `${prefix}${randomPart}`;
|
||||
}
|
||||
|
||||
// =============== 项目相关方法 ===============
|
||||
|
||||
getProjects(): Project[] {
|
||||
return this.projects;
|
||||
}
|
||||
|
||||
getProjectPaths(): Map<string, string> {
|
||||
return this.projectPaths;
|
||||
}
|
||||
|
||||
getProjectPath(projectId: string): string | undefined {
|
||||
return this.projectPaths.get(projectId);
|
||||
}
|
||||
|
||||
setProjectPath(projectId: string, path: string): void {
|
||||
this.projectPaths.set(projectId, path);
|
||||
}
|
||||
|
||||
async createProject(name: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('p', this.projects);
|
||||
const newProject: Project = {
|
||||
id: newId,
|
||||
name: name
|
||||
};
|
||||
this.projects.push(newProject);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateProjectName(projectId: string, newName: string): boolean {
|
||||
const project = this.projects.find(p => p.id === projectId);
|
||||
if (project) {
|
||||
project.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteProject(projectId: string): boolean {
|
||||
const project = this.projects.find(p => p.id === projectId);
|
||||
if (!project) return false;
|
||||
|
||||
// 先把要删的 id 都算出来,再统一过滤
|
||||
const relatedAircrafts = this.aircrafts.filter(a => a.projectId === projectId);
|
||||
const aircraftIds = relatedAircrafts.map(a => a.id);
|
||||
|
||||
const relatedContainers = this.containers.filter(c => aircraftIds.includes(c.aircraftId));
|
||||
const containerIds = relatedContainers.map(c => c.id);
|
||||
|
||||
// 真正删除数据
|
||||
this.projects = this.projects.filter(p => p.id !== projectId);
|
||||
this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId);
|
||||
this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId));
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
this.projectPaths.delete(projectId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 飞行器相关方法 ===============
|
||||
|
||||
getAircraftsByProject(projectId: string): Aircraft[] {
|
||||
return this.aircrafts.filter(a => a.projectId === projectId);
|
||||
}
|
||||
|
||||
async createAircraft(name: string, projectId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('a', this.aircrafts);
|
||||
const newAircraft: Aircraft = {
|
||||
id: newId,
|
||||
name: name,
|
||||
projectId: projectId
|
||||
};
|
||||
this.aircrafts.push(newAircraft);
|
||||
|
||||
// 创建飞行器目录
|
||||
await this.createAircraftDirectory(newAircraft);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateAircraftName(aircraftId: string, newName: string): boolean {
|
||||
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
|
||||
if (aircraft) {
|
||||
aircraft.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteAircraft(aircraftId: string): boolean {
|
||||
const aircraft = this.aircrafts.find(a => a.id === aircraftId);
|
||||
if (!aircraft) return false;
|
||||
|
||||
// ⚠️ 修正点:先在删除 containers 之前,算出要删的 containerIds
|
||||
const relatedContainers = this.containers.filter(c => c.aircraftId === aircraftId);
|
||||
const containerIds = relatedContainers.map(c => c.id);
|
||||
|
||||
// 删除飞机自身和容器
|
||||
this.aircrafts = this.aircrafts.filter(a => a.id !== aircraftId);
|
||||
this.containers = this.containers.filter(c => c.aircraftId !== aircraftId);
|
||||
|
||||
// 再删关联的 config 和 moduleFolder
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 容器相关方法 ===============
|
||||
|
||||
getContainersByAircraft(aircraftId: string): Container[] {
|
||||
return this.containers.filter(c => c.aircraftId === aircraftId);
|
||||
}
|
||||
|
||||
async createContainer(name: string, aircraftId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('c', this.containers);
|
||||
const newContainer: Container = {
|
||||
id: newId,
|
||||
name: name,
|
||||
aircraftId: aircraftId
|
||||
};
|
||||
this.containers.push(newContainer);
|
||||
|
||||
await this.createContainerDirectory(newContainer);
|
||||
await this.createDefaultConfigs(newContainer);
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateContainerName(containerId: string, newName: string): boolean {
|
||||
const container = this.containers.find(c => c.id === containerId);
|
||||
if (container) {
|
||||
container.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteContainer(containerId: string): boolean {
|
||||
const container = this.containers.find(c => c.id === containerId);
|
||||
if (!container) return false;
|
||||
|
||||
this.containers = this.containers.filter(c => c.id !== containerId);
|
||||
this.configs = this.configs.filter(cfg => cfg.containerId !== containerId);
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => folder.containerId !== containerId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 配置相关方法 ===============
|
||||
|
||||
getConfigsByContainer(containerId: string): Config[] {
|
||||
return this.configs.filter(cfg => cfg.containerId === containerId);
|
||||
}
|
||||
|
||||
getConfig(configId: string): Config | undefined {
|
||||
return this.configs.find(c => c.id === configId);
|
||||
}
|
||||
|
||||
async createConfig(name: string, containerId: string): Promise<string> {
|
||||
const newId = this.generateUniqueId('cfg', this.configs);
|
||||
const newConfig: Config = {
|
||||
id: newId,
|
||||
name: name,
|
||||
fileName: name.toLowerCase().replace(/\s+/g, '_'),
|
||||
content: `# ${name} 配置文件\n# 创建时间: ${new Date().toLocaleString()}\n# 您可以在此编辑配置内容\n\n`,
|
||||
containerId: containerId
|
||||
};
|
||||
this.configs.push(newConfig);
|
||||
|
||||
await this.ensureContainerDirectoryExists(containerId);
|
||||
return newId;
|
||||
}
|
||||
|
||||
updateConfigName(configId: string, newName: string): boolean {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (config) {
|
||||
config.name = newName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
deleteConfig(configId: string): boolean {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (!config) return false;
|
||||
|
||||
this.configs = this.configs.filter(c => c.id !== configId);
|
||||
return true;
|
||||
}
|
||||
|
||||
// =============== 模块文件夹相关方法 ===============
|
||||
|
||||
getModuleFoldersByContainer(containerId: string): ModuleFolder[] {
|
||||
return this.moduleFolders.filter(folder => folder.containerId === containerId);
|
||||
}
|
||||
|
||||
getModuleFolder(folderId: string): ModuleFolder | undefined {
|
||||
return this.moduleFolders.find(f => f.id === folderId);
|
||||
}
|
||||
|
||||
addModuleFolder(folder: ModuleFolder): void {
|
||||
this.moduleFolders.push(folder);
|
||||
}
|
||||
|
||||
updateModuleFolder(folderId: string, updates: Partial<ModuleFolder>): boolean {
|
||||
const folderIndex = this.moduleFolders.findIndex(f => f.id === folderId);
|
||||
if (folderIndex === -1) return false;
|
||||
|
||||
this.moduleFolders[folderIndex] = {
|
||||
...this.moduleFolders[folderIndex],
|
||||
...updates
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
deleteModuleFolder(folderId: string): boolean {
|
||||
const folder = this.moduleFolders.find(f => f.id === folderId);
|
||||
if (!folder) return false;
|
||||
|
||||
this.moduleFolders = this.moduleFolders.filter(f => f.id !== folderId);
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// =============== 文件系统操作 ===============
|
||||
|
||||
private async createAircraftDirectory(aircraft: Aircraft): Promise<void> {
|
||||
try {
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) {
|
||||
console.warn('未找到项目路径,跳过创建飞行器目录');
|
||||
return;
|
||||
}
|
||||
|
||||
const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
|
||||
await vscode.workspace.fs.createDirectory(aircraftDir);
|
||||
console.log(`✅ 创建飞行器目录: ${aircraftDir.fsPath}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`创建飞行器目录失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async createContainerDirectory(container: Container): Promise<void> {
|
||||
try {
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) {
|
||||
console.warn('未找到对应的飞行器,跳过创建容器目录');
|
||||
return;
|
||||
}
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) {
|
||||
console.warn('未找到项目路径,跳过创建容器目录');
|
||||
return;
|
||||
}
|
||||
|
||||
const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
|
||||
const containerDir = vscode.Uri.joinPath(aircraftDir, container.name);
|
||||
|
||||
await vscode.workspace.fs.createDirectory(aircraftDir);
|
||||
await vscode.workspace.fs.createDirectory(containerDir);
|
||||
console.log(`✅ 创建容器目录: ${containerDir.fsPath}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`创建容器目录失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureContainerDirectoryExists(containerId: string): Promise<void> {
|
||||
try {
|
||||
const container = this.containers.find(c => c.id === containerId);
|
||||
if (!container) return;
|
||||
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) return;
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) return;
|
||||
|
||||
const aircraftDir = vscode.Uri.joinPath(vscode.Uri.file(projectPath), aircraft.name);
|
||||
const containerDir = vscode.Uri.joinPath(aircraftDir, container.name);
|
||||
|
||||
await vscode.workspace.fs.createDirectory(aircraftDir);
|
||||
await vscode.workspace.fs.createDirectory(containerDir);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`确保容器目录存在失败: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async createDefaultConfigs(container: Container): Promise<void> {
|
||||
this.configs.push({
|
||||
id: this.generateUniqueId('cfg', this.configs),
|
||||
name: '配置1',
|
||||
fileName: 'dockerfile',
|
||||
content: `# ${container.name} 的 Dockerfile\nFROM ubuntu:20.04\n\n# 设置工作目录\nWORKDIR /app\n\n# 复制文件\nCOPY . .\n\n# 安装依赖\nRUN apt-get update && apt-get install -y \\\n python3 \\\n python3-pip\n\n# 暴露端口\nEXPOSE 8080\n\n# 启动命令\nCMD ["python3", "app.py"]`,
|
||||
containerId: container.id
|
||||
});
|
||||
|
||||
this.configs.push({
|
||||
id: this.generateUniqueId('cfg', this.configs),
|
||||
name: '配置2',
|
||||
fileName: 'docker-compose.yml',
|
||||
content: `# ${container.name} 的 Docker Compose 配置\nversion: '3.8'\n\nservices:\n ${container.name.toLowerCase().replace(/\s+/g, '-')}:\n build: .\n container_name: ${container.name}\n ports:\n - "8080:8080"\n environment:\n - NODE_ENV=production\n volumes:\n - ./data:/app/data\n restart: unless-stopped`,
|
||||
containerId: container.id
|
||||
});
|
||||
}
|
||||
|
||||
// =============== 数据持久化 ===============
|
||||
|
||||
async saveCurrentProjectData(projectId: string): Promise<boolean> {
|
||||
try {
|
||||
const projectPath = this.projectPaths.get(projectId);
|
||||
if (!projectPath) {
|
||||
console.warn('未找到项目存储路径,数据将不会保存');
|
||||
return false;
|
||||
}
|
||||
|
||||
const data = this.getProjectData(projectId);
|
||||
return await StorageService.saveProjectData(projectPath, data);
|
||||
} catch (error) {
|
||||
console.error('保存项目数据失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async loadProjectData(projectPath: string): Promise<string | null> {
|
||||
try {
|
||||
const data = await StorageService.loadProjectData(projectPath);
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const projectId = data.projects[0]?.id;
|
||||
if (!projectId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 先根据旧的 projectId 算出要清理的 aircraft / container / config / moduleFolder
|
||||
const existingAircrafts = this.aircrafts.filter(a => a.projectId === projectId);
|
||||
const aircraftIds = existingAircrafts.map(a => a.id);
|
||||
|
||||
const existingContainers = this.containers.filter(c => aircraftIds.includes(c.aircraftId));
|
||||
const containerIds = existingContainers.map(c => c.id);
|
||||
|
||||
// 清理旧数据(同一个 projectId 的)
|
||||
this.projects = this.projects.filter(p => p.id !== projectId);
|
||||
this.aircrafts = this.aircrafts.filter(a => a.projectId !== projectId);
|
||||
this.containers = this.containers.filter(c => !aircraftIds.includes(c.aircraftId));
|
||||
this.configs = this.configs.filter(cfg => !containerIds.includes(cfg.containerId));
|
||||
this.moduleFolders = this.moduleFolders.filter(folder => !containerIds.includes(folder.containerId));
|
||||
|
||||
// 加载新数据
|
||||
this.projects.push(...data.projects);
|
||||
this.aircrafts.push(...data.aircrafts);
|
||||
this.containers.push(...data.containers);
|
||||
this.configs.push(...data.configs);
|
||||
this.moduleFolders.push(...data.moduleFolders);
|
||||
|
||||
this.projectPaths.set(projectId, projectPath);
|
||||
return projectId;
|
||||
} catch (error) {
|
||||
console.error('加载项目数据失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private getProjectData(projectId: string): any {
|
||||
return {
|
||||
projects: [this.projects.find(p => p.id === projectId)!],
|
||||
aircrafts: this.aircrafts.filter(a => a.projectId === projectId),
|
||||
containers: this.containers.filter(c => {
|
||||
const aircraft = this.aircrafts.find(a => a.id === c.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
}),
|
||||
configs: this.configs.filter(cfg => {
|
||||
const container = this.containers.find(c => c.id === cfg.containerId);
|
||||
if (!container) return false;
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
}),
|
||||
moduleFolders: this.moduleFolders.filter(folder => {
|
||||
const container = this.containers.find(c => c.id === folder.containerId);
|
||||
if (!container) return false;
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
return aircraft && aircraft.projectId === projectId;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
// =============== 工具方法 ===============
|
||||
|
||||
getModuleFolderFullPath(folder: ModuleFolder): string | null {
|
||||
const container = this.containers.find(c => c.id === folder.containerId);
|
||||
if (!container) return null;
|
||||
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) return null;
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) return null;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
getConfigFilePath(configId: string): string | null {
|
||||
const config = this.configs.find(c => c.id === configId);
|
||||
if (!config) return null;
|
||||
|
||||
const container = this.containers.find(c => c.id === config.containerId);
|
||||
if (!container) return null;
|
||||
|
||||
const aircraft = this.aircrafts.find(a => a.id === container.aircraftId);
|
||||
if (!aircraft) return null;
|
||||
|
||||
const projectPath = this.projectPaths.get(aircraft.projectId);
|
||||
if (!projectPath) return null;
|
||||
|
||||
return path.join(projectPath, aircraft.name, container.name, config.fileName);
|
||||
}
|
||||
|
||||
async deleteConfigFileFromDisk(configId: string): Promise<boolean> {
|
||||
const filePath = this.getConfigFilePath(configId);
|
||||
if (!filePath) return false;
|
||||
|
||||
try {
|
||||
if (fs.existsSync(filePath)) {
|
||||
await fs.promises.unlink(filePath);
|
||||
console.log(`✅ 已删除配置文件: ${filePath}`);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`删除配置文件失败: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
135
src/panels/services/StorageService.ts
Normal file
135
src/panels/services/StorageService.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
// src/panels/services/StorageService.ts
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { ProjectData } from '../types/CommonTypes';
|
||||
|
||||
/**
|
||||
* 数据存储服务
|
||||
* 负责项目的持久化存储和加载
|
||||
*/
|
||||
export class StorageService {
|
||||
/**
|
||||
* 加载项目数据
|
||||
*/
|
||||
static async loadProjectData(projectPath: string): Promise<ProjectData | null> {
|
||||
try {
|
||||
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
|
||||
|
||||
try {
|
||||
await vscode.workspace.fs.stat(dataUri);
|
||||
} catch {
|
||||
console.warn('未找到项目数据文件');
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileData = await vscode.workspace.fs.readFile(dataUri);
|
||||
const dataStr = new TextDecoder().decode(fileData);
|
||||
const data: ProjectData = JSON.parse(dataStr);
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('加载项目数据失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存项目数据
|
||||
*/
|
||||
static async saveProjectData(projectPath: string, data: ProjectData): Promise<boolean> {
|
||||
try {
|
||||
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
|
||||
|
||||
const uint8Array = new TextEncoder().encode(JSON.stringify(data, null, 2));
|
||||
await vscode.workspace.fs.writeFile(dataUri, uint8Array);
|
||||
|
||||
console.log('✅ 项目数据已保存');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('保存项目数据失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目路径是否有数据
|
||||
*/
|
||||
static async checkProjectPathHasData(projectPath: string): Promise<boolean> {
|
||||
try {
|
||||
const dataUri = vscode.Uri.joinPath(vscode.Uri.file(projectPath), '.dcsp-data.json');
|
||||
await vscode.workspace.fs.stat(dataUri);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载仓库配置
|
||||
*/
|
||||
static async loadRepoConfigs(extensionUri: vscode.Uri): Promise<any[]> {
|
||||
try {
|
||||
const configPath = path.join(extensionUri.fsPath, 'dcsp-repos.json');
|
||||
|
||||
if (!fs.existsSync(configPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const content = await fs.promises.readFile(configPath, 'utf8');
|
||||
if (!content.trim()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(content);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed;
|
||||
} else if (parsed && Array.isArray(parsed.repos)) {
|
||||
return parsed.repos;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载仓库配置失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保仓库配置文件存在
|
||||
*/
|
||||
static async ensureRepoConfigFileExists(extensionUri: vscode.Uri): Promise<void> {
|
||||
const configPath = path.join(extensionUri.fsPath, 'dcsp-repos.json');
|
||||
if (!fs.existsSync(configPath)) {
|
||||
const defaultContent = JSON.stringify(
|
||||
{
|
||||
repos: [
|
||||
{
|
||||
name: 'example-repo',
|
||||
url: 'https://github.com/username/repo.git',
|
||||
username: '',
|
||||
token: ''
|
||||
}
|
||||
]
|
||||
},
|
||||
null,
|
||||
2
|
||||
);
|
||||
await fs.promises.writeFile(configPath, defaultContent, 'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开仓库配置文件
|
||||
*/
|
||||
static async openRepoConfig(extensionUri: vscode.Uri): Promise<void> {
|
||||
try {
|
||||
await StorageService.ensureRepoConfigFileExists(extensionUri);
|
||||
const configPath = path.join(extensionUri.fsPath, 'dcsp-repos.json');
|
||||
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(configPath));
|
||||
await vscode.window.showTextDocument(doc);
|
||||
} catch (error) {
|
||||
vscode.window.showErrorMessage(`打开仓库配置文件失败: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
// src/panels/types/CommonTypes.ts
|
||||
// 统一的核心类型定义
|
||||
|
||||
// =============================================
|
||||
// 基础实体接口
|
||||
// =============================================
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -27,22 +30,27 @@ export interface Config {
|
||||
containerId: string;
|
||||
}
|
||||
|
||||
// 统一的模块文件夹接口
|
||||
export type ModuleFolderType = 'git' | 'local';
|
||||
|
||||
// 模块文件夹接口
|
||||
export interface ModuleFolder {
|
||||
id: string;
|
||||
name: string;
|
||||
type: 'git' | 'local';
|
||||
type: ModuleFolderType;
|
||||
localPath: string;
|
||||
containerId: string;
|
||||
uploaded?: boolean;
|
||||
|
||||
/** 🟦 记录原始文件夹名(用于判断用户是否重命名) */
|
||||
/** 记录原始文件夹名(用于判断用户是否重命名) */
|
||||
originalFolderName?: string;
|
||||
|
||||
/** 🟦 记录创建时使用的仓库 URL(用于判断是否同仓库) */
|
||||
/** 记录创建时使用的仓库 URL(用于判断是否同仓库) */
|
||||
originalRepoUrl?: string;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 数据模型接口
|
||||
// =============================================
|
||||
|
||||
// 完整数据模型接口
|
||||
export interface ProjectData {
|
||||
@@ -53,10 +61,115 @@ export interface ProjectData {
|
||||
moduleFolders: ModuleFolder[];
|
||||
}
|
||||
|
||||
// Git文件树结构
|
||||
// =============================================
|
||||
// Git 相关类型
|
||||
// =============================================
|
||||
|
||||
export interface GitFileTree {
|
||||
name: string;
|
||||
type: 'file' | 'folder';
|
||||
path: string;
|
||||
children?: GitFileTree[];
|
||||
}
|
||||
|
||||
export interface GitBranch {
|
||||
name: string;
|
||||
isCurrent: boolean;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 服务配置类型
|
||||
// =============================================
|
||||
|
||||
// 仓库配置项
|
||||
export interface RepoConfigItem {
|
||||
name: string;
|
||||
url: string;
|
||||
username?: string;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 树状结构类型
|
||||
// =============================================
|
||||
|
||||
// 树状分支节点接口
|
||||
export interface BranchTreeNode {
|
||||
name: string;
|
||||
fullName: string;
|
||||
isLeaf: boolean;
|
||||
children: BranchTreeNode[];
|
||||
level: number;
|
||||
expanded?: boolean;
|
||||
branch?: GitBranch;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 状态类型
|
||||
// =============================================
|
||||
|
||||
// 视图状态类型
|
||||
export type ViewType = 'projects' | 'aircrafts' | 'containers' | 'configs';
|
||||
|
||||
// =============================================
|
||||
// 工具类型
|
||||
// =============================================
|
||||
|
||||
// 可选属性类型
|
||||
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
||||
|
||||
// 可空类型
|
||||
export type Nullable<T> = T | null;
|
||||
export type Optional<T> = T | undefined;
|
||||
|
||||
// =============================================
|
||||
// 事件消息类型基础
|
||||
// =============================================
|
||||
|
||||
export interface BaseMessage {
|
||||
type: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// ID参数类型
|
||||
export interface WithId {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface WithProjectId {
|
||||
projectId: string;
|
||||
}
|
||||
|
||||
export interface WithAircraftId {
|
||||
aircraftId: string;
|
||||
}
|
||||
|
||||
export interface WithContainerId {
|
||||
containerId: string;
|
||||
}
|
||||
|
||||
export interface WithConfigId {
|
||||
configId: string;
|
||||
}
|
||||
|
||||
export interface WithModuleFolderId {
|
||||
folderId: string;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// 文件操作类型
|
||||
// =============================================
|
||||
|
||||
export interface FileOperationResult {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: any;
|
||||
}
|
||||
|
||||
export interface DirectoryInfo {
|
||||
path: string;
|
||||
exists: boolean;
|
||||
isEmpty: boolean;
|
||||
files: string[];
|
||||
}
|
||||
88
src/panels/views/AircraftView.ts
Normal file → Executable file
88
src/panels/views/AircraftView.ts
Normal file → Executable file
@@ -6,8 +6,6 @@ export class AircraftView extends BaseView {
|
||||
render(data?: { aircrafts: AircraftViewData[] }): string {
|
||||
const aircrafts = data?.aircrafts || [];
|
||||
|
||||
console.log('AircraftView 渲染数据:', aircrafts);
|
||||
|
||||
const aircraftsHtml = aircrafts.map(aircraft => `
|
||||
<tr>
|
||||
<td>
|
||||
@@ -62,7 +60,7 @@ export class AircraftView extends BaseView {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
margin: 0 auto; /* 确保按钮在容器内居中 */
|
||||
margin: 0 auto;
|
||||
}
|
||||
.btn-new:hover {
|
||||
background: var(--vscode-button-hoverBackground);
|
||||
@@ -76,7 +74,6 @@ export class AircraftView extends BaseView {
|
||||
.aircraft-name:hover {
|
||||
background: var(--vscode-input-background);
|
||||
}
|
||||
/* 专门为新建按钮的容器添加样式 */
|
||||
.new-button-container {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
@@ -99,7 +96,6 @@ export class AircraftView extends BaseView {
|
||||
</thead>
|
||||
<tbody>
|
||||
${aircraftsHtml}
|
||||
<!-- 修复:使用专门的容器类确保居中 -->
|
||||
<tr>
|
||||
<td colspan="3" class="new-button-container">
|
||||
<button class="btn-new" onclick="createNewAircraft()">+ 新建飞行器</button>
|
||||
@@ -188,88 +184,8 @@ export class AircraftView extends BaseView {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 对话框函数
|
||||
function showConfirmDialog(title, message, onConfirm, onCancel) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'confirmModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closeConfirmDialog(true)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
window.confirmCallback = function(result) {
|
||||
if (result && onConfirm) {
|
||||
onConfirm();
|
||||
} else if (!result && onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
delete window.confirmCallback;
|
||||
};
|
||||
}
|
||||
|
||||
function closeConfirmDialog(result) {
|
||||
const modal = document.getElementById('confirmModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.confirmCallback) {
|
||||
window.confirmCallback(result);
|
||||
}
|
||||
}
|
||||
|
||||
function showPromptDialog(title, message, defaultValue, onConfirm) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'promptModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<input type="text" id="promptInput" value="\${defaultValue}" style="width: 100%; margin: 10px 0; padding: 6px; background: var(--vscode-input-background); color: var(--vscode-input-foreground); border: 1px solid var(--vscode-input-border);">
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closePromptDialog(null)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closePromptDialog(document.getElementById('promptInput').value)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('promptInput');
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.promptCallback = onConfirm;
|
||||
}
|
||||
|
||||
function closePromptDialog(result) {
|
||||
const modal = document.getElementById('promptModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.promptCallback) {
|
||||
window.promptCallback(result);
|
||||
}
|
||||
delete window.promptCallback;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/panels/views/BaseView.ts
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export abstract class BaseView {
|
||||
@@ -9,7 +10,10 @@ export abstract class BaseView {
|
||||
|
||||
abstract render(data?: any): string;
|
||||
|
||||
protected getStyles(): string {
|
||||
/**
|
||||
* 获取通用的样式和脚本
|
||||
*/
|
||||
protected getBaseStylesAndScripts(): string {
|
||||
return `
|
||||
<style>
|
||||
body {
|
||||
@@ -173,6 +177,95 @@ export abstract class BaseView {
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
// 对话框工具函数
|
||||
function showConfirmDialog(title, message, onConfirm, onCancel) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'confirmModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closeConfirmDialog(true)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
window.confirmCallback = function(result) {
|
||||
if (result && onConfirm) {
|
||||
onConfirm();
|
||||
} else if (!result && onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
delete window.confirmCallback;
|
||||
};
|
||||
}
|
||||
|
||||
function closeConfirmDialog(result) {
|
||||
const modal = document.getElementById('confirmModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.confirmCallback) {
|
||||
window.confirmCallback(result);
|
||||
}
|
||||
}
|
||||
|
||||
function showPromptDialog(title, message, defaultValue, onConfirm) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'promptModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<input type="text" id="promptInput" value="\${defaultValue}" style="width: 100%; margin: 10px 0; padding: 6px; background: var(--vscode-input-background); color: var(--vscode-input-foreground); border: 1px solid var(--vscode-input-border);">
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closePromptDialog(null)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closePromptDialog(document.getElementById('promptInput').value)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('promptInput');
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.promptCallback = onConfirm;
|
||||
}
|
||||
|
||||
function closePromptDialog(result) {
|
||||
const modal = document.getElementById('promptModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.promptCallback) {
|
||||
window.promptCallback(result);
|
||||
}
|
||||
delete window.promptCallback;
|
||||
}
|
||||
</script>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取仓库选择对话框的脚本(仅ConfigView需要)
|
||||
*/
|
||||
protected getRepoSelectScript(): string {
|
||||
return `
|
||||
<script>
|
||||
(function () {
|
||||
// 避免在同一个 Webview 中重复注册
|
||||
if (window.__dcspRepoDialogInitialized) {
|
||||
@@ -205,7 +298,7 @@ export abstract class BaseView {
|
||||
|
||||
var noRepoTip = '';
|
||||
if (!hasRepos) {
|
||||
noRepoTip = '<div style="margin-top:8px; font-size:12px; color: var(--vscode-descriptionForeground);">当前配置中没有仓库,请先在“仓库配置”中添加。</div>';
|
||||
noRepoTip = '<div style="margin-top:8px; font-size:12px; color: var(--vscode-descriptionForeground);">当前配置中没有仓库,请先在"仓库配置"中添加。</div>';
|
||||
}
|
||||
|
||||
overlay.innerHTML =
|
||||
@@ -232,7 +325,6 @@ export abstract class BaseView {
|
||||
|
||||
if (cancelBtn) {
|
||||
cancelBtn.addEventListener('click', function () {
|
||||
// 通知后端取消,清理可能的状态(例如待上传的本地文件夹)
|
||||
if (typeof vscode !== 'undefined') {
|
||||
vscode.postMessage({
|
||||
type: 'repoSelectCanceled'
|
||||
@@ -275,4 +367,11 @@ export abstract class BaseView {
|
||||
</script>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取样式(被子类覆盖)
|
||||
*/
|
||||
protected getStyles(): string {
|
||||
return this.getBaseStylesAndScripts();
|
||||
}
|
||||
}
|
||||
@@ -2,22 +2,12 @@ import { BaseView } from './BaseView';
|
||||
import { ContainerConfigData, ConfigViewData } from '../types/ViewTypes';
|
||||
import { ModuleFolder } from '../types/CommonTypes';
|
||||
|
||||
// Git 分支接口
|
||||
interface GitBranch {
|
||||
name: string;
|
||||
isCurrent: boolean;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
// Git 文件树接口
|
||||
interface GitFileTree {
|
||||
name: string;
|
||||
type: 'file' | 'folder';
|
||||
path: string;
|
||||
children?: GitFileTree[];
|
||||
}
|
||||
|
||||
// 树状分支节点接口
|
||||
interface BranchTreeNode {
|
||||
name: string;
|
||||
fullName: string;
|
||||
@@ -32,7 +22,7 @@ export class ConfigView extends BaseView {
|
||||
render(data?: ContainerConfigData & {
|
||||
moduleFolders?: ModuleFolder[];
|
||||
currentModuleFolder?: ModuleFolder;
|
||||
moduleFolderFileTree?: GitFileTree[];
|
||||
moduleFolderFileTree?: any[];
|
||||
moduleFolderLoading?: boolean;
|
||||
gitBranches?: GitBranch[];
|
||||
}): string {
|
||||
@@ -44,7 +34,7 @@ export class ConfigView extends BaseView {
|
||||
const moduleFolderLoading = data?.moduleFolderLoading || false;
|
||||
const gitBranches = data?.gitBranches || [];
|
||||
|
||||
// 生成配置列表的 HTML - 包含配置文件和模块文件夹
|
||||
// 生成配置列表的 HTML
|
||||
const configsHtml = configs.map((config: ConfigViewData) => `
|
||||
<tr>
|
||||
<td>
|
||||
@@ -61,10 +51,9 @@ export class ConfigView extends BaseView {
|
||||
</tr>
|
||||
`).join('');
|
||||
|
||||
// 生成模块文件夹的 HTML - 按类别分类显示
|
||||
// 生成模块文件夹的 HTML
|
||||
const moduleFoldersHtml = moduleFolders.map((folder: ModuleFolder) => {
|
||||
const icon = '📁';
|
||||
// 根据类型和上传状态确定类别显示
|
||||
let category = folder.type === 'git' ? 'git' : 'local';
|
||||
if (folder.uploaded) {
|
||||
category += '(已上传)';
|
||||
@@ -75,14 +64,12 @@ export class ConfigView extends BaseView {
|
||||
return `
|
||||
<tr>
|
||||
<td>
|
||||
<!-- 左侧:📁 动力学仓库 / test,仅用于重命名 -->
|
||||
<span class="editable clickable" onclick="renameModuleFolder('${folder.id}', '${folder.name}')">
|
||||
${icon} ${folder.name}
|
||||
</span>
|
||||
</td>
|
||||
<td class="category-${folder.type}">${category}</td>
|
||||
<td>
|
||||
<!-- 右侧:文件/文件夹栏,只负责选择并打开文件,不再触发重命名 -->
|
||||
<span class="clickable"
|
||||
onclick="openTheModuleFolder('${folder.id}', '${folder.type}')">
|
||||
📄 ${fileName}
|
||||
@@ -96,7 +83,7 @@ export class ConfigView extends BaseView {
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
// 生成分支选择的 HTML - 使用树状结构(首次渲染可以为空,后续通过 message 更新)
|
||||
// 生成分支选择的 HTML
|
||||
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesTreeHtml(gitBranches) : '';
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
@@ -105,7 +92,8 @@ export class ConfigView extends BaseView {
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>配置管理</title>
|
||||
${this.getStyles()}
|
||||
${this.getBaseStylesAndScripts()}
|
||||
${this.getRepoSelectScript()}
|
||||
<style>
|
||||
.url-input-section {
|
||||
background: var(--vscode-panel-background);
|
||||
@@ -291,16 +279,6 @@ export class ConfigView extends BaseView {
|
||||
color: var(--vscode-charts-orange);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.category-git(已上传) {
|
||||
color: var(--vscode-gitDecoration-addedResourceForeground);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.category-local(已上传) {
|
||||
color: var(--vscode-gitDecoration-addedResourceForeground);
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -357,12 +335,11 @@ export class ConfigView extends BaseView {
|
||||
|
||||
<script>
|
||||
const vscode = acquireVsCodeApi();
|
||||
let currentConfigId = null;
|
||||
let selectedBranches = new Set();
|
||||
let branchTreeData = [];
|
||||
let selectedConfigs = new Set();
|
||||
|
||||
// 只负责“改名”的函数
|
||||
// 只负责"改名"的函数
|
||||
function renameModuleFolder(folderId, oldName) {
|
||||
showPromptDialog(
|
||||
'重命名模块文件夹',
|
||||
@@ -400,16 +377,14 @@ export class ConfigView extends BaseView {
|
||||
|
||||
// 在 VSCode 中打开配置文件
|
||||
function openConfigFileInVSCode(configId) {
|
||||
console.log('📄 在 VSCode 中打开配置文件:', configId);
|
||||
vscode.postMessage({
|
||||
type: 'openConfigFileInVSCode',
|
||||
configId: configId
|
||||
});
|
||||
}
|
||||
|
||||
// 统一功能:打开模块文件夹(这里只负责打开,而不是改名)
|
||||
// 统一功能:打开模块文件夹
|
||||
function openTheModuleFolder(id, type) {
|
||||
console.log('📂 打开模块文件夹:', { id, type });
|
||||
vscode.postMessage({
|
||||
type: 'openTheModuleFolder',
|
||||
id: id,
|
||||
@@ -438,50 +413,40 @@ export class ConfigView extends BaseView {
|
||||
'确认删除',
|
||||
'确定删除这个配置文件吗?',
|
||||
function() {
|
||||
console.log('🗑️ 删除配置文件:', configId);
|
||||
vscode.postMessage({
|
||||
type: 'deleteConfig',
|
||||
configId: configId
|
||||
});
|
||||
},
|
||||
function() {
|
||||
console.log('❌ 用户取消删除配置文件');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 删除模块文件夹功能
|
||||
function deleteModuleFolder(folderId) {
|
||||
console.log('🗑️ 尝试删除模块文件夹:', folderId);
|
||||
|
||||
showConfirmDialog(
|
||||
'确认删除模块文件夹',
|
||||
'确定删除这个模块文件夹吗?这将删除文件夹及其所有内容。',
|
||||
function() {
|
||||
console.log('✅ 用户确认删除模块文件夹:', folderId);
|
||||
vscode.postMessage({
|
||||
type: 'deleteModuleFolder',
|
||||
folderId: folderId
|
||||
});
|
||||
},
|
||||
function() {
|
||||
console.log('❌ 用户取消删除模块文件夹');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 上传模块文件夹功能(区分 local / git)
|
||||
// 上传模块文件夹功能
|
||||
function uploadModuleFolder(folderId, folderType) {
|
||||
console.log('📤 上传模块文件夹:', { folderId, folderType });
|
||||
|
||||
if (folderType === 'git') {
|
||||
// git 类型:先选仓库,然后后端根据是否改名/是否同仓库判断是更新原分支还是新分支上传
|
||||
vscode.postMessage({
|
||||
type: 'openRepoSelectForGitUpload',
|
||||
folderId: folderId
|
||||
});
|
||||
} else {
|
||||
// local 类型:走 local → git 的初始化上传逻辑
|
||||
vscode.postMessage({
|
||||
type: 'openRepoSelectForUpload',
|
||||
folderId: folderId
|
||||
@@ -493,9 +458,8 @@ export class ConfigView extends BaseView {
|
||||
vscode.postMessage({ type: 'goBackToContainers' });
|
||||
}
|
||||
|
||||
// 🔁 通过弹窗获取仓库(用于克隆)
|
||||
// 通过弹窗获取仓库
|
||||
function openRepoSelect() {
|
||||
console.log('📦 请求仓库列表以选择 Git 仓库(用于克隆分支)');
|
||||
vscode.postMessage({
|
||||
type: 'openRepoSelect'
|
||||
});
|
||||
@@ -508,7 +472,6 @@ export class ConfigView extends BaseView {
|
||||
} else {
|
||||
selectedBranches.delete(branchName);
|
||||
}
|
||||
console.log('选中的分支:', Array.from(selectedBranches));
|
||||
}
|
||||
|
||||
function cloneSelectedBranches() {
|
||||
@@ -517,8 +480,6 @@ export class ConfigView extends BaseView {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🚀 开始克隆选中的分支:', Array.from(selectedBranches));
|
||||
|
||||
vscode.postMessage({
|
||||
type: 'cloneBranches',
|
||||
branches: Array.from(selectedBranches)
|
||||
@@ -553,7 +514,6 @@ export class ConfigView extends BaseView {
|
||||
}
|
||||
|
||||
updateMergeButtonState();
|
||||
console.log('选中的配置文件:', Array.from(selectedConfigs));
|
||||
}
|
||||
|
||||
function updateMergeButtonState() {
|
||||
@@ -568,8 +528,6 @@ export class ConfigView extends BaseView {
|
||||
alert('请至少选择两个配置文件进行合并');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('🔄 开始合并选中的配置文件:', Array.from(selectedConfigs));
|
||||
|
||||
showMergeDialog();
|
||||
}
|
||||
@@ -761,94 +719,11 @@ export class ConfigView extends BaseView {
|
||||
return html;
|
||||
}
|
||||
|
||||
// 对话框函数
|
||||
function showConfirmDialog(title, message, onConfirm, onCancel) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'confirmModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closeConfirmDialog(true)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
window.confirmCallback = function(result) {
|
||||
if (result && onConfirm) {
|
||||
onConfirm();
|
||||
} else if (!result && onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
delete window.confirmCallback;
|
||||
};
|
||||
}
|
||||
|
||||
function closeConfirmDialog(result) {
|
||||
const modal = document.getElementById('confirmModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.confirmCallback) {
|
||||
window.confirmCallback(result);
|
||||
}
|
||||
}
|
||||
|
||||
function showPromptDialog(title, message, defaultValue, onConfirm) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'promptModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<input type="text" id="promptInput" value="\${defaultValue}" style="width: 100%; margin: 10px 0; padding: 6px; background: var(--vscode-input-background); color: var(--vscode-input-foreground); border: 1px solid var(--vscode-input-border);">
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closePromptDialog(null)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closePromptDialog(document.getElementById('promptInput').value)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('promptInput');
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.promptCallback = onConfirm;
|
||||
}
|
||||
|
||||
function closePromptDialog(result) {
|
||||
const modal = document.getElementById('promptModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.promptCallback) {
|
||||
window.promptCallback(result);
|
||||
}
|
||||
delete window.promptCallback;
|
||||
}
|
||||
|
||||
// 消息监听
|
||||
window.addEventListener('message', event => {
|
||||
const message = event.data;
|
||||
console.log('📨 前端收到后端消息:', message);
|
||||
|
||||
if (message.type === 'branchesFetched') {
|
||||
console.log('🌿 收到分支数据:', message.branches);
|
||||
console.log('🌳 收到分支树数据:', message.branchTree);
|
||||
branchTreeData = message.branchTree || [];
|
||||
renderBranchTree(branchTreeData);
|
||||
}
|
||||
@@ -856,8 +731,6 @@ export class ConfigView extends BaseView {
|
||||
|
||||
// 初始化
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
console.log('📄 ConfigView 页面加载完成');
|
||||
|
||||
document.addEventListener('change', function(event) {
|
||||
if (event.target.classList.contains('config-checkbox')) {
|
||||
const configId = event.target.getAttribute('data-id');
|
||||
@@ -965,16 +838,4 @@ export class ConfigView extends BaseView {
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
private countLeafNodes(nodes: BranchTreeNode[]): number {
|
||||
let count = 0;
|
||||
nodes.forEach(node => {
|
||||
if (node.isLeaf) {
|
||||
count++;
|
||||
} else {
|
||||
count += this.countLeafNodes(node.children);
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
83
src/panels/views/ContainerView.ts
Normal file → Executable file
83
src/panels/views/ContainerView.ts
Normal file → Executable file
@@ -5,10 +5,9 @@ import { AircraftConfigData, ContainerViewData } from '../types/ViewTypes';
|
||||
export class ContainerView extends BaseView {
|
||||
render(data?: AircraftConfigData): string {
|
||||
const project = data?.project;
|
||||
const aircraft = data?.aircraft; // 新增:获取飞行器数据
|
||||
const aircraft = data?.aircraft;
|
||||
const containers = data?.containers || [];
|
||||
|
||||
// 生成容器列表的 HTML
|
||||
const containersHtml = containers.map((container: ContainerViewData) => `
|
||||
<tr>
|
||||
<td>
|
||||
@@ -117,86 +116,6 @@ export class ContainerView extends BaseView {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 对话框函数(与之前相同)
|
||||
function showConfirmDialog(title, message, onConfirm, onCancel) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'confirmModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closeConfirmDialog(true)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
window.confirmCallback = function(result) {
|
||||
if (result && onConfirm) {
|
||||
onConfirm();
|
||||
} else if (!result && onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
delete window.confirmCallback;
|
||||
};
|
||||
}
|
||||
|
||||
function closeConfirmDialog(result) {
|
||||
const modal = document.getElementById('confirmModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.confirmCallback) {
|
||||
window.confirmCallback(result);
|
||||
}
|
||||
}
|
||||
|
||||
function showPromptDialog(title, message, defaultValue, onConfirm) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'promptModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<input type="text" id="promptInput" value="\${defaultValue}" style="width: 100%; margin: 10px 0; padding: 6px; background: var(--vscode-input-background); color: var(--vscode-input-foreground); border: 1px solid var(--vscode-input-border);">
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closePromptDialog(null)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closePromptDialog(document.getElementById('promptInput').value)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('promptInput');
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.promptCallback = onConfirm;
|
||||
}
|
||||
|
||||
function closePromptDialog(result) {
|
||||
const modal = document.getElementById('promptModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.promptCallback) {
|
||||
window.promptCallback(result);
|
||||
}
|
||||
delete window.promptCallback;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/panels/views/ProjectView.ts
|
||||
import { BaseView } from './BaseView';
|
||||
import { ProjectViewData } from '../types/ViewTypes';
|
||||
|
||||
@@ -136,13 +137,11 @@ export class ProjectView extends BaseView {
|
||||
|
||||
function configureProject(projectId, projectName, isConfigured) {
|
||||
if (isConfigured) {
|
||||
// 已配置的项目直接打开
|
||||
vscode.postMessage({
|
||||
type: 'openProject',
|
||||
projectId: projectId
|
||||
});
|
||||
} else {
|
||||
// 未配置的项目需要设置路径
|
||||
vscode.postMessage({
|
||||
type: 'configureProject',
|
||||
projectId: projectId,
|
||||
@@ -189,7 +188,7 @@ export class ProjectView extends BaseView {
|
||||
);
|
||||
}
|
||||
|
||||
// 项目名称编辑功能 - 修复版本
|
||||
// 项目名称编辑功能
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
document.addEventListener('click', function(event) {
|
||||
if (event.target.classList.contains('project-name')) {
|
||||
@@ -220,88 +219,8 @@ export class ProjectView extends BaseView {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 对话框函数 - 只保留一份
|
||||
function showConfirmDialog(title, message, onConfirm, onCancel) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'confirmModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closeConfirmDialog(true)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
window.confirmCallback = function(result) {
|
||||
if (result && onConfirm) {
|
||||
onConfirm();
|
||||
} else if (!result && onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
delete window.confirmCallback;
|
||||
};
|
||||
}
|
||||
|
||||
function closeConfirmDialog(result) {
|
||||
const modal = document.getElementById('confirmModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.confirmCallback) {
|
||||
window.confirmCallback(result);
|
||||
}
|
||||
}
|
||||
|
||||
function showPromptDialog(title, message, defaultValue, onConfirm) {
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
overlay.id = 'promptModal';
|
||||
|
||||
overlay.innerHTML = \`
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-title">\${title}</div>
|
||||
<div>\${message}</div>
|
||||
<input type="text" id="promptInput" value="\${defaultValue}" style="width: 100%; margin: 10px 0; padding: 6px; background: var(--vscode-input-background); color: var(--vscode-input-foreground); border: 1px solid var(--vscode-input-border);">
|
||||
<div class="modal-buttons">
|
||||
<button class="modal-btn modal-btn-secondary" onclick="closePromptDialog(null)">取消</button>
|
||||
<button class="modal-btn modal-btn-primary" onclick="closePromptDialog(document.getElementById('promptInput').value)">确定</button>
|
||||
</div>
|
||||
</div>
|
||||
\`;
|
||||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
setTimeout(() => {
|
||||
const input = document.getElementById('promptInput');
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.promptCallback = onConfirm;
|
||||
}
|
||||
|
||||
function closePromptDialog(result) {
|
||||
const modal = document.getElementById('promptModal');
|
||||
if (modal) {
|
||||
modal.remove();
|
||||
}
|
||||
if (window.promptCallback) {
|
||||
window.promptCallback(result);
|
||||
}
|
||||
delete window.promptCallback;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user