getStyleSheets.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = [];
  9. let sheetsIconList = [];
  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 = [];
  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 = [];
  49. let sheetsIconList = [];
  50. for (let i = 0; i < styles.length; i++) {
  51. if (styles[i].href && styles[i].href.indexOf('netdna.bootstrapcdn.com') > -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. const getFlagIconfont = () => {
  76. return new Promise((resolve, reject) => {
  77. nextTick(() => {
  78. const styles: any = document.styleSheets;
  79. let sheetsList = [];
  80. let sheetsIconList = [];
  81. for (let i = 0; i < styles.length; i++) {
  82. if (styles[i].href && styles[i].href.indexOf('cdn.jsdelivr.net') > -1) {
  83. sheetsList.push(styles[i]);
  84. }
  85. }
  86. for (let i = 0; i < sheetsList.length; i++) {
  87. for (let j = 0; j < sheetsList[i].cssRules.length; j++) {
  88. if (
  89. sheetsList[i].cssRules[j].selectorText &&
  90. sheetsList[i].cssRules[j].selectorText.indexOf('.fi-') === 0 &&
  91. !sheetsList[i].cssRules[j].selectorText.endsWith('.fis')
  92. ) {
  93. sheetsIconList.push(
  94. `${sheetsList[i].cssRules[j].selectorText.substring(1, sheetsList[i].cssRules[j].selectorText.length)}`
  95. );
  96. }
  97. }
  98. }
  99. if (sheetsIconList.length > 0) resolve(sheetsIconList.reverse());
  100. else reject('未获取到值,请刷新重试');
  101. })
  102. })
  103. }
  104. /**
  105. * 获取字体图标 `document.styleSheets`
  106. * @method ali 获取阿里字体图标 `<i class="iconfont 图标类名"></i>`
  107. * @method ele 获取 element plus 自带图标 `<i class="图标类名"></i>`
  108. * @method ali 获取 fontawesome 的图标 `<i class="fa 图标类名"></i>`
  109. */
  110. const initIconfont = {
  111. // iconfont
  112. ali: () => {
  113. return getAlicdnIconfont();
  114. },
  115. // element plus
  116. ele: () => {
  117. return getElementPlusIconfont();
  118. },
  119. // fontawesome
  120. awe: () => {
  121. return getAwesomeIconfont();
  122. },
  123. flag: () => {
  124. return getFlagIconfont();
  125. }
  126. };
  127. // 导出方法
  128. export default initIconfont;