Skip to content

Commit 2e0d5df

Browse files
authored
fix(theme): use Pinia appStore as single theme source for editor (#137)
The Monaco editor and markdown code highlighting read theme from a separate useTheme composable (localStorage 'sqlkit-theme', default 'system') instead of the appStore. Every editor mount created fresh composable instances whose onMounted re-applied the auto-detected system theme, overwriting the user's dark/light choice from Settings. Unify on appStore.uiThemeType as the single source of truth: - useMonacoEditor/SQLEditor: derive isDark from appStore.uiThemeType - markdown-render: load atom-one-light/dark highlight theme by theme - getEditorTheme(): fix 'vs-light' to valid Monaco theme 'vs' - remove dead useTheme composable and ThemeToggle component
1 parent ff7d5a5 commit 2e0d5df

8 files changed

Lines changed: 32 additions & 126 deletions

File tree

‎src/components/README.md‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ This directory contains the shadcn-vue UI components used in SQLKit.
1818
Located in `src/components/layout/`:
1919

2020
- **AppLayout** - Main application layout wrapper
21-
- **AppHeader** - Top navigation header with theme toggle
21+
- **AppHeader** - Top navigation header
2222
- **AppSidebar** - Left sidebar navigation
23-
- **ThemeToggle** - Dark/light mode toggle button
2423

2524
## Theme
2625

@@ -50,14 +49,9 @@ import { Input } from '@/components/ui/input'
5049

5150
## Theme System
5251

53-
The theme system uses the `useTheme` composable:
52+
The theme system is managed by the Pinia `appStore` (`src/store/appStore.ts`):
5453

55-
```vue
56-
<script setup lang="ts">
57-
import { useTheme } from '@/composables/useTheme'
58-
59-
const { theme: _theme, isDark: _isDark, setTheme: _setTheme, toggleTheme: _toggleTheme } = useTheme()
60-
</script>
61-
```
62-
63-
Theme preferences are persisted in localStorage and respect system preferences.
54+
- `themeType` (`auto` | `dark` | `light`) is the user preference, persisted via pinia-plugin-persistedstate.
55+
- `uiThemeType` (`dark` | `light`) is the resolved effective theme.
56+
- `applyUiTheme` toggles the `theme` attribute and `.dark` class on `<html>`.
57+
- Monaco editor themes are derived from `appStore.uiThemeType` in `useMonacoEditor`.

‎src/components/SQLEditor.vue‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useI18n } from 'vue-i18n'
77
import { ProgressBar } from '@/components/ui/progress'
88
import { useMonacoEditor } from '@/composables/useMonacoEditor'
99
import { usePlatform } from '@/composables/usePlatform'
10-
import { useTheme } from '@/composables/useTheme'
10+
import { ThemeType, useAppStore } from '@/store/appStore'
1111
1212
type Props = {
1313
modelValue?: string
@@ -48,7 +48,8 @@ const emit = defineEmits<{
4848
}>()
4949
5050
const editorContainer = ref<HTMLElement | null>(null)
51-
const { isDark } = useTheme()
51+
const appStore = useAppStore()
52+
const isDark = computed(() => appStore.uiThemeType === ThemeType.DARK)
5253
const { modifierKey, altKey } = usePlatform()
5354
const { t } = useI18n()
5455

‎src/components/layout/ThemeToggle.vue‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎src/components/markdown-render.vue‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
<script setup lang="ts">
22
import DOMPurify from 'dompurify'
33
import hljs from 'highlight.js'
4+
import hljsThemeDarkUrl from 'highlight.js/styles/atom-one-dark.css?url'
5+
import hljsThemeLightUrl from 'highlight.js/styles/atom-one-light.css?url'
46
import MarkdownIt from 'markdown-it'
57
import taskLists from 'markdown-it-task-lists'
6-
import { onMounted, onUnmounted, ref, watch } from 'vue'
8+
import { onUnmounted, ref, watch } from 'vue'
79
import { useI18n } from 'vue-i18n'
810
import { toast } from '@/composables/useNotifications'
11+
import { ThemeType, useAppStore } from '@/store/appStore'
912
1013
const props = defineProps<{
1114
markdown: string
1215
}>()
1316
1417
const { t } = useI18n()
18+
const appStore = useAppStore()
1519
16-
// Lazy-load highlight.js theme CSS after mount to avoid blocking initial render
17-
let _themeLoaded = false
18-
onMounted(() => {
19-
if (!_themeLoaded) {
20-
_themeLoaded = true
21-
import('highlight.js/styles/atom-one-dark.css')
20+
// Load the highlight.js theme matching the current UI theme via a single
21+
// module-level <link> so it can be swapped at runtime without flicker.
22+
let highlightLink: HTMLLinkElement | null = null
23+
function applyHighlightTheme(uiThemeType: Exclude<ThemeType, ThemeType.AUTO>) {
24+
const href = uiThemeType === ThemeType.DARK ? hljsThemeDarkUrl : hljsThemeLightUrl
25+
if (!highlightLink) {
26+
highlightLink = document.createElement('link')
27+
highlightLink.rel = 'stylesheet'
28+
document.head.appendChild(highlightLink)
2229
}
23-
})
30+
highlightLink.href = href
31+
}
32+
watch(() => appStore.uiThemeType, applyHighlightTheme, { immediate: true })
2433
2534
const parsedMarkdown = ref('')
2635

‎src/composables/useMonacoEditor.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
55

66
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
77
import { onBeforeUnmount } from 'vue'
8+
import { ThemeType, useAppStore } from '@/store/appStore'
89
import { hasGrammar } from './sqlCompletion/dialects'
910
import { getCompletionProvider } from './sqlCompletion/provider'
1011
import {
@@ -15,7 +16,6 @@ import {
1516
parseSqlStatements,
1617
SQL_EXECUTE_GUTTER_CLASS,
1718
} from './useSqlStatements'
18-
import { useTheme } from './useTheme'
1919

2020
// Import only SQL-related grammars instead of all Monaco languages
2121
// (Typescript/CSS/HTML/JSON workers alone add ~8MB to the bundle).
@@ -62,7 +62,7 @@ export function useMonacoEditor(containerRef: Ref<HTMLElement | null>, initialVa
6262
let parsedStatements: SqlStatement[] = []
6363
let registeredCallbacks: EditorCallbacks | null = null
6464
let refreshDebounceTimer: ReturnType<typeof setTimeout> | null = null
65-
const { isDark } = useTheme()
65+
const appStore = useAppStore()
6666

6767
/** Monaco ships grammars only for sql/mysql/pgsql; other dialect ids use the generic sql grammar. Completion still resolves the real dialect profile per-editor. */
6868
const resolveEditorLanguage = (id: SQLDialect): SQLDialect => hasGrammar(id) ? id : 'sql'
@@ -98,7 +98,7 @@ export function useMonacoEditor(containerRef: Ref<HTMLElement | null>, initialVa
9898
editor = monaco.editor.create(containerRef.value, {
9999
value: initialValue.value,
100100
language: resolveEditorLanguage(options.language || 'sql'),
101-
theme: isDark.value ? 'vs-dark' : 'vs',
101+
theme: appStore.uiThemeType === ThemeType.DARK ? 'vs-dark' : 'vs',
102102
automaticLayout: true,
103103
readOnly: options.readOnly || false,
104104
minimap: { enabled: options.minimap !== false },

‎src/composables/useTheme.ts‎

Lines changed: 0 additions & 85 deletions
This file was deleted.

‎src/store/appStore.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export const useAppStore = defineStore('app', {
306306
this.sidebarCollapsed = collapsed
307307
},
308308
getEditorTheme() {
309-
return this.uiThemeType === ThemeType.DARK ? 'vs-dark' : 'vs-light'
309+
return this.uiThemeType === ThemeType.DARK ? 'vs-dark' : 'vs'
310310
},
311311

312312
// ── Provider CRUD ────────────────────────────────────────────────────

‎tests/store/appStore.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ describe('appStore', () => {
202202
})
203203

204204
describe('getEditorTheme', () => {
205-
it('should return vs-light for light theme', () => {
205+
it('should return vs for light theme', () => {
206206
const store = useAppStore()
207207
store.uiThemeType = ThemeType.LIGHT
208208

209-
expect(store.getEditorTheme()).toBe('vs-light')
209+
expect(store.getEditorTheme()).toBe('vs')
210210
})
211211

212212
it('should return vs-dark for dark theme', () => {

0 commit comments

Comments
 (0)