|
1 | | -// Licensed to the .NET Foundation under one or more agreements. |
2 | | -// The .NET Foundation licenses this file to you under the Apache 2.0 License |
3 | | -// See the LICENSE file in the project root for more information. |
4 | | -// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone |
| 1 | +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Website: https://www.blazor.zone or https://argozhang.github.io/ |
5 | 4 |
|
6 | 5 | using BootstrapBlazor.Components; |
7 | | -using Microsoft.Extensions.DependencyInjection; |
8 | 6 | using System.Text.RegularExpressions; |
| 7 | +using static System.Runtime.InteropServices.JavaScript.JSType; |
9 | 8 |
|
10 | 9 | namespace UniTestIconPark; |
11 | 10 |
|
@@ -206,7 +205,6 @@ public void ElementIcon_Ok() |
206 | 205 | [GeneratedRegex("svg\">(.*)</svg>")] |
207 | 206 | private static partial Regex SvgRegex(); |
208 | 207 |
|
209 | | - |
210 | 208 | [Fact] |
211 | 209 | public void OctIcon_Ok() |
212 | 210 | { |
@@ -275,4 +273,91 @@ public void OctIcon_Ok() |
275 | 273 | writer.WriteLine("</svg>"); |
276 | 274 | writer.Close(); |
277 | 275 | } |
| 276 | + |
| 277 | + [Fact] |
| 278 | + public void UniverIcon_Ok() |
| 279 | + { |
| 280 | + var services = new ServiceCollection(); |
| 281 | + services.AddBootstrapBlazor(); |
| 282 | + var provider = services.BuildServiceProvider(); |
| 283 | + var zipService = provider.GetRequiredService<IZipArchiveService>(); |
| 284 | + |
| 285 | + var root = AppContext.BaseDirectory; |
| 286 | + var downloadFile = Path.Combine(root, "Univer", "univer.zip"); |
| 287 | + Assert.True(File.Exists(downloadFile)); |
| 288 | + |
| 289 | + var downloadFolder = Path.Combine(root, "univer-icons"); |
| 290 | + if (Directory.Exists(downloadFolder)) |
| 291 | + { |
| 292 | + Directory.Delete(downloadFolder, true); |
| 293 | + } |
| 294 | + zipService.ExtractToDirectory(downloadFile, downloadFolder, true); |
| 295 | + |
| 296 | + var folder = new DirectoryInfo(downloadFolder); |
| 297 | + |
| 298 | + // 处理 List 文件 |
| 299 | + var iconListFile = Path.Combine(root, $"../../../Univer/UniverIconList.razor"); |
| 300 | + if (File.Exists(iconListFile)) |
| 301 | + { |
| 302 | + File.Delete(iconListFile); |
| 303 | + } |
| 304 | + |
| 305 | + // 处理 svg 文件 |
| 306 | + var svgFile = Path.Combine(root, $"../../../Univer/univer.svg"); |
| 307 | + if (File.Exists(svgFile)) |
| 308 | + { |
| 309 | + File.Delete(svgFile); |
| 310 | + } |
| 311 | + using var listWriter = new StreamWriter(File.OpenWrite(iconListFile)); |
| 312 | + using var writer = new StreamWriter(File.OpenWrite(svgFile)); |
| 313 | + writer.WriteLine("<svg xmlns=\"http://www.w3.org/2000/svg\">"); |
| 314 | + foreach (var icon in folder.EnumerateFiles("*.svg", SearchOption.AllDirectories)) |
| 315 | + { |
| 316 | + var id = Path.GetFileNameWithoutExtension(icon.Name); |
| 317 | + using var reader = new StreamReader(icon.OpenRead()); |
| 318 | + var data = reader.ReadToEnd(); |
| 319 | + reader.Close(); |
| 320 | + |
| 321 | + // find viewBox |
| 322 | + var viewBox = FindViewBox(data); |
| 323 | + |
| 324 | + // find <svg |
| 325 | + var index = data.IndexOf("<svg "); |
| 326 | + if (index > -1) |
| 327 | + { |
| 328 | + data = data[index..]; |
| 329 | + } |
| 330 | + index = data.IndexOf('>'); |
| 331 | + if (index > -1) |
| 332 | + { |
| 333 | + data = data[(index + 1)..]; |
| 334 | + } |
| 335 | + |
| 336 | + var target = data.Replace("</svg>", "").Trim(); |
| 337 | + target = target.Replace("fill=\"black\"", "fill=\"currentColor\""); |
| 338 | + target = target.Replace("stroke=\"black\"", "stroke=\"currentColor\""); |
| 339 | + target = $" <symbol viewBox=\"{viewBox}\" id=\"{id}\">{target}</symbol>"; |
| 340 | + writer.WriteLine(target); |
| 341 | + |
| 342 | + listWriter.WriteLine($"<UniverIcon Name=\"{id}\"></UniverIcon>"); |
| 343 | + } |
| 344 | + writer.WriteLine("</svg>"); |
| 345 | + writer.Close(); |
| 346 | + } |
| 347 | + |
| 348 | + private static string FindViewBox(string svg) |
| 349 | + { |
| 350 | + var index = svg.IndexOf(" viewBox=\""); |
| 351 | + if (index > -1) |
| 352 | + { |
| 353 | + index += 10; |
| 354 | + svg = svg[index..]; |
| 355 | + } |
| 356 | + index = svg.IndexOf('\"'); |
| 357 | + if (index > -1) |
| 358 | + { |
| 359 | + svg = svg[..index]; |
| 360 | + } |
| 361 | + return svg; |
| 362 | + } |
278 | 363 | } |
0 commit comments