Skip to content

Commit

Permalink
Moved OnLinkClicked event to MarkdownTextBlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
azchohfi authored and michael-hawker committed Nov 22, 2024
1 parent a5ac5d9 commit 02d27e4
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="MarkdownTextBlockExperiment.Samples.MarkdownTextBlockCustomSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -25,7 +25,8 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<controls:MarkdownTextBlock Grid.Column="0"
<controls:MarkdownTextBlock x:Name="MarkdownTextBlock1"
Grid.Column="0"
Config="{x:Bind LiveMarkdownConfig, Mode=OneTime}"
Text="{x:Bind MarkdownTextBox.Text, Mode=OneWay}" />
<TextBox x:Name="MarkdownTextBox"
Expand All @@ -37,7 +38,8 @@
FontSize="16"
FontWeight="Bold"
Text="Built-in Sample" />
<controls:MarkdownTextBlock Grid.Row="3"
<controls:MarkdownTextBlock x:Name="MarkdownTextBlock2"
Grid.Row="3"
Config="{x:Bind MarkdownConfig, Mode=OneTime}"
Text="{x:Bind Text, Mode=OneTime}" />
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,14 @@ public MarkdownTextBlockCustomSample()
{
this.InitializeComponent();
_config = new MarkdownConfig();
_config.OnLinkClicked += this._config_OnLinkClicked;
_liveConfig = new MarkdownConfig();
_liveConfig.OnLinkClicked += this._config_OnLinkClicked;
_text = _markdown;
MarkdownTextBox.Text = "# Hello World\n\n";
MarkdownTextBlock1.OnLinkClicked += MarkdownTextBlock_OnLinkClicked;
MarkdownTextBlock2.OnLinkClicked += MarkdownTextBlock_OnLinkClicked;
}

private void _config_OnLinkClicked(object? sender, LinkClickedEventArgs e)
private void MarkdownTextBlock_OnLinkClicked(object? sender, LinkClickedEventArgs e)
{
Debug.WriteLine($"Link Clicked: {e.Uri}");
}
Expand Down
4 changes: 2 additions & 2 deletions components/MarkdownTextBlock/src/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void WriteHtml(WinUIRenderer renderer, HtmlNodeCollection nodes)
var myHyperlinkButton = new MyHyperlinkButton(node, renderer.Config.BaseUrl);
myHyperlinkButton.ClickEvent += (sender, e) =>
{
renderer.Config.RaiseLinkClickedEvent(renderer, ((HyperlinkButton)sender).NavigateUri);
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(((HyperlinkButton)sender).NavigateUri);
};
hyperLink = myHyperlinkButton;
}
Expand All @@ -45,7 +45,7 @@ public static void WriteHtml(WinUIRenderer renderer, HtmlNodeCollection nodes)
var myHyperlink = new MyHyperlink(node, renderer.Config.BaseUrl);
myHyperlink.ClickEvent += (sender, e) =>
{
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(sender.NavigateUri);
};
hyperLink = myHyperlink;
}
Expand Down
2 changes: 0 additions & 2 deletions components/MarkdownTextBlock/src/MarkdownConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public record MarkdownConfig
public IImageProvider? ImageProvider { get; set; }
public ISVGRenderer? SVGRenderer { get; set; }
public MarkdownThemes Themes { get; set; } = MarkdownThemes.Default;
public event EventHandler<LinkClickedEventArgs>? OnLinkClicked;
internal void RaiseLinkClickedEvent(object? sender, Uri uri) => OnLinkClicked?.Invoke(sender, new LinkClickedEventArgs(uri));

public static MarkdownConfig Default = new();
}
6 changes: 5 additions & 1 deletion components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public string Text
set => SetValue(TextProperty, value);
}

public event EventHandler<LinkClickedEventArgs>? OnLinkClicked;

internal void RaiseLinkClickedEvent(Uri uri) => OnLinkClicked?.Invoke(this, new LinkClickedEventArgs(uri));

private static void OnConfigChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
Expand Down Expand Up @@ -110,7 +114,7 @@ private void Build()
{
if (_renderer == null)
{
_renderer = new WinUIRenderer(_document, Config);
_renderer = new WinUIRenderer(_document, Config, this);
}
_pipeline.Setup(_renderer);
ApplyText(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void Write(WinUIRenderer renderer, AutolinkInline link)
var autolink = new MyAutolinkInline(link);
autolink.ClickEvent += (sender, e) =>
{
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(sender.NavigateUri);
};

renderer.Push(autolink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override void Write(WinUIRenderer renderer, LinkInline link)
var myHyperlinkButton = new MyHyperlinkButton(link, renderer.Config.BaseUrl);
myHyperlinkButton.ClickEvent += (sender, e) =>
{
renderer.Config.RaiseLinkClickedEvent(renderer, ((HyperlinkButton)sender).NavigateUri);
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(((HyperlinkButton)sender).NavigateUri);
};
renderer.Push(myHyperlinkButton);
}
Expand All @@ -42,7 +42,7 @@ protected override void Write(WinUIRenderer renderer, LinkInline link)
var hyperlink = new MyHyperlink(link, renderer.Config.BaseUrl);
hyperlink.ClickEvent += (sender, e) =>
{
renderer.Config.RaiseLinkClickedEvent(renderer, sender.NavigateUri);
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(sender.NavigateUri);
};

renderer.Push(hyperlink);
Expand Down
4 changes: 3 additions & 1 deletion components/MarkdownTextBlock/src/Renderers/WinUIRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public MarkdownConfig Config
get => _config;
set => _config = value;
}
public MarkdownTextBlock MarkdownTextBlock { get; }

public WinUIRenderer(MyFlowDocument document, MarkdownConfig config)
public WinUIRenderer(MyFlowDocument document, MarkdownConfig config, MarkdownTextBlock markdownTextBlock)
{
_buffer = new char[1024];
Config = config;
MarkdownTextBlock = markdownTextBlock;
FlowDocument = document;
// set style
_stack.Push(FlowDocument);
Expand Down

0 comments on commit 02d27e4

Please sign in to comment.