Add Reset button to all themes with default value snapshot

main
Warinyourself 3 weeks ago
parent fd711496c5
commit 5ab9be1053

@ -7,6 +7,14 @@ import { randomizeSettingsTheme as buildRandomThemeSettings } from '@/utils/them
import { setCSSVariable } from '@/utils/dom'
import { AppThemes, defaultTheme } from '@/utils/constant'
// Snapshot of default input values captured before any mutations
const _defaults = new Map<string, Record<string, AppInputThemeValue>>(
AppThemes.map((theme) => [
theme.name,
Object.fromEntries((theme.settings || []).map((input) => [input.name, input.value]))
])
)
export const useThemeStore = defineStore('theme', () => {
const currentTheme = ref('')
const themes = ref(AppThemes as AppTheme[])
@ -26,6 +34,17 @@ export const useThemeStore = defineStore('theme', () => {
theme.settings = buildRandomThemeSettings(theme)
}
function resetTheme() {
const defaults = _defaults.get(currentTheme.value)
if (!defaults) return
const target = themes.value.find(({ name }) => name === currentTheme.value)
if (!target?.settings) return
target.settings = target.settings.map((input) => ({
...input,
value: defaults[input.name] ?? input.value
}))
}
async function changeTheme(themeName: string, themeSettings?: AppTheme['settings']) {
const isExistTheme = getThemeByName(themeName)
const finalTheme = isExistTheme ? themeName : themes.value[0]!.name
@ -88,6 +107,7 @@ export const useThemeStore = defineStore('theme', () => {
getThemeByName,
getThemeInput,
randomizeSettingsTheme,
resetTheme,
changeTheme,
changeSettingsTheme,
changeThemeInput,

@ -43,6 +43,16 @@ export const randomButton: AppInputButton = {
}
}
export const resetButton: AppInputButton = {
name: 'button',
value: 'button',
label: 'input.reset',
type: 'button',
callback() {
void import('@/store/theme').then(({ useThemeStore }) => useThemeStore().resetTheme())
}
}
export function buildInputSlider({
name = 'animation-speed',
value = 5,

Loading…
Cancel
Save