-
-
Notifications
You must be signed in to change notification settings - Fork 997
/
Anchor.razor.cs
305 lines (260 loc) 路 9.47 KB
/
Anchor.razor.cs
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
锘縰sing System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using AntDesign.JsInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace AntDesign
{
public partial class Anchor : AntDomComponentBase, IAnchor
{
private string _ballClass = "ant-anchor-ink-ball";
private string _ballStyle = string.Empty;
private ElementReference _ink;
private DomRect _selfDom;
private AnchorLink _activeLink;
private bool _activatedByClick = false;
private AnchorLink _lastActiveLink;
private Dictionary<string, decimal> _linkTops;
private List<AnchorLink> _flatLinks;
private List<AnchorLink> _links = new List<AnchorLink>();
private bool _linksChanged = false;
[Inject]
private DomEventService DomEventService { get; set; }
#region Parameters
private string _key;
/// <summary>
/// used to refresh links list when the key changed.
/// </summary>
[Parameter]
public string Key
{
get => _key;
set
{
if (_key != value)
{
_key = value;
_linksChanged = true;
Clear();
}
}
}
[Parameter]
public RenderFragment ChildContent { get; set; }
/// <summary>
/// Fixed mode of Anchor
/// </summary>
[Parameter]
public bool Affix { get; set; } = true;
/// <summary>
/// Bounding distance of anchor area
/// </summary>
[Parameter]
public int Bounds { get; set; } = 5;
/// <summary>
/// Scrolling container
/// </summary>
[Parameter]
public Func<string> GetContainer { get; set; } = () => "window";
/// <summary>
/// Pixels to offset from bottom when calculating position of scroll
/// </summary>
[Parameter]
public int? OffsetBottom { get; set; }
/// <summary>
/// Pixels to offset from top when calculating position of scroll
/// </summary>
[Parameter]
public int? OffsetTop { get; set; } = 0;
/// <summary>
/// Whether show ink-balls in Fixed mode
/// </summary>
[Parameter]
public bool ShowInkInFixed { get; set; } = false;
/// <summary>
/// set the handler to handle click event
/// </summary>
[Parameter]
public EventCallback<Tuple<MouseEventArgs, AnchorLink>> OnClick { get; set; }
/// <summary>
/// Customize the anchor highlight
/// </summary>
[Parameter]
public Func<string> GetCurrentAnchor { get; set; }
/// <summary>
/// Anchor scroll offset, default as <see cref="OffsetTop"/>
/// </summary>
[Parameter]
public int? TargetOffset { get; set; }
[Parameter]
public EventCallback<string> OnChange { get; set; }
#endregion Parameters
protected override void OnInitialized()
{
base.OnInitialized();
string prefixCls = "ant-anchor";
ClassMapper.Add(prefixCls)
.If($"{prefixCls}-rtl", () => RTL);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
if (GetCurrentAnchor is null)
{
DomEventService.AddEventListener("window", "scroll", OnScroll, false);
}
}
if (firstRender || _linksChanged)
{
_linksChanged = false;
_selfDom = await JsInvokeAsync<DomRect>(JSInteropConstants.GetBoundingClientRect, _ink);
_linkTops = new Dictionary<string, decimal>();
_flatLinks = FlatChildren();
foreach (var link in _flatLinks)
{
_linkTops[link.Href] = 1;
}
if (GetCurrentAnchor != null)
{
AnchorLink link = _flatLinks.SingleOrDefault(l => l.Href == GetCurrentAnchor());
if (link != null)
{
try
{
DomRect hrefDom = await link.GetHrefDom(true);
if (hrefDom != null)
{
_activatedByClick = false;
await ActivateAsync(link, true);
// the offset does not matter, since the dictionary's value will not change any more in case user set up GetCurrentAnchor
_linkTops[link.Href] = hrefDom.top;
StateHasChanged();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
public void Remove(AnchorLink anchorLink)
{
_links.Remove(anchorLink);
}
public void Add(AnchorLink anchorLink)
{
if (!_links.Where(l => !string.IsNullOrEmpty(l.Href))
.Select(l => l.Href)
.Contains(anchorLink.Href))
{
_links.Add(anchorLink);
}
}
public void Clear()
{
foreach (IAnchor link in _links)
{
link.Clear();
}
_links.Clear();
}
public List<AnchorLink> FlatChildren()
{
List<AnchorLink> results = new List<AnchorLink>();
foreach (IAnchor child in _links)
{
results.AddRange(child.FlatChildren());
}
results.Distinct(new AnchorLinkEqualityComparer());
return results;
}
private async void OnScroll(JsonElement obj)
{
if (!_activatedByClick && _flatLinks != null)
{
_activeLink = null;
_flatLinks.ForEach(l => l.Activate(false));
int offset = OffsetBottom.HasValue ? OffsetBottom.Value : -OffsetTop.Value;
foreach (var link in _flatLinks)
{
try
{
DomRect hrefDom = await link.GetHrefDom();
if (hrefDom != null)
{
_linkTops[link.Href] = hrefDom.top + offset;
}
}
catch (Exception ex)
{
_linkTops[link.Href] = 1;
}
}
string activeKey = _linkTops.Where(p => (int)p.Value <= 0).OrderBy(p => p.Value).LastOrDefault().Key;
if (!string.IsNullOrEmpty(activeKey))
{
_activeLink = _flatLinks.FirstOrDefault(l => l.Href == activeKey);
await ActivateAsync(_activeLink, true);
}
if (Affix && _activeLink != null)
{
_ballClass = "ant-anchor-ink-ball visible";
decimal top = (_activeLink.LinkDom.top - _selfDom.top) + _activeLink.LinkDom.height / 2 - 2;
_ballStyle = $"top: {top}px;";
}
else
{
_ballClass = "ant-anchor-ink-ball";
_ballStyle = string.Empty;
}
StateHasChanged();
}
_activatedByClick = false;
}
private async Task ActivateAsync(AnchorLink anchorLink, bool active)
{
if (anchorLink == null)
return;
anchorLink?.Activate(active);
if (active && _activeLink != _lastActiveLink)
{
_lastActiveLink = _activeLink;
if (OnChange.HasDelegate)
{
await OnChange.InvokeAsync(anchorLink.Href);
}
}
}
public async Task OnLinkClickAsync(MouseEventArgs args, AnchorLink anchorLink)
{
await JsInvokeAsync("window.eval", $"window.location.hash='{anchorLink._hash}'");
if (OnClick.HasDelegate)
{
await OnClick.InvokeAsync(new Tuple<MouseEventArgs, AnchorLink>(args, anchorLink));
}
// forced to activate the link even the dom is not at the top of the browser
// occurs when the dom is located at the bottom of the page
_activatedByClick = true;
_activeLink = anchorLink;
// deactivate everythin else
_flatLinks.ForEach(l => l.Activate(false));
await ActivateAsync(anchorLink, true);
// forced to render when user click on another link that is at the same height with the previously activated dom
StateHasChanged();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
DomEventService.RemoveEventListerner<JsonElement>("window", "scroll", OnScroll);
}
}
}