Skip to content

Commit b41d8f0

Browse files
azchohfimichael-hawker
authored andcommitted
Added OnLinkClicked event.
1 parent b1939e3 commit b41d8f0

File tree

9 files changed

+100
-21
lines changed

9 files changed

+100
-21
lines changed

components/MarkdownTextBlock/samples/MarkdownTextBlockCustomSample.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,15 @@ public MarkdownTextBlockCustomSample()
604604
{
605605
this.InitializeComponent();
606606
_config = new MarkdownConfig();
607+
_config.OnLinkClicked += this._config_OnLinkClicked;
607608
_liveConfig = new MarkdownConfig();
609+
_liveConfig.OnLinkClicked += this._config_OnLinkClicked;
608610
_text = _markdown;
609611
MarkdownTextBox.Text = "# Hello World\n\n";
610612
}
613+
614+
private void _config_OnLinkClicked(object? sender, LinkClickedEventArgs e)
615+
{
616+
Debug.WriteLine($"Link Clicked: {e.Uri}");
617+
}
611618
}

components/MarkdownTextBlock/src/HtmlWriter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ public static void WriteHtml(WinUIRenderer renderer, HtmlNodeCollection nodes)
3333
IAddChild hyperLink;
3434
if (node.ChildNodes.Any(n => n.Name != "#text"))
3535
{
36-
hyperLink = new MyHyperlinkButton(node, renderer.Config.BaseUrl);
36+
var myHyperlinkButton = new MyHyperlinkButton(node, renderer.Config.BaseUrl);
37+
myHyperlinkButton.ClickEvent += (sender, e) =>
38+
{
39+
renderer.Config.RaiseLinkClickedEvent(renderer, ((HyperlinkButton)sender).NavigateUri);
40+
};
41+
hyperLink = myHyperlinkButton;
3742
}
3843
else
3944
{
40-
hyperLink = new MyHyperlink(node, renderer.Config.BaseUrl);
45+
var myHyperlink = new MyHyperlink(node, renderer.Config.BaseUrl);
46+
myHyperlink.ClickEvent += (sender, e) =>
47+
{
48+
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
49+
};
50+
hyperLink = myHyperlink;
4151
}
4252
renderer.Push(hyperLink);
4353
WriteHtml(renderer, node.ChildNodes);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
6+
7+
public class LinkClickedEventArgs : EventArgs
8+
{
9+
public Uri Uri { get; }
10+
11+
public LinkClickedEventArgs(Uri uri)
12+
{
13+
this.Uri = uri;
14+
}
15+
}

components/MarkdownTextBlock/src/MarkdownConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public record MarkdownConfig
1212
public IImageProvider? ImageProvider { get; set; }
1313
public ISVGRenderer? SVGRenderer { get; set; }
1414
public MarkdownThemes Themes { get; set; } = MarkdownThemes.Default;
15+
public event EventHandler<LinkClickedEventArgs>? OnLinkClicked;
16+
internal void RaiseLinkClickedEvent(object? sender, Uri uri) => OnLinkClicked?.Invoke(sender, new LinkClickedEventArgs(uri));
1517

1618
public static MarkdownConfig Default = new();
1719
}

components/MarkdownTextBlock/src/Renderers/ObjectRenderers/Inlines/AutoLinkInlineRenderer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ protected override void Write(WinUIRenderer renderer, AutolinkInline link)
2626
}
2727

2828
var autolink = new MyAutolinkInline(link);
29+
autolink.ClickEvent += (sender, e) =>
30+
{
31+
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
32+
};
2933

3034
renderer.Push(autolink);
3135

components/MarkdownTextBlock/src/Renderers/ObjectRenderers/Inlines/LinkInlineRenderer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,20 @@ protected override void Write(WinUIRenderer renderer, LinkInline link)
3030
{
3131
if (link.FirstChild is LinkInline linkInlineChild && linkInlineChild.IsImage)
3232
{
33-
renderer.Push(new MyHyperlinkButton(link, renderer.Config.BaseUrl));
33+
var myHyperlinkButton = new MyHyperlinkButton(link, renderer.Config.BaseUrl);
34+
myHyperlinkButton.ClickEvent += (sender, e) =>
35+
{
36+
renderer.Config.RaiseLinkClickedEvent(renderer, ((HyperlinkButton)sender).NavigateUri);
37+
};
38+
renderer.Push(myHyperlinkButton);
3439
}
3540
else
3641
{
3742
var hyperlink = new MyHyperlink(link, renderer.Config.BaseUrl);
43+
hyperlink.ClickEvent += (sender, e) =>
44+
{
45+
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
46+
};
3847

3948
renderer.Push(hyperlink);
4049
}

components/MarkdownTextBlock/src/TextElements/MyAutolinkInline.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ internal class MyAutolinkInline : IAddChild
1212

1313
public TextElement TextElement { get; private set; }
1414

15+
public event TypedEventHandler<Hyperlink, HyperlinkClickEventArgs>? ClickEvent
16+
{
17+
add
18+
{
19+
((Hyperlink)TextElement).Click += value;
20+
}
21+
remove
22+
{
23+
((Hyperlink)TextElement).Click -= value;
24+
}
25+
}
26+
1527
public MyAutolinkInline(AutolinkInline autoLinkInline)
1628
{
1729
_autoLinkInline = autoLinkInline;
@@ -21,7 +33,6 @@ public MyAutolinkInline(AutolinkInline autoLinkInline)
2133
};
2234
}
2335

