forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffViewerPanel.razor
More file actions
428 lines (392 loc) · 17.2 KB
/
DiffViewerPanel.razor
File metadata and controls
428 lines (392 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Domain.Service
@using System.Text.Json
@inject IGitService GitService
@inject ICliExecutorService CliExecutorService
@inject ILocalizationService L
<div class="h-full flex flex-col bg-white">
<!-- 头部:文件和版本选择 -->
<div class="p-3 sm:p-4 border-b border-gray-200 flex-shrink-0 space-y-3">
<!-- 文件路径输入 -->
<div class="flex items-center gap-2">
<label class="text-sm font-semibold text-gray-700 whitespace-nowrap">@T("diffViewer.filePathLabel"):</label>
<input type="text"
@bind="_selectedFilePath"
@bind:event="oninput"
placeholder="@T("diffViewer.filePathPlaceholder")"
class="flex-1 px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-500" />
</div>
<!-- 版本选择 -->
<div class="flex items-center gap-2">
<label class="text-sm font-semibold text-gray-700 whitespace-nowrap">@T("diffViewer.compareVersionsLabel"):</label>
<input type="text"
@bind="_fromCommit"
@bind:event="oninput"
placeholder="@T("diffViewer.fromPlaceholder")"
class="flex-1 px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-500" />
<span class="text-gray-400">→</span>
<input type="text"
@bind="_toCommit"
@bind:event="oninput"
placeholder="@T("diffViewer.toPlaceholder")"
class="flex-1 px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-500" />
</div>
<!-- 操作按钮 -->
<div class="flex items-center gap-2">
<button @onclick="LoadDiff"
disabled="@_isLoading"
class="btn-primary px-4 py-2 text-sm flex items-center gap-2 disabled:opacity-50">
@if (_isLoading)
{
<div class="animate-spin h-4 w-4 border-2 border-white rounded-full border-t-transparent"></div>
}
else
{
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path>
</svg>
}
@T("diffViewer.compare")
</button>
<button @onclick="ToggleViewMode"
class="btn-default px-4 py-2 text-sm flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
@(_splitView ? T("diffViewer.unifiedView") : T("diffViewer.splitView"))
</button>
@if (_diffResult != null)
{
<div class="ml-auto flex items-center gap-4 text-sm">
<span class="text-green-600 flex items-center gap-1">
<span class="font-semibold">+@_diffResult.AddedLines</span>
<span class="text-gray-500">@T("diffViewer.lines")</span>
</span>
<span class="text-red-600 flex items-center gap-1">
<span class="font-semibold">-@_diffResult.DeletedLines</span>
<span class="text-gray-500">@T("diffViewer.lines")</span>
</span>
</div>
}
</div>
</div>
<!-- 内容区域 -->
<div class="flex-1 overflow-auto p-4">
@if (!_isGitRepo)
{
<!-- 非 Git 仓库提示 -->
<div class="flex flex-col items-center justify-center h-full text-gray-400">
<div class="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mb-4">
<svg class="w-10 h-10 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
</div>
<p class="text-base font-semibold text-gray-600">@T("diffViewer.notGitTitle")</p>
<p class="text-sm mt-2 text-gray-500">@T("diffViewer.notGitDesc")</p>
</div>
}
else if (_diffResult == null && !_isLoading)
{
<!-- 未加载差异提示 -->
<div class="flex flex-col items-center justify-center h-full text-gray-400">
<div class="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mb-4">
<svg class="w-10 h-10 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path>
</svg>
</div>
<p class="text-base font-semibold text-gray-600">@T("diffViewer.emptyTitle")</p>
<p class="text-sm mt-2 text-gray-500">@T("diffViewer.emptyDesc")</p>
</div>
}
else if (_diffResult != null)
{
@if (_splitView)
{
<!-- 分割视图 -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
<!-- 左侧:原始版本 -->
<div class="border border-gray-200 rounded-lg overflow-hidden">
<div class="bg-red-50 px-4 py-2 border-b border-red-200 flex items-center gap-2">
<svg class="w-4 h-4 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 12H4"></path>
</svg>
<span class="font-semibold text-red-700">@T("diffViewer.originalVersion", ("from", _fromCommit))</span>
</div>
<div class="bg-white overflow-x-auto">
<table class="w-full text-sm font-mono">
@foreach (var line in _diffResult.DiffLines.Where(l => l.Type != DiffLineType.Added))
{
<tr class="@GetLineClass(line)">
<td class="px-2 py-1 text-gray-400 text-right select-none border-r border-gray-200 w-12">
@line.OldLineNumber
</td>
<td class="px-4 py-1 whitespace-pre">@line.Content</td>
</tr>
}
</table>
</div>
</div>
<!-- 右侧:新版本 -->
<div class="border border-gray-200 rounded-lg overflow-hidden">
<div class="bg-green-50 px-4 py-2 border-b border-green-200 flex items-center gap-2">
<svg class="w-4 h-4 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
<span class="font-semibold text-green-700">@T("diffViewer.newVersion", ("to", _toCommit))</span>
</div>
<div class="bg-white overflow-x-auto">
<table class="w-full text-sm font-mono">
@foreach (var line in _diffResult.DiffLines.Where(l => l.Type != DiffLineType.Deleted))
{
<tr class="@GetLineClass(line)">
<td class="px-2 py-1 text-gray-400 text-right select-none border-r border-gray-200 w-12">
@line.NewLineNumber
</td>
<td class="px-4 py-1 whitespace-pre">@line.Content</td>
</tr>
}
</table>
</div>
</div>
</div>
}
else
{
<!-- 统一视图 -->
<div class="border border-gray-200 rounded-lg overflow-hidden">
<div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
<span class="font-semibold text-gray-700">@T("diffViewer.title")</span>
<span class="text-sm text-gray-500 ml-2">(@_fromCommit → @_toCommit)</span>
</div>
<div class="bg-white overflow-x-auto">
<table class="w-full text-sm font-mono">
@foreach (var line in _diffResult.DiffLines)
{
<tr class="@GetLineClass(line)">
<td class="px-2 py-1 text-gray-400 text-right select-none border-r border-gray-200 w-12">
@line.OldLineNumber
</td>
<td class="px-2 py-1 text-gray-400 text-right select-none border-r border-gray-200 w-12">
@line.NewLineNumber
</td>
<td class="px-1 py-1 text-center select-none border-r border-gray-200 w-8">
@GetLineSymbol(line.Type)
</td>
<td class="px-4 py-1 whitespace-pre">@line.Content</td>
</tr>
}
</table>
</div>
</div>
}
}
</div>
</div>
@code {
[Parameter]
public string SessionId { get; set; } = string.Empty;
[Parameter]
public string InitialFilePath { get; set; } = string.Empty;
[Parameter]
public string InitialFromCommit { get; set; } = string.Empty;
[Parameter]
public string InitialToCommit { get; set; } = "HEAD";
private string _selectedFilePath = string.Empty;
private string _fromCommit = string.Empty;
private string _toCommit = "HEAD";
private GitDiffResult? _diffResult = null;
private bool _isLoading = false;
private bool _isGitRepo = false;
private bool _splitView = false;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
protected override async Task OnInitializedAsync()
{
await LoadTranslationsAsync();
await CheckGitRepository();
// 初始化参数
if (!string.IsNullOrEmpty(InitialFilePath))
{
_selectedFilePath = InitialFilePath;
}
if (!string.IsNullOrEmpty(InitialFromCommit))
{
_fromCommit = InitialFromCommit;
}
if (!string.IsNullOrEmpty(InitialToCommit))
{
_toCommit = InitialToCommit;
}
// 如果所有参数都有,自动加载对比
if (!string.IsNullOrEmpty(_selectedFilePath) &&
!string.IsNullOrEmpty(_fromCommit) &&
!string.IsNullOrEmpty(_toCommit))
{
await LoadDiff();
}
}
#region 本地化辅助方法
private async Task LoadTranslationsAsync()
{
try
{
_currentLanguage = await L.GetCurrentLanguageAsync();
var allTranslations = await L.GetAllTranslationsAsync(_currentLanguage);
_translations = FlattenTranslations(allTranslations);
}
catch (Exception ex)
{
Console.WriteLine($"[文件对比] 加载翻译资源失败: {ex.Message}");
}
}
private Dictionary<string, string> FlattenTranslations(Dictionary<string, object> source, string prefix = "")
{
var result = new Dictionary<string, string>();
foreach (var kvp in source)
{
var key = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}.{kvp.Key}";
if (kvp.Value is JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.Object)
{
var nested = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonElement.GetRawText());
if (nested != null)
{
foreach (var item in FlattenTranslations(nested, key))
{
result[item.Key] = item.Value;
}
}
}
else if (jsonElement.ValueKind == JsonValueKind.String)
{
result[key] = jsonElement.GetString() ?? key;
}
}
else if (kvp.Value is Dictionary<string, object> dict)
{
foreach (var item in FlattenTranslations(dict, key))
{
result[item.Key] = item.Value;
}
}
else if (kvp.Value is string str)
{
result[key] = str;
}
}
return result;
}
private string T(string key)
{
if (_translations.TryGetValue(key, out var translation))
{
return translation;
}
var parts = key.Split('.');
return parts.Length > 0 ? parts[^1] : key;
}
private string T(string key, params (string name, string value)[] parameters)
{
var text = T(key);
foreach (var (name, value) in parameters)
{
text = text.Replace($"{{{name}}}", value);
}
return text;
}
#endregion
protected override async Task OnParametersSetAsync()
{
bool shouldReload = false;
// 检查参数是否变化
if (!string.IsNullOrEmpty(InitialFilePath) && InitialFilePath != _selectedFilePath)
{
_selectedFilePath = InitialFilePath;
shouldReload = true;
}
if (!string.IsNullOrEmpty(InitialFromCommit) && InitialFromCommit != _fromCommit)
{
_fromCommit = InitialFromCommit;
shouldReload = true;
}
if (!string.IsNullOrEmpty(InitialToCommit) && InitialToCommit != _toCommit)
{
_toCommit = InitialToCommit;
shouldReload = true;
}
// 如果参数变化且所有必要参数都有,重新加载
if (shouldReload &&
!string.IsNullOrEmpty(_selectedFilePath) &&
!string.IsNullOrEmpty(_fromCommit) &&
!string.IsNullOrEmpty(_toCommit))
{
await LoadDiff();
}
}
private async Task CheckGitRepository()
{
try
{
var workspacePath = CliExecutorService.GetSessionWorkspacePath(SessionId);
_isGitRepo = await Task.Run(() => GitService.IsGitRepository(workspacePath));
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine($"检查 Git 仓库失败: {ex.Message}");
}
}
private async Task LoadDiff()
{
if (string.IsNullOrWhiteSpace(_selectedFilePath) ||
string.IsNullOrWhiteSpace(_fromCommit) ||
string.IsNullOrWhiteSpace(_toCommit))
{
return;
}
_isLoading = true;
_diffResult = null;
StateHasChanged();
try
{
var workspacePath = CliExecutorService.GetSessionWorkspacePath(SessionId);
_diffResult = await GitService.GetFileDiffAsync(workspacePath, _selectedFilePath, _fromCommit, _toCommit);
}
catch (Exception ex)
{
Console.WriteLine($"加载文件差异失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private void ToggleViewMode()
{
_splitView = !_splitView;
StateHasChanged();
}
private string GetLineClass(DiffLine line)
{
return line.Type switch
{
DiffLineType.Added => "bg-green-50 hover:bg-green-100",
DiffLineType.Deleted => "bg-red-50 hover:bg-red-100",
DiffLineType.Modified => "bg-yellow-50 hover:bg-yellow-100",
_ => "hover:bg-gray-50"
};
}
private string GetLineSymbol(DiffLineType type)
{
return type switch
{
DiffLineType.Added => "+",
DiffLineType.Deleted => "-",
DiffLineType.Modified => "~",
_ => " "
};
}
}