Skip to content

Commit 168d3e6

Browse files
committed
fix(locales): 🐛 修复刷新 灰色模式 色弱模式不生效
1 parent 11d2c7c commit 168d3e6

File tree

9 files changed

+84
-91
lines changed

9 files changed

+84
-91
lines changed

index.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@
172172
if (primaryColor) {
173173
const loading = document.getElementById('html_loding');
174174
loading.style.color = primaryColor;
175-
const style = document.getElementById('admin-style-root-color');
176-
style.innerHTML = `html:root{ --el-color-primary: ${primaryColor};\n}`;
177-
// const body = document.documentElement;
178-
// body.style.setProperty('--el-color-primary', primaryColor);
179175
}
180176
}
181177
</script>

src/components/Application/AppTheme.vue

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22
import { useColorMode } from '@vueuse/core';
33
import { watch } from 'vue';
44
import SvgIcon from '../SvgIcon/index.vue';
5-
import { useTransformTheme } from '@/hooks/useTransformTheme';
65
import { useRootSetting } from '@/hooks/setting/useRootSetting';
6+
import type { AppConfig } from '@/store/types';
7+
import { updateColor } from '@/utils/theme/transformTheme';
78
89
const color = useColorMode();
910
10-
const { setAppConfigMode } = useRootSetting();
11-
12-
const { updateColor } = useTransformTheme();
11+
const { appConfig, setAppConfigMode } = useRootSetting();
1312
1413
const toggleDarkMode = () => {
15-
setAppConfigMode({ themeMode: color.value });
14+
setAppConfigMode({ themeMode: color.value as AppConfig['themeMode'] });
1615
};
1716
18-
watch(
19-
color,
20-
() => {
21-
toggleDarkMode();
22-
updateColor();
23-
},
24-
{ immediate: true },
25-
);
17+
watch(color, () => {
18+
toggleDarkMode();
19+
updateColor(appConfig.value.primaryColor, color.value as AppConfig['themeMode']);
20+
});
2621
</script>
2722

2823
<template>

src/config/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { App } from 'vue';
22
import { getConfigInfo } from '@/server/config';
33
import type { AppConfig } from '@/store/types';
44
import { setStorageConfig } from '@/utils/storage';
5+
import { configTheme } from '@/utils/theme/transformTheme';
56

67
let config: AppConfig = {} as AppConfig;
78

@@ -23,6 +24,7 @@ export async function getServerConfig(app: App): Promise<AppConfig> {
2324
throw `\npublic文件夹下无法查找到serverConfig配置文件\nUnable to find serverconfig configuration file under public folder`;
2425
}
2526
}
27+
configTheme(config);
2628
setStorageConfig({ ...config.StorageConfig, prefix: config.title });
2729
app.config.globalProperties.$config = getConfig();
2830
return config;

src/hooks/useTransformTheme.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/layouts/pageLayouts/components/Seting/ThemeSettings/index.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<script setup lang="ts">
22
import { ref, watch } from 'vue';
3-
import { useTransformTheme } from '@/hooks/useTransformTheme';
43
import SvgIcon from '@/components/SvgIcon/index.vue';
54
import type { AppConfig } from '@/store/types';
65
import { useRootSetting } from '@/hooks/setting/useRootSetting';
7-
8-
const { updateColor, themeHtmlClassName } = useTransformTheme();
6+
import { themeHtmlClassName, updateColor } from '@/utils/theme/transformTheme';
97
108
const { appConfig, setAppConfigMode } = useRootSetting();
119
@@ -17,7 +15,7 @@
1715
1816
watch([pureColor], () => {
1917
setAppConfigMode({ primaryColor: pureColor.value });
20-
updateColor();
18+
updateColor(pureColor.value, appConfig.value.themeMode);
2119
});
2220
2321
const htmlGrey = ref<boolean>(greyMode || false);

src/layouts/pageLayouts/components/Seting/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<div class="setting">
5757
<el-drawer
5858
v-model="drawer"
59-
custom-class="setting-drawer"
6059
:title="$t('layout.setup')"
6160
:size="320"
6261
@close="emit('update:modelValue', false)"

