You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7 KiB

  1. import { App, defineComponent } from "vue";
  2. import icon from "./src/Icon.vue";
  3. import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
  4. import iconifyIconOffline from "./src/iconifyIconOffline";
  5. import iconifyIconOnline from "./src/iconifyIconOnline";
  6. /**
  7. * find icon component
  8. * @param icon icon图标
  9. * @returns component
  10. */
  11. export function findIconReg(icon: string) {
  12. // fontawesome4
  13. const fa4Reg = /^fa-/;
  14. // fontawesome5+
  15. const fa5Reg = /^FA-/;
  16. // iconfont
  17. const iFReg = /^IF-/;
  18. // remixicon
  19. const riReg = /^RI-/;
  20. // typeof icon === "function" 属于SVG
  21. if (fa5Reg.test(icon)) {
  22. const text = icon.split(fa5Reg)[1];
  23. return findIcon(
  24. text.slice(0, text.indexOf(" ") == -1 ? text.length : text.indexOf(" ")),
  25. "FA",
  26. text.slice(text.indexOf(" ") + 1, text.length)
  27. );
  28. } else if (fa4Reg.test(icon)) {
  29. return findIcon(icon.split(fa4Reg)[1], "fa");
  30. } else if (iFReg.test(icon)) {
  31. return findIcon(icon.split(iFReg)[1], "IF");
  32. } else if (typeof icon === "function") {
  33. return findIcon(icon, "SVG");
  34. } else if (riReg.test(icon)) {
  35. return findIcon(icon.split(riReg)[1], "RI");
  36. } else {
  37. return findIcon(icon, "EL");
  38. }
  39. }
  40. // 支持fontawesome、iconfont、remixicon、element-plus/icons、自定义svg
  41. export function findIcon(icon: String, type = "EL", property?: string) {
  42. if (type === "FA") {
  43. return defineComponent({
  44. name: "FaIcon",
  45. setup() {
  46. return { icon, property };
  47. },
  48. components: { FontAwesomeIcon },
  49. template: `<font-awesome-icon :icon="icon" v-bind:[property]="true" />`
  50. });
  51. } else if (type === "fa") {
  52. return defineComponent({
  53. name: "faIcon",
  54. data() {
  55. return { icon: `fa ${icon}` };
  56. },
  57. template: `<i :class="icon" />`
  58. });
  59. } else if (type === "IF") {
  60. return defineComponent({
  61. name: "IfIcon",
  62. data() {
  63. return { icon: `iconfont ${icon}` };
  64. },
  65. template: `<i :class="icon" />`
  66. });
  67. } else if (type === "RI") {
  68. return defineComponent({
  69. name: "RiIcon",
  70. data() {
  71. return { icon: `ri-${icon}` };
  72. },
  73. template: `<i :class="icon" />`
  74. });
  75. } else if (type === "EL") {
  76. return defineComponent({
  77. name: "ElIcon",
  78. data() {
  79. return { icon };
  80. },
  81. template: `<IconifyIconOffline :icon="icon" />`
  82. });
  83. } else if (type === "SVG") {
  84. return icon;
  85. }
  86. }
  87. export const Icon = Object.assign(icon, {
  88. install(app: App) {
  89. app.component(icon.name, icon);
  90. }
  91. });
  92. export const IconifyIconOffline = iconifyIconOffline;
  93. export const IconifyIconOnline = iconifyIconOnline;
  94. export default {
  95. Icon,
  96. IconifyIconOffline,
  97. IconifyIconOnline
  98. };