Skip to content

Commit 1efe80f

Browse files
committed
feat: 增加 OnCropEndAsync 回调方法
1 parent 39d9997 commit 1efe80f

2 files changed

Lines changed: 95 additions & 47 deletions

File tree

src/components/BootstrapBlazor.ImageCropper/ImageCropper.razor.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

55
using Microsoft.AspNetCore.Components;
6+
using Microsoft.JSInterop;
67

78
namespace BootstrapBlazor.Components;
89

@@ -29,6 +30,12 @@ public partial class ImageCropper
2930
[Parameter]
3031
public Func<ImageCropperResult, Task>? OnCropAsync { get; set; }
3132

33+
/// <summary>
34+
/// 获得/设置 剪裁调整结束回调方法
35+
/// </summary>
36+
[Parameter]
37+
public Func<Task>? OnCropEndAsync { get; set; }
38+
3239
/// <summary>
3340
/// 获取/设置 裁剪选项
3441
/// </summary>
@@ -81,7 +88,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
8188
/// <inheritdoc/>
8289
/// </summary>
8390
/// <returns></returns>
84-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Options ?? new());
91+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
92+
{
93+
Options = Options ?? new(),
94+
TriggerOnCropEndAsync = OnCropEndAsync != null ? nameof(TriggerOnCropEndAsync) : null,
95+
});
8596

8697
/// <summary>
8798
/// 剪裁方法 自动触发 <see cref="OnCropAsync"/> 回调方法
@@ -153,4 +164,17 @@ public Task Disable()
153164
/// <param name="angle">旋转角度</param>
154165
/// <returns></returns>
155166
public async Task Rotate(int angle) => await InvokeVoidAsync("rotate", Id, angle);
167+
168+
/// <summary>
169+
///
170+
/// </summary>
171+
/// <returns></returns>
172+
[JSInvokable]
173+
public async Task TriggerOnCropEndAsync()
174+
{
175+
if(OnCropEndAsync != null)
176+
{
177+
await Task.CompletedTask;
178+
}
179+
}
156180
}

src/components/BootstrapBlazor.ImageCropper/ImageCropper.razor.js

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Data from '../BootstrapBlazor/modules/data.js'
33
import { addLink } from '../BootstrapBlazor/modules/utility.js'
44

5-
export async function init(id, options) {
5+
export async function init(id, invoke, options) {
66
await addLink("./_content/BootstrapBlazor.ImageCropper/cropper.bundle.css");
77

88
const el = document.getElementById(id);
@@ -11,9 +11,9 @@ export async function init(id, options) {
1111
}
1212

1313
const image = el.querySelector(".bb-cropper-image");
14-
const cropper = new Cropper(image, getOptions(options));
14+
const cropper = new Cropper(image, getOptions(options.options));
1515

16-
Data.set(id, cropper);
16+
Data.set(id, { el, invoke, options, cropper });
1717
}
1818

1919
const getOptions = op => {
@@ -27,86 +27,110 @@ const getOptions = op => {
2727
}
2828

2929
export function dispose(id) {
30-
const cropper = Data.get(id);
30+
const ic = Data.get(id);
3131
Data.remove(id);
3232

33-
if (cropper != null) {
34-
cropper.destroy();
33+
if (ic != null) {
34+
const { cropper } = ic;
35+
if (cropper) {
36+
cropper.destroy();
37+
}
3538
}
3639
}
3740

3841
export function crop(id) {
3942
let ret = null;
40-
const cropper = Data.get(id);
41-
if (cropper != null) {
42-
const { isRound } = cropper.options;
43-
cropper.crop();
44-
let resultData = cropper.getCroppedCanvas();
45-
if (isRound) {
46-
resultData = getRoundCanvas(resultData);
43+
const ic = Data.get(id);
44+
if (ic != null) {
45+
const { cropper, options } = ic;
46+
if (cropper !== null) {
47+
cropper.crop();
48+
let resultData = cropper.getCroppedCanvas();
49+
50+
const { isRound } = options.options;
51+
if (isRound) {
52+
resultData = getRoundCanvas(resultData);
53+
}
54+
ret = resultData.toDataURL();
55+
resultData = null;
4756
}
48-
ret = resultData.toDataURL();
49-
resultData = null;
5057
}
5158
return ret;
5259
}
5360

5461
export function replace(id, url) {
55-
const cropper = Data.get(id);
56-
if (cropper != null) {
57-
cropper.replace(url);
62+
const ic = Data.get(id);
63+
if (ic != null) {
64+
const { cropper } = ic;
65+
if (cropper) {
66+
cropper.replace(url);
67+
}
5868
}
5969
}
6070

6171
export function reset(id) {
62-
const cropper = Data.get(id);
63-
if (cropper != null) {
64-
cropper.reset();
72+
const ic = Data.get(id);
73+
if (ic != null) {
74+
const { cropper } = ic;
75+
if (cropper) {
76+
cropper.reset();
77+
}
6578
}
6679
}
6780

6881
export function setDragMode(id, mode) {
69-
const cropper = Data.get(id);
70-
if (cropper != null) {
71-
cropper.setDragMode(mode);
82+
const ic = Data.get(id);
83+
if (ic != null) {
84+
const { cropper } = ic;
85+
if (cropper) {
86+
cropper.setDragMode(mode);
87+
}
7288
}
7389
}
7490

7591
export function rotate(id, angle) {
76-
const cropper = Data.get(id);
77-
if (cropper != null) {
78-
cropper.rotate(angle);
92+
const ic = Data.get(id);
93+
if (ic != null) {
94+
const { cropper } = ic;
95+
if (cropper) {
96+
cropper.rotate(angle);
97+
}
7998
}
8099
}
81100

82101
export function clear(id) {
83-
const cropper = Data.get(id);
84-
if (cropper != null) {
85-
cropper.clear();
102+
const ic = Data.get(id);
103+
if (ic != null) {
104+
const { cropper } = ic;
105+
if (cropper) {
106+
cropper.clear();
107+
}
86108
}
87109
}
88110

89111
export async function enable(id) {
90-
const cropper = Data.get(id);
91-
if (cropper != null) {
92-
cropper.enable();
93-
}
94-
95-
const el = document.getElementById(id);
96-
if (el) {
97-
el.classList.remove("disabled");
112+
const ic = Data.get(id);
113+
if (ic != null) {
114+
const { el, cropper } = ic;
115+
if (cropper) {
116+
cropper.enable();
117+
}
118+
if (el) {
119+
el.classList.remove("disabled");
120+
}
98121
}
99122
}
100123

101124
export async function disable(id) {
102-
const cropper = Data.get(id);
103-
if (cropper != null) {
104-
cropper.disable();
105-
}
106-
107-
const el = document.getElementById(id);
108-
if (el) {
109-
el.classList.add("disabled");
125+
const ic = Data.get(id);
126+
if (ic != null) {
127+
const { el, cropper } = ic;
128+
if (cropper) {
129+
cropper.disable();
130+
}
131+
if (el) {
132+
el.classList.add("disabled");
133+
}
110134
}
111135
}
112136

0 commit comments

Comments
 (0)