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

75 lines
1.8 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-11-28 16:39:26 +08:00
// fontawesome
const faReg = /^FA-/;
// iconfont
const iFReg = /^IF-/;
// typeof icon === "function" 属于SVG
2021-11-16 22:17:57 +08:00
if (faReg.test(icon)) {
2021-11-28 16:39:26 +08:00
const text = icon.split(faReg)[1];
return findIcon(
text.slice(0, text.indexOf(" ")),
"FA",
text.slice(text.indexOf(" ") + 1, text.length)
);
} else if (iFReg.test(icon)) {
return findIcon(icon.split(iFReg)[1], "IF");
} else if (typeof icon === "function") {
return findIcon(icon, "SVG");
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
// 支持fontawesome、iconfont、element-plus/icons、自定义svg
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-11-28 16:39:26 +08:00
} else if (type === "IF") {
return defineComponent({
name: "IfIcon",
data() {
return { icon: `iconfont ${icon}` };
},
template: `<i :class="icon" />`
});
} 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
};