diff --git a/Constants/UIConstants.cs b/Constants/UIConstants.cs
new file mode 100644
index 0000000..a15ccbe
--- /dev/null
+++ b/Constants/UIConstants.cs
@@ -0,0 +1,28 @@
+namespace AcdiuTools.Constants
+{
+ ///
+ /// 全局 UI 常量定义
+ ///
+ public static class UIConstants
+ {
+ ///
+ /// 资源路径常量
+ ///
+ public static class Paths
+ {
+ ///
+ /// 默认 Bootstrap Icons SVG Sprite 文件路径
+ ///
+ public const string DefaultIconSprite = "/lib/bootstrap-icons-1.13.1/bootstrap-icons.svg";
+ }
+
+ ///
+ /// BS SVG 图标默认需要添加的 CSS 类名
+ ///
+ public static class Classes
+ {
+ public const string DefaultWidth = "w-1r";
+ public const string DefaultHeight = "h-1r";
+ }
+ }
+}
diff --git a/TagHelpers/BsIconTagHelper.cs b/TagHelpers/BsIconTagHelper.cs
index 3a6188e..cfa5a3c 100644
--- a/TagHelpers/BsIconTagHelper.cs
+++ b/TagHelpers/BsIconTagHelper.cs
@@ -1,127 +1,167 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
+using Microsoft.VisualBasic;
using System.Threading.Tasks;
+using AcdiuTools.Constants;
namespace AcdiuTools.TagHelpers
{
///
- /// SVG Sprite 封装标签
- /// 用法:
+ /// SVG Sprite 封装标签。
+ /// 支持自动缩放逻辑:若未指定宽高,则默认添加 w-1r/h-1r 类名以适配响应式根字号。
///
+ ///
+ /// 用法: <bsicon i="heart-fill" w="20" f="red" />
+ /// 或: <bsicon icon="alarm" width="1rem" cn="my-style" />
+ ///
[HtmlTargetElement("bsicon")]
public class BsIconTagHelper : TagHelper
{
- // 路径指向你存放总 SVG 的位置
- ///
- /// 表示用于引用图标符号的 SVG矢量图库文件的相对路径
- ///
- /// 此路径应指向包含所有 图标定义的SVG文件
- /// 如果图标库被移动或升级,则需要更新该值
- const string spritePath = "/lib/bootstrap-icons-1.13.1/bootstrap-icons.svg";
+ // 内部逻辑变量
+ private string _iconName = string.Empty;
+ private string _width = string.Empty;
+ private string _height = string.Empty;
+ private string _fill = "currentColor";
+
+ #region 属性定义 (支持别名)
///
- /// 图标名称 (必填)
+ /// 图标名称 (必填)。对应 SVG 中的 ID。
///
[HtmlAttributeName("i")]
- public required string IconName { get; set; }
+ public string I { get => _iconName; set => _iconName = value; }
///
- /// 宽度 (默认为空,若为空时设置了高度,则使用高度的值,否则为元素 ClassName 添加 w-1r 以使其默认宽度为 1rem)
+ /// 图标名称 (别名)。
+ ///
+ [HtmlAttributeName("icon")]
+ public string Icon { get => _iconName; set => _iconName = value; }
+
+ ///
+ /// 宽度。支持数字或 CSS 单位(如 16, 1rem)。
+ /// 若为空则尝试使用高度值,若均为空则添加默认响应式类名。
///
[HtmlAttributeName("w")]
- public string Width { get; set; } = string.Empty;
+ public string W { get => _width; set => _width = value; }
///
- /// 高度 (默认为空,若为空时设置了宽度,则使用宽度的值,否则为元素 ClassName 添加 h-1r 以使其默认高度为 1rem)
+ /// 宽度 (别名)。
+ ///
+ [HtmlAttributeName("width")]
+ public string Width { get => _width; set => _width = value; }
+
+ ///
+ /// 高度。支持数字或 CSS 单位。
///
[HtmlAttributeName("h")]
- public string Height { get; set; } = string.Empty;
+ public string H { get => _height; set => _height = value; }
///
- /// 填充颜色 (默认 currentColor 即当前元素颜色)
+ /// 高度 (别名)。
+ ///
+ [HtmlAttributeName("height")]
+ public string Height { get => _height; set => _height = value; }
+
+ ///
+ /// 填充颜色。默认为 currentColor。
///
[HtmlAttributeName("f")]
- public string Fill { get; set; } = "currentColor";
+ public string F { get => _fill; set => _fill = value; }
///
- /// 自定义 class 名称,允许用户添加额外的样式类 (默认空)
+ /// 填充颜色 (别名)。
+ ///
+ [HtmlAttributeName("fill")]
+ public string Fill { get => _fill; set => _fill = value; }
+
+ ///
+ /// 自定义 CSS 类名。
+ ///
+ [HtmlAttributeName("c")]
+ public string C { get; set; } = string.Empty;
+
+ ///
+ /// 自定义 CSS 类名 (别名)。
///
[HtmlAttributeName("cn")]
+ public string CN { get; set; } = string.Empty;
+
+ ///
+ /// 自定义 CSS 类名 (别名)。
+ ///
+ [HtmlAttributeName("class")]
public string ClassName { get; set; } = string.Empty;
///
- /// 根据指定的图标名称和属性,处理标签助手以渲染SVG矢量图标
+ /// 自定义 SVG Sprite 路径。
+ /// 不填则使用系统默认常量路径。
+ ///
+ [HtmlAttributeName("path")]
+ public string CustomPath { get; set; } = string.Empty;
+
+ #endregion
+
+ ///
+ /// 处理标签渲染逻辑
///
- /// 若未指定图标名称或为空,则输出被抑制且不生成SVG
- /// 渲染后,该方法会将原始标签替换为<svg>元素并设置标准的SVG属性
- /// 包括类名、宽度、高度、填充颜色和图标ID名称。该SVG引用了 中的图标
- /// 使用<use>元素引用的矢量图形
- /// 用于标签辅助程序执行的上下文,包含当前HTML标签及其属性的相关信息
- /// 标签辅助器的输出,用于修改渲染的HTML元素及其内容
public override void Process(TagHelperContext context, TagHelperOutput output)
{
- if (string.IsNullOrWhiteSpace(IconName))
+ if (string.IsNullOrWhiteSpace(_iconName))
{
- output.SuppressOutput(); // 如果没写图标名,则不渲染
+ output.SuppressOutput();
return;
}
- // 1. 将外层标签替换为