2020-05-14 16:26:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2020-08-14 07:13:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-08-14 07:13:28 +08:00
|
|
|
|
using AntDesign.core.Extensions;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-06-10 21:44:21 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
|
|
|
|
public class IconService
|
|
|
|
|
{
|
2020-08-04 15:10:29 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<string, ValueTask<string>> _svgCache = new ConcurrentDictionary<string, ValueTask<string>>();
|
2020-08-14 07:13:28 +08:00
|
|
|
|
private static IDictionary<string, string[]> _iconfiles;
|
2020-08-31 15:59:47 +08:00
|
|
|
|
private readonly static HttpClient _httpClient = new HttpClient();
|
2020-06-10 21:44:21 +08:00
|
|
|
|
private IJSRuntime _js;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
|
2020-06-16 16:25:22 +08:00
|
|
|
|
private Uri _baseAddress;
|
|
|
|
|
|
2020-08-31 15:59:47 +08:00
|
|
|
|
public IconService(NavigationManager navigationManager, IJSRuntime js)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-06-16 16:25:22 +08:00
|
|
|
|
if (navigationManager != null)
|
|
|
|
|
_baseAddress = new Uri(navigationManager.BaseUri);
|
2020-06-10 21:44:21 +08:00
|
|
|
|
|
|
|
|
|
_js = js;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask<string> GetIconImg(string type, string theme)
|
|
|
|
|
{
|
2020-08-14 07:13:28 +08:00
|
|
|
|
if (!await IconExists(theme, type))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 15:10:29 +08:00
|
|
|
|
var cacheKey = $"{theme}/{type}";
|
|
|
|
|
var iconImg = await _svgCache.GetOrAdd(cacheKey, async key =>
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-08-04 15:10:29 +08:00
|
|
|
|
var res = await _httpClient.GetAsync(new Uri(_baseAddress, $"_content/AntDesign/icons/{key}.svg"));
|
2020-05-14 16:26:21 +08:00
|
|
|
|
if (res.IsSuccessStatusCode)
|
|
|
|
|
{
|
2020-08-04 15:10:29 +08:00
|
|
|
|
return await res.Content.ReadAsStringAsync();
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
2020-08-04 15:10:29 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-14 16:26:21 +08:00
|
|
|
|
return iconImg;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 15:10:29 +08:00
|
|
|
|
public static string GetStyledSvg(string svgImg, string width = "1em", string height = "1em", string fill = "currentColor", int rotate = 0)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(svgImg))
|
|
|
|
|
{
|
2020-08-04 15:10:29 +08:00
|
|
|
|
var svgStyle = $"focusable=\"false\" width=\"{width}\" height=\"{height}\" fill=\"{fill}\" {(rotate == 0 ? "" : $"style=\"transform: rotate({rotate}deg);\"")}";
|
2020-08-14 07:13:28 +08:00
|
|
|
|
return svgImg.Insert(svgImg.IndexOf("<svg", StringComparison.Ordinal) + 4, $" {svgStyle} ");
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 21:44:21 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask CreateFromIconfontCN(string scriptUrl)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(scriptUrl))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _js.InvokeVoidAsync(JSInteropConstants.CreateIconFromfontCN, scriptUrl);
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
2020-08-14 07:13:28 +08:00
|
|
|
|
|
2020-08-16 22:51:24 +08:00
|
|
|
|
public async Task<IDictionary<string, string[]>> GetAllIcons()
|
2020-08-14 07:13:28 +08:00
|
|
|
|
{
|
|
|
|
|
_iconfiles ??= await _httpClient.GetFromJsonAsync<IDictionary<string, string[]>>(new Uri(_baseAddress, $"_content/AntDesign/icons/icons.json").ToString());
|
2020-08-16 22:51:24 +08:00
|
|
|
|
return _iconfiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask<bool> IconExists(string theme, string type)
|
|
|
|
|
{
|
|
|
|
|
_iconfiles ??= await GetAllIcons();
|
2020-08-14 07:13:28 +08:00
|
|
|
|
|
|
|
|
|
if (!_iconfiles.TryGetValue(theme, out var files))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return type.IsIn(files);
|
|
|
|
|
}
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|