2020-05-14 16:26:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2020-06-10 21:44:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
private static readonly ConcurrentDictionary<string, string> _svgCache = new ConcurrentDictionary<string, string>();
|
2020-06-10 21:44:21 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<string, string> _customCache = new ConcurrentDictionary<string, string>();
|
2020-05-14 16:26:21 +08:00
|
|
|
|
private readonly HttpClient _httpClient;
|
2020-06-10 21:44:21 +08:00
|
|
|
|
private IJSRuntime _js;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
|
2020-06-10 21:44:21 +08:00
|
|
|
|
public IconService(HttpClient httpClient, NavigationManager navigationManager, IJSRuntime js)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-06-10 21:44:21 +08:00
|
|
|
|
if (httpClient != null && navigationManager != null)
|
|
|
|
|
httpClient.BaseAddress = new Uri(navigationManager.BaseUri);
|
|
|
|
|
|
2020-05-14 16:26:21 +08:00
|
|
|
|
_httpClient = httpClient;
|
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)
|
|
|
|
|
{
|
|
|
|
|
var iconImg = string.Empty;
|
|
|
|
|
if (_svgCache.TryGetValue($"{theme}-{type}", out var img))
|
|
|
|
|
{
|
|
|
|
|
iconImg = img;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-29 00:33:49 +08:00
|
|
|
|
var res = await _httpClient.GetAsync($"_content/AntDesign/icons/{theme}/{type}.svg");
|
2020-05-14 16:26:21 +08:00
|
|
|
|
if (res.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
iconImg = await res.Content.ReadAsStringAsync();
|
|
|
|
|
_svgCache.TryAdd($"{theme}-{type}", iconImg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return iconImg;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 21:44:21 +08:00
|
|
|
|
public static string GetStyledSvg(string svgImg, string width = "1em", string height = "1em", string fill = "currentColor", int rotate = 0, bool spin = false)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-06-10 21:44:21 +08:00
|
|
|
|
var svgStyle = $"focusable=\"false\" width=\"{width}\" height=\"{height}\" fill=\"{fill}\" style=\"transform: rotate({rotate}deg); \" {(spin ? "class=\"anticon-spin\"" : "")}";
|
2020-05-14 16:26:21 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(svgImg))
|
|
|
|
|
{
|
|
|
|
|
return svgImg.Insert(svgImg.IndexOf("svg", StringComparison.Ordinal) + 3, $" {svgStyle} ");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 21:44:21 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask CreateFromIconfontCN(string scriptUrl)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(scriptUrl))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if (_customCache.ContainsKey(scriptUrl))
|
|
|
|
|
//{
|
|
|
|
|
// return;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
await _js.InvokeVoidAsync(JSInteropConstants.CreateIconFromfontCN, scriptUrl);
|
|
|
|
|
//_customCache[scriptUrl] = scriptUrl;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|