import * as vscode from 'vscode'; import { GitService } from './GitService'; export class CommitPanel { public static currentPanel: CommitPanel | undefined; private readonly _panel: vscode.WebviewPanel; private readonly _extensionPath: string; private readonly _gitService: GitService; private _disposables: vscode.Disposable[] = []; public static show(extensionContext: vscode.ExtensionContext, gitService: GitService) { const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined; if (CommitPanel.currentPanel) { CommitPanel.currentPanel._panel.reveal(column); return; } const panel = vscode.window.createWebviewPanel( 'commitPanel', 'Git Commits', column || vscode.ViewColumn.One, { enableScripts: true } ); // 修正:传递正确的参数 CommitPanel.currentPanel = new CommitPanel(panel, extensionContext, gitService); } constructor(panel: vscode.WebviewPanel, extensionContext: vscode.ExtensionContext, gitService: GitService) { this._panel = panel; this._extensionPath = extensionContext.extensionPath; this._gitService = gitService; this._panel.onDidDispose(() => this.dispose(), null, this._disposables); this._panel.onDidChangeViewState(e => { if (this._panel.visible) { this._update(); } }, null, this._disposables); this._panel.webview.onDidReceiveMessage(message => { switch (message.command) { case 'filterCommits': this._filterCommits(message.filter); return; case 'showCommitFiles': this._showCommitFiles(message.commit); return; } }, null, this._disposables); this._update(); } public dispose() { CommitPanel.currentPanel = undefined; this._panel.dispose(); while (this._disposables.length) { const x = this._disposables.pop(); if (x) { x.dispose(); } } } private async _update() { const commits = await this._gitService.getCommits(); this._panel.webview.html = this._getHtmlForWebview(commits); } private async _filterCommits(filter: string) { const commits = await this._gitService.getCommits(filter); this._panel.webview.postMessage({ command: 'updateCommits', commits }); } private async _showCommitFiles(commit: string) { const files = await this._gitService.getCommitFiles(commit); this._panel.webview.postMessage({ command: 'showCommitFiles', files }); } private _getHtmlForWebview(commits: any[]) { const commitListHtml = commits.map(commit => `
  • ${commit.message}
  • `).join(''); return ` Git Commits
    `; } }