Skip to content

Commit 5e9c931

Browse files
committed
refactor(theme): improve robustness and code quality
1 parent 9d6cd5e commit 5e9c931

4 files changed

Lines changed: 22 additions & 38 deletions

File tree

src/main/ipc/handlers/theme.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,21 @@ async function openThemesDir(): Promise<void> {
301301

302302
function resolveThemeTemplatePath(): string {
303303
const themesDir = ensureThemesDir()
304-
let index = 0
304+
const maxAttempts = 1000
305305

306-
while (true) {
306+
for (let index = 0; index < maxAttempts; index++) {
307307
const suffix = index === 0 ? '' : `-${index}`
308308
const fileName = `${THEME_TEMPLATE_BASE_NAME}${suffix}${THEME_FILE_EXT}`
309309
const filePath = path.join(themesDir, fileName)
310310

311311
if (!existsSync(filePath)) {
312312
return filePath
313313
}
314-
315-
index += 1
316314
}
315+
316+
throw new Error(
317+
`Could not find available theme template name after ${maxAttempts} attempts`,
318+
)
317319
}
318320

319321
function createThemeTemplate(): string {

src/renderer/components/editor/Editor.vue

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const {
4141
isFocusedSearch,
4242
isShowJsonVisualizer,
4343
} = useApp()
44-
const { getEditorThemeName } = useTheme()
44+
const { editorThemeName } = useTheme()
4545
4646
const { addToUpdateContentQueue } = useSnippetUpdate()
4747
@@ -125,7 +125,7 @@ async function init() {
125125
editor = CodeMirror(el, {
126126
value: selectedSnippetContent.value?.value || ' ',
127127
mode: selectedSnippetContent.value?.language || 'plain_text',
128-
theme: getEditorThemeName(),
128+
theme: editorThemeName.value,
129129
lineWrapping: settings.wrap,
130130
lineNumbers: true,
131131
tabSize: settings.tabSize,
@@ -239,12 +239,9 @@ async function init() {
239239
})
240240
})
241241
242-
watch(
243-
() => getEditorThemeName(),
244-
(themeName) => {
245-
editor?.setOption('theme', themeName)
246-
},
247-
)
242+
watch(editorThemeName, (themeName) => {
243+
editor?.setOption('theme', themeName)
244+
})
248245
249246
watch(
250247
() => settings.fontSize,

src/renderer/components/editor/markdown/Markdown.vue

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'codemirror/theme/oceanic-next.css'
1616
const isDev = import.meta.env.DEV
1717
1818
const { selectedSnippetContent } = useSnippets()
19-
const { isDark, getEditorThemeName } = useTheme()
19+
const { isDark, editorThemeName } = useTheme()
2020
const { scaleToShow, onZoom } = useMarkdown()
2121
2222
const route = useRoute()
@@ -107,7 +107,7 @@ function renderCodeBlockEditors() {
107107
const editor = CodeMirror(container as HTMLElement, {
108108
value: blockData.value,
109109
mode: blockData.language,
110-
theme: getEditorThemeName(),
110+
theme: editorThemeName.value,
111111
readOnly: true,
112112
lineNumbers: false,
113113
lineWrapping: true,
@@ -156,14 +156,11 @@ watch(renderedContent, () => {
156156
})
157157
})
158158
159-
watch(
160-
() => getEditorThemeName(),
161-
(theme) => {
162-
codeEditors.value.forEach((editor) => {
163-
editor.setOption('theme', theme)
164-
})
165-
},
166-
)
159+
watch(editorThemeName, (theme) => {
160+
codeEditors.value.forEach((editor) => {
161+
editor.setOption('theme', theme)
162+
})
163+
})
167164
168165
watch(isDark, () => {
169166
renderMermaidBlocks()

src/renderer/composables/useTheme.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ async function processThemeReloadQueue(): Promise<void> {
237237
}
238238
}
239239

240-
function getEditorThemeName(): string {
240+
const editorThemeName = computed(() => {
241241
const baseTheme
242242
= resolvedThemeType.value === 'dark' ? DARK_EDITOR_THEME : LIGHT_EDITOR_THEME
243243

@@ -250,23 +250,11 @@ function getEditorThemeName(): string {
250250
}
251251

252252
return baseTheme
253-
}
253+
})
254254

255255
async function initTheme(): Promise<void> {
256256
await loadCustomThemes()
257-
258-
const selectedTheme = currentThemeId.value
259-
260-
if (isBuiltInTheme(selectedTheme)) {
261-
applyBuiltInTheme(selectedTheme)
262-
return
263-
}
264-
265-
const isApplied = await applyCustomTheme(selectedTheme)
266-
267-
if (!isApplied) {
268-
fallbackToAuto()
269-
}
257+
await setTheme(currentThemeId.value)
270258
}
271259

272260
function onThemeChanged() {
@@ -297,6 +285,6 @@ export function useTheme() {
297285
isDark,
298286
setTheme,
299287
loadCustomThemes,
300-
getEditorThemeName,
288+
editorThemeName,
301289
}
302290
}

0 commit comments

Comments
 (0)