import { Icon, Svg } from './interface' const base_css = (prefixIcon: string, size: string) => { return ` .${prefixIcon} { display: inline-block; width: ${size}; height: ${size}; background-repeat: no-repeat; background-position: center; background-size: 100%; } \n ` } const generateStyleClass = (name: string, svg: string) => { return ` .${name} { background: url(${svg}); } ` } export class Match { content: string prefixIcon: string icons: Icon[] svgs: Svg[] css: string constructor(prefixIcon: string = 't-icon', size: string = '16px') { this.content = '' this.icons = [] this.svgs = [] this.prefixIcon = prefixIcon this.css = base_css(prefixIcon, size) } matchesContent(data: string): Match { const reg: RegExp = /(\)/gim const content = data.match(reg) || [] this.content = content.toString() return this } matchesIcon(): Match { const { content, prefixIcon } = this const reg: RegExp = /(\)/gim const icons: string[] = content.match(reg) || [] this.icons = icons.map(s => { s = s.replace('icon-', `${prefixIcon}-`) const ids: string[] | null = s.match(/id\=\"(.*?)\"/) const name = (ids && ids[1]) || '' const icon = s.replace(/symbol/gi, 'svg') return { name, icon } }) return this } generateCss() { this.svgs.forEach(({ name, svg }) => { this.css += generateStyleClass(name, svg) }) return this } setContent(content: string): Match { this.matchesContent(content) return this } svg2DataUrl(svgStr: string): string { svgStr = svgStr.replace( /\ ({ name, svg: svg2DataUrl(icon) })) return this } }