-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathMyHeading.cs
More file actions
101 lines (89 loc) · 3.02 KB
/
MyHeading.cs
File metadata and controls
101 lines (89 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 HtmlAgilityPack;
using Markdig.Syntax;
namespace CommunityToolkit.WinUI.Controls.TextElements;
internal class MyHeading : IAddChild
{
private Paragraph _paragraph;
private HeadingBlock? _headingBlock;
private HtmlNode? _htmlNode;
private MarkdownConfig _config;
public bool IsHtml => _htmlNode != null;
public TextElement TextElement
{
get => _paragraph;
}
public MyHeading(HeadingBlock headingBlock, MarkdownConfig config)
{
_headingBlock = headingBlock;
_paragraph = new Paragraph();
_config = config;
SetHProperties(headingBlock.Level);
}
public MyHeading(HtmlNode htmlNode, MarkdownConfig config)
{
_htmlNode = htmlNode;
_paragraph = new Paragraph();
_config = config;
var align = _htmlNode.GetAttribute("align", "left");
_paragraph.TextAlignment = align switch
{
"left" => TextAlignment.Left,
"right" => TextAlignment.Right,
"center" => TextAlignment.Center,
"justify" => TextAlignment.Justify,
_ => TextAlignment.Left,
};
SetHProperties(int.Parse(htmlNode.Name.Substring(1)));
}
private void SetHProperties(int level)
{
_paragraph.FontSize = level switch
{
1 => _config.Themes.H1FontSize,
2 => _config.Themes.H2FontSize,
3 => _config.Themes.H3FontSize,
4 => _config.Themes.H4FontSize,
5 => _config.Themes.H5FontSize,
_ => _config.Themes.H6FontSize,
};
_paragraph.Foreground = level switch
{
1 => _config.Themes.H1Foreground,
2 => _config.Themes.H2Foreground,
3 => _config.Themes.H3Foreground,
4 => _config.Themes.H4Foreground,
5 => _config.Themes.H5Foreground,
_ => _config.Themes.H6Foreground,
};
_paragraph.FontWeight = level switch
{
1 => _config.Themes.H1FontWeight,
2 => _config.Themes.H2FontWeight,
3 => _config.Themes.H3FontWeight,
4 => _config.Themes.H4FontWeight,
5 => _config.Themes.H5FontWeight,
_ => _config.Themes.H6FontWeight,
};
_paragraph.Margin = level switch
{
1 => _config.Themes.H1Margin,
2 => _config.Themes.H2Margin,
3 => _config.Themes.H3Margin,
4 => _config.Themes.H4Margin,
5 => _config.Themes.H5Margin,
_ => _config.Themes.H6Margin,
};
}
public void AddChild(IAddChild child)
{
if (child.TextElement is Inline inlineChild)
{
if (child is ICascadeChild cascadeChild)
cascadeChild.InheritProperties(this);
_paragraph.Inlines.Add(inlineChild);
}
}
}