src/layouts/pageLayouts/components/SideNavigationBar/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
v-if="isPhoneScreen"
5454
v-model="drawer"
5555
:with-header="false"
56-
custom-class="drawer-sidebar"
56+
class="drawer-sidebar"
5757
direction="ltr"
5858
:before-close="handleClose"
5959
>

src/store/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface AppConfig {
1515
collapseMenu: boolean;
1616
// 菜单显示模式: 'vertical':左侧模式 | 'horizontal':顶部模式 | 'blend':混合模式
1717
sidebarMode: SidebarMode;
18-
// 主题模式:夜间主题、白天主题
19-
themeMode: string;
18+
// 主题模式:白天主题、夜间主题
19+
themeMode: 'light' | 'dark';
2020
// 国际化
2121
locale: string;
2222
// storage配置

src/utils/theme/transformTheme.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { AppConfig } from '@/store/types';
2+
3+
const body = document.documentElement as HTMLElement;
4+
5+
// hex转rgb
6+
function hexToRgb(str: string) {
7+
const hxs: string[] = str.replace('#', '').match(/../g) || [];
8+
const newHxs: number[] = [];
9+
for (let i = 0; i < 3; i++) newHxs[i] = parseInt(hxs[i], 16);
10+
return newHxs;
11+
}
12+
13+
// rgb转hex
14+
function rgbToHex(a: number, b: number, c: number) {
15+
const hexs = [a.toString(16), b.toString(16), c.toString(16)];
16+
for (let i = 0; i < 3; i++) {
17+
if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`;
18+
}
19+
return `#${hexs.join('')}`;
20+
}
21+
22+
// 参考element style 计算
23+
function mix(color1: string, color2: string, weight: number) {
24+
weight = Math.max(Math.min(Number(weight), 1), 0);
25+
const mainColor = hexToRgb(color1);
26+
const mixColor = hexToRgb(color2);
27+
const hex = [];
28+
for (let i = 0; i < mainColor.length; i++)
29+
hex[i] = Math.round(mainColor[i] * (1 - weight) + mixColor[i] * weight);
30+
return rgbToHex(hex[0], hex[1], hex[2]);
31+
}
32+
33+
export function updateColor(primaryColor: string, themeMode: 'light' | 'dark') {
34+
if (!primaryColor) return;
35+
36+
const style = document.getElementById('admin-style-root-color');
37+
38+
const mixColor = themeMode === 'dark' ? '#141414' : '#ffffff';
39+
let innerHTML = `html${
40+
themeMode === 'dark' ? '.dark' : ''
41+
}:root{ --el-color-primary: ${primaryColor};\n`;
42+
43+
// body.style.setProperty('--el-color-primary', primaryColor);
44+
for (let i = 1; i <= 9; i++) {
45+
// body.style.setProperty(`--el-color-primary-light-${i}`, mix(primaryColor, mixWhite, i * 0.1));
46+
innerHTML += `--el-color-primary-light-${i}: ${mix(primaryColor, mixColor, i * 0.1)};\n`;
47+
}
48+
49+
if (style) style.innerHTML = innerHTML + '}';
50+
}
51+
52+
export function themeHtmlClassName(className: string, isShow: boolean) {
53+
if (isShow) {
54+
body.classList.add(className);
55+
} else {
56+
body.classList.remove(className);
57+
}
58+
}
59+
60+
export function configTheme(appConfig: AppConfig) {
61+
if (!appConfig) return;
62+
const { primaryColor, themeMode, colorWeaknessMode, greyMode } = appConfig;
63+
64+
updateColor(primaryColor, themeMode);
65+
if (greyMode || colorWeaknessMode) {
66+
if (greyMode) themeHtmlClassName('html-grey', greyMode);
67+
else if (colorWeaknessMode) themeHtmlClassName('html-weakness', colorWeaknessMode);
68+
}
69+
}

0 commit comments

Comments
 (0)