Skip to content

Commit

Permalink
MarkdownTextBlock: Allows passing an existing MarkdownDocument as the…
Browse files Browse the repository at this point in the history
… content, closes CommunityToolkit#596
  • Loading branch information
Juan Osorio committed Nov 14, 2024
1 parent 05709d0 commit 6131f9a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 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 Down Expand Up @@ -39,6 +39,6 @@
Text="Built-in Sample" />
<controls:MarkdownTextBlock Grid.Row="3"
Config="{x:Bind MarkdownConfig, Mode=OneTime}"
Text="{x:Bind Text, Mode=OneTime}" />
MarkdownDocument="{x:Bind Doc, Mode=OneTime}" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
using Markdig;
using Markdig.Syntax;

namespace MarkdownTextBlockExperiment.Samples;

Expand All @@ -14,7 +16,7 @@ public sealed partial class MarkdownTextBlockCustomSample : Page
{
private MarkdownConfig _config;
private MarkdownConfig _liveConfig;
private string _text;
private MarkdownDocument _doc;
private const string _markdown = @"
This control was originally written by [Nero Cui](https://github.com/nerocui) for [JitHub](https://github.com/JitHubApp/JitHubV2). The control is powered by the popular [Markdig](https://github.com/xoofx/markdig) markdown parsing library and *almost* supports the full markdown syntax, with a focus on super-efficient parsing and rendering.
Expand Down Expand Up @@ -594,18 +596,18 @@ public MarkdownConfig LiveMarkdownConfig
set => _liveConfig = value;
}

public string Text
public MarkdownDocument Doc
{
get => _text;
set => _text = value;
get => _doc;
set => _doc = value;
}

public MarkdownTextBlockCustomSample()
{
this.InitializeComponent();
_config = new MarkdownConfig();
_liveConfig = new MarkdownConfig();
_text = _markdown;
_doc = Markdown.Parse(_markdown);
MarkdownTextBox.Text = "# Hello World\n\n";
}
}
26 changes: 23 additions & 3 deletions components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.Renderers;
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
using Markdig;
using Markdig.Syntax;

namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;

Expand All @@ -30,6 +31,12 @@ public partial class MarkdownTextBlock : Control
typeof(MarkdownTextBlock),
new PropertyMetadata(null, OnTextChanged));

private static readonly DependencyProperty MarkdownDocumentProperty = DependencyProperty.Register(
nameof(MarkdownDocument),
typeof(MarkdownDocument),
typeof(MarkdownTextBlock),
new PropertyMetadata(null, OnMarkdownDocumentChanged));

public MarkdownConfig Config
{
get => (MarkdownConfig)GetValue(ConfigProperty);
Expand All @@ -42,6 +49,12 @@ public string Text
set => SetValue(TextProperty, value);
}

public MarkdownDocument? MarkdownDocument
{
get => (MarkdownDocument?)GetValue(MarkdownDocumentProperty);
set => SetValue(MarkdownDocumentProperty, value);
}

private static void OnConfigChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
Expand All @@ -51,6 +64,14 @@ private static void OnConfigChanged(DependencyObject d, DependencyPropertyChange
}

private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
{
self.MarkdownDocument = string.IsNullOrEmpty(self.Text) ? null : Markdown.Parse(self.Text, self._pipeline);
}
}

private static void OnMarkdownDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MarkdownTextBlock self && e.NewValue != null)
{
Expand Down Expand Up @@ -96,10 +117,9 @@ private void ApplyText(bool rerender)
_renderer.ReloadDocument();
}

if (!string.IsNullOrEmpty(Text))
if (MarkdownDocument != null)
{
var markdown = Markdown.Parse(Text, _pipeline);
_renderer.Render(markdown);
_renderer.Render(MarkdownDocument);
}
}
}
Expand Down

0 comments on commit 6131f9a

Please sign in to comment.