Skip to content

Commit c6b3f02

Browse files
committed
颜色主题记忆
1 parent be25b98 commit c6b3f02

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/App.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<template>
2-
<s-page theme="auto" id="soberpage" ref="soberPage" :class="theme.value">
2+
<s-page :theme="theme.initialValue" id="soberpage" ref="soberPage" :class="theme.value">
33
<s-drawer>
44
<Navbar @toggle-drawer="toogleDrawer" />
55
<s-drawer
6-
theme="auto"
76
id="content"
87
:class="{ onDrawerResize }"
98
ref="innerDrawer"

src/consts.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,22 +386,34 @@ export const fnTypeArr = [
386386
},
387387
] as const satisfies FnType[];
388388

389-
390389
// Datum define
391390
import { defineStore } from "pinia";
392391
import { computed, ref } from "vue";
393392
export const useProfile = defineStore("profile", () => {
394393
const data = ref<InternalDatum[]>([
395394
{ fnType: "linear", graphType: "polyline", fn: "x^2", key: 1 },
396395
]);
397-
const getOriginalCopy = (forExport?: boolean) => toOriginalDatum(data.value, forExport);
396+
const getOriginalCopy = (forExport?: boolean) =>
397+
toOriginalDatum(data.value, forExport);
398398
return { data, getOriginalCopy };
399399
});
400+
400401
// Theme define
401402
export const useTheme = defineStore("theme", () => {
402403
const themeValues = ["auto", "dark", "light"] as const;
403-
const index = ref(0);
404+
const initialIndex = (() => {
405+
if (typeof localStorage !== "undefined") {
406+
const savedTheme = Number(localStorage.getItem("theme"));
407+
if ([0, 1, 2].includes(savedTheme)) return savedTheme as 0 | 1 | 2;
408+
}
409+
return 0;
410+
})();
411+
const index = ref(initialIndex);
412+
const initialValue = themeValues[initialIndex];
404413
const value = computed(() => themeValues[index.value]);
405-
const toogle = () => index.value = (index.value + 1) % themeValues.length
406-
return { index, value, toogle }
407-
})
414+
const toogle = () => {
415+
index.value = ((index.value + 1) % themeValues.length) as 0 | 1 | 2;
416+
localStorage.setItem("theme", index.value.toString());
417+
};
418+
return { initialIndex,initialValue, index, value, toogle };
419+
});

0 commit comments

Comments
 (0)