getStyleSheets.ts 3.6 KB

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