forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickActionsPanel.razor.cs
More file actions
386 lines (349 loc) · 11.5 KB
/
QuickActionsPanel.razor.cs
File metadata and controls
386 lines (349 loc) · 11.5 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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Text.Json;
using WebCodeCli.Domain.Domain.Model;
using WebCodeCli.Domain.Domain.Service;
namespace WebCodeCli.Components;
public partial class QuickActionsPanel : ComponentBase
{
[Inject] private IJSRuntime JSRuntime { get; set; } = default!;
[Inject] private ILocalizationService L { get; set; } = default!;
[Inject] private IQuickActionService QuickActionService { get; set; } = default!;
[Parameter] public EventCallback<string> OnActionSelected { get; set; }
[Parameter] public bool DefaultCollapsed { get; set; } = true;
private List<QuickAction> _actions = new();
private bool _isLoading = true;
private bool _showCustomizeModal = false;
private bool _showActionDialog = false;
private QuickAction? _editingAction = null;
private ActionFormModel _actionForm = new();
private bool _isCollapsed = true; // 默认折叠状态
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
private string LocalizedTitle => T("quickActions.title");
private string LocalizedCustomize => T("quickActions.customize");
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
protected override async Task OnInitializedAsync()
{
_isCollapsed = DefaultCollapsed;
await LoadTranslationsAsync();
await LoadActionsAsync();
}
private async Task LoadActionsAsync()
{
_isLoading = true;
StateHasChanged();
try
{
// 从后端加载快捷操作
var savedActions = await QuickActionService.GetAllAsync();
if (savedActions is { Count: > 0 })
{
_actions = savedActions;
Console.WriteLine($"[快捷操作] 成功从后端加载 {savedActions.Count} 个快捷操作");
}
else
{
Console.WriteLine("[快捷操作] 后端无数据,初始化默认快捷操作");
await ResetActionsToDefaultAsync();
}
}
catch (Exception ex)
{
Console.WriteLine($"[快捷操作] 加载失败: {ex.Message}");
_actions = GetDefaultActions();
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private List<QuickAction> GetDefaultActions()
{
return new List<QuickAction>
{
new()
{
Id = "optimize-code",
Title = "优化代码",
Content = "请优化以下代码的性能和可读性,并说明优化的原因:",
Icon = "🔧",
Order = 1,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+1"
},
new()
{
Id = "add-comments",
Title = "添加注释",
Content = "请为以下代码添加详细的中文注释,包括函数说明、参数说明和关键逻辑说明:",
Icon = "📝",
Order = 2,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+2"
},
new()
{
Id = "fix-bug",
Title = "修复 Bug",
Content = "请帮我分析并修复以下代码中的 Bug,并解释问题原因:",
Icon = "🐛",
Order = 3,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+3"
},
new()
{
Id = "refactor-code",
Title = "重构代码",
Content = "请重构以下代码,提高代码质量和可维护性,遵循 SOLID 原则:",
Icon = "🔄",
Order = 4,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+4"
},
new()
{
Id = "generate-tests",
Title = "生成测试",
Content = "请为以下代码生成单元测试用例:",
Icon = "🧪",
Order = 5,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+5"
},
new()
{
Id = "code-review",
Title = "代码审查",
Content = "请进行代码审查,指出潜在问题和改进建议,包括:\n1. 代码质量\n2. 安全性\n3. 性能\n4. 可维护性",
Icon = "👁️",
Order = 6,
IsCustom = false,
IsEnabled = true,
Hotkey = "Ctrl+6"
}
};
}
private async Task SaveActionsAsync()
{
try
{
// 使用后端服务保存
await QuickActionService.SaveAllAsync(_actions);
Console.WriteLine($"[快捷操作] 已保存 {_actions.Count} 个快捷操作到后端");
}
catch (Exception ex)
{
Console.WriteLine($"[快捷操作] 保存失败: {ex.Message}");
}
}
private async Task OnActionClick(QuickAction action)
{
// 增加使用次数
action.UsageCount++;
await SaveActionsAsync();
// 触发事件
if (OnActionSelected.HasDelegate)
{
await OnActionSelected.InvokeAsync(action.Content);
}
}
private void ToggleCollapse()
{
_isCollapsed = !_isCollapsed;
}
private void ToggleCustomize()
{
_showCustomizeModal = !_showCustomizeModal;
}
private void CloseCustomizeModal()
{
_showCustomizeModal = false;
}
private async Task ToggleActionEnabled(QuickAction action)
{
action.IsEnabled = !action.IsEnabled;
await SaveActionsAsync();
StateHasChanged();
}
private void ShowAddDialog()
{
_editingAction = null;
_actionForm = new ActionFormModel
{
Icon = "⭐"
};
_showActionDialog = true;
}
private void ShowEditDialog(QuickAction action)
{
_editingAction = action;
_actionForm = new ActionFormModel
{
Title = action.Title,
Content = action.Content,
Icon = action.Icon,
Hotkey = action.Hotkey
};
_showActionDialog = true;
}
private void CloseActionDialog()
{
_showActionDialog = false;
_editingAction = null;
_actionForm = new ActionFormModel();
}
private async Task SaveAction()
{
if (string.IsNullOrWhiteSpace(_actionForm.Title) || string.IsNullOrWhiteSpace(_actionForm.Content))
{
return;
}
if (_editingAction != null)
{
// 编辑现有操作
_editingAction.Title = _actionForm.Title;
_editingAction.Content = _actionForm.Content;
_editingAction.Icon = _actionForm.Icon;
_editingAction.Hotkey = _actionForm.Hotkey;
_editingAction.UpdatedAt = DateTime.Now;
}
else
{
// 添加新操作
var newAction = new QuickAction
{
Id = Guid.NewGuid().ToString(),
Title = _actionForm.Title,
Content = _actionForm.Content,
Icon = _actionForm.Icon,
Hotkey = _actionForm.Hotkey,
Order = _actions.Count + 1,
IsCustom = true,
IsEnabled = true,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
_actions.Add(newAction);
}
await SaveActionsAsync();
CloseActionDialog();
StateHasChanged();
}
private async Task DeleteAction(QuickAction action)
{
if (!action.IsCustom)
{
return;
}
_actions.Remove(action);
await SaveActionsAsync();
StateHasChanged();
}
private async Task ResetActionsToDefaultAsync()
{
Console.WriteLine("[快捷操作] 重置为默认快捷操作");
_actions = GetDefaultActions();
try
{
// 清除后端的快捷操作并保存默认值
await QuickActionService.ClearAsync();
Console.WriteLine("[快捷操作] 已清除后端的快捷操作");
await SaveActionsAsync();
}
catch (Exception ex)
{
Console.WriteLine($"[快捷操作] 重置存储失败: {ex.Message}");
}
}
private class ActionFormModel
{
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string Icon { get; set; } = string.Empty;
public string Hotkey { get; set; } = string.Empty;
}
#region 本地化辅助方法
/// <summary>
/// 加载翻译资源
/// </summary>
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}");
}
}
/// <summary>
/// 将嵌套的翻译字典展平为点分隔的键
/// </summary>
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;
}
/// <summary>
/// 获取翻译文本
/// </summary>
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;
}
#endregion
}