2021-04-15 14:19:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2021-01-20 15:53:32 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign.Core.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class JSRuntimeExtensions
|
|
|
|
|
{
|
|
|
|
|
public static bool IsBrowser(this IJSRuntime jsRuntime) => RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
|
2021-04-15 14:19:26 +08:00
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
public static async Task BlurAsyc(this IJSRuntime jSRuntime, ElementReference target)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await jSRuntime.InvokeVoidAsync(JSInteropConstants.Blur, target);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 14:19:26 +08:00
|
|
|
|
public static async Task FocusAsync(this IJSRuntime jSRuntime, ElementReference target, bool preventScroll = false)
|
|
|
|
|
{
|
2021-05-27 18:13:26 +08:00
|
|
|
|
#if NET6_0_OR_GREATER
|
|
|
|
|
await target.FocusAsync(preventScroll);
|
2021-04-15 14:19:26 +08:00
|
|
|
|
#else
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await jSRuntime.InvokeVoidAsync(JSInteropConstants.Focus, target, preventScroll);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2021-04-18 16:06:06 +08:00
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
public static async Task FocusAsync(this IJSRuntime jSRuntime, ElementReference target, FocusBehavior behavior, bool preventScroll = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await jSRuntime.InvokeVoidAsync(JSInteropConstants.Focus, target, preventScroll, behavior);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-18 16:06:06 +08:00
|
|
|
|
public static ValueTask SetSelectionStartAsync(this IJSRuntime jSRuntime, ElementReference target, int selectionStart) =>
|
|
|
|
|
jSRuntime.InvokeVoidAsync(JSInteropConstants.SetSelectionStart, target, selectionStart);
|
2021-04-27 14:03:39 +08:00
|
|
|
|
|
|
|
|
|
public static ValueTask<bool> IsResizeObserverSupported(this IJSRuntime jSRuntime) => jSRuntime.InvokeAsync<bool>(JSInteropConstants.IsResizeObserverSupported);
|
2021-01-20 15:53:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|