getStyleSheets.ts 3.6 KB

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