2020-06-30 22:03:31 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-31 11:51:11 +08:00
|
|
|
|
using System.Diagnostics;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
2020-08-31 11:51:11 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
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";
|
2021-03-12 17:02:11 +08:00
|
|
|
|
private string _ballStyle = string.Empty;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
private ElementReference _ink;
|
|
|
|
|
private DomRect _selfDom;
|
|
|
|
|
private AnchorLink _activeLink;
|
2020-07-16 13:59:56 +08:00
|
|
|
|
private bool _activatedByClick = false;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
private AnchorLink _lastActiveLink;
|
|
|
|
|
private Dictionary<string, decimal> _linkTops;
|
|
|
|
|
private List<AnchorLink> _flatLinks;
|
|
|
|
|
private List<AnchorLink> _links = new List<AnchorLink>();
|
2020-07-16 13:59:56 +08:00
|
|
|
|
private bool _linksChanged = false;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private DomEventService DomEventService { get; set; }
|
|
|
|
|
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 22:03:31 +08:00
|
|
|
|
[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
|
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
|
|
|
|
string prefixCls = "ant-anchor";
|
|
|
|
|
ClassMapper.Add(prefixCls)
|
|
|
|
|
.If($"{prefixCls}-rtl", () => RTL);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 23:48:43 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-21 23:48:43 +08:00
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
2020-06-30 22:03:31 +08:00
|
|
|
|
|
2020-07-21 23:48:43 +08:00
|
|
|
|
if (firstRender)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-21 23:48:43 +08:00
|
|
|
|
if (GetCurrentAnchor is null)
|
|
|
|
|
{
|
2020-08-26 11:13:36 +08:00
|
|
|
|
DomEventService.AddEventListener("window", "scroll", OnScroll, false);
|
2020-07-21 23:48:43 +08:00
|
|
|
|
}
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
if (firstRender || _linksChanged)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_linksChanged = false;
|
|
|
|
|
|
2020-09-09 00:52:14 +08:00
|
|
|
|
_selfDom = await JsInvokeAsync<DomRect>(JSInteropConstants.GetBoundingClientRect, _ink);
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_linkTops = new Dictionary<string, decimal>();
|
|
|
|
|
_flatLinks = FlatChildren();
|
|
|
|
|
foreach (var link in _flatLinks)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_linkTops[link.Href] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetCurrentAnchor != null)
|
|
|
|
|
{
|
|
|
|
|
AnchorLink link = _flatLinks.SingleOrDefault(l => l.Href == GetCurrentAnchor());
|
|
|
|
|
if (link != null)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
try
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
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
|
2021-04-17 22:14:01 +08:00
|
|
|
|
_linkTops[link.Href] = hrefDom.Top;
|
2020-07-16 13:59:56 +08:00
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 23:48:43 +08:00
|
|
|
|
public void Remove(AnchorLink anchorLink)
|
|
|
|
|
{
|
|
|
|
|
_links.Remove(anchorLink);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 22:03:31 +08:00
|
|
|
|
public void Add(AnchorLink anchorLink)
|
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
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();
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<AnchorLink> FlatChildren()
|
|
|
|
|
{
|
|
|
|
|
List<AnchorLink> results = new List<AnchorLink>();
|
|
|
|
|
|
|
|
|
|
foreach (IAnchor child in _links)
|
|
|
|
|
{
|
|
|
|
|
results.AddRange(child.FlatChildren());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
results.Distinct(new AnchorLinkEqualityComparer());
|
2020-06-30 22:03:31 +08:00
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void OnScroll(JsonElement obj)
|
|
|
|
|
{
|
2020-08-31 11:51:11 +08:00
|
|
|
|
if (!_activatedByClick && _flatLinks != null)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_activeLink = null;
|
|
|
|
|
_flatLinks.ForEach(l => l.Activate(false));
|
|
|
|
|
|
|
|
|
|
int offset = OffsetBottom.HasValue ? OffsetBottom.Value : -OffsetTop.Value;
|
|
|
|
|
foreach (var link in _flatLinks)
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
try
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2020-07-16 13:59:56 +08:00
|
|
|
|
DomRect hrefDom = await link.GetHrefDom();
|
|
|
|
|
if (hrefDom != null)
|
|
|
|
|
{
|
2021-04-17 22:14:01 +08:00
|
|
|
|
_linkTops[link.Href] = hrefDom.Top + offset;
|
2020-07-16 13:59:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_linkTops[link.Href] = 1;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 13:59:56 +08:00
|
|
|
|
|
|
|
|
|
string activeKey = _linkTops.Where(p => (int)p.Value <= 0).OrderBy(p => p.Value).LastOrDefault().Key;
|
|
|
|
|
if (!string.IsNullOrEmpty(activeKey))
|
2020-06-30 22:03:31 +08:00
|
|
|
|
{
|
2021-03-12 17:02:11 +08:00
|
|
|
|
_activeLink = _flatLinks.FirstOrDefault(l => l.Href == activeKey);
|
2020-07-16 13:59:56 +08:00
|
|
|
|
await ActivateAsync(_activeLink, true);
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
if (Affix && _activeLink != null)
|
|
|
|
|
{
|
|
|
|
|
_ballClass = "ant-anchor-ink-ball visible";
|
2021-04-17 22:14:01 +08:00
|
|
|
|
decimal top = (_activeLink.LinkDom.Top - _selfDom.Top) + _activeLink.LinkDom.Height / 2 - 2;
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_ballStyle = $"top: {top}px;";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ballClass = "ant-anchor-ink-ball";
|
|
|
|
|
_ballStyle = string.Empty;
|
|
|
|
|
}
|
2020-06-30 22:03:31 +08:00
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
StateHasChanged();
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 13:59:56 +08:00
|
|
|
|
_activatedByClick = false;
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ActivateAsync(AnchorLink anchorLink, bool active)
|
|
|
|
|
{
|
2021-03-12 17:02:11 +08:00
|
|
|
|
if (anchorLink == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
anchorLink?.Activate(active);
|
2020-06-30 22:03:31 +08:00
|
|
|
|
|
|
|
|
|
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}'");
|
2020-07-16 13:59:56 +08:00
|
|
|
|
|
2020-06-30 22:03:31 +08:00
|
|
|
|
if (OnClick.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnClick.InvokeAsync(new Tuple<MouseEventArgs, AnchorLink>(args, anchorLink));
|
|
|
|
|
}
|
2020-07-16 13:59:56 +08:00
|
|
|
|
|
|
|
|
|
// 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();
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
2020-08-26 11:13:36 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
|
|
|
|
DomEventService.RemoveEventListerner<JsonElement>("window", "scroll", OnScroll);
|
|
|
|
|
}
|
2020-06-30 22:03:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|