-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconify.razor.cs
155 lines (122 loc) · 4.29 KB
/
Iconify.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Text;
using System.Text.Encodings.Web;
using System.Xml;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using SysAdminsMedia.BlazorIconify.Extensions;
namespace SysAdminsMedia.BlazorIconify;
public class Iconify : ComponentBase
{
private string _svg = string.Empty;
private bool _initialized;
[Inject] public HttpClient HttpClient { get; set; } = null!;
[Inject] public ILocalStorageService LocalStorage { get; set; } = null!;
[Inject] public Registry Registry { get; set; } = null!;
[Parameter(CaptureUnmatchedValues = true)]
public Dictionary<string, object> Attributes { get; set; } = null!;
[Parameter] public string Icon { get; set; } = string.Empty;
[Parameter] public string? Color { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
_initialized = true;
if (!firstRender) return;
if (string.IsNullOrEmpty(Icon))
{
// Fallback to error icon if no icon is provided
Icon = Registry.GetErrorIcon();
return;
}
if (string.IsNullOrEmpty(Color))
{
Color = Registry.GetDefaultColor();
}
// Only fetch the icon if it has changed
if (await Registry.IsCached(Icon, Color))
{
var metadata = await Registry.GetIcon(Icon, Color);
if (metadata is null) return;
var svg = TryParseToXml(metadata.Content);
if (svg is null) return;
UpdateSvg(svg);
}
else
{
string iconUrl = $"{Registry.GetApiUrl()}{Icon.Replace(':', '/')}.svg";
if (!string.IsNullOrEmpty(Color))
{
iconUrl += $"?color={UrlEncoder.Default.Encode(Color)}";
}
_svg = await FetchIconAsync(iconUrl);
if (string.IsNullOrEmpty(_svg))
{
Console.WriteLine($"Failed to fetch icon {(!string.IsNullOrEmpty(Icon) ? Icon : "\"null\"")}");
return;
}
var svg = TryParseToXml(_svg);
if (svg is null) return;
UpdateSvg(svg);
await Registry.AddIcon(new IconMetaData
{
Name = Icon,
Content = _svg,
Color = Color,
TimeFetched = DateTime.Now
});
}
if (string.IsNullOrEmpty(_svg))
Console.WriteLine($"Failed to fetch icon {this}");
StateHasChanged();
Console.WriteLine("RENDER ICONIFY");
}
protected override async Task OnParametersSetAsync()
{
if (!_initialized) return;
await OnAfterRenderAsync(true);
}
private async Task<string> FetchIconAsync(string url)
{
if (string.IsNullOrEmpty(Icon)) return string.Empty;
var response = await HttpClient.GetByteArrayAsync(url);
var iconContents = Encoding.UTF8.GetString(response);
if (iconContents is not "404" && response is not ({ Length: 0 } or null))
return iconContents;
iconContents = string.Empty;
return iconContents;
}
private static XmlDocument? TryParseToXml(string content)
{
try
{
var document = new XmlDocument();
document.LoadXml(content);
return document;
}
catch (XmlException ex)
{
Console.WriteLine("Failed to parse xml from svg file.");
}
return null;
}
private void UpdateSvg(XmlDocument document)
{
var rootElement = document.DocumentElement;
switch (rootElement)
{
case null:
Console.WriteLine("No root element.");
return;
case not { Name: "svg" } or null:
Console.WriteLine("Failed to find svg element.");
return;
}
rootElement.SetAttribute("class", $"{Attributes.Get("i-class")} icon");
rootElement.SetAttribute("style", Attributes.Get("i-style"));
rootElement.RemoveAttribute("width");
rootElement.RemoveAttribute("height");
foreach (XmlElement child in rootElement.ChildNodes)
{
if (child.Name.ToLower() is not "path") continue;
}
_svg = rootElement.OuterXml;
}
}