index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. await this.setTheme(val)
  33. }
  34. },
  35. created() {
  36. if(this.defaultTheme !== ORIGINAL_THEME) {
  37. this.setTheme(this.defaultTheme)
  38. }
  39. },
  40. methods: {
  41. async setTheme(val) {
  42. const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
  43. if (typeof val !== 'string') return
  44. const themeCluster = this.getThemeCluster(val.replace('#', ''))
  45. const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
  46. const getHandler = (variable, id) => {
  47. return () => {
  48. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
  49. const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
  50. let styleTag = document.getElementById(id)
  51. if (!styleTag) {
  52. styleTag = document.createElement('style')
  53. styleTag.setAttribute('id', id)
  54. document.head.appendChild(styleTag)
  55. }
  56. styleTag.innerText = newStyle
  57. }
  58. }
  59. if (!this.chalk) {
  60. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  61. await this.getCSSString(url, 'chalk')
  62. }
  63. const chalkHandler = getHandler('chalk', 'chalk-style')
  64. chalkHandler()
  65. const styles = [].slice.call(document.querySelectorAll('style'))
  66. .filter(style => {
  67. const text = style.innerText
  68. return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
  69. })
  70. styles.forEach(style => {
  71. const { innerText } = style
  72. if (typeof innerText !== 'string') return
  73. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
  74. })
  75. this.$emit('change', val)
  76. },
  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>