mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-12-05 07:46:03 +08:00
feat(projects): support theme perset to override component library presets.
This commit is contained in:
@@ -33,6 +33,8 @@ type ThemePreset = Pick<
|
|||||||
desc: string;
|
desc: string;
|
||||||
i18nkey?: string;
|
i18nkey?: string;
|
||||||
version: string;
|
version: string;
|
||||||
|
/** Optional NaiveUI theme overrides */
|
||||||
|
naiveui?: App.Theme.NaiveUIThemeOverride;
|
||||||
};
|
};
|
||||||
|
|
||||||
const presetModules = import.meta.glob('@/theme/preset/*.json', { eager: true, import: 'default' });
|
const presetModules = import.meta.glob('@/theme/preset/*.json', { eager: true, import: 'default' });
|
||||||
@@ -80,7 +82,7 @@ const getPresetDesc = (preset: ThemePreset): string => {
|
|||||||
|
|
||||||
const applyPreset = (preset: ThemePreset): void => {
|
const applyPreset = (preset: ThemePreset): void => {
|
||||||
const mergedPreset = defu(preset, themeSettings);
|
const mergedPreset = defu(preset, themeSettings);
|
||||||
const { themeScheme, grayscale, colourWeakness, layout, watermark, ...rest } = mergedPreset;
|
const { themeScheme, grayscale, colourWeakness, layout, watermark, naiveui, ...rest } = mergedPreset;
|
||||||
themeStore.setThemeScheme(themeScheme);
|
themeStore.setThemeScheme(themeScheme);
|
||||||
themeStore.setGrayscale(grayscale);
|
themeStore.setGrayscale(grayscale);
|
||||||
themeStore.setColourWeakness(colourWeakness);
|
themeStore.setColourWeakness(colourWeakness);
|
||||||
@@ -100,6 +102,9 @@ const applyPreset = (preset: ThemePreset): void => {
|
|||||||
tokens: { ...rest.tokens }
|
tokens: { ...rest.tokens }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Apply NaiveUI theme overrides if present
|
||||||
|
themeStore.setNaiveThemeOverrides(naiveui);
|
||||||
|
|
||||||
window.$message?.success($t('theme.appearance.preset.applySuccess'));
|
window.$message?.success($t('theme.appearance.preset.applySuccess'));
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
|
|||||||
/** Theme settings */
|
/** Theme settings */
|
||||||
const settings: Ref<App.Theme.ThemeSetting> = ref(initThemeSettings());
|
const settings: Ref<App.Theme.ThemeSetting> = ref(initThemeSettings());
|
||||||
|
|
||||||
|
/** Optional NaiveUI theme overrides from preset */
|
||||||
|
const naiveThemeOverrides: Ref<App.Theme.NaiveUIThemeOverride | undefined> = ref(undefined);
|
||||||
|
|
||||||
/** Watermark time instance with controls */
|
/** Watermark time instance with controls */
|
||||||
const { now: watermarkTime, pause: pauseWatermarkTime, resume: resumeWatermarkTime } = useNow({ controls: true });
|
const { now: watermarkTime, pause: pauseWatermarkTime, resume: resumeWatermarkTime } = useNow({ controls: true });
|
||||||
|
|
||||||
@@ -53,7 +56,7 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/** Naive theme */
|
/** Naive theme */
|
||||||
const naiveTheme = computed(() => getNaiveTheme(themeColors.value, settings.value));
|
const naiveTheme = computed(() => getNaiveTheme(themeColors.value, settings.value, naiveThemeOverrides.value));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Settings json
|
* Settings json
|
||||||
@@ -198,6 +201,15 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set NaiveUI theme overrides
|
||||||
|
*
|
||||||
|
* @param overrides NaiveUI theme overrides or undefined to clear
|
||||||
|
*/
|
||||||
|
function setNaiveThemeOverrides(overrides?: App.Theme.NaiveUIThemeOverride) {
|
||||||
|
naiveThemeOverrides.value = overrides;
|
||||||
|
}
|
||||||
|
|
||||||
/** Only run timer when watermark is visible and time display is enabled */
|
/** Only run timer when watermark is visible and time display is enabled */
|
||||||
function updateWatermarkTimer() {
|
function updateWatermarkTimer() {
|
||||||
const { watermark } = settings.value;
|
const { watermark } = settings.value;
|
||||||
@@ -284,6 +296,7 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
|
|||||||
updateThemeColors,
|
updateThemeColors,
|
||||||
setThemeLayout,
|
setThemeLayout,
|
||||||
setWatermarkEnableUserName,
|
setWatermarkEnableUserName,
|
||||||
setWatermarkEnableTime
|
setWatermarkEnableTime,
|
||||||
|
setNaiveThemeOverrides
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -236,11 +236,15 @@ function getNaiveThemeColors(colors: App.Theme.ThemeColor, recommended = false)
|
|||||||
/**
|
/**
|
||||||
* Get naive theme
|
* Get naive theme
|
||||||
*
|
*
|
||||||
* @param settings Theme settings object.
|
* @param colors Theme colors
|
||||||
* @param settings.recommendColor Whether to use recommended color palette.
|
* @param settings Theme settings object
|
||||||
* @param settings.themeRadius Border radius to use in the theme (in px).
|
* @param overrides Optional manual overrides from preset
|
||||||
*/
|
*/
|
||||||
export function getNaiveTheme(colors: App.Theme.ThemeColor, settings: App.Theme.ThemeSetting) {
|
export function getNaiveTheme(
|
||||||
|
colors: App.Theme.ThemeColor,
|
||||||
|
settings: App.Theme.ThemeSetting,
|
||||||
|
overrides?: GlobalThemeOverrides
|
||||||
|
) {
|
||||||
const { primary: colorLoading } = colors;
|
const { primary: colorLoading } = colors;
|
||||||
|
|
||||||
const theme: GlobalThemeOverrides = {
|
const theme: GlobalThemeOverrides = {
|
||||||
@@ -256,5 +260,7 @@ export function getNaiveTheme(colors: App.Theme.ThemeColor, settings: App.Theme.
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return theme;
|
// If there are overrides, merge them with priority
|
||||||
|
// overrides has higher priority than auto-generated theme
|
||||||
|
return overrides ? defu(overrides, theme) : theme;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,5 +34,19 @@
|
|||||||
"base-text": "rgb(224, 224, 224)"
|
"base-text": "rgb(224, 224, 224)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"naiveui": {
|
||||||
|
"Alert": {
|
||||||
|
"borderRadiusMedium": "12px",
|
||||||
|
"fontWeightStrong": "600",
|
||||||
|
"paddingMedium": "0 20px"
|
||||||
|
},
|
||||||
|
"Card": {
|
||||||
|
"borderRadius": "16px",
|
||||||
|
"paddingMedium": "24px"
|
||||||
|
},
|
||||||
|
"Input": {
|
||||||
|
"borderRadius": "10px"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/typings/app.d.ts
vendored
3
src/typings/app.d.ts
vendored
@@ -4,6 +4,9 @@ declare namespace App {
|
|||||||
namespace Theme {
|
namespace Theme {
|
||||||
type ColorPaletteNumber = import('@sa/color').ColorPaletteNumber;
|
type ColorPaletteNumber = import('@sa/color').ColorPaletteNumber;
|
||||||
|
|
||||||
|
/** NaiveUI theme overrides that can be specified in preset */
|
||||||
|
type NaiveUIThemeOverride = import('naive-ui').GlobalThemeOverrides;
|
||||||
|
|
||||||
/** Theme setting */
|
/** Theme setting */
|
||||||
interface ThemeSetting {
|
interface ThemeSetting {
|
||||||
/** Theme scheme */
|
/** Theme scheme */
|
||||||
|
|||||||
Reference in New Issue
Block a user