|
| 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 | + |
| 10 | +<script> |
| 11 | +const version = require('element-ui/package.json').version // element-ui version from node_modules |
| 12 | +const ORIGINAL_THEME = '#409EFF' // default color |
| 13 | +export default { |
| 14 | + data() { |
| 15 | + return { |
| 16 | + chalk: '', // content of theme-chalk css |
| 17 | + theme: '' |
| 18 | + } |
| 19 | + }, |
| 20 | + computed: { |
| 21 | + defaultTheme() { |
| 22 | + return this.$store.state.app.theme |
| 23 | + } |
| 24 | + }, |
| 25 | + watch: { |
| 26 | + defaultTheme: { |
| 27 | + handler: function(val, oldVal) { |
| 28 | + this.theme = val |
| 29 | + }, |
| 30 | + immediate: true |
| 31 | + }, |
| 32 | + async theme(val) { |
| 33 | + const oldVal = this.chalk ? this.theme : ORIGINAL_THEME |
| 34 | + if (typeof val !== 'string') return |
| 35 | + const themeCluster = this.getThemeCluster(val.replace('#', '')) |
| 36 | + const originalCluster = this.getThemeCluster(oldVal.replace('#', '')) |
| 37 | + const $message = this.$message({ |
| 38 | + message: '修改主题色中', |
| 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 | + |
| 138 | +<style> |
| 139 | +.theme-message, |
| 140 | +.theme-picker-dropdown { |
| 141 | + z-index: 99999 !important; |
| 142 | +} |
| 143 | +.theme-picker .el-color-picker__trigger { |
| 144 | + height: 26px !important; |
| 145 | + width: 26px !important; |
| 146 | + padding: 2px; |
| 147 | +} |
| 148 | +.theme-picker-dropdown .el-color-dropdown__link-btn { |
| 149 | + display: none; |
| 150 | +} |
| 151 | +</style> |
0 commit comments