Files
wireguard-dashboard-admin/src/components/ReIcon/index.ts

99 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-11-16 22:17:57 +08:00
import { App, defineComponent } from "vue";
2021-10-16 16:16:58 +08:00
import icon from "./src/Icon.vue";
2021-11-16 22:17:57 +08:00
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { iconComponents } from "/@/plugins/element-plus";
2021-10-16 16:16:58 +08:00
2021-11-16 22:17:57 +08:00
/**
* find icon component
* @param icon icon图标
* @returns component
*/
export function findIconReg(icon: string) {
2021-12-21 17:05:54 +08:00
// fontawesome4
const fa4Reg = /^fa-/;
// fontawesome5+
const fa5Reg = /^FA-/;
2021-11-28 16:39:26 +08:00
// iconfont
const iFReg = /^IF-/;
2021-12-17 15:18:48 +08:00
// remixicon
const riReg = /^RI-/;
2021-11-28 16:39:26 +08:00
// typeof icon === "function" 属于SVG
2021-12-21 17:05:54 +08:00
if (fa5Reg.test(icon)) {
const text = icon.split(fa5Reg)[1];
2021-11-28 16:39:26 +08:00
return findIcon(
2021-12-21 17:05:54 +08:00
text.slice(0, text.indexOf(" ") == -1 ? text.length : text.indexOf(" ")),
2021-11-28 16:39:26 +08:00
"FA",
text.slice(text.indexOf(" ") + 1, text.length)
);
2021-12-21 17:05:54 +08:00
} else if (fa4Reg.test(icon)) {
return findIcon(icon.split(fa4Reg)[1], "fa");
2021-11-28 16:39:26 +08:00
} else if (iFReg.test(icon)) {
return findIcon(icon.split(iFReg)[1], "IF");
} else if (typeof icon === "function") {
return findIcon(icon, "SVG");
2021-12-17 15:18:48 +08:00
} else if (riReg.test(icon)) {
return findIcon(icon.split(riReg)[1], "RI");
2021-11-16 22:17:57 +08:00
} else {
2021-11-28 16:39:26 +08:00
return findIcon(icon, "EL");
2021-11-16 22:17:57 +08:00
}
}
2021-11-28 16:39:26 +08:00
2021-12-17 15:18:48 +08:00
// 支持fontawesome、iconfont、remixicon、element-plus/icons、自定义svg
2021-11-28 16:39:26 +08:00
export function findIcon(icon: String, type = "EL", property?: string) {
if (type === "FA") {
2021-11-16 22:17:57 +08:00
return defineComponent({
name: "FaIcon",
2021-11-28 16:39:26 +08:00
setup() {
return { icon, property };
2021-11-16 22:17:57 +08:00
},
components: { FontAwesomeIcon },
2021-11-28 16:39:26 +08:00
template: `<font-awesome-icon :icon="icon" v-bind:[property]="true" />`
2021-11-16 22:17:57 +08:00
});
2021-12-21 17:05:54 +08:00
} else if (type === "fa") {
return defineComponent({
name: "faIcon",
data() {
return { icon: `fa ${icon}` };
},
template: `<i :class="icon" />`
});
2021-11-28 16:39:26 +08:00
} else if (type === "IF") {
return defineComponent({
name: "IfIcon",
data() {
return { icon: `iconfont ${icon}` };
},
template: `<i :class="icon" />`
});
2021-12-17 15:18:48 +08:00
} else if (type === "RI") {
return defineComponent({
name: "RIIcon",
data() {
return { icon: `ri-${icon}` };
},
template: `<i :class="icon" />`
});
2021-11-28 16:39:26 +08:00
} else if (type === "EL") {
2021-11-16 22:17:57 +08:00
const components = iconComponents.filter(
component => component.name === icon
);
if (components.length > 0) {
return components[0];
} else {
return null;
}
2021-11-28 16:39:26 +08:00
} else if (type === "SVG") {
return icon;
2021-11-16 22:17:57 +08:00
}
}
2021-11-28 16:39:26 +08:00
export const Icon = Object.assign(icon, {
install(app: App) {
app.component(icon.name, icon);
}
});
export default {
Icon
};