Skip to content

Commit 5b8369f

Browse files
authored
fix UI bugs (#200)
## Target <!-- Why are you making this change? --> #### Open Questions <!-- OPTIONAL - [ ] Use the GitHub checklists to spark discussion on issues that may arise from your approach. Please tick the box and explain your answer. --> ## Checklist <!-- It serves as a gentle reminder for common tasks. Confirm it's done and check everything that applies. --> - [ ] Documentation updated - [ ] Tests cover new or modified code - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] New dependencies added - [ ] Includes breaking changes - [ ] Version bumped ## Visuals <!-- OPTIONAL Show results both before and after this change. When the output changes, it can be a screenshot of a trace, metric, or log illustrating the change. -->
1 parent 1d308b1 commit 5b8369f

File tree

8 files changed

+72
-53
lines changed

8 files changed

+72
-53
lines changed

src/Cropper.Blazor/Client/Components/AspectRatioSettings.razor.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.ComponentModel.DataAnnotations;
2-
using Cropper.Blazor.Client.Pages;
32
using Cropper.Blazor.Components;
43
using Cropper.Blazor.Models;
54
using Microsoft.AspNetCore.Components;
@@ -18,7 +17,7 @@ public decimal? MaxAspectRatio
1817
set
1918
{
2019
maxAspectRatio = value;
21-
ApplyAspectRatioRulesForCropperAsync();
20+
InvokeAsync(ApplyAspectRatioRulesForCropperAsync);
2221
}
2322
}
2423

@@ -28,7 +27,7 @@ public decimal? MinAspectRatio
2827
set
2928
{
3029
minAspectRatio = value;
31-
ApplyAspectRatioRulesForCropperAsync();
30+
InvokeAsync(ApplyAspectRatioRulesForCropperAsync);
3231
}
3332
}
3433

@@ -68,12 +67,16 @@ public async Task ApplyAspectRatioRulesForCropperAsync()
6867
if (aspectRatio < minAspectRatio || aspectRatio > maxAspectRatio)
6968
{
7069
decimal? newCropBoxWidth = cropBoxData.Height * ((minAspectRatio + maxAspectRatio) / 2);
70+
decimal? left = (containerData.Width - newCropBoxWidth) / 2;
7171

7272
CropperComponent!.SetCropBoxData(new SetCropBoxDataOptions
7373
{
74-
Left = (containerData.Width - newCropBoxWidth) / 2,
74+
Left = left,
7575
Width = newCropBoxWidth,
7676
});
77+
78+
cropBoxData = await CropperComponent!.GetCropBoxDataAsync();
79+
aspectRatio = cropBoxData.Width / cropBoxData.Height;
7780
}
7881

7982
SetUpAspectRatio(aspectRatio);

src/Cropper.Blazor/Client/Components/Docs/SectionContent.razor.cs

+45-34
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
using Cropper.Blazor.Client.Models;
33
using Microsoft.AspNetCore.Components;
44
using MudBlazor;
5-
using MudBlazor.Services;
65
using MudBlazor.Utilities;
76

87
namespace Cropper.Blazor.Client.Components.Docs;
98

