Files
one-pipe-system/src/utils/constants/iconfont.ts
sexygoat 222e5bb11a Initial commit: One Pipe System
完整的管理系统,包含账户管理、卡片管理、套餐管理、财务管理等功能模块。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-22 16:35:33 +08:00

67 lines
1.7 KiB
TypeScript

// 提取 iconfont 图标
export interface IconfontType {
className: string
unicode?: string
}
interface StyleSheetError {
sheet: CSSStyleSheet
error: unknown
}
function extractIconFromRule(rule: CSSRule): IconfontType | null {
if (!(rule instanceof CSSStyleRule)) return null
const { selectorText, style } = rule
if (!selectorText?.startsWith('.iconsys-') || !selectorText.includes('::before')) return null
const className = selectorText.substring(1).replace('::before', '')
const content = style.getPropertyValue('content')
if (!content) return null
const unicode = content.replace(/['"\\]/g, '')
return {
className,
unicode: unicode ? `&#x${getUnicode(unicode)};` : undefined
}
}
const processedErrors = new Set<StyleSheetError>()
export function extractIconClasses(): IconfontType[] {
const iconInfos: IconfontType[] = []
try {
Array.from(document.styleSheets).forEach((sheet) => {
try {
const rules = Array.from(sheet.cssRules || sheet.rules)
rules.forEach((rule) => {
const iconInfo = extractIconFromRule(rule)
if (iconInfo) {
iconInfos.push(iconInfo)
}
})
} catch (error) {
const styleSheetError: StyleSheetError = { sheet, error }
if (!processedErrors.has(styleSheetError)) {
console.warn('Cannot read cssRules from stylesheet:', {
error,
sheetHref: sheet.href
})
processedErrors.add(styleSheetError)
}
}
})
} catch (error) {
console.error('Failed to process stylesheets:', error)
}
return iconInfos
}
export function getUnicode(charCode: string): string {
if (!charCode) return ''
return charCode.charCodeAt(0).toString(16).padStart(4, '0')
}