From c7d33030b1f0f4e02fa9e49e102ab5b0afff81b5 Mon Sep 17 00:00:00 2001 From: Novout Date: Tue, 16 Nov 2021 23:25:51 -0300 Subject: [PATCH] feat(editor): detect system preferred color scheme --- src/plugin/theme/external.ts | 25 ++++++++++++++++++++++--- src/plugin/theme/utils.ts | 3 ++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/plugin/theme/external.ts b/src/plugin/theme/external.ts index ecf1c4b33..c0fcb5afc 100644 --- a/src/plugin/theme/external.ts +++ b/src/plugin/theme/external.ts @@ -3,8 +3,9 @@ import { onAfterMounted } from '../core/cycle' import { ThemeNormalize } from './utils' import { useProjectStore } from '@/store/project' import { useEnv } from '@/use/env' -import { useDefines } from '../../use/defines' +import { useDefines } from '@/use/defines' import { BetterWriteThemes } from '@/types/editor' +import { ColorSchemeType, usePreferredColorScheme } from '@vueuse/core' export const setTheme = () => { onAfterMounted(() => { @@ -17,11 +18,10 @@ export const setThemeInvokate = () => { const PROJECT = useProjectStore() const env = useEnv() - const defines = useDefines() let theme = EDITOR.configuration.theme - if (PROJECT.name === env.projectEmpty()) theme = defines.themes()[1] + if (PROJECT.name === env.projectEmpty()) theme = setDefaultColorTheme() const value = ThemeNormalize(theme) @@ -29,6 +29,25 @@ export const setThemeInvokate = () => { document.body.classList.add(value) } +export const setDefaultColorTheme = (): BetterWriteThemes => { + const defines = useDefines() + const color = usePreferredColorScheme() + + let _color = defines.themes()[1] // for any case, dark theme + + switch (color.value) { + case 'light': + _color = defines.themes()[0] // white theme + break + case 'dark': + case 'no-preference': + _color = defines.themes()[1] // dark theme + break + } + + return _color +} + export const setEditorLogo = (theme: BetterWriteThemes) => { let logo = '' diff --git a/src/plugin/theme/utils.ts b/src/plugin/theme/utils.ts index 060530c37..76548372e 100644 --- a/src/plugin/theme/utils.ts +++ b/src/plugin/theme/utils.ts @@ -1,4 +1,5 @@ import { BetterWriteThemes } from '@/types/editor' +import { setDefaultColorTheme } from './external' export const ThemeNormalize = (theme: BetterWriteThemes) => { return ( @@ -8,6 +9,6 @@ export const ThemeNormalize = (theme: BetterWriteThemes) => { 'BetterWrite - Rise': 'betterwrite-rise', 'BetterWrite - Ascend': 'betterwrite-ascend', 'BetterWrite - Harmonic': 'betterwrite-harmonic', - }[theme] || 'betterwrite-dark' + }[theme] || setDefaultColorTheme() ) }