Skip to content

Commit 8a3508f

Browse files
committed
Add another HTML panel for the HtmlKit output in WPF
1 parent c3754a6 commit 8a3508f

2 files changed

Lines changed: 67 additions & 11 deletions

File tree

‎Source/Demo/WPF/MainControl.xaml‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,48 @@
3737
<WebBrowser x:Name="_webBrowser" Grid.Column="2" Visibility="Collapsed"/>
3838
</Grid>
3939
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" Height="4" Background="#BFDBFF" />
40-
<xctk:RichTextBox x:Name="_htmlEditor" Grid.Row="2" VerticalScrollBarVisibility="Visible" BorderThickness="0"
41-
TextChanged="OnHtmlEditor_TextChanged" />
40+
<Grid Grid.Row="2">
41+
<Grid.ColumnDefinitions>
42+
<ColumnDefinition Width="*" MinWidth="150" />
43+
<ColumnDefinition Width="4" />
44+
<ColumnDefinition Width="*" MinWidth="150" />
45+
</Grid.ColumnDefinitions>
46+
<Grid Grid.Column="0">
47+
<Grid.RowDefinitions>
48+
<RowDefinition Height="Auto" />
49+
<RowDefinition Height="*" />
50+
</Grid.RowDefinitions>
51+
<Grid Grid.Row="0" Margin="0,0,0,2">
52+
<Grid.ColumnDefinitions>
53+
<ColumnDefinition Width="*" />
54+
<ColumnDefinition Width="Auto" />
55+
<ColumnDefinition Width="Auto" />
56+
</Grid.ColumnDefinitions>
57+
<Label Grid.Column="0" VerticalAlignment="Center">Input HTML</Label>
58+
<Label Grid.Column="1" Margin="0,0,10,0" VerticalAlignment="Center" FontSize="11" Foreground="Blue" Cursor="Hand"
59+
MouseLeftButtonUp="OnRefreshLink_MouseLeftButtonUp">
60+
<Underline>Refresh</Underline>
61+
</Label>
62+
<CheckBox x:Name="_coloredCheckBox" Grid.Column="2" VerticalAlignment="Center" FontSize="11" Foreground="Blue" Cursor="Hand"
63+
Click="OnColoredCheckbox_click">
64+
<Underline>Colored</Underline>
65+
</CheckBox>
66+
</Grid>
67+
<xctk:RichTextBox x:Name="_htmlEditor" Grid.Row="1" BorderThickness="0"
68+
VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"
69+
TextChanged="OnHtmlEditor_TextChanged" />
70+
</Grid>
71+
<GridSplitter Grid.Column="1" Width="4" VerticalAlignment="Stretch" ResizeBehavior="PreviousAndNext" Background="#BFDBFF" />
72+
<Grid Grid.Column="2">
73+
<Grid.RowDefinitions>
74+
<RowDefinition Height="Auto" />
75+
<RowDefinition Height="*" />
76+
</Grid.RowDefinitions>
77+
<Label Grid.Row="0" VerticalAlignment="Center">Fixed HTML</Label>
78+
<xctk:RichTextBox x:Name="_fixedHtmlEditor" Grid.Row="1" BorderThickness="0" IsReadOnly="True"
79+
VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" />
80+
</Grid>
81+
</Grid>
4282
</Grid>
43-
<Label HorizontalAlignment="Right" Margin="0,0,16,14" VerticalAlignment="Bottom" Grid.Column="1" FontSize="11" Foreground="Blue" Cursor="Hand"
44-
MouseLeftButtonUp="OnRefreshLink_MouseLeftButtonUp">
45-
<Underline>Refresh</Underline>
46-
</Label>
47-
<CheckBox x:Name="_coloredCheckBox" HorizontalAlignment="Right" Margin="0,0,22,2" VerticalAlignment="Bottom" Grid.Column="1" FontSize="11" Foreground="Blue" Cursor="Hand"
48-
Click="OnColoredCheckbox_click">
49-
<Underline>Colored</Underline>
50-
</CheckBox>
5183
</Grid>
5284
</UserControl>

‎Source/Demo/WPF/MainControl.xaml.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public string GetHtml()
128128
public void SetHtml(string html)
129129
{
130130
_htmlPanel.Text = html;
131+
UpdateFixedHtmlView();
131132
if (string.IsNullOrWhiteSpace(html))
132133
{
133134
_htmlPanel.InvalidateMeasure();
@@ -219,9 +220,11 @@ private void OnTreeView_SelectedItemChanged(object sender, RoutedPropertyChanged
219220
{
220221
_htmlPanel.AvoidImagesLateLoading = !sample.FullName.Contains("Many images");
221222
_htmlPanel.Text = sample.Html;
223+
UpdateFixedHtmlView();
222224
}
223225
catch (Exception ex)
224226
{
227+
SetFixedHtmlText(string.Empty);
225228
MessageBox.Show(ex.ToString(), "Failed to render HTML");
226229
}
227230

@@ -255,9 +258,11 @@ private void OnUpdateHtmlTimerTick(object state)
255258
try
256259
{
257260
_htmlPanel.Text = GetHtmlEditorText();
261+
UpdateFixedHtmlView();
258262
}
259263
catch (Exception ex)
260264
{
265+
SetFixedHtmlText(string.Empty);
261266
MessageBox.Show(ex.ToString(), "Failed to render HTML");
262267
}
263268

@@ -307,6 +312,23 @@ private string GetFixedHtml()
307312
return html;
308313
}
309314

315+
/// <summary>
316+
/// Update the fixed html shown in the read-only editor.
317+
/// </summary>
318+
private void UpdateFixedHtmlView()
319+
{
320+
SetFixedHtmlText(HtmlPrettyPrinter.Format(_htmlPanel.GetHtml() ?? string.Empty), _coloredCheckBox.IsChecked.GetValueOrDefault(false));
321+
}
322+
323+
/// <summary>
324+
/// Set formatted HTML text in the fixed HTML viewer.
325+
/// </summary>
326+
private void SetFixedHtmlText(string text, bool color = true)
327+
{
328+
text = text ?? string.Empty;
329+
_fixedHtmlEditor.Text = color ? HtmlSyntaxHighlighter.Process(text) : text.Replace("\n", "\\par ");
330+
}
331+
310332
/// <summary>
311333
/// Reload the html shown in the html editor by running coloring again.
312334
/// </summary>
@@ -320,7 +342,9 @@ private void OnRefreshLink_MouseLeftButtonUp(object sender, MouseButtonEventArgs
320342
/// </summary>
321343
private void OnColoredCheckbox_click(object sender, RoutedEventArgs e)
322344
{
323-
SetColoredText(GetHtmlEditorText(), _coloredCheckBox.IsChecked.GetValueOrDefault(false));
345+
var color = _coloredCheckBox.IsChecked.GetValueOrDefault(false);
346+
SetColoredText(GetHtmlEditorText(), color);
347+
UpdateFixedHtmlView();
324348
}
325349

326350
/// <summary>

0 commit comments

Comments
 (0)