2
2
using Cropper . Blazor . Client . Models ;
3
3
using Microsoft . AspNetCore . Components ;
4
4
using MudBlazor ;
5
- using MudBlazor . Services ;
6
5
using MudBlazor . Utilities ;
7
6
8
7
namespace Cropper . Blazor . Client . Components . Docs ;
9
8
10
- public partial class SectionContent
9
+ public partial class SectionContent : IBrowserViewportObserver
11
10
{
12
11
[ Inject ] protected IJsApiService ? JsApiService { get ; set ; }
13
- [ Inject ] IBreakpointService BreakpointService { get ; set ; } = null ! ;
12
+ [ Inject ] IBrowserViewportService BreakpointService { get ; set ; } = null ! ;
14
13
15
14
protected string Classname =>
16
15
new CssBuilder ( "docs-section-content" )
@@ -41,28 +40,30 @@ public partial class SectionContent
41
40
. AddClass ( "show-code" , HasCode && ShowCode )
42
41
. Build ( ) ;
43
42
44
- [ Parameter ] public string Class { get ; set ; }
43
+ [ Parameter ] public string Class { get ; set ; } = string . Empty ;
45
44
[ Parameter ] public bool DarkenBackground { get ; set ; }
46
45
[ Parameter ] public bool Outlined { get ; set ; } = true ;
47
46
[ Parameter ] public bool ShowCode { get ; set ; } = true ;
48
47
[ Parameter ] public bool Block { get ; set ; }
49
48
[ Parameter ] public bool FullWidth { get ; set ; }
50
- [ Parameter ] public string Code { get ; set ; }
51
- [ Parameter ] public string HighLight { get ; set ; }
52
- [ Parameter ] public IEnumerable < CodeFile > Codes { get ; set ; }
53
- [ Parameter ] public RenderFragment ChildContent { get ; set ; }
49
+ [ Parameter ] public string Code { get ; set ; } = string . Empty ;
50
+ [ Parameter ] public string HighLight { get ; set ; } = string . Empty ;
51
+ [ Parameter ] public IEnumerable < CodeFile > ? Codes { get ; set ; } = null ;
52
+ [ Parameter ] public RenderFragment ChildContent { get ; set ; } = null ! ;
54
53
55
54
private bool HasCode ;
56
- public string ActiveCode ;
55
+ public string ActiveCode = string . Empty ;
57
56
58
57
private bool IsVerticalAlign = false ;
59
58
59
+ Guid IBrowserViewportObserver . Id { get ; } = Guid . NewGuid ( ) ;
60
+
60
61
protected override void OnParametersSet ( )
61
62
{
62
63
if ( Codes != null )
63
64
{
64
65
HasCode = true ;
65
- ActiveCode = Codes . FirstOrDefault ( ) . code ;
66
+ ActiveCode = Codes . First ( ) . code ;
66
67
}
67
68
else if ( ! string . IsNullOrWhiteSpace ( Code ) )
68
69
{
@@ -75,11 +76,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
75
76
{
76
77
if ( firstRender )
77
78
{
78
- await BreakpointService ! . SubscribeAsync ( ( br ) =>
79
- {
80
- IsVerticalAlign = BreakpointService ! . IsMediaSize ( br , Breakpoint . Xs ) ;
81
- InvokeAsync ( StateHasChanged ) ;
82
- } ) ;
79
+ await BreakpointService ! . SubscribeAsync ( this , fireImmediately : true ) ;
83
80
}
84
81
85
82
await base . OnAfterRenderAsync ( firstRender ) ;
@@ -116,35 +113,49 @@ RenderFragment CodeComponent(string code) => builder =>
116
113
{
117
114
try
118
115
{
119
- var key = typeof ( SectionContent ) . Assembly . GetManifestResourceNames ( ) . FirstOrDefault ( x => x . Contains ( $ ".{ code } Code.html") ) ;
120
- using ( var stream = typeof ( SectionContent ) . Assembly . GetManifestResourceStream ( key ) )
121
- using ( var reader = new StreamReader ( stream ) )
122
- {
123
- var read = reader . ReadToEnd ( ) ;
116
+ string ? key = typeof ( SectionContent ) . Assembly . GetManifestResourceNames ( ) . FirstOrDefault ( x => x . Contains ( $ ".{ code } Code.html") ) ;
117
+ using var stream = typeof ( SectionContent ) . Assembly . GetManifestResourceStream ( key ! ) ;
118
+ using var reader = new StreamReader ( stream ! ) ;
119
+ var read = reader . ReadToEnd ( ) ;
124
120
125
- if ( ! string . IsNullOrEmpty ( HighLight ) )
121
+ if ( ! string . IsNullOrEmpty ( HighLight ) )
122
+ {
123
+ if ( HighLight . Contains ( ',' ) )
126
124
{
127
- if ( HighLight . Contains ( "," ) )
128
- {
129
- var highlights = HighLight . Split ( "," ) ;
125
+ var highlights = HighLight . Split ( "," ) ;
130
126
131
- foreach ( var value in highlights )
132
- {
133
- read = Regex . Replace ( read , $ "{ value } (?=\\ s|\" )", $ "<mark>$&</mark>") ;
134
- }
135
- }
136
- else
127
+ foreach ( var value in highlights )
137
128
{
138
- read = Regex . Replace ( read , $ "{ HighLight } (?=\\ s|\" )", $ "<mark>$&</mark>") ;
129
+ read = Regex . Replace ( read , $ "{ value } (?=\\ s|\" )", $ "<mark>$&</mark>") ;
139
130
}
140
131
}
141
-
142
- builder . AddMarkupContent ( 0 , read ) ;
132
+ else
133
+ {
134
+ read = Regex . Replace ( read , $ "{ HighLight } (?=\\ s|\" )", $ "<mark>$&</mark>") ;
135
+ }
143
136
}
137
+
138
+ builder . AddMarkupContent ( 0 , read ) ;
144
139
}
145
140
catch ( Exception ex )
146
141
{
147
- Console . WriteLine ( ex . Message ) ;
142
+ Console . WriteLine ( ex . StackTrace ) ;
148
143
}
149
144
} ;
145
+
146
+ public async Task NotifyBrowserViewportChangeAsync ( BrowserViewportEventArgs browserViewportEventArgs )
147
+ {
148
+ if ( browserViewportEventArgs . BrowserWindowSize . Width < 600 )
149
+ {
150
+ IsVerticalAlign = true ;
151
+ }
152
+ else
153
+ {
154
+ IsVerticalAlign = false ;
155
+ }
156
+
157
+ await InvokeAsync ( StateHasChanged ) ;
158
+ }
159
+
160
+ public async ValueTask DisposeAsync ( ) => await BreakpointService . UnsubscribeAsync ( this ) ;
150
161
}
0 commit comments