@fish-render/icons
Version:
Fish Render icon library for Vue 3
55 lines (46 loc) • 1.56 kB
text/typescript
/**
* 加载 SVG 图标内容
* @param iconName 图标名称
* @returns Promise<string> SVG 内容
*/
export async function loadSvgIcon(iconName: string): Promise<string> {
const { buildIconPath, getIconConfig } = await import('./iconConfig')
const config = getIconConfig()
// 尝试静态路径(只有当 path 不为空时)
if (config.path) {
const iconPath = buildIconPath(iconName)
// 使用 fetch 加载静态文件
try {
const response = await fetch(iconPath)
if (response.ok) {
return await response.text()
} else {
throw new Error(`Icon not found: ${iconPath}`)
}
} catch (error) {
throw error
}
}
// 如果没有配置路径,抛出错误
throw new Error(`No icon path configured. Please set 'path' in icon config.`)
}
/**
* 处理 SVG 内容,提取 viewBox 和内容
* @param svgText 原始 SVG 文本
* @returns { viewBox: string, content: string }
*/
export function processSvgContent(svgText: string): { viewBox: string; content: string } {
// 提取 viewBox
const viewBoxMatch = svgText.match(/viewBox="([^"]*)"/)
const viewBox = viewBoxMatch ? viewBoxMatch[1] : '0 0 24 24'
// 提取 SVG 内容(去掉 svg 标签)
const contentMatch = svgText.match(/<svg[^>]*>([\s\S]*)<\/svg>/)
let content: string
if (contentMatch) {
content = contentMatch[1]
} else {
// 如果没有匹配到,尝试直接提取内容
content = svgText.replace(/<svg[^>]*>/, '').replace(/<\/svg>/, '')
}
return { viewBox, content }
}