Skip to content

Commit

Permalink
修复主题问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lvzhenbo committed Sep 23, 2024
1 parent 177ed26 commit 5cafe05
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [滚动条在线编辑器](https://lvzhenbo.github.io/scrollbar-editor/)
# [滚动条编辑器](https://lvzhenbo.github.io/scrollbar-editor/)

[Scrollbar.app](https://github.com/henripar/scrollbar) 的中文优化版,在原版的基础上添加了自定义选择器名称和交汇处颜色
[Scrollbar.app](https://github.com/henripar/scrollbar) 的中文优化版,在原版的基础上添加了自定义选择器名称和交汇处颜色以及默认自动跟随系统主题功能(可自己手动切换,暂时不加入持久化)

## 推荐 IDE 设置

Expand Down
31 changes: 25 additions & 6 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
<script setup lang="ts">
import { zhCN, dateZhCN, useOsTheme, darkTheme, lightTheme } from 'naive-ui';
import { zhCN, dateZhCN, useOsTheme, darkTheme, lightTheme, type GlobalTheme } from 'naive-ui';
import hljs from 'highlight.js/lib/core';
import css from 'highlight.js/lib/languages/css';
import { osThemeKey, switchThemeKey } from './utils';
import { switchThemeKey } from './utils';
hljs.registerLanguage('css', css);
const osThemeRef = useOsTheme();
const theme = computed(() => {
return osThemeRef.value === 'dark' && switchTheme.value ? darkTheme : lightTheme;
});
const theme = ref<GlobalTheme>(lightTheme);
const switchTheme = ref(false);
onMounted(() => {
setTheme();
});
const setTheme = (val?: boolean) => {
if (val === true) {
theme.value = darkTheme;
} else if (val === false) {
theme.value = lightTheme;
} else {
theme.value = osThemeRef.value === 'dark' ? darkTheme : lightTheme;
}
};
watch(
osThemeRef,
(val) => {
if (val === 'dark') {
switchTheme.value = true;
setTheme(true);
} else {
switchTheme.value = false;
setTheme(false);
}
},
{ immediate: true },
);
provide(osThemeKey, osThemeRef);
watch(switchTheme, (val) => {
setTheme(val);
});
provide(switchThemeKey, switchTheme);
</script>

Expand Down
5 changes: 2 additions & 3 deletions src/components/Content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@
import { useClipboard } from '@vueuse/core';
import { WeatherMoon16Regular, WeatherSunny16Regular } from '@vicons/fluent';
import { GithubFilled } from '@vicons/antd';
import { osThemeKey, switchThemeKey } from '@/utils';
import { switchThemeKey } from '@/utils';
const osThemeRef = inject(osThemeKey);
const switchTheme = inject(switchThemeKey);
const codeBg = computed(() => {
return osThemeRef?.value === 'dark' && switchTheme?.value ? 'bg-slate-950' : 'bg-slate-50';
return switchTheme?.value ? 'bg-slate-950' : 'bg-slate-50';
});
const message = useMessage();
const { copy } = useClipboard();
Expand Down
5 changes: 0 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import { useOsTheme } from 'naive-ui';

const osThemeRef = useOsTheme();

export const osThemeKey = Symbol() as InjectionKey<typeof osThemeRef>;
export const switchThemeKey = Symbol() as InjectionKey<Ref<boolean>>;

0 comments on commit 5cafe05

Please sign in to comment.