starter\magzine\gitbook 主题dashboard元素

This commit is contained in:
tangly1024.com
2024-11-19 19:00:57 +08:00
parent ed947d866e
commit 2988426a2f
19 changed files with 1018 additions and 81 deletions

View File

@@ -8,17 +8,50 @@ import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'
export const { THEMES = [] } = getConfig().publicRuntimeConfig
/**
* 获取主配置
* 获取主配置
* @param {string} themeQuery - 主题查询参数(支持多个主题用逗号分隔)
* @returns {Promise<object>} 主题配置对象
*/
export const getThemeConfig = async themeQuery => {
if (themeQuery && themeQuery !== BLOG.THEME) {
const THEME_CONFIG = await import(`@/themes/${themeQuery}`).then(
m => m.THEME_CONFIG
)
return THEME_CONFIG
} else {
return ThemeComponents?.THEME_CONFIG
// 如果 themeQuery 存在且不等于默认主题,处理多主题情况
if (typeof themeQuery === 'string' && themeQuery.trim()) {
// 取 themeQuery 中第一个主题(以逗号为分隔符)
const themeName = themeQuery.split(',')[0].trim()
// 如果 themeQuery 不等于当前默认主题,则加载指定主题的配置
if (themeName !== BLOG.THEME) {
try {
// 动态导入主题配置
const THEME_CONFIG = await import(`@/themes/${themeName}`)
.then(m => m.THEME_CONFIG)
.catch(err => {
console.error(`Failed to load theme ${themeName}:`, err)
return null // 主题加载失败时返回 null 或者其他默认值
})
// 如果主题配置加载成功,返回配置
if (THEME_CONFIG) {
return THEME_CONFIG
} else {
// 如果加载失败,返回默认主题配置
console.warn(
`Loading ${themeName} failed. Falling back to default theme.`
)
return ThemeComponents?.THEME_CONFIG
}
} catch (error) {
// 如果 import 过程中出现异常,返回默认主题配置
console.error(
`Error loading theme configuration for ${themeName}:`,
error
)
return ThemeComponents?.THEME_CONFIG
}
}
}
// 如果没有 themeQuery 或 themeQuery 与默认主题相同,返回默认主题配置
return ThemeComponents?.THEME_CONFIG
}
/**