iconfont.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { nextTick } from 'vue'
  2. import { loadCss, loadJs } from './common'
  3. import * as elIcons from '@element-plus/icons-vue'
  4. const cssUrls: Array<string> = [
  5. '//at.alicdn.com/t/font_3135462_5axiswmtpj.css', // 示例链接,建议替换
  6. // '//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css',
  7. '//cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css',
  8. ]
  9. const jsUrls: Array<string> = []
  10. export default function init() {
  11. if (cssUrls.length > 0) {
  12. cssUrls.map((v) => {
  13. loadCss(v)
  14. })
  15. }
  16. if (jsUrls.length > 0) {
  17. jsUrls.map((v) => {
  18. loadJs(v)
  19. })
  20. }
  21. }
  22. /*
  23. * 获取当前页面中从指定域名加载到的样式表内容
  24. * 样式表未载入前无法获取
  25. */
  26. function getStylesFromDomain(domain: string) {
  27. let sheets = []
  28. const styles: StyleSheetList = document.styleSheets
  29. for (const key in styles) {
  30. if (styles[key].href && (styles[key].href as string).indexOf(domain) > -1) {
  31. sheets.push(styles[key])
  32. }
  33. }
  34. return sheets
  35. }
  36. /*
  37. * 获取本地自带的图标
  38. * /src/assets/icons文件夹内的svg文件
  39. */
  40. export function getLocalIconfontNames() {
  41. return new Promise<string[]>((resolve, reject) => {
  42. nextTick(() => {
  43. let iconfonts: string[] = []
  44. let svgEl = document.getElementById('local-icon')
  45. if (svgEl?.dataset.iconName) {
  46. iconfonts = (svgEl?.dataset.iconName as string).split(',')
  47. }
  48. if (iconfonts.length > 0) {
  49. resolve(iconfonts)
  50. } else {
  51. reject('No Local Icons')
  52. }
  53. })
  54. })
  55. }
  56. /*
  57. * 获取 Awesome-Iconfont 的 name 列表
  58. */
  59. export function getAwesomeIconfontNames() {
  60. return new Promise<string[]>((resolve, reject) => {
  61. nextTick(() => {
  62. let iconfonts = []
  63. let sheets = getStylesFromDomain('netdna.bootstrapcdn.com/font-awesome/')
  64. for (const key in sheets) {
  65. let rules: any = sheets[key].cssRules
  66. for (const k in rules) {
  67. if (rules[k].selectorText && /^\.fa-(.*)::before$/g.test(rules[k].selectorText)) {
  68. if (rules[k].selectorText.indexOf(', ') > -1) {
  69. let iconNames = rules[k].selectorText.split(', ')
  70. /*
  71. // 含图标别名
  72. for (const i_k in iconNames) {
  73. iconfonts.push(`${iconNames[i_k].substring(1, iconNames[i_k].length).replace(/\:\:before/gi, '')}`)
  74. } */
  75. iconfonts.push(`${iconNames[0].substring(1, iconNames[0].length).replace(/\:\:before/gi, '')}`)
  76. } else {
  77. iconfonts.push(`${rules[k].selectorText.substring(1, rules[k].selectorText.length).replace(/\:\:before/gi, '')}`)
  78. }
  79. }
  80. }
  81. }
  82. if (iconfonts.length > 0) {
  83. resolve(iconfonts)
  84. } else {
  85. reject('No AwesomeIcon style sheet')
  86. }
  87. })
  88. })
  89. }
  90. /*
  91. * 获取 Iconfont 的 name 列表
  92. */
  93. export function getIconfontNames() {
  94. return new Promise<string[]>((resolve, reject) => {
  95. nextTick(() => {
  96. let iconfonts = []
  97. let sheets = getStylesFromDomain('at.alicdn.com')
  98. for (const key in sheets) {
  99. let rules: any = sheets[key].cssRules
  100. for (const k in rules) {
  101. if (rules[k].selectorText && /^\.icon-(.*)::before$/g.test(rules[k].selectorText)) {
  102. iconfonts.push(`${rules[k].selectorText.substring(1, rules[k].selectorText.length).replace(/\:\:before/gi, '')}`)
  103. }
  104. }
  105. }
  106. if (iconfonts.length > 0) {
  107. resolve(iconfonts)
  108. } else {
  109. reject('No Iconfont style sheet')
  110. }
  111. })
  112. })
  113. }
  114. /*
  115. * 获取element plus 自带的图标
  116. */
  117. export function getElementPlusIconfontNames() {
  118. return new Promise<string[]>((resolve, reject) => {
  119. nextTick(() => {
  120. let iconfonts = []
  121. const icons = elIcons as any
  122. for (const i in icons) {
  123. iconfonts.push(`el-icon-${icons[i].name}`)
  124. }
  125. if (iconfonts.length > 0) {
  126. resolve(iconfonts)
  127. } else {
  128. reject('No ElementPlus Icons')
  129. }
  130. })
  131. })
  132. }