Files
yunzerwebsiteallinone/uniapp/components/FaIcon.vue
T
2026-11-17 17:16:02 +08:00

135 lines
3.0 KiB
Vue

<template>
<text class="fa-icon" :class="fontClass" :style="iconStyle">{{ iconChar }}</text>
</template>
<script setup>
import { computed } from 'vue'
import { FA_ICONS } from '@/utils/fa-icons.js'
const props = defineProps({
name: {
type: String,
required: true
},
type: {
type: String,
default: 'solid'
},
color: {
type: String,
default: 'inherit'
},
size: {
type: [Number, String],
default: 16
}
})
const parsed = computed(() => {
const raw = String(props.name || '').trim()
if (!raw) return { name: '', brands: false }
if (/\bfa-(solid|regular|light|thin|duotone|brands)\b|\bfas\b|\bfar\b|\bfal\b|\bfab\b/.test(raw)) {
const tokens = raw.split(/\s+/)
let brands = false
let icon = ''
for (const t of tokens) {
if (/^(fa-brands|fab)$/.test(t)) brands = true
else if (/^(fa-solid|fas|fa-regular|far|fa-light|fal|fa-thin|fa-duotone)$/.test(t)) continue
else if (t.startsWith('fa-')) icon = t.slice(3)
else if (t && t !== 'fa') icon = t
}
return { name: icon, brands }
}
return { name: raw.replace(/^fa-/, ''), brands: props.type === 'brands' }
})
// Element Plus / 历史菜单使用的是 PascalCase 图标名,这里统一转换为 Font Awesome 名称
const ICON_ALIAS = {
notebook: 'book',
tickets: 'ticket',
ticket: 'ticket',
document: 'file-lines',
files: 'folder-open',
calendar: 'calendar-days',
reading: 'book-open',
postcard: 'envelope',
chatdotround: 'comments',
chatdot: 'comment',
editpen: 'pen',
pencil: 'pen',
pricetag: 'tag',
operation: 'sliders',
coin: 'coins',
goods: 'box',
setting: 'gear',
dataline: 'chart-line',
officebuilding: 'building',
fold: 'folder',
menu: 'bars',
search: 'magnifying-glass',
bell: 'bell',
notification: 'bell',
homefilled: 'house',
position: 'location-dot',
promotion: 'gift',
discount: 'tag',
userfilled: 'user',
documentcopy: 'copy',
memo: 'note-sticky',
management: 'list',
tools: 'wrench',
service: 'headset'
}
function toKebabCase(name) {
return String(name)
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.toLowerCase()
}
const iconChar = computed(() => {
const name = parsed.value.name
if (!name) return ''
if (FA_ICONS[name]) return FA_ICONS[name]
const kebab = toKebabCase(name)
if (FA_ICONS[kebab]) return FA_ICONS[kebab]
const alias = ICON_ALIAS[kebab]
if (alias && FA_ICONS[alias]) return FA_ICONS[alias]
return ''
})
const fontClass = computed(() =>
parsed.value.brands ? 'fa-font-brands' : 'fa-font-solid'
)
const iconStyle = computed(() => ({
color: props.color,
fontSize: typeof props.size === 'number' ? `${props.size}px` : props.size,
lineHeight: 1
}))
</script>
<style scoped>
.fa-icon {
display: inline-block;
font-style: normal;
vertical-align: middle;
}
</style>
<style>
.fa-font-solid {
font-family: 'Font Awesome 7 Free';
font-weight: 900;
font-style: normal;
}
.fa-font-brands {
font-family: 'Font Awesome 7 Brands';
font-weight: 400;
font-style: normal;
}
</style>