Skip to content

Commit 51be23d

Browse files
authored
doc(ImageCropper): add round image sample code (#4443)
* refactor: 增加 throw 防止 js 导入失败时无提示 * doc: 更新 ImageCropper 示例 * doc: 更新示例 * feat: 增加圆形头像示例 * chroe: bump version 8.1.0 * test: 更新单元测试 * refactor: 重构代码
1 parent a760712 commit 51be23d

File tree

7 files changed

+88
-60
lines changed

7 files changed

+88
-60
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<PackageReference Include="BootstrapBlazor.Holiday" Version="8.0.1" />
4646
<PackageReference Include="BootstrapBlazor.Html2Pdf" Version="8.2.1" />
4747
<PackageReference Include="BootstrapBlazor.IconPark" Version="8.0.4" />
48-
<PackageReference Include="BootstrapBlazor.ImageCropper" Version="0.0.4" />
48+
<PackageReference Include="BootstrapBlazor.ImageCropper" Version="8.1.0" />
4949
<PackageReference Include="BootstrapBlazor.Live2DDisplay" Version="8.0.0" />
5050
<PackageReference Include="BootstrapBlazor.Markdown" Version="8.0.0" />
5151
<PackageReference Include="BootstrapBlazor.MaterialDesign" Version="8.0.3" />

src/BootstrapBlazor.Server/Components/Samples/ImageCroppers.razor

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,35 @@
66
<PackageTips Name="BootstrapBlazor.ImageCropper" />
77

88
<DemoBlock Title="@Localizer["ImageCropperNormalText"]" Introduction="@Localizer["ImageCropperNormalIntro"]" Name="Normal">
9-
<ImageCropper @ref="Cropper" Url="@images[0]" DefaultButton="false" />
9+
<ImageCropper @ref="_cropper" Url="@images[0]"></ImageCropper>
1010
<section ignore>
1111
<BootstrapInputGroup>
12-
<Button Text="OK" OnClick="(async () => Base64 = await Cropper.Crop())" />
13-
<Button Text="@Localizer["ImageCropperResetText"]" OnClick="Cropper.Reset" />
12+
<Button Text="OK" OnClick="Crop" />
13+
<Button Text="@Localizer["ImageCropperResetText"]" OnClick="_cropper.Reset" />
1414
<Button Text="@Localizer["ImageCropperReplaceText"]" OnClick="OnClickReplace" />
15+
<Button Text="@Localizer["ImageCropperRotateText"]" OnClick="Rotate" />
16+
<Button Text="@Localizer["ImageCropperEnableText"]" OnClick="_cropper.Enable" />
17+
<Button Text="@Localizer["ImageCropperDisabledText"]" OnClick="_cropper.Disable" />
18+
<Button Text="@Localizer["ImageCropperClearText"]" OnClick="_cropper.Clear" />
1519
</BootstrapInputGroup>
16-
<Textarea Value="@Base64" rows="3" class="mt-3" />
20+
@if (!string.IsNullOrEmpty(_base64String))
21+
{
22+
<img src="@_base64String" style="width: 240px;" />
23+
<Textarea Value="@_base64String" rows="3" class="mt-3" />
24+
}
25+
</section>
26+
</DemoBlock>
27+
28+
<DemoBlock Title="@Localizer["ImageCropperNormalText"]" Introduction="@Localizer["ImageCropperNormalIntro"]" Name="Normal">
29+
<ImageCropper @ref="_roundCropper" Url="@images[0]" CropperShape="ImageCropperShape.Round" Options="_roundOptions" />
30+
<section ignore>
31+
<BootstrapInputGroup>
32+
<Button Text="OK" OnClick="RoundCrop" />
33+
</BootstrapInputGroup>
34+
@if (!string.IsNullOrEmpty(_base64String2))
35+
{
36+
<img src="@_base64String2" style="width: 240px;" />
37+
}
1738
</section>
1839
</DemoBlock>
1940

src/BootstrapBlazor.Server/Components/Samples/ImageCroppers.razor.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,42 @@
55
namespace BootstrapBlazor.Server.Components.Samples;
66

77
/// <summary>
8-
/// ImageCroppers
8+
/// ImageCropper 组件示例
99
/// </summary>
1010
public partial class ImageCroppers
1111
{
12+
private ImageCropper _cropper = default!;
1213

13-
[NotNull]
14-
ImageCropper? Cropper { get; set; }
14+
private ImageCropper _roundCropper = default!;
1515

16-
private string[] images = ["./images/picture.jpg", "./images/ImageList2.jpeg"];
16+
private readonly string[] images = ["./images/picture.jpg", "./images/ImageList2.jpeg"];
1717

1818
private int index = 0;
1919

20-
private string? Base64 { get; set; }
20+
private string? _base64String;
21+
22+
private string? _base64String2;
23+
24+
private ImageCropperOption _roundOptions = new() { IsRound = true, Radius = "50%" };
2125

2226
private async Task OnClickReplace()
2327
{
2428
index = index == 0 ? 1 : 0;
25-
await Cropper.Replace(images[index]);
29+
await _cropper.Replace(images[index]);
30+
}
31+
32+
private async Task Crop()
33+
{
34+
_base64String = await _cropper.Crop();
2635
}
2736

37+
private async Task RoundCrop()
38+
{
39+
_base64String2 = await _roundCropper.Crop();
40+
}
41+
42+
private Task Rotate() => _cropper.Rotate(90);
43+
2844
/// <summary>
2945
/// GetAttributes
3046
/// </summary>
@@ -41,51 +57,35 @@ protected AttributeItem[] GetAttributes() =>
4157
},
4258
new()
4359
{
44-
Name = "DefaultButton",
45-
Description = Localizer["AttributesImageCropperDefaultButton"],
60+
Name = "IsDisabled",
61+
Description = Localizer["AttributesImageCropperIsDisabled"],
4662
Type = "bool",
47-
ValueList = "-",
48-
DefaultValue = "true"
63+
ValueList = "true|false",
64+
DefaultValue = "false"
4965
},
5066
new()
5167
{
52-
Name = "Preview",
53-
Description = Localizer["AttributesImageCropperPreview"],
54-
Type = "bool",
55-
ValueList = "-",
56-
DefaultValue = "true"
57-
},
58-
new()
59-
{
60-
Name = "OnResult()",
61-
Description = Localizer["AttributesImageCropperOnResult"],
62-
Type = "Func",
63-
ValueList = "-",
64-
DefaultValue = "-"
65-
},
66-
new()
67-
{
68-
Name = "OnBase64Result()",
69-
Description = Localizer["AttributesImageCropperOnBase64Result"],
70-
Type = "Func",
68+
Name = "OnCropAsync",
69+
Description = Localizer["AttributesImageCropperOnCropAsync"],
70+
Type = "Func<ImageCropperResult, Task>",
7171
ValueList = "-",
7272
DefaultValue = "-"
7373
},
7474
new()
7575
{
76-
Name = "Crop()",
77-
Description = Localizer["AttributesImageCropperCrop"],
78-
Type = "Task",
76+
Name = "Options",
77+
Description = Localizer["AttributesImageCropperOptions"],
78+
Type = "ImageCropperOption",
7979
ValueList = "-",
8080
DefaultValue = "-"
8181
},
8282
new()
8383
{
84-
Name = "CropToStream()",
85-
Description = Localizer["AttributesImageCropperCropToStream"],
86-
Type = "Task",
84+
Name = "CropperShape",
85+
Description = Localizer["AttributesImageCropperShape"],
86+
Type = "ImageCropperShape",
8787
ValueList = "-",
8888
DefaultValue = "-"
89-
},
89+
}
9090
];
9191
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6499,13 +6499,15 @@
64996499
"ImageCropperNormalIntro": "Url parameter setting image",
65006500
"ImageCropperResetText": "Reset",
65016501
"ImageCropperReplaceText": "Replace",
6502-
"AttributesImageCropperUrl": "Image URL or Base64 dataurl",
6503-
"AttributesImageCropperDefaultButton": "Show default button",
6504-
"AttributesImageCropperPreview": "Show preview after cropping",
6505-
"AttributesImageCropperOnResult": "Crop result Stream callback method",
6506-
"AttributesImageCropperOnBase64Result": "Crop result base64 callback method",
6507-
"AttributesImageCropperCrop": "Crop, return base64, and execute OnResult/OnBase64Result callback",
6508-
"AttributesImageCropperCropToStream": "Crop, return Stream"
6502+
"ImageCropperRotateText": "Rotate",
6503+
"ImageCropperEnableText": "Enable",
6504+
"ImageCropperDisabledText": "Disabled",
6505+
"ImageCropperClearText": "Clear",
6506+
"AttributesImageCropperUrl": "Image Url",
6507+
"AttributesImageCropperIsDisabled": "Whether the component is disabled",
6508+
"AttributesImageCropperOnCropAsync": "Crop result callback method",
6509+
"AttributesImageCropperOptions": "Crop option",
6510+
"AttributesImageCropperShape": "Crop shape"
65096511
},
65106512
"BootstrapBlazor.Server.Components.Samples.Translators": {
65116513
"TranslatorsTitle": "Azure Translator",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6499,13 +6499,15 @@
64996499
"ImageCropperNormalIntro": "Url 参数设置图片",
65006500
"ImageCropperResetText": "复位",
65016501
"ImageCropperReplaceText": "替换",
6502-
"AttributesImageCropperUrl": "图片URL 或 Base64 dataurl",
6503-
"AttributesImageCropperDefaultButton": "显示默认按钮",
6504-
"AttributesImageCropperPreview": "显示剪裁后预览",
6505-
"AttributesImageCropperOnResult": "剪裁结果 Stream 回调方法",
6506-
"AttributesImageCropperOnBase64Result": "剪裁结果 base64 回调方法",
6507-
"AttributesImageCropperCrop": "剪裁,返回 base64, 并执行 OnResult/OnBase64Result 回调",
6508-
"AttributesImageCropperCropToStream": "剪裁,返回 Stream"
6502+
"ImageCropperRotateText": "旋转",
6503+
"ImageCropperEnableText": "启用",
6504+
"ImageCropperDisabledText": "禁用",
6505+
"ImageCropperClearText": "清除",
6506+
"AttributesImageCropperUrl": "图片地址",
6507+
"AttributesImageCropperIsDisabled": "是否被禁用",
6508+
"AttributesImageCropperOnCropAsync": "剪裁结果回调方法",
6509+
"AttributesImageCropperOptions": "裁剪选项",
6510+
"AttributesImageCropperShape": "裁剪形状"
65096511
},
65106512
"BootstrapBlazor.Server.Components.Samples.Translators": {
65116513
"TranslatorsTitle": "AzureTranslator 翻译服务",

src/BootstrapBlazor/Extensions/JSModuleExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ public static class JSModuleExtensions
2525
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
2626
public static async Task<JSModule> LoadModule(this IJSRuntime jsRuntime, string fileName, string? version = null)
2727
{
28-
JSModule? module = null;
2928
if (!string.IsNullOrEmpty(version))
3029
{
3130
fileName = $"{fileName}?v={version}";
3231
}
32+
33+
JSModule? module;
3334
try
3435
{
3536
var jSObjectReference = await jsRuntime.InvokeAsync<IJSObjectReference>(identifier: "import", fileName);
3637
module = new JSModule(jSObjectReference);
3738
}
38-
catch (Exception) { }
39-
return module ?? new JSModule(null);
39+
catch (Exception)
40+
{
41+
throw;
42+
}
43+
return module;
4044
}
4145

4246
/// <summary>

test/UnitTest/Extensions/JSModuleExtensionsTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public async Task LoadModule_Ok()
1919
public async Task LoadModule_Exception()
2020
{
2121
var jsRuntime = new MockJSRuntime();
22-
var module = await jsRuntime.LoadModule("./mock.js", "test");
23-
Assert.NotNull(module);
22+
await Assert.ThrowsAsync<TaskCanceledException>(() => jsRuntime.LoadModule("./mock.js", "test"));
2423
}
2524

2625
[Fact]

0 commit comments

Comments
 (0)