getStyleSheets.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { nextTick } from 'vue';
  2. import * as svg from '@element-plus/icons-vue';
  3. // 获取阿里字体图标
  4. const getAlicdnIconfont = () => {
  5. return new Promise((resolve, reject) => {
  6. nextTick(() => {
  7. const styles: any = document.styleSheets;
  8. let sheetsList = [] as any[];
  9. let sheetsIconList = [] as any[];
  10. for (let i = 0; i < styles.length; i++) {
  11. if (styles[i].href && styles[i].href.indexOf('at.alicdn.com') > -1) {
  12. sheetsList.push(styles[i]);
  13. }
  14. }
  15. for (let i = 0; i < sheetsList.length; i++) {
  16. for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
  17. if (sheetsList[i].cssRules[j].selectorText && sheetsList[i].cssRules[j].selectorText.indexOf('.icon-') > -1) {
  18. sheetsIconList.push(
  19. `${sheetsList[i].cssRules[j].selectorText.substring(1, sheetsList[i].cssRules[j].selectorText.length).replace(/\:\:before/gi, '')}`
  20. );
  21. }
  22. }
  23. }
  24. if (sheetsIconList.length > 0) resolve(sheetsIconList);
  25. else reject('未获取到值,请刷新重试');
  26. });
  27. });
  28. };
  29. // 初始化获取 css 样式,获取 element plus 自带 svg 图标,增加了 ele- 前缀,使用时:ele-Aim
  30. const getElementPlusIconfont = () => {
  31. return new Promise((resolve, reject) => {
  32. nextTick(() => {
  33. const icons = svg as any;
  34. const sheetsIconList = [] as any[];
  35. for (const i in icons) {
  36. sheetsIconList.push(`ele-${icons[i].name}`);
  37. }
  38. if (sheetsIconList.length > 0) resolve(sheetsIconList);
  39. else reject('未获取到值,请刷新重试');
  40. });
  41. });
  42. };
  43. // 初始化获取 css 样式,这里使用 fontawesome 的图标
  44. const getAwesomeIconfont = () => {
  45. return new Promise((resolve, reject) => {
  46. nextTick(() => {
  47. const styles: any = document.styleSheets;
  48. let sheetsList = [] as any[];
  49. let sheetsIconList = [] as any[];
  50. for (let i = 0; i < styles.length; i++) {
  51. if (styles[i].href && styles[i].href.indexOf('font-awesome') > -1) {
  52. sheetsList.push(styles[i]);
  53. }
  54. }
  55. for (let i = 0; i < sheetsList.length; i++) {
  56. for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
  57. if (
  58. sheetsList[i].cssRules[j].selectorText &&
  59. sheetsList[i].cssRules[j].selectorText.indexOf('.fa-') === 0 &&
  60. sheetsList[i].cssRules[j].selectorText.indexOf(',') === -1
  61. ) {
  62. if (/::before/.test(sheetsList[i].cssRules[j].selectorText)) {
  63. sheetsIconList.push(
  64. `${sheetsList[i].cssRules[j].selectorText.substring(1, sheetsList[i].cssRules[j].selectorText.length).replace(/\:\:before/gi, '')}`
  65. );
  66. }
  67. }
  68. }
  69. }
  70. if (sheetsIconList.length > 0) resolve(sheetsIconList.reverse());
  71. else reject('未获取到值,请刷新重试');
  72. });
  73. });
  74. };
  75. /*
  76. * 获取本地自带的图标
  77. * /src/assets/icons文件夹内的svg文件
  78. */
  79. const getLocalIconfontNames = () => {
  80. return new Promise<string[]>((resolve, reject) => {
  81. nextTick(() => {
  82. let iconfonts: string[] = [];
  83. const svgEl = document.getElementById('local-icon');
  84. if (svgEl?.dataset.iconName) {
  85. iconfonts = (svgEl?.dataset.iconName as string).split(',');
  86. }
  87. if (iconfonts.length > 0) {
  88. resolve(iconfonts);
  89. } else {
  90. reject('No Local Icons');
  91. }
  92. });
  93. });
  94. };
  95. /**
  96. * 获取字体图标 `document.styleSheets`
  97. * @method ali 获取阿里字体图标 `<i class="iconfont 图标类名"></i>`
  98. * @method ele 获取 element plus 自带图标 `<i class="图标类名"></i>`
  99. * @method ali 获取 fontawesome 的图标 `<i class="fa 图标类名"></i>`
  100. */
  101. const initIconfont = {
  102. // iconfont
  103. ali: () => {
  104. return getAlicdnIconfont();
  105. },
  106. // element plus
  107. ele: () => {
  108. return getElementPlusIconfont();
  109. },
  110. // fontawesome
  111. awe: () => {
  112. return getAwesomeIconfont();
  113. },
  114. local: () => {
  115. return getLocalIconfontNames();
  116. },
  117. };
  118. // 导出方法
  119. export default initIconfont;