Files
Media.RCL/MediaJsInterop.cs

61 lines
1.7 KiB
C#

using Microsoft.JSInterop;
using System.Threading.Tasks;
namespace Generic.Media;
public class MediaJsInterop : IAsyncDisposable
{
private readonly IJSRuntime _jsRuntime;
private string? _imgId;
public MediaJsInterop(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
public async Task InitCropperAsync<T>(string imgId, double aspectRatio, DotNetObjectReference<T> dotNetObject) where T : class
{
_imgId = imgId;
await _jsRuntime.InvokeVoidAsync("initCropper", imgId, new { aspectRatio = aspectRatio, viewMode = 1, autoCropArea = 1 }, dotNetObject);
}
public async Task<string> GetCroppedImageAsync()
{
if (_imgId == null) return string.Empty;
return await _jsRuntime.InvokeAsync<string>("getCroppedImage", _imgId);
}
public async Task SetAspectRatioAsync(double ratio)
{
if (_imgId == null) return;
await _jsRuntime.InvokeVoidAsync("setCropperAspectRatio", _imgId, ratio);
}
public async Task DestroyCropperAsync()
{
if (_imgId == null) return;
await _jsRuntime.InvokeVoidAsync("destroyCropper", _imgId);
}
public async Task SetFullWidthAsync()
{
if (_imgId == null) return;
await _jsRuntime.InvokeVoidAsync("setCropperFullWidth", _imgId);
}
public async Task SetFullHeightAsync()
{
if (_imgId == null) return;
await _jsRuntime.InvokeVoidAsync("setCropperFullHeight", _imgId);
}
public async ValueTask DisposeAsync()
{
// Try to clean up if we have an ID
if (_imgId != null)
{
try { await DestroyCropperAsync(); } catch { }
}
}
}