koel/resources/assets/js/stores/themeStore.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-07-04 19:57:32 +02:00
import { clone } from 'lodash'
2022-04-22 00:51:48 +02:00
import { reactive } from 'vue'
2022-04-24 11:50:45 +03:00
import { preferenceStore as preferences } from '@/stores'
2022-07-04 19:57:32 +02:00
import themes from '@/themes'
2022-04-20 11:34:15 +02:00
2022-04-15 16:24:30 +02:00
export const themeStore = {
2022-07-04 19:47:46 +02:00
defaultProperties: {
'--color-text-primary': undefined,
'--color-text-secondary': undefined,
'--color-bg-primary': undefined,
'--color-bg-secondary': undefined,
'--color-highlight': undefined,
'--bg-image': undefined,
'--bg-position': undefined,
'--bg-attachment': undefined,
'--bg-size': undefined
2022-07-25 10:35:15 +02:00
} as Record<ThemeableProperty, string | undefined>,
2022-07-04 19:47:46 +02:00
2022-04-22 00:51:48 +02:00
state: reactive({
2022-07-04 19:57:32 +02:00
themes
2022-04-22 00:51:48 +02:00
}),
2022-04-15 16:24:30 +02:00
init () {
2022-07-04 19:47:46 +02:00
for (let key in this.defaultProperties) {
this.defaultProperties[key] = document.documentElement.style.getPropertyValue(key)
}
2022-04-15 16:24:30 +02:00
this.applyThemeFromPreference()
},
setTheme (theme: Theme) {
document.documentElement.setAttribute('data-theme', theme.id)
2022-07-04 19:47:46 +02:00
let properties = Object.assign(clone(this.defaultProperties), theme.properties ?? {})
2022-04-15 16:24:30 +02:00
2022-07-04 19:47:46 +02:00
for (let key in properties) {
document.documentElement.style.setProperty(key, properties[key])
}
preferences.theme = theme.id
2022-04-22 00:51:48 +02:00
this.state.themes.forEach(t => (t.selected = t.id === theme.id))
2022-04-15 16:24:30 +02:00
},
2024-01-24 23:39:47 +01:00
getThemeById (id: Theme['id']) {
2022-04-15 16:24:30 +02:00
return this.state.themes.find(theme => theme.id === id)
},
2024-07-05 14:20:30 +02:00
getDefaultTheme () {
2022-04-15 16:24:30 +02:00
return this.getThemeById('classic')!
},
2024-07-05 14:20:30 +02:00
applyThemeFromPreference () {
2022-04-15 16:24:30 +02:00
const theme = preferences.theme
? (this.getThemeById(preferences.theme) ?? this.getDefaultTheme())
: this.getDefaultTheme()
this.setTheme(theme)
}
}