forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCompleteDropdown.razor.cs
More file actions
217 lines (183 loc) · 6.04 KB
/
AutoCompleteDropdown.razor.cs
File metadata and controls
217 lines (183 loc) · 6.04 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
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using WebCodeCli.Domain.Domain.Service;
namespace WebCodeCli.Components;
public partial class AutoCompleteDropdown : ComponentBase
{
[Inject] private IJSRuntime JSRuntime { get; set; } = default!;
[Inject] private IInputHistoryService InputHistoryService { get; set; } = default!;
[Parameter] public EventCallback<string> OnSuggestionSelected { get; set; }
[Parameter] public string TargetElementId { get; set; } = "input-message";
private List<Suggestion> _suggestions = new();
private bool _isVisible = false;
private int _selectedIndex = 0;
private string _searchText = string.Empty;
private bool _showTemplateHint = false;
private string _positionLeft = "0px";
private string _positionTop = "0px";
private string _positionWidth = "400px";
public async Task ShowSuggestions(string searchText, List<Suggestion> suggestions)
{
_searchText = searchText;
_suggestions = suggestions.Take(10).ToList(); // 最多显示10条
_selectedIndex = 0;
_isVisible = _suggestions.Any();
_showTemplateHint = !searchText.Contains("@");
if (_isVisible)
{
await UpdatePosition();
}
StateHasChanged();
}
public void Hide()
{
_isVisible = false;
_suggestions.Clear();
_selectedIndex = 0;
StateHasChanged();
}
public async Task HandleKeyDown(KeyboardEventArgs e)
{
if (!_isVisible || !_suggestions.Any())
{
return;
}
switch (e.Key)
{
case "ArrowDown":
_selectedIndex = (_selectedIndex + 1) % _suggestions.Count;
StateHasChanged();
break;
case "ArrowUp":
_selectedIndex = _selectedIndex > 0 ? _selectedIndex - 1 : _suggestions.Count - 1;
StateHasChanged();
break;
case "Enter":
if (_selectedIndex >= 0 && _selectedIndex < _suggestions.Count)
{
await SelectSuggestion(_selectedIndex);
}
break;
case "Escape":
Hide();
break;
}
}
private async Task SelectSuggestion(int index)
{
if (index < 0 || index >= _suggestions.Count)
{
return;
}
var suggestion = _suggestions[index];
// 更新使用次数
suggestion.UsageCount++;
// 保存到历史记录
if (suggestion.Type == SuggestionType.Template)
{
await SaveToHistory(suggestion.Text);
}
// 触发选择事件
if (OnSuggestionSelected.HasDelegate)
{
await OnSuggestionSelected.InvokeAsync(suggestion.Text);
}
Hide();
}
private async Task SaveToHistory(string text)
{
try
{
await InputHistoryService.SaveAsync(text);
}
catch (Exception ex)
{
Console.WriteLine($"保存输入历史失败: {ex.Message}");
}
}
private async Task UpdatePosition()
{
try
{
// 获取输入框的位置和尺寸
var rect = await JSRuntime.InvokeAsync<ElementRect>("eval",
$@"(() => {{
const el = document.getElementById('{TargetElementId}');
if (!el) return null;
const rect = el.getBoundingClientRect();
return {{
left: rect.left,
top: rect.bottom + window.scrollY,
width: rect.width
}};
}})()");
if (rect != null)
{
_positionLeft = $"{rect.Left}px";
_positionTop = $"{rect.Top + 5}px"; // 5px 间距
_positionWidth = $"{Math.Max(rect.Width, 300)}px";
}
}
catch (Exception ex)
{
Console.WriteLine($"更新位置失败: {ex.Message}");
}
}
private string GetPositionStyle()
{
return $"left: {_positionLeft}; top: {_positionTop}; width: {_positionWidth};";
}
private string GetSuggestionClass(bool isSelected)
{
return isSelected
? "bg-gradient-to-r from-blue-50 to-indigo-50 border-l-4 border-blue-500"
: "hover:bg-gray-50 border-l-4 border-transparent";
}
private string GetTypeTagClass(SuggestionType type)
{
return type == SuggestionType.History
? "bg-gray-100 text-gray-600"
: "bg-purple-100 text-purple-600";
}
private string GetTypeLabel(SuggestionType type)
{
return type == SuggestionType.History ? "历史" : "模板";
}
private MarkupString HighlightMatch(string text, string searchText)
{
if (string.IsNullOrWhiteSpace(searchText))
{
return new MarkupString(text);
}
var index = text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
if (index == -1)
{
return new MarkupString(text);
}
var before = text.Substring(0, index);
var match = text.Substring(index, searchText.Length);
var after = text.Substring(index + searchText.Length);
var highlighted = $"{before}<mark class=\"bg-yellow-200 text-gray-900 font-semibold\">{match}</mark>{after}";
return new MarkupString(highlighted);
}
private class ElementRect
{
public double Left { get; set; }
public double Top { get; set; }
public double Width { get; set; }
}
}
public class Suggestion
{
public string Text { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public SuggestionType Type { get; set; }
public int UsageCount { get; set; }
public DateTime Timestamp { get; set; } = DateTime.Now;
}
public enum SuggestionType
{
History,
Template
}