theme-picker.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <el-color-picker
  3. class="theme-picker"
  4. popper-class="theme-picker-dropdown"
  5. v-model="theme"></el-color-picker>
  6. </template>
  7. <style>
  8. .theme-picker {
  9. height: 80px;
  10. display: inline-block;
  11. @utils-vertical-center;
  12. }
  13. .theme-picker .el-color-picker__trigger {
  14. vertical-align: middle;
  15. }
  16. .theme-picker-dropdown .el-color-dropdown__link-btn {
  17. display: none;
  18. }
  19. </style>
  20. <script>
  21. import { version } from 'main/index.js';
  22. const ORIGINAL_THEME = '#409EFF';
  23. export default {
  24. data() {
  25. return {
  26. chalk: '',
  27. theme: ORIGINAL_THEME
  28. };
  29. },
  30. watch: {
  31. theme(val, oldVal) {
  32. if (typeof val !== 'string') return;
  33. const themeCluster = this.getThemeCluster(val.replace('#', ''));
  34. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''));
  35. const handler = () => {
  36. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''));
  37. let newStyle = this.updateStyle(this.chalk, originalCluster, themeCluster);
  38. let styleTag = document.getElementById('theme-style');
  39. if (!styleTag) {
  40. styleTag = document.createElement('style');
  41. styleTag.setAttribute('id', 'theme-style');
  42. document.head.appendChild(styleTag);
  43. }
  44. styleTag.innerText = newStyle;
  45. };
  46. if (!this.chalk) {
  47. this.getChalkString(handler);
  48. } else {
  49. handler();
  50. }
  51. const styles = [].slice.call(document.querySelectorAll('style'))
  52. .filter(style => {
  53. const text = style.innerText;
  54. return new RegExp(oldVal).test(text) && !/Chalk Variables/.test(text);
  55. });
  56. styles.forEach(style => {
  57. const { innerText } = style;
  58. if (typeof innerText !== 'string') return;
  59. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster);
  60. });
  61. }
  62. },
  63. methods: {
  64. updateStyle(style, oldCluster, newCluster) {
  65. let newStyle = style;
  66. oldCluster.forEach((color, index) => {
  67. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index]);
  68. });
  69. return newStyle;
  70. },
  71. getChalkString(callback) {
  72. const xhr = new XMLHttpRequest();
  73. xhr.onreadystatechange = () => {
  74. if (xhr.readyState === 4 && xhr.status === 200) {
  75. this.chalk = xhr.responseText.replace(/@font-face{[^}]+}/, '');
  76. callback();
  77. }
  78. };
  79. xhr.open('GET', `https://unpkg.com/element-ui@${ version }/lib/theme-chalk/index.css`);
  80. xhr.send();
  81. },
  82. getThemeCluster(theme) {
  83. const tintColor = (color, tint) => {
  84. let red = parseInt(color.slice(0, 2), 16);
  85. let green = parseInt(color.slice(2, 4), 16);
  86. let blue = parseInt(color.slice(4, 6), 16);
  87. if (tint === 0) { // when primary color is in its rgb space
  88. return [red, green, blue].join(',');
  89. } else {
  90. red += Math.round(tint * (255 - red));
  91. green += Math.round(tint * (255 - green));
  92. blue += Math.round(tint * (255 - blue));
  93. red = red.toString(16);
  94. green = green.toString(16);
  95. blue = blue.toString(16);
  96. return `#${ red }${ green }${ blue }`;
  97. }
  98. };
  99. const clusters = [theme];
  100. for (let i = 0; i <= 9; i++) {
  101. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))));
  102. }
  103. return clusters;
  104. }
  105. }
  106. };
  107. </script>