From b4f9875858de78ff197c8fbf92a5c7a427fc6f4c Mon Sep 17 00:00:00 2001 From: Juan Osorio Date: Tue, 3 Dec 2024 10:13:04 -0800 Subject: [PATCH 1/2] MarkdownTextBlock: Adds OnMarkdownParsed event --- .../src/MarkdownParsedEventArgs.cs | 16 ++++++++++++++++ .../src/MarkdownTextBlock.Events.cs | 18 ++++++++++++++++++ .../src/MarkdownTextBlock.xaml.cs | 3 +-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 components/MarkdownTextBlock/src/MarkdownParsedEventArgs.cs create mode 100644 components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs diff --git a/components/MarkdownTextBlock/src/MarkdownParsedEventArgs.cs b/components/MarkdownTextBlock/src/MarkdownParsedEventArgs.cs new file mode 100644 index 000000000..8f96eb19c --- /dev/null +++ b/components/MarkdownTextBlock/src/MarkdownParsedEventArgs.cs @@ -0,0 +1,16 @@ +// 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. + +using Markdig.Syntax; + +namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock; + +public sealed class MarkdownParsedEventArgs : EventArgs +{ + public MarkdownDocument Document { get; } + public MarkdownParsedEventArgs(MarkdownDocument document) + { + Document = document; + } +} diff --git a/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs b/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs new file mode 100644 index 000000000..48aab0ed9 --- /dev/null +++ b/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs @@ -0,0 +1,18 @@ +// 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. + +namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock; + +partial class MarkdownTextBlock +{ + /// + /// Event raised when a markdown link is clicked. + /// + public event EventHandler? OnLinkClicked; + + /// + /// Event raised when markdown is done parsing, with a complete MarkdownDocument. + /// + public event EventHandler? OnMarkdownParsed; +} diff --git a/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs b/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs index 01e05751f..7a5c5a6f1 100644 --- a/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs +++ b/components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs @@ -55,8 +55,6 @@ public MarkdownDocument? MarkdownDocument private set => SetValue(MarkdownDocumentProperty, value); } - public event EventHandler? OnLinkClicked; - internal void RaiseLinkClickedEvent(Uri uri) => OnLinkClicked?.Invoke(this, new LinkClickedEventArgs(uri)); private static void OnConfigChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -116,6 +114,7 @@ private void ApplyText(bool rerender) if (!string.IsNullOrEmpty(Text)) { this.MarkdownDocument = Markdown.Parse(Text, _pipeline); + OnMarkdownParsed?.Invoke(this, new MarkdownParsedEventArgs(this.MarkdownDocument)); _renderer.Render(this.MarkdownDocument); } } From 2e205eedfac8163d7a8c291808082fc6d30db12d Mon Sep 17 00:00:00 2001 From: Juan Osorio Date: Tue, 3 Dec 2024 10:15:29 -0800 Subject: [PATCH 2/2] Clarify timing of event --- components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs b/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs index 48aab0ed9..5faf4e48c 100644 --- a/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs +++ b/components/MarkdownTextBlock/src/MarkdownTextBlock.Events.cs @@ -13,6 +13,7 @@ partial class MarkdownTextBlock /// /// Event raised when markdown is done parsing, with a complete MarkdownDocument. + /// It is always raised before the control renders the document. /// public event EventHandler? OnMarkdownParsed; }