index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <el-color-picker
  3. v-model="theme"
  4. :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
  5. class="theme-picker"
  6. popper-class="theme-picker-dropdown"
  7. />
  8. </template>
  9. <script>
  10. const version = require('element-ui/package.json').version // element-ui version from node_modules
  11. const ORIGINAL_THEME = '#409EFF' // default color
  12. export default {
  13. data() {
  14. return {
  15. chalk: '', // content of theme-chalk css
  16. theme: ''
  17. }
  18. },
  19. computed: {
  20. defaultTheme() {
  21. return this.$store.state.settings.theme
  22. }
  23. },
  24. watch: {
  25. defaultTheme: {
  26. handler: function(val, oldVal) {
  27. this.theme = val
  28. },
  29. immediate: true
  30. },
  31. async theme(val) {
  32. const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
  33. if (typeof val !== 'string') return
  34. const themeCluster = this.getThemeCluster(val.replace('#', ''))
  35. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
  36. console.log(themeCluster, originalCluster)
  37. const $message = this.$message({
  38. message: ' Compiling the theme',
  39. customClass: 'theme-message',
  40. type: 'success',
  41. duration: 0,
  42. iconClass: 'el-icon-loading'
  43. })
  44. const getHandler = (variable, id) => {
  45. return () => {
  46. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
  47. const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
  48. let styleTag = document.getElementById(id)
  49. if (!styleTag) {
  50. styleTag = document.createElement('style')
  51. styleTag.setAttribute('id', id)
  52. document.head.appendChild(styleTag)
  53. }
  54. styleTag.innerText = newStyle
  55. }
  56. }
  57. if (!this.chalk) {
  58. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  59. await this.getCSSString(url, 'chalk')
  60. }
  61. const chalkHandler = getHandler('chalk', 'chalk-style')
  62. chalkHandler()
  63. const styles = [].slice.call(document.querySelectorAll('style'))
  64. .filter(style => {
  65. const text = style.innerText
  66. return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
  67. })
  68. styles.forEach(style => {
  69. const { innerText } = style
  70. if (typeof innerText !== 'string') return
  71. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
  72. })
  73. this.$emit('change', val)
  74. $message.close()
  75. }
  76. },
  77. methods: {
  78. updateStyle(style, oldCluster, newCluster) {
  79. let newStyle = style
  80. oldCluster.forEach((color, index) => {
  81. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
  82. })
  83. return newStyle
  84. },
  85. getCSSString(url, variable) {
  86. return new Promise(resolve => {
  87. const xhr = new XMLHttpRequest()
  88. xhr.onreadystatechange = () => {
  89. if (xhr.readyState === 4 && xhr.status === 200) {
  90. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
  91. resolve()
  92. }
  93. }
  94. xhr.open('GET', url)
  95. xhr.send()
  96. })
  97. },
  98. getThemeCluster(theme) {
  99. const tintColor = (color, tint) => {
  100. let red = parseInt(color.slice(0, 2), 16)
  101. let green = parseInt(color.slice(2, 4), 16)
  102. let blue = parseInt(color.slice(4, 6), 16)
  103. if (tint === 0) { // when primary color is in its rgb space
  104. return [red, green, blue].join(',')
  105. } else {
  106. red += Math.round(tint * (255 - red))
  107. green += Math.round(tint * (255 - green))
  108. blue += Math.round(tint * (255 - blue))
  109. red = red.toString(16)
  110. green = green.toString(16)
  111. blue = blue.toString(16)
  112. return `#${red}${green}${blue}`
  113. }
  114. }
  115. const shadeColor = (color, shade) => {
  116. let red = parseInt(color.slice(0, 2), 16)
  117. let green = parseInt(color.slice(2, 4), 16)
  118. let blue = parseInt(color.slice(4, 6), 16)
  119. red = Math.round((1 - shade) * red)
  120. green = Math.round((1 - shade) * green)
  121. blue = Math.round((1 - shade) * blue)
  122. red = red.toString(16)
  123. green = green.toString(16)
  124. blue = blue.toString(16)
  125. return `#${red}${green}${blue}`
  126. }
  127. const clusters = [theme]
  128. for (let i = 0; i <= 9; i++) {
  129. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  130. }
  131. clusters.push(shadeColor(theme, 0.1))
  132. return clusters
  133. }
  134. }
  135. }
  136. </script>
  137. <style>
  138. .theme-message,
  139. .theme-picker-dropdown {
  140. z-index: 99999 !important;
  141. }
  142. .theme-picker .el-color-picker__trigger {
  143. height: 26px !important;
  144. width: 26px !important;
  145. padding: 2px;
  146. }
  147. .theme-picker-dropdown .el-color-dropdown__link-btn {
  148. display: none;
  149. }
  150. </style>