24-
2536
public void AddChild(IAddChild child)
2637
{
2738
try

components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ internal class MyHyperlink : IAddChild
1414
private HtmlNode? _htmlNode;
1515
private string? _baseUrl;
1616

17+
public event TypedEventHandler<Hyperlink, HyperlinkClickEventArgs> ClickEvent
18+
{
19+
add
20+
{
21+
_hyperlink.Click += value;
22+
}
23+
remove
24+
{
25+
_hyperlink.Click -= value;
26+
}
27+
}
28+
1729
public bool IsHtml => _htmlNode != null;
1830

1931
public TextElement TextElement

components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
99

1010
internal class MyHyperlinkButton : IAddChild
1111
{
12-
private HyperlinkButton? _hyperLinkButton;
12+
private HyperlinkButton _hyperLinkButton;
1313
private InlineUIContainer _inlineUIContainer = new InlineUIContainer();
14-
private MyFlowDocument? _flowDoc;
14+
private MyFlowDocument _flowDoc;
1515
private string? _baseUrl;
1616
private LinkInline? _linkInline;
1717
private HtmlNode? _htmlNode;
18+
19+
public event RoutedEventHandler? ClickEvent
20+
{
21+
add
22+
{
23+
_hyperLinkButton.Click += value;
24+
}
25+
remove
26+
{
27+
_hyperLinkButton.Click -= value;
28+
}
29+
}
1830

1931
public bool IsHtml => _htmlNode != null;
2032

@@ -24,43 +36,40 @@ public TextElement TextElement
2436
}
2537

2638
public MyHyperlinkButton(LinkInline linkInline, string? baseUrl)
39+
: this(linkInline.GetDynamicUrl != null ? linkInline.GetDynamicUrl() ?? linkInline.Url : linkInline.Url, baseUrl, null, linkInline)
2740
{
28-
_baseUrl = baseUrl;
29-
var url = linkInline.GetDynamicUrl != null ? linkInline.GetDynamicUrl() ?? linkInline.Url : linkInline.Url;
30-
_linkInline = linkInline;
31-
Init(url, baseUrl);
3241
}
3342

3443
public MyHyperlinkButton(HtmlNode htmlNode, string? baseUrl)
44+
: this(htmlNode.GetAttribute("href", "#"), baseUrl, htmlNode, null)
3545
{
36-
_baseUrl = baseUrl;
37-
_htmlNode = htmlNode;
38-
var url = htmlNode.GetAttribute("href", "#");
39-
Init(url, baseUrl);
4046
}
4147

42-
private void Init(string? url, string? baseUrl)
48+
private MyHyperlinkButton(string? url, string? baseUrl, HtmlNode? htmlNode, LinkInline? linkInline)
4349
{
44-
_hyperLinkButton = new HyperlinkButton()
50+
_baseUrl = baseUrl;
51+
_htmlNode = htmlNode;
52+
_linkInline = linkInline;
53+
_hyperLinkButton = new HyperlinkButton
4554
{
4655
NavigateUri = Extensions.GetUri(url, baseUrl),
4756
};
4857
_hyperLinkButton.Padding = new Thickness(0);
4958
_hyperLinkButton.Margin = new Thickness(0);
50-
if (IsHtml && _htmlNode != null)
59+
if (_htmlNode != null)
5160
{
5261
_flowDoc = new MyFlowDocument(_htmlNode);
5362
}
54-
else if (_linkInline != null)
63+
else
5564
{
56-
_flowDoc = new MyFlowDocument(_linkInline);
65+
_flowDoc = new MyFlowDocument(_linkInline!);
5766
}
5867
_inlineUIContainer.Child = _hyperLinkButton;
59-
_hyperLinkButton.Content = _flowDoc?.RichTextBlock;
68+
_hyperLinkButton.Content = _flowDoc.RichTextBlock;
6069
}
6170

6271
public void AddChild(IAddChild child)
6372
{
64-
_flowDoc?.AddChild(child);
73+
_flowDoc.AddChild(child);
6574
}
6675
}

0 commit comments

Comments
 (0)