10-
public partial class SectionContent
9+
public partial class SectionContent : IBrowserViewportObserver
1110
{
1211
[Inject] protected IJsApiService? JsApiService { get; set; }
13-
[Inject] IBreakpointService BreakpointService { get; set; } = null!;
12+
[Inject] IBrowserViewportService BreakpointService { get; set; } = null!;
1413

1514
protected string Classname =>
1615
new CssBuilder("docs-section-content")
@@ -41,28 +40,30 @@ public partial class SectionContent
4140
.AddClass("show-code", HasCode && ShowCode)
4241
.Build();
4342

44-
[Parameter] public string Class { get; set; }
43+
[Parameter] public string Class { get; set; } = string.Empty;
4544
[Parameter] public bool DarkenBackground { get; set; }
4645
[Parameter] public bool Outlined { get; set; } = true;
4746
[Parameter] public bool ShowCode { get; set; } = true;
4847
[Parameter] public bool Block { get; set; }
4948
[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!;
5453

5554
private bool HasCode;
56-
public string ActiveCode;
55+
public string ActiveCode = string.Empty;
5756

5857
private bool IsVerticalAlign = false;
5958

59+
Guid IBrowserViewportObserver.Id { get; } = Guid.NewGuid();
60+
6061
protected override void OnParametersSet()
6162
{
6263
if (Codes != null)
6364
{
6465
HasCode = true;
65-
ActiveCode = Codes.FirstOrDefault().code;
66+
ActiveCode = Codes.First().code;
6667
}
6768
else if (!string.IsNullOrWhiteSpace(Code))
6869
{
@@ -75,11 +76,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
7576
{
7677
if (firstRender)
7778
{
78-
await BreakpointService!.SubscribeAsync((br) =>
79-
{
80-
IsVerticalAlign = BreakpointService!.IsMediaSize(br, Breakpoint.Xs);
81-
InvokeAsync(StateHasChanged);
82-
});
79+
await BreakpointService!.SubscribeAsync(this, fireImmediately: true);
8380
}
8481

8582
await base.OnAfterRenderAsync(firstRender);
@@ -116,35 +113,49 @@ RenderFragment CodeComponent(string code) => builder =>
116113
{
117114
try
118115
{
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();
124120

125-
if (!string.IsNullOrEmpty(HighLight))
121+
if (!string.IsNullOrEmpty(HighLight))
122+
{
123+
if (HighLight.Contains(','))
126124
{
127-
if (HighLight.Contains(","))
128-
{
129-
var highlights = HighLight.Split(",");
125+
var highlights = HighLight.Split(",");
130126

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)
137128
{
138-
read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"<mark>$&</mark>");
129+
read = Regex.Replace(read, $"{value}(?=\\s|\")", $"<mark>$&</mark>");
139130
}
140131
}
141-
142-
builder.AddMarkupContent(0, read);
132+
else
133+
{
134+
read = Regex.Replace(read, $"{HighLight}(?=\\s|\")", $"<mark>$&</mark>");
135+
}
143136
}
137+
138+
builder.AddMarkupContent(0, read);
144139
}
145140
catch (Exception ex)
146141
{
147-
Console.WriteLine(ex.Message);
142+
Console.WriteLine(ex.StackTrace);
148143
}
149144
};
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);
150161
}

src/Cropper.Blazor/Client/Components/ZoomRatioSettings.razor.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using Cropper.Blazor.Components;
1+
using System.ComponentModel.DataAnnotations;
2+
using Cropper.Blazor.Components;
23
using Cropper.Blazor.Events.ZoomEvent;
34
using Cropper.Blazor.Models;
45
using Microsoft.AspNetCore.Components;
56
using Microsoft.JSInterop;
6-
using System.ComponentModel.DataAnnotations;
77

88
namespace Cropper.Blazor.Client.Components
99
{
@@ -18,7 +18,7 @@ private decimal? MinZoomRatio
1818
set
1919
{
2020
minZoomRatio = value;
21-
ApplyZoomRulesForCropperAsync();
21+
InvokeAsync(ApplyZoomRulesForCropperAsync);
2222
}
2323
}
2424
private decimal? MaxZoomRatio
@@ -27,7 +27,7 @@ private decimal? MaxZoomRatio
2727
set
2828
{
2929
maxZoomRatio = value;
30-
ApplyZoomRulesForCropperAsync();
30+
InvokeAsync(ApplyZoomRulesForCropperAsync);
3131
}
3232
}
3333
[Inject] private IJSRuntime? JSRuntime { get; set; }

src/Cropper.Blazor/Client/Pages/CropperDemo.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@
117117

118118
<MudButtonGroup Color="Color.Primary" Variant="Variant.Filled">
119119
<MudTooltip Text="cropper.scaleX(-1)">
120-
<MudIconButton Icon="@Icons.Material.Filled.SwapHoriz" OnClick="@(()=>ScaleX(-1m*ScaleXValue))" Title="Flip Horizontal" />
120+
<MudIconButton Icon="@Icons.Material.Filled.SwapHoriz" OnClick="@(()=>ScaleX(-1m * ScaleXValue))" Title="Flip Horizontal" />
121121
</MudTooltip>
122122
<MudTooltip Text="cropper.scaleY(-1)">
123-
<MudIconButton Icon="@Icons.Material.Filled.SwapVert" OnClick="@(()=>ScaleY(-1m*ScaleYValue))" Title="Flip Vertical" />
123+
<MudIconButton Icon="@Icons.Material.Filled.SwapVert" OnClick="@(()=>ScaleY(-1m * ScaleYValue))" Title="Flip Vertical" />
124124
</MudTooltip>
125125
</MudButtonGroup>
126126

src/Cropper.Blazor/Client/Pages/CropperDemo.razor.cs

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public async void OnCropEvent(JSEventData<CropEvent> cropJSEvent)
8282
{
8383
if (cropJSEvent?.Detail is not null)
8484
{
85+
ScaleXValue = cropJSEvent.Detail.ScaleX;
86+
ScaleYValue = cropJSEvent.Detail.ScaleY;
87+
8588
decimal width = Math.Round(cropJSEvent.Detail.Width ?? 0);
8689
decimal height = Math.Round(cropJSEvent.Detail.Height ?? 0);
8790

src/Cropper.Blazor/Client/Pages/Index.razor

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</MudItem>
2323

2424
<MudItem xl="12" lg="12" md="12" sm="12" xs="12" Class="d-flex justify-center">
25-
<MudText Typo="Typo.h3" Align="Align.Center" Class="pa-0 ma-0 text-gradient">Cropper.Blazor</MudText>
25+
<MudText Typo="Typo.h3" Style="word-break: break-all;" Align="Align.Center" Class="pa-0 ma-0 text-gradient">Cropper.Blazor</MudText>
2626
</MudItem>
2727

2828

@@ -42,12 +42,12 @@
4242

4343
<MudItem xl="12" lg="12" md="12" sm="12" xs="12" Class="d-flex justify-center gap-4 pb-8">
4444
<MudButton Color="Color.Primary" Variant="Variant.Filled" DisableElevation="true"
45-
Class="button-gradient" Size="Size.Large" Href="/demo">
46-
View Demo
45+
Class="button-gradient" Size="Size.Large" Href="/demo">
46+
<MudText Align="Align.Center">View Demo</MudText>
4747
</MudButton>
4848
<MudButton Color="Color.Default" Variant="Variant.Outlined" StartIcon="@Icons.Custom.Brands.GitHub"
4949
Size="Size.Large" Class="ms-4" Href="https://github.com/CropperBlazor/Cropper.Blazor" Target="_blank">
50-
Star on GitHub
50+
<MudText Align="Align.Center">Star on GitHub</MudText>
5151
</MudButton>
5252
</MudItem>
5353
<p align="center">

src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/CodeSnippetsCompiler.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace Cropper.Blazor.Client.Compiler;
88

9-
public class CodeSnippetsCompiler
9+
public partial class CodeSnippetsCompiler
1010
{
11-
public bool Execute()
11+
public static bool Execute()
1212
{
1313
var paths = new Paths();
1414
var success = true;
@@ -61,7 +61,10 @@ public bool Execute()
6161
private static string EscapeComponentSource(string path)
6262
{
6363
var source = File.ReadAllText(path, Encoding.UTF8);
64-
source = Regex.Replace(source, "@(namespace|layout|page) .+?\n", string.Empty);
64+
source = EscapeComponentSourceRegex().Replace(source, string.Empty);
6565
return source.Replace("\"", "\"\"").Trim();
6666
}
67+
68+
[GeneratedRegex("@(namespace|layout|page) .+?\n")]
69+
private static partial Regex EscapeComponentSourceRegex();
6770
}

src/Cropper.Blazor/Cropper.Blazor.Client.Compiler/Program.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public class Program
88
public static int Main()
99
{
1010
var stopWatch = Stopwatch.StartNew();
11-
var success =
12-
new CodeSnippetsCompiler().Execute();
11+
var success = CodeSnippetsCompiler.Execute();
1312

1413
Console.WriteLine($"Docs.Compiler completed in {stopWatch.ElapsedMilliseconds} msecs");
1514
return success ? 0 : 1;

0 commit comments

Comments
 (0)