|
1 | | -// Licensed to the .NET Foundation under one or more agreements. |
2 | | -// The .NET Foundation licenses this file to you under the MIT license. |
3 | | -// See the LICENSE file in the project root for more information. |
4 | | - |
5 | | -using Markdig.Syntax; |
6 | | -using RomanNumerals; |
7 | | -using System.Globalization; |
8 | | - |
9 | | -namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements; |
10 | | - |
11 | | -internal class MyList : IAddChild |
12 | | -{ |
13 | | - private Paragraph _paragraph; |
14 | | - private InlineUIContainer _container; |
15 | | - private StackPanel _stackPanel; |
16 | | - private ListBlock _listBlock; |
17 | | - private BulletType _bulletType; |
18 | | - private bool _isOrdered; |
19 | | - private int _startIndex = 1; |
20 | | - private int _index = 1; |
21 | | - private const string _dot = "• "; |
22 | | - |
23 | | - public TextElement TextElement |
24 | | - { |
25 | | - get => _paragraph; |
26 | | - } |
27 | | - |
28 | | - public MyList(ListBlock listBlock) |
29 | | - { |
30 | | - _paragraph = new Paragraph(); |
31 | | - _container = new InlineUIContainer(); |
32 | | - _stackPanel = new StackPanel(); |
33 | | - _listBlock = listBlock; |
34 | | - |
35 | | - if (listBlock.IsOrdered) |
36 | | - { |
37 | | - _isOrdered = true; |
38 | | - _bulletType = ToBulletType(listBlock.BulletType); |
39 | | - |
40 | | - if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart)) |
41 | | - { |
42 | | - _startIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo); |
43 | | - _index = _startIndex; |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - _stackPanel.Orientation = Orientation.Vertical; |
48 | | - _stackPanel.Margin = new Thickness(left: 0, top: 8, right: 0, bottom: 8); |
49 | | - _container.Child = _stackPanel; |
50 | | - _paragraph.Inlines.Add(_container); |
51 | | - } |
52 | | - |
53 | | - public void AddChild(IAddChild child) |
54 | | - { |
55 | | - var grid = new Grid(); |
56 | | - grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Pixel) }); |
57 | | - grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Pixel) }); |
58 | | - grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); |
59 | | - string bullet; |
60 | | - if (_isOrdered) |
61 | | - { |
62 | | - bullet = _bulletType switch |
63 | | - { |
64 | | - BulletType.Number => $"{_index}. ", |
65 | | - BulletType.LowerAlpha => $"{_index.ToAlphabetical()}. ", |
66 | | - BulletType.UpperAlpha => $"{_index.ToAlphabetical().ToUpper()}. ", |
67 | | - BulletType.LowerRoman => $"{_index.ToRomanNumerals().ToLower()} ", |
68 | | - BulletType.UpperRoman => $"{_index.ToRomanNumerals().ToUpper()} ", |
69 | | - BulletType.Circle => _dot, |
70 | | - _ => _dot |
71 | | - }; |
72 | | - _index++; |
73 | | - } |
74 | | - else |
75 | | - { |
76 | | - bullet = _dot; |
77 | | - } |
78 | | - var textBlock = new TextBlock() |
79 | | - { |
80 | | - Text = bullet, |
81 | | - }; |
82 | | - textBlock.SetValue(Grid.ColumnProperty, 0); |
83 | | - textBlock.VerticalAlignment = VerticalAlignment.Top; |
84 | | - textBlock.TextAlignment = TextAlignment.Right; |
85 | | - AutomationProperties.SetAccessibilityView(textBlock, AccessibilityView.Raw); |
86 | | - grid.Children.Add(textBlock); |
87 | | - var flowDoc = new MyFlowDocument(); |
88 | | - flowDoc.AddChild(child); |
89 | | - |
90 | | - flowDoc.RichTextBlock.SetValue(Grid.ColumnProperty, 2); |
91 | | - flowDoc.RichTextBlock.Padding = new Thickness(0); |
92 | | - flowDoc.RichTextBlock.VerticalAlignment = VerticalAlignment.Top; |
93 | | - grid.Children.Add(flowDoc.RichTextBlock); |
94 | | - |
95 | | - _stackPanel.Children.Add(grid); |
96 | | - } |
97 | | - |
98 | | - private BulletType ToBulletType(char bullet) |
99 | | - { |
100 | | - // Gets or sets the type of the bullet (e.g: '1', 'a', 'A', 'i', 'I'). |
101 | | - switch (bullet) |
102 | | - { |
103 | | - case '1': |
104 | | - return BulletType.Number; |
105 | | - case 'a': |
106 | | - return BulletType.LowerAlpha; |
107 | | - case 'A': |
108 | | - return BulletType.UpperAlpha; |
109 | | - case 'i': |
110 | | - return BulletType.LowerRoman; |
111 | | - case 'I': |
112 | | - return BulletType.UpperRoman; |
113 | | - default: |
114 | | - return BulletType.Circle; |
115 | | - } |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -internal enum BulletType |
120 | | -{ |
121 | | - Circle, |
122 | | - Number, |
123 | | - LowerAlpha, |
124 | | - UpperAlpha, |
125 | | - LowerRoman, |
126 | | - UpperRoman |
127 | | -} |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Markdig.Syntax; |
| 6 | +using RomanNumerals; |
| 7 | +using System.Globalization; |
| 8 | + |
| 9 | +namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements; |
| 10 | + |
| 11 | +internal class MyList : IAddChild |
| 12 | +{ |
| 13 | + private Paragraph _paragraph; |
| 14 | + private InlineUIContainer _container; |
| 15 | + private StackPanel _stackPanel; |
| 16 | + private ListBlock _listBlock; |
| 17 | + private BulletType _bulletType; |
| 18 | + private bool _isOrdered; |
| 19 | + private int _startIndex = 1; |
| 20 | + private int _index = 1; |
| 21 | + private const string _dot = "• "; |
| 22 | + |
| 23 | + public TextElement TextElement |
| 24 | + { |
| 25 | + get => _paragraph; |
| 26 | + } |
| 27 | + |
| 28 | + public MyList(ListBlock listBlock) |
| 29 | + { |
| 30 | + _paragraph = new Paragraph(); |
| 31 | + _container = new InlineUIContainer(); |
| 32 | + _stackPanel = new StackPanel(); |
| 33 | + _listBlock = listBlock; |
| 34 | + |
| 35 | + if (listBlock.IsOrdered) |
| 36 | + { |
| 37 | + _isOrdered = true; |
| 38 | + _bulletType = ToBulletType(listBlock.BulletType); |
| 39 | + |
| 40 | + if (listBlock.OrderedStart != null && (listBlock.DefaultOrderedStart != listBlock.OrderedStart)) |
| 41 | + { |
| 42 | + _startIndex = int.Parse(listBlock.OrderedStart, NumberFormatInfo.InvariantInfo); |
| 43 | + _index = _startIndex; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + _stackPanel.Orientation = Orientation.Vertical; |
| 48 | + _stackPanel.Margin = new Thickness(left: 0, top: 8, right: 0, bottom: 8); |
| 49 | + _container.Child = _stackPanel; |
| 50 | + _paragraph.Inlines.Add(_container); |
| 51 | + } |
| 52 | + |
| 53 | + public void AddChild(IAddChild child) |
| 54 | + { |
| 55 | + var grid = new Grid(); |
| 56 | + grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20, GridUnitType.Pixel) }); |
| 57 | + grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10, GridUnitType.Pixel) }); |
| 58 | + grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); |
| 59 | + string bullet; |
| 60 | + if (_isOrdered) |
| 61 | + { |
| 62 | + bullet = _bulletType switch |
| 63 | + { |
| 64 | + BulletType.Number => $"{_index}. ", |
| 65 | + BulletType.LowerAlpha => $"{_index.ToAlphabetical()}. ", |
| 66 | + BulletType.UpperAlpha => $"{_index.ToAlphabetical().ToUpper()}. ", |
| 67 | + BulletType.LowerRoman => $"{_index.ToRomanNumerals().ToLower()} ", |
| 68 | + BulletType.UpperRoman => $"{_index.ToRomanNumerals().ToUpper()} ", |
| 69 | + BulletType.Circle => _dot, |
| 70 | + _ => _dot |
| 71 | + }; |
| 72 | + _index++; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + bullet = _dot; |
| 77 | + } |
| 78 | + var textBlock = new TextBlock() |
| 79 | + { |
| 80 | + Text = bullet, |
| 81 | + }; |
| 82 | + textBlock.SetValue(Grid.ColumnProperty, 0); |
| 83 | + textBlock.VerticalAlignment = VerticalAlignment.Top; |
| 84 | + textBlock.TextAlignment = TextAlignment.Right; |
| 85 | + // Mark this Raw so narrator users will not set focus on it |
| 86 | + AutomationProperties.SetAccessibilityView(textBlock, AccessibilityView.Raw); |
| 87 | + grid.Children.Add(textBlock); |
| 88 | + var flowDoc = new MyFlowDocument(); |
| 89 | + flowDoc.AddChild(child); |
| 90 | + |
| 91 | + flowDoc.RichTextBlock.SetValue(Grid.ColumnProperty, 2); |
| 92 | + flowDoc.RichTextBlock.Padding = new Thickness(0); |
| 93 | + flowDoc.RichTextBlock.VerticalAlignment = VerticalAlignment.Top; |
| 94 | + grid.Children.Add(flowDoc.RichTextBlock); |
| 95 | + |
| 96 | + _stackPanel.Children.Add(grid); |
| 97 | + } |
| 98 | + |
| 99 | + private BulletType ToBulletType(char bullet) |
| 100 | + { |
| 101 | + // Gets or sets the type of the bullet (e.g: '1', 'a', 'A', 'i', 'I'). |
| 102 | + switch (bullet) |
| 103 | + { |
| 104 | + case '1': |
| 105 | + return BulletType.Number; |
| 106 | + case 'a': |
| 107 | + return BulletType.LowerAlpha; |
| 108 | + case 'A': |
| 109 | + return BulletType.UpperAlpha; |
| 110 | + case 'i': |
| 111 | + return BulletType.LowerRoman; |
| 112 | + case 'I': |
| 113 | + return BulletType.UpperRoman; |
| 114 | + default: |
| 115 | + return BulletType.Circle; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +internal enum BulletType |
| 121 | +{ |
| 122 | + Circle, |
| 123 | + Number, |
| 124 | + LowerAlpha, |
| 125 | + UpperAlpha, |
| 126 | + LowerRoman, |
| 127 | + UpperRoman |
| 128 | +} |
0 commit comments