Skip to content

Commit

Permalink
支持实时渲染 markdown (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy authored Jun 28, 2024
1 parent 9f24e58 commit 442f890
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public MarkdownTextBlock()
.UseTaskLists()
.UsePipeTables()
.Build();

Unloaded += OnUnloaded;
}

/// <inheritdoc/>
Expand All @@ -88,6 +90,12 @@ private void ApplyConfig(MarkdownConfig config)

private void ApplyText(string text, bool rerender)
{
if (string.IsNullOrEmpty(text) && _renderer != null)
{
Clean();
return;
}

var markdown = Markdig.Markdown.Parse(text, _pipeline);
if (_renderer != null)
{
Expand All @@ -100,6 +108,18 @@ private void ApplyText(string text, bool rerender)
}
}

private void OnUnloaded(object sender, RoutedEventArgs e)
{
Clean();
}

private void Clean()
{
_document.RichTextBlock.Blocks.Clear();
_renderer.Clear();
GC.Collect();
}

private void Build()
{
if (Config != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public override object Render(MarkdownObject markdownObject)
return FlowDocument ?? new();
}

public void ReloadDocument()
public void Clear(bool removeRenderers = false)
{
_stack.Clear();
FlowDocument.RichTextBlock.Blocks.Clear();
ObjectRenderers.Clear();
}

public void ReloadDocument()
{
Clear();
_stack.Push(FlowDocument);
LoadOverridenRenderers();
}
Expand Down
16 changes: 11 additions & 5 deletions src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionHistory.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:items="using:RodelAgent.UI.ViewModels.Items"
xmlns:labs="using:CommunityToolkit.Labs.WinUI"
xmlns:local="using:RodelAgent.UI.Controls.Chat"
xmlns:markdown="using:RodelAgent.UI.Controls.Markdown"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

Expand Down Expand Up @@ -43,22 +44,27 @@
Margin="0,0,0,20"
HorizontalAlignment="Left"
Visibility="{x:Bind ViewModel.IsResponding, Mode=OneWay}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="0.2*" />
</Grid.ColumnDefinitions>
<controls:ColorfulBorder
MinWidth="240"
MinHeight="40"
BorderThickness="1"
CornerRadius="{StaticResource ControlCornerRadius}"
IsLoading="{x:Bind ViewModel.IsResponding, Mode=OneWay}" />
<Grid
MinWidth="200"
MinHeight="40"
MaxWidth="480"
Margin="1"
HorizontalAlignment="Stretch"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
CornerRadius="{StaticResource ControlCornerRadius}">
<TextBlock
<markdown:MarkdownTextBlock Margin="12,8" Text="{x:Bind ViewModel.TempMessage, Mode=OneWay}" />
<!--<TextBlock
Margin="12,8"
IsTextSelectionEnabled="True"
Text="{x:Bind ViewModel.TempMessage, Mode=OneWay}"
TextWrapping="Wrap" />
TextWrapping="Wrap" />-->
</Grid>
</Grid>

Expand Down

0 comments on commit 442f890

Please sign in to comment.