refactor: load and save settings from a single object
This commit is contained in:
parent
cd1f294600
commit
5b4c278b83
4 changed files with 49 additions and 13 deletions
18
app.vue
18
app.vue
|
@ -8,6 +8,7 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import ContextMenu from '~/components/ContextMenu.vue';
|
import ContextMenu from '~/components/ContextMenu.vue';
|
||||||
import { render } from 'vue';
|
import { render } from 'vue';
|
||||||
|
import settingLoad from './utils/settingLoad';
|
||||||
|
|
||||||
const banner = useState("banner", () => false);
|
const banner = useState("banner", () => false);
|
||||||
|
|
||||||
|
@ -43,21 +44,14 @@ function contextMenuHandler(e: MouseEvent) {
|
||||||
//]);
|
//]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentTheme = "dark" // default theme
|
const currentTheme = settingLoad("selectedThemeUrl") ?? "dark"
|
||||||
const savedTheme = localStorage.getItem("selectedTheme");
|
|
||||||
if (savedTheme) {
|
|
||||||
currentTheme = savedTheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseURL = useRuntimeConfig().app.baseURL;
|
const baseURL = useRuntimeConfig().app.baseURL;
|
||||||
|
|
||||||
useHead({
|
useHead({
|
||||||
link: [
|
link: [{
|
||||||
{
|
rel: "stylesheet",
|
||||||
rel: "stylesheet",
|
href: `${baseURL}themes/${currentTheme}.css`
|
||||||
href: `${baseURL}themes/${currentTheme}.css`
|
}]
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
import settingSave from '~/utils/settingSave';
|
||||||
|
|
||||||
const runtimeConfig = useRuntimeConfig()
|
const runtimeConfig = useRuntimeConfig()
|
||||||
const defaultThemes = runtimeConfig.public.defaultThemes
|
const defaultThemes = runtimeConfig.public.defaultThemes
|
||||||
const baseURL = runtimeConfig.app.baseURL;
|
const baseURL = runtimeConfig.app.baseURL;
|
||||||
|
@ -46,7 +48,7 @@ function changeTheme(id: string, url: string) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem("selectedTheme", id);
|
settingSave("selectedThemeUrl", id)
|
||||||
|
|
||||||
// if the theme didn't originally load for some reason, create it
|
// if the theme didn't originally load for some reason, create it
|
||||||
if (!themeLinkElement) {
|
if (!themeLinkElement) {
|
||||||
|
|
19
utils/settingLoad.ts
Normal file
19
utils/settingLoad.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
export default (key: string): any => {
|
||||||
|
let clientSettingsItem: string | null = localStorage.getItem("clientSettings")
|
||||||
|
if (typeof clientSettingsItem != "string") {
|
||||||
|
clientSettingsItem = "{}"
|
||||||
|
}
|
||||||
|
|
||||||
|
let clientSettings: { [key: string]: any } = {}
|
||||||
|
try {
|
||||||
|
clientSettings = JSON.parse(clientSettingsItem)
|
||||||
|
} catch {
|
||||||
|
clientSettings = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof clientSettings !== "object") {
|
||||||
|
clientSettings = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientSettings[key]
|
||||||
|
}
|
21
utils/settingSave.ts
Normal file
21
utils/settingSave.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
export default (key: string, value: any): void => {
|
||||||
|
let clientSettingsItem: string | null = localStorage.getItem("clientSettings")
|
||||||
|
if (typeof clientSettingsItem != "string") {
|
||||||
|
clientSettingsItem = "{}"
|
||||||
|
}
|
||||||
|
|
||||||
|
let clientSettings: { [key: string]: any } = {}
|
||||||
|
try {
|
||||||
|
clientSettings = JSON.parse(clientSettingsItem)
|
||||||
|
} catch {
|
||||||
|
clientSettings = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof clientSettings !== "object") {
|
||||||
|
clientSettings = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
clientSettings[key] = value
|
||||||
|
|
||||||
|
localStorage.setItem("clientSettings", JSON.stringify(clientSettings))
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue