mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-21 13:59:03 +08:00
修复主题切换后事件解析异常导致崩溃
This commit is contained in:
@@ -18,13 +18,15 @@ export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ childre
|
|||||||
const currentThemeRef = useRef<themeConfig | null>(null)
|
const currentThemeRef = useRef<themeConfig | null>(null)
|
||||||
|
|
||||||
const applyThemeConfig = useCallback((theme: themeConfig) => {
|
const applyThemeConfig = useCallback((theme: themeConfig) => {
|
||||||
const { tdesign, custom } = theme.config
|
if (!theme) return
|
||||||
|
const tdesign = theme.config?.tdesign || {}
|
||||||
|
const custom = theme.config?.custom || {}
|
||||||
const root = document.documentElement
|
const root = document.documentElement
|
||||||
const prevKeys = appliedStyleKeysRef.current
|
const prevKeys = appliedStyleKeysRef.current
|
||||||
for (const k of prevKeys) root.style.removeProperty(k)
|
for (const k of prevKeys) root.style.removeProperty(k)
|
||||||
const nextKeys: string[] = []
|
const nextKeys: string[] = []
|
||||||
|
|
||||||
root.setAttribute("theme-mode", theme.mode)
|
root.setAttribute("theme-mode", theme.mode || "light")
|
||||||
|
|
||||||
const brandColor = tdesign.brandColor || "#1677FF"
|
const brandColor = tdesign.brandColor || "#1677FF"
|
||||||
const hex = brandColor.replace("#", "")
|
const hex = brandColor.replace("#", "")
|
||||||
@@ -38,7 +40,7 @@ export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ childre
|
|||||||
nextKeys.push("--ss-primary-rgb")
|
nextKeys.push("--ss-primary-rgb")
|
||||||
|
|
||||||
if (brandColor) {
|
if (brandColor) {
|
||||||
const colorMap = generateColorMap(brandColor, theme.mode)
|
const colorMap = generateColorMap(brandColor, theme.mode || "light")
|
||||||
Object.entries(colorMap).forEach(([key, value]) => {
|
Object.entries(colorMap).forEach(([key, value]) => {
|
||||||
root.style.setProperty(key, value)
|
root.style.setProperty(key, value)
|
||||||
nextKeys.push(key)
|
nextKeys.push(key)
|
||||||
|
|||||||
@@ -53,8 +53,13 @@ const api = {
|
|||||||
deleteTheme: (themeId: string): Promise<{ success: boolean }> =>
|
deleteTheme: (themeId: string): Promise<{ success: boolean }> =>
|
||||||
invoke("theme_delete", { themeId }),
|
invoke("theme_delete", { themeId }),
|
||||||
onThemeChanged: (callback: (theme: themeConfig) => void): Promise<UnlistenFn> => {
|
onThemeChanged: (callback: (theme: themeConfig) => void): Promise<UnlistenFn> => {
|
||||||
return listen<{ theme: themeConfig }>("theme:updated", (event) => {
|
return listen<themeConfig | { theme?: themeConfig }>("theme:updated", (event) => {
|
||||||
callback(event.payload.theme)
|
const payload = event.payload as themeConfig | { theme?: themeConfig } | undefined
|
||||||
|
const theme =
|
||||||
|
payload && typeof payload === "object" && "theme" in payload
|
||||||
|
? payload.theme
|
||||||
|
: (payload as themeConfig | undefined)
|
||||||
|
if (theme) callback(theme)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user