2020-05-14 16:26:21 +08:00
|
|
|
|
using System;
|
2020-08-14 07:13:28 +08:00
|
|
|
|
using System.Collections.Generic;
|
2022-06-18 15:11:55 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-18 15:11:55 +08:00
|
|
|
|
using System.Xml.Linq;
|
2020-06-10 21:44:21 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
|
2020-12-08 10:01:43 +08:00
|
|
|
|
#pragma warning disable CA1822
|
|
|
|
|
#pragma warning disable CA1054
|
|
|
|
|
#pragma warning disable CA1062
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
|
|
|
|
public class IconService
|
|
|
|
|
{
|
2020-06-10 21:44:21 +08:00
|
|
|
|
private IJSRuntime _js;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
|
2020-12-08 10:01:43 +08:00
|
|
|
|
public IconService(IJSRuntime js)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-06-10 21:44:21 +08:00
|
|
|
|
_js = js;
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 15:11:55 +08:00
|
|
|
|
internal static string GetIconImg(string type, string theme)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2020-12-08 10:01:43 +08:00
|
|
|
|
var iconImg = IconStore.GetIcon(type, theme);
|
2020-08-04 15:10:29 +08:00
|
|
|
|
|
2020-12-08 10:01:43 +08:00
|
|
|
|
if (string.IsNullOrEmpty(iconImg))
|
2020-08-04 15:10:29 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2020-05-14 16:26:21 +08:00
|
|
|
|
return iconImg;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
internal static string GetStyledSvg(Icon icon)
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2023-01-13 23:27:35 +08:00
|
|
|
|
string svgClass = icon.Spin || icon.Type == "loading" ? "anticon-spin" : null;
|
2022-09-04 10:51:32 +08:00
|
|
|
|
|
|
|
|
|
var svg = !string.IsNullOrEmpty(icon.IconFont) ?
|
|
|
|
|
$"<svg><use xlink:href=#{icon.IconFont} /></svg>"
|
|
|
|
|
: GetIconImg(icon.Type.ToLowerInvariant(), icon.Theme.ToLowerInvariant());
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(svg))
|
2020-05-14 16:26:21 +08:00
|
|
|
|
{
|
2022-09-04 10:51:32 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string svgStyle = $"focusable=\"false\" width=\"{icon.Width}\" height=\"{icon.Height}\" fill=\"{icon.Fill}\" {(icon.Rotate == 0 ? $"style=\"pointer-events: none;\"" : $"style=\"pointer-events: none;transform: rotate({icon.Rotate}deg);\"")}";
|
|
|
|
|
if (!string.IsNullOrEmpty(svgClass))
|
|
|
|
|
{
|
|
|
|
|
svgStyle += $" class=\"{svgClass}\"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var iconSvg = svg.Insert(svg.IndexOf("<svg", StringComparison.Ordinal) + 4, $" {svgStyle} ");
|
2020-11-12 15:39:58 +08:00
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
if (icon.Theme == IconThemeType.Twotone)
|
|
|
|
|
{
|
|
|
|
|
return GetTwoToneIconSvg(iconSvg, icon.TwotoneColor, icon.SecondaryColor);
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
return iconSvg;
|
2020-06-10 21:44:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-09-04 14:28:03 +08:00
|
|
|
|
public static IDictionary<string, string[]> GetAllIcons()
|
|
|
|
|
=> IconStore.AllIconsByTheme.Value;
|
2020-08-16 22:51:24 +08:00
|
|
|
|
|
2022-09-04 14:28:03 +08:00
|
|
|
|
public static bool IconExists(string iconTheme = "", string iconName = "")
|
2020-08-16 22:51:24 +08:00
|
|
|
|
{
|
2022-09-04 14:28:03 +08:00
|
|
|
|
var icon = IconStore.GetIcon(iconName, iconTheme);
|
2020-08-14 07:13:28 +08:00
|
|
|
|
|
2020-12-08 10:01:43 +08:00
|
|
|
|
return !string.IsNullOrEmpty(icon);
|
2020-08-14 07:13:28 +08:00
|
|
|
|
}
|
2022-06-18 15:11:55 +08:00
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
public async Task<string> GetSecondaryColor(string primaryColor)
|
|
|
|
|
{
|
|
|
|
|
var secondaryColors = await _js.InvokeAsync<string[]>(JSInteropConstants.GenerateColor, primaryColor);
|
|
|
|
|
return secondaryColors[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetTwoToneIconSvg(string iconSvg, string primaryColor, string secondaryColor)
|
2022-06-18 15:11:55 +08:00
|
|
|
|
{
|
2022-09-04 10:51:32 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(secondaryColor))
|
|
|
|
|
{
|
|
|
|
|
return iconSvg.Replace("fill=\"#333\"", $"fill=\"{primaryColor}\"");
|
|
|
|
|
}
|
2022-06-18 15:11:55 +08:00
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
iconSvg = iconSvg.Replace("fill=\"#333\"", "fill=\"primaryColor\"")
|
|
|
|
|
.Replace("fill=\"#E6E6E6\"", "fill=\"secondaryColor\"")
|
|
|
|
|
.Replace("fill=\"#D9D9D9\"", "fill=\"secondaryColor\"")
|
|
|
|
|
.Replace("fill=\"#D8D8D8\"", "fill=\"secondaryColor\"");
|
2022-06-18 15:11:55 +08:00
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
var document = XDocument.Load(new StringReader(iconSvg));
|
|
|
|
|
var svgRoot = document.Root;
|
|
|
|
|
foreach (var path in svgRoot.Nodes().OfType<XElement>())
|
2022-06-18 15:11:55 +08:00
|
|
|
|
{
|
|
|
|
|
if (path.Attribute("fill")?.Value == "secondaryColor")
|
|
|
|
|
{
|
2022-09-04 10:51:32 +08:00
|
|
|
|
path.SetAttributeValue("fill", secondaryColor);
|
2022-06-18 15:11:55 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-09-04 10:51:32 +08:00
|
|
|
|
path.SetAttributeValue("fill", primaryColor);
|
2022-06-18 15:11:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 10:51:32 +08:00
|
|
|
|
return svgRoot.ToString();
|
2022-06-18 15:11:55 +08:00
|
|
|
|
}
|
2020-05-14 16:26:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|