Files
dm-manage-web/src/components/ThemePicker/index.vue

169 lines
4.7 KiB
Vue
Raw Normal View History

2023-02-15 09:17:05 +08:00
<template>
2023-03-21 00:53:28 +08:00
<el-color-picker v-model="theme" :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]" class="theme-picker" popper-class="theme-picker-dropdown" />
2023-02-15 09:17:05 +08:00
</template>
<script>
2023-03-21 00:53:28 +08:00
const version = require('element-ui/package.json').version; // element-ui version from node_modules
const ORIGINAL_THEME = '#409EFF'; // default color
2023-02-15 09:17:05 +08:00
export default {
data() {
return {
chalk: '', // content of theme-chalk css
theme: ''
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
},
computed: {
defaultTheme() {
2023-03-21 00:53:28 +08:00
return this.$store.state.settings.theme;
2023-02-15 09:17:05 +08:00
}
},
watch: {
defaultTheme: {
2023-03-21 00:53:28 +08:00
handler: function (val, oldVal) {
this.theme = val;
2023-02-15 09:17:05 +08:00
},
immediate: true
},
async theme(val) {
2023-03-21 00:53:28 +08:00
await this.setTheme(val);
2023-02-15 09:17:05 +08:00
}
},
created() {
2023-03-21 00:53:28 +08:00
if (this.defaultTheme !== ORIGINAL_THEME) {
this.setTheme(this.defaultTheme);
2023-02-15 09:17:05 +08:00
}
},
methods: {
async setTheme(val) {
2023-03-21 00:53:28 +08:00
const oldVal = this.chalk ? this.theme : ORIGINAL_THEME;
if (typeof val !== 'string') return;
const themeCluster = this.getThemeCluster(val.replace('#', ''));
const originalCluster = this.getThemeCluster(oldVal.replace('#', ''));
2023-02-15 09:17:05 +08:00
const getHandler = (variable, id) => {
return () => {
2023-03-21 00:53:28 +08:00
const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''));
const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
let styleTag = document.getElementById(id);
2023-02-15 09:17:05 +08:00
if (!styleTag) {
2023-03-21 00:53:28 +08:00
styleTag = document.createElement('style');
styleTag.setAttribute('id', id);
document.head.appendChild(styleTag);
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
styleTag.innerText = newStyle;
};
};
2023-02-15 09:17:05 +08:00
if (!this.chalk) {
2023-03-21 00:53:28 +08:00
const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`;
await this.getCSSString(url, 'chalk');
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
const chalkHandler = getHandler('chalk', 'chalk-style');
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
chalkHandler();
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
const styles = [].slice.call(document.querySelectorAll('style')).filter((style) => {
const text = style.innerText;
return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text);
});
styles.forEach((style) => {
const { innerText } = style;
if (typeof innerText !== 'string') return;
style.innerText = this.updateStyle(innerText, originalCluster, themeCluster);
});
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
this.$emit('change', val);
2023-02-15 09:17:05 +08:00
},
updateStyle(style, oldCluster, newCluster) {
2023-03-21 00:53:28 +08:00
let newStyle = style;
2023-02-15 09:17:05 +08:00
oldCluster.forEach((color, index) => {
2023-03-21 00:53:28 +08:00
newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index]);
});
return newStyle;
2023-02-15 09:17:05 +08:00
},
getCSSString(url, variable) {
2023-03-21 00:53:28 +08:00
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
2023-02-15 09:17:05 +08:00
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
2023-03-21 00:53:28 +08:00
this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '');
resolve();
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
};
xhr.open('GET', url);
xhr.send();
});
2023-02-15 09:17:05 +08:00
},
getThemeCluster(theme) {
const tintColor = (color, tint) => {
2023-03-21 00:53:28 +08:00
let red = parseInt(color.slice(0, 2), 16);
let green = parseInt(color.slice(2, 4), 16);
let blue = parseInt(color.slice(4, 6), 16);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
if (tint === 0) {
// when primary color is in its rgb space
return [red, green, blue].join(',');
2023-02-15 09:17:05 +08:00
} else {
2023-03-21 00:53:28 +08:00
red += Math.round(tint * (255 - red));
green += Math.round(tint * (255 - green));
blue += Math.round(tint * (255 - blue));
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
red = red.toString(16);
green = green.toString(16);
blue = blue.toString(16);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
return `#${red}${green}${blue}`;
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
const shadeColor = (color, shade) => {
2023-03-21 00:53:28 +08:00
let red = parseInt(color.slice(0, 2), 16);
let green = parseInt(color.slice(2, 4), 16);
let blue = parseInt(color.slice(4, 6), 16);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
red = Math.round((1 - shade) * red);
green = Math.round((1 - shade) * green);
blue = Math.round((1 - shade) * blue);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
red = red.toString(16);
green = green.toString(16);
blue = blue.toString(16);
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
return `#${red}${green}${blue}`;
};
2023-02-15 09:17:05 +08:00
2023-03-21 00:53:28 +08:00
const clusters = [theme];
2023-02-15 09:17:05 +08:00
for (let i = 0; i <= 9; i++) {
2023-03-21 00:53:28 +08:00
clusters.push(tintColor(theme, Number((i / 10).toFixed(2))));
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
clusters.push(shadeColor(theme, 0.1));
return clusters;
2023-02-15 09:17:05 +08:00
}
}
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
</script>
<style>
.theme-message,
.theme-picker-dropdown {
z-index: 99999 !important;
}
.theme-picker .el-color-picker__trigger {
height: 26px !important;
width: 26px !important;
padding: 2px;
}
.theme-picker-dropdown .el-color-dropdown__link-btn {
display: none;
}
</style>