Skip to content

Commit 52b3082

Browse files
committed
feat: add OnHyperLinkClicked callback
1 parent e1d06d8 commit 52b3082

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

MdXaml/Markdown.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public class Markdown : DependencyObject, IMarkdown, IUriContext
8181

8282
public ICommand? HyperlinkCommand { get; set; }
8383

84+
public HyperLinkClickCallback? OnHyperLinkClicked { get; set; }
85+
8486
public Uri? BaseUri { get; set; }
8587

8688
private MdXamlPlugins? _plugins;
@@ -579,6 +581,18 @@ private Inline TreatsAsHref(Match match)
579581
var result = Create<Hyperlink, Inline>(PrivateRunSpanGamut(linkText));
580582
result.CommandParameter = url;
581583
result.Command = HyperlinkCommand;
584+
if (OnHyperLinkClicked is not null)
585+
{
586+
result.Click += (sender, e) =>
587+
{
588+
var hyperlink = sender as Hyperlink;
589+
if (hyperlink is not null)
590+
{
591+
string? url = hyperlink.CommandParameter as string;
592+
OnHyperLinkClicked.Invoke(url!);
593+
}
594+
};
595+
}
582596

583597
if (!DisabledTootip)
584598
{
@@ -2148,6 +2162,8 @@ private static TResult Create<TResult, TContent>(IEnumerable<TContent> content)
21482162
#endregion
21492163
}
21502164

2165+
public delegate void HyperLinkClickCallback(string url);
2166+
21512167
internal class ParseParam
21522168
{
21532169
public IBlockParser[] PrimaryBlocks { get; }

MdXaml/MarkdownScrollViewer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ public SyntaxVersion Syntax
365365
}
366366
}
367367

368+
private HyperLinkClickCallback _onHyperLinkClicked;
369+
public HyperLinkClickCallback OnHyperLinkClicked
370+
{
371+
get => _onHyperLinkClicked;
372+
set
373+
{
374+
_onHyperLinkClicked = value;
375+
Engine.OnHyperLinkClicked = value;
376+
}
377+
}
378+
368379
public new FlowDocument? Document
369380
{
370381
get
@@ -452,6 +463,7 @@ private void UpdateClickAction()
452463
}
453464

454465
Engine.HyperlinkCommand = new FlowDocumentJumpAnchorIfNecessary(this, command);
466+
Engine.OnHyperLinkClicked = _onHyperLinkClicked;
455467
}
456468

457469
internal void ScrollTo(string fragment, bool updateSourceProperty)

samples/MdXaml.Demo2/MainWindow.xaml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
<Window x:Class="MdXaml.Demo2.MainWindow"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:mdxam="clr-namespace:MdXaml;assembly=MdXaml"
7-
8-
xmlns:local="clr-namespace:MdXaml.Demo2"
9-
mc:Ignorable="d"
1+
<Window x:Class="MdXaml.Demo2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MdXaml.Demo2" xmlns:mdxam="clr-namespace:MdXaml;assembly=MdXaml" mc:Ignorable="d"
102
Title="MainWindow" Height="450" Width="800">
113

124
<Window.DataContext>
@@ -25,9 +17,8 @@
2517
<mdxam:MarkdownScrollViewer
2618
VerticalAlignment="Stretch"
2719
HorizontalAlignment="Stretch"
28-
ClickAction="DisplayWithRelativePath"
29-
Source="{Binding MdSource,Mode=TwoWay}"
30-
/>
20+
ClickAction="DisplayWithRelativePath" OnHyperLinkClicked="OnLinkClicked"
21+
Source="{Binding MdSource,Mode=TwoWay}" />
3122
</DockPanel>
3223
</TabItem>
3324

@@ -39,6 +30,7 @@
3930
VerticalAlignment="Stretch"
4031
HorizontalAlignment="Stretch"
4132
ClickAction="DisplayWithRelativePath"
33+
x:Name="MdCont1"
4234
Height="150"
4335
Markdown="{Binding Text}"/>
4436
</DataTemplate>

samples/MdXaml.Demo2/MainWindow.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -24,5 +25,7 @@ public MainWindow()
2425
{
2526
InitializeComponent();
2627
}
28+
29+
public void OnLinkClicked(string url) => Debug.Print($"URL clicked: {url}");
2730
}
2831
}

0 commit comments

Comments
 (0)