0
0

新增git分支显示功能

This commit is contained in:
xubing
2025-11-27 18:34:57 +08:00
parent fecbcdc59b
commit 9f44664b0b
6 changed files with 749 additions and 167 deletions

View File

@@ -5,10 +5,19 @@ import { ContainerConfigData, ConfigViewData } from '../types/ViewTypes';
interface GitBranch {
name: string;
isCurrent: boolean;
isRemote: boolean;
selected?: boolean;
}
interface BranchTreeNode {
name: string;
fullName: string;
isLeaf: boolean;
children: BranchTreeNode[];
level: number;
expanded?: boolean;
branch?: GitBranch;
}
// Git 文件树接口
interface GitFileTree {
name: string;
@@ -28,6 +37,17 @@ interface GitRepo {
containerId: string;
}
// 树状分支节点接口
interface BranchTreeNode {
name: string;
fullName: string;
isLeaf: boolean;
children: BranchTreeNode[];
level: number;
expanded?: boolean;
branch?: GitBranch;
}
export class ConfigView extends BaseView {
render(data?: ContainerConfigData & {
gitRepos?: GitRepo[];
@@ -86,8 +106,8 @@ export class ConfigView extends BaseView {
`;
}).join('');
// 生成分支选择的 HTML
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesHtml(gitBranches) : '';
// 生成分支选择的 HTML - 使用树状结构
const branchesHtml = gitBranches.length > 0 ? this.generateBranchesTreeHtml(gitBranches) : '';
return `<!DOCTYPE html>
<html lang="zh-CN">
@@ -131,6 +151,65 @@ export class ConfigView extends BaseView {
background: var(--vscode-inputValidation-errorBackground);
opacity: 0.8;
}
/* 树状分支样式 */
.branch-tree {
font-family: 'Courier New', monospace;
user-select: none;
margin: 0;
padding: 0;
}
.branch-node {
padding: 2px 0;
display: flex;
align-items: center;
cursor: pointer;
line-height: 1.2;
}
.branch-node:hover {
background: var(--vscode-list-hoverBackground);
}
.branch-icon {
margin-right: 4px;
font-size: 16px;
width: 16px;
text-align: center;
}
.branch-expand {
margin-right: 2px;
cursor: pointer;
width: 14px;
text-align: center;
}
.branch-checkbox {
margin-right: 6px;
}
.branch-name {
flex: 1;
font-size: 13px;
}
.branch-children {
margin-left: 16px;
border-left: 1px solid var(--vscode-panel-border);
padding-left: 8px;
}
.branch-leaf {
margin-left: 16px;
}
.current-branch {
color: var(--vscode-gitDecoration-untrackedResourceForeground);
font-weight: bold;
}
/* 树图标样式 */
.tree-icon {
font-size: 2em;
}
/* 分支树标题样式 */
.branch-tree-title {
font-size: 1.2em;
}
</style>
</head>
<body>
@@ -188,6 +267,7 @@ export class ConfigView extends BaseView {
let currentConfigId = null;
let selectedBranches = new Set();
let currentRepoUrl = '';
let branchTreeData = [];
// 配置管理功能
function editConfigName(configId, currentName) {
@@ -309,7 +389,7 @@ export class ConfigView extends BaseView {
}
function toggleBranch(branchName) {
const checkbox = document.getElementById('branch-' + branchName.replace(/[^a-zA-Z0-9]/g, '-'));
const checkbox = document.getElementById('branch-' + branchName.replace(/[^a-zA-Z0-9-]/g, '-'));
if (checkbox.checked) {
selectedBranches.add(branchName);
} else {
@@ -370,35 +450,124 @@ export class ConfigView extends BaseView {
});
}
// 动态渲染分支选择区域
function renderBranchSelection(branches, repoUrl) {
// 树状分支功能
function toggleBranchNode(nodeId) {
const node = findNodeById(branchTreeData, nodeId);
if (node && !node.isLeaf) {
node.expanded = !node.expanded;
renderBranchTree(branchTreeData);
}
}
function expandAllBranches() {
setAllExpanded(branchTreeData, true);
renderBranchTree(branchTreeData);
}
function collapseAllBranches() {
setAllExpanded(branchTreeData, false);
renderBranchTree(branchTreeData);
}
function setAllExpanded(nodes, expanded) {
nodes.forEach(node => {
if (!node.isLeaf) {
node.expanded = expanded;
if (node.children) {
setAllExpanded(node.children, expanded);
}
}
});
}
function findNodeById(nodes, nodeId) {
for (const node of nodes) {
if (node.fullName === nodeId) return node;
if (node.children) {
const found = findNodeById(node.children, nodeId);
if (found) return found;
}
}
return null;
}
// 动态渲染树状分支
function renderBranchTree(treeData) {
const container = document.getElementById('branchSelectionContainer');
if (branches.length === 0) {
if (!treeData || treeData.length === 0) {
container.innerHTML = '<div style="text-align: center; padding: 10px; color: var(--vscode-descriptionForeground);">未找到分支</div>';
return;
}
let branchesHtml = '<div class="branch-selection" style="margin: 15px 0; padding: 15px; background: var(--vscode-panel-background); border-radius: 4px;">';
branchesHtml += '<h4>🌿 选择要克隆的分支 (共 ' + branches.length + ' 个)</h4>';
branchesHtml += '<div style="max-height: 200px; overflow-y: auto; margin: 10px 0;">';
let html = '<div class="branch-selection" style="margin: 10px 0; padding: 10px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4 class="branch-tree-title" style="margin: 0 0 8px 0;"><span class="tree-icon">🌳</span> 分支树</h4>';
branches.forEach(branch => {
branchesHtml += '<div class="branch-item" style="padding: 8px; border-bottom: 1px solid var(--vscode-panel-border); display: flex; align-items: center;">';
branchesHtml += '<input type="checkbox" id="branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-') + '" ';
branchesHtml += (branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\\'' + branch.name + '\\')" style="margin-right: 10px;">';
branchesHtml += '<label for="branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-') + '" style="flex: 1; cursor: pointer;">';
branchesHtml += (branch.isCurrent ? '⭐ ' : '') + branch.name;
branchesHtml += '</label></div>';
// 展开/收起按钮
html += '<div style="margin-bottom: 8px;">';
html += '<button class="back-btn" onclick="expandAllBranches()" style="margin-right: 8px; padding: 2px 6px; font-size: 11px;">展开全部</button>';
html += '<button class="back-btn" onclick="collapseAllBranches()" style="padding: 2px 6px; font-size: 11px;">收起全部</button>';
html += '</div>';
html += '<div class="branch-tree">';
html += renderBranchNodes(treeData, 0);
html += '</div>';
html += '<div style="margin-top: 12px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 8px; padding: 4px 8px; font-size: 12px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()" style="padding: 4px 8px; font-size: 12px;">取消</button>';
html += '</div></div>';
container.innerHTML = html;
}
function renderBranchNodes(nodes, level) {
let html = '';
nodes.forEach(node => {
const indent = level * 20;
const nodeId = node.fullName.replace(/[^a-zA-Z0-9-]/g, '-');
if (node.isLeaf) {
// 叶子节点(实际分支)
html += '<div class="branch-node branch-leaf" style="margin-left: ' + indent + 'px;">';
html += '<input type="checkbox" id="branch-' + nodeId + '" class="branch-checkbox" ';
html += (node.branch && node.branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\\'' + node.fullName + '\\')">';
html += '<span class="branch-icon">🌿</span>';
html += '<span class="branch-name ' + (node.branch && node.branch.isCurrent ? 'current-branch' : '') + '">';
html += node.name;
html += (node.branch && node.branch.isCurrent ? ' ⭐' : '');
html += '</span>';
html += '</div>';
} else {
// 文件夹节点
html += '<div class="branch-node" style="margin-left: ' + indent + 'px;">';
html += '<span class="branch-expand" onclick="toggleBranchNode(\\'' + node.fullName + '\\')">';
html += (node.expanded ? '🪵' : '🪵');
html += '</span>';
html += '<span class="branch-icon">🪵</span>';
html += '<span class="branch-name">' + node.name + '</span>';
html += '</div>';
if (node.expanded && node.children) {
html += '<div class="branch-children">';
html += renderBranchNodes(node.children, level + 1);
html += '</div>';
}
}
});
branchesHtml += '</div>';
branchesHtml += '<div style="margin-top: 15px;">';
branchesHtml += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 10px;">✅ 克隆选中分支</button>';
branchesHtml += '<button class="back-btn" onclick="cancelBranchSelection()">取消</button>';
branchesHtml += '</div></div>';
container.innerHTML = branchesHtml;
return html;
}
function countLeafNodes(nodes) {
let count = 0;
nodes.forEach(node => {
if (node.isLeaf) {
count++;
} else if (node.children) {
count += countLeafNodes(node.children);
}
});
return count;
}
// 对话框函数
@@ -488,7 +657,9 @@ export class ConfigView extends BaseView {
if (message.type === 'branchesFetched') {
console.log('🌿 收到分支数据:', message.branches);
renderBranchSelection(message.branches, message.repoUrl);
console.log('🌳 收到分支树数据:', message.branchTree);
branchTreeData = message.branchTree || [];
renderBranchTree(branchTreeData);
}
});
@@ -501,28 +672,124 @@ export class ConfigView extends BaseView {
</html>`;
}
private generateBranchesHtml(branches: GitBranch[]): string {
private generateBranchesTreeHtml(branches: GitBranch[]): string {
if (branches.length === 0) return '';
let html = '<div class="branch-selection" style="margin: 15px 0; padding: 15px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4>🌿 选择要克隆的分支 (共 ' + branches.length + ' 个)</h4>';
html += '<div style="max-height: 200px; overflow-y: auto; margin: 10px 0;">';
// 构建分支树
const branchTree = this.buildBranchTree(branches);
branches.forEach(branch => {
const branchId = 'branch-' + branch.name.replace(/[^a-zA-Z0-9]/g, '-');
html += '<div class="branch-item" style="padding: 8px; border-bottom: 1px solid var(--vscode-panel-border); display: flex; align-items: center;">';
html += '<input type="checkbox" id="' + branchId + '" ';
html += (branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\'' + branch.name + '\')" style="margin-right: 10px;">';
html += '<label for="' + branchId + '" style="flex: 1; cursor: pointer;">';
html += (branch.isCurrent ? '⭐ ' : '') + branch.name;
html += (branch.isRemote ? '(远程)' : '(本地)') + '</label></div>';
});
html += '<div style="margin-top: 15px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 10px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()">取消</button>';
let html = '<div class="branch-selection" style="margin: 10px 0; padding: 10px; background: var(--vscode-panel-background); border-radius: 4px;">';
html += '<h4 class="branch-tree-title" style="margin: 0 0 8px 0;"><span class="tree-icon">🌳</span> 分支树</h4>';
html += '<div style="margin-bottom: 8px;">';
html += '<button class="back-btn" onclick="expandAllBranches()" style="margin-right: 8px; padding: 2px 6px; font-size: 11px;">展开全部</button>';
html += '<button class="back-btn" onclick="collapseAllBranches()" style="padding: 2px 6px; font-size: 11px;">收起全部</button>';
html += '</div>';
html += '<div class="branch-tree">';
html += this.renderBranchTreeNodes(branchTree, 0);
html += '</div>';
html += '<div style="margin-top: 12px;">';
html += '<button class="btn-new" onclick="cloneSelectedBranches()" style="margin-right: 8px; padding: 4px 8px; font-size: 12px;">✅ 克隆选中分支</button>';
html += '<button class="back-btn" onclick="cancelBranchSelection()" style="padding: 4px 8px; font-size: 12px;">取消</button>';
html += '</div></div>';
return html;
}
/**
* 构建分支树状结构
*/
private buildBranchTree(branches: GitBranch[]): BranchTreeNode[] {
const root: BranchTreeNode[] = [];
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 => n.name === part);
if (!node) {
node = {
name: part,
fullName: fullName,
isLeaf: isLeaf,
children: [],
level: i
};
currentLevel.push(node);
}
if (isLeaf) {
node.branch = branch;
} else {
node.expanded = node.expanded || true; // 默认展开
}
currentLevel = node.children;
}
});
return root;
}
/**
* 渲染分支树节点
*/
private renderBranchTreeNodes(nodes: BranchTreeNode[], level: number): string {
let html = '';
nodes.forEach(node => {
const indent = level * 20;
const nodeId = node.fullName.replace(/[^a-zA-Z0-9-]/g, '-');
if (node.isLeaf) {
// 叶子节点(实际分支)
html += '<div class="branch-node branch-leaf" style="margin-left: ' + indent + 'px;">';
html += '<input type="checkbox" id="branch-' + nodeId + '" class="branch-checkbox" ';
html += (node.branch && node.branch.selected ? 'checked' : '') + ' onchange="toggleBranch(\'' + node.fullName + '\')">';
html += '<span class="branch-icon">🌿</span>';
html += '<span class="branch-name ' + (node.branch && node.branch.isCurrent ? 'current-branch' : '') + '">';
html += node.name;
html += (node.branch && node.branch.isCurrent ? ' ⭐' : '');
html += '</span>';
html += '</div>';
} else {
// 文件夹节点
html += '<div class="branch-node" style="margin-left: ' + indent + 'px;">';
html += '<span class="branch-expand" onclick="toggleBranchNode(\'' + node.fullName + '\')">';
html += (node.expanded ? '🪵' : '🪵');
html += '</span>';
html += '<span class="branch-icon">🪵</span>';
html += '<span class="branch-name">' + node.name + '</span>';
html += '</div>';
if (node.expanded && node.children && node.children.length > 0) {
html += '<div class="branch-children">';
html += this.renderBranchTreeNodes(node.children, level + 1);
html += '</div>';
}
}
});
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;
}
}