index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. const $message = this.$message({
  37. message: ' Compiling the theme',
  38. customClass: 'theme-message',
  39. type: 'success',
  40. duration: 0,
  41. iconClass: 'el-icon-loading'
  42. })
  43. const getHandler = (variable, id) => {
  44. return () => {
  45. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
  46. const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
  47. let styleTag = document.getElementById(id)
  48. if (!styleTag) {
  49. styleTag = document.createElement('style')
  50. styleTag.setAttribute('id', id)
  51. document.head.appendChild(styleTag)
  52. }
  53. styleTag.innerText = newStyle
  54. }
  55. }
  56. if (!this.chalk) {
  57. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  58. await this.getCSSString(url, 'chalk')
  59. }
  60. const chalkHandler = getHandler('chalk', 'chalk-style')
  61. chalkHandler()
  62. const styles = [].slice.call(document.querySelectorAll('style'))
  63. .filter(style => {
  64. const text = style.innerText
  65. return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
  66. })
  67. styles.forEach(style => {
  68. const { innerText } = style
  69. if (typeof innerText !== 'string') return
  70. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
  71. })
  72. this.$emit('change', val)
  73. $message.close()
  74. }
  75. },
  76. methods: {
  77. updateStyle(style, oldCluster, newCluster) {
  78. let newStyle = style
  79. oldCluster.forEach((color, index) => {
  80. newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
  81. })
  82. return newStyle
  83. },
  84. getCSSString(url, variable) {
  85. return new Promise(resolve => {
  86. const xhr = new XMLHttpRequest()
  87. xhr.onreadystatechange = () => {
  88. if (xhr.readyState === 4 && xhr.status === 200) {
  89. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
  90. resolve()
  91. }
  92. }
  93. xhr.open('GET', url)
  94. xhr.send()
  95. })
  96. },
  97. getThemeCluster(theme) {
  98. const tintColor = (color, tint) => {
  99. let red = parseInt(color.slice(0, 2), 16)
  100. let green = parseInt(color.slice(2, 4), 16)
  101. let blue = parseInt(color.slice(4, 6), 16)
  102. if (tint === 0) { // when primary color is in its rgb space
  103. return [red, green, blue].join(',')
  104. } else {
  105. red += Math.round(tint * (255 - red))
  106. green += Math.round(tint * (255 - green))
  107. blue += Math.round(tint * (255 - blue))
  108. red = red.toString(16)
  109. green = green.toString(16)
  110. blue = blue.toString(16)
  111. return `#${red}${green}${blue}`
  112. }
  113. }
  114. const shadeColor = (color, shade) => {
  115. let red = parseInt(color.slice(0, 2), 16)
  116. let green = parseInt(color.slice(2, 4), 16)
  117. let blue = parseInt(color.slice(4, 6), 16)
  118. red = Math.round((1 - shade) * red)
  119. green = Math.round((1 - shade) * green)
  120. blue = Math.round((1 - shade) * blue)
  121. red = red.toString(16)
  122. green = green.toString(16)
  123. blue = blue.toString(16)
  124. return `#${red}${green}${blue}`
  125. }
  126. const clusters = [theme]
  127. for (let i = 0; i <= 9; i++) {
  128. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  129. }
  130. clusters.push(shadeColor(theme, 0.1))
  131. return clusters
  132. }
  133. }
  134. }
  135. </script>
  136. <style>
  137. .theme-message,
  138. .theme-picker-dropdown {
  139. z-index: 99999 !important;
  140. }
  141. .theme-picker .el-color-picker__trigger {
  142. height: 26px !important;
  143. width: 26px !important;
  144. padding: 2px;
  145. }
  146. .theme-picker-dropdown .el-color-dropdown__link-btn {
  147. display: none;
  148. }
  149. </style>