Skip to content

Commit 2417497

Browse files
committed
fix(theme): review hardening — guard pref init, validate SET_SINGLE key, remove dead code
preference.vue: restore try/catch around addThemeStyle in onMounted so a theme failure cannot block ASK_FOR_USER_PREFERENCE. preferences.js: add typeof guard in SET_SINGLE_PREFERENCE to reject unknown keys, matching the existing guard in SET_USER_PREFERENCE. theme.js: delete unused ORIGINAL_THEME constant and getThemeCluster function (zero callers across entire codebase).
1 parent 288dfef commit 2417497

3 files changed

Lines changed: 4 additions & 37 deletions

File tree

src/renderer/src/pages/preference.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ watch(theme, (newValue, oldValue) => {
4848
onMounted(() => {
4949
nextTick(() => {
5050
const state = window.marktext.initialState || DEFAULT_STYLE
51-
addThemeStyle(state.theme)
51+
try {
52+
addThemeStyle(state.theme)
53+
} catch (_) { /* theme failure must not block pref init */ }
5254
preferencesStore.ASK_FOR_USER_PREFERENCE()
5355
})
5456
})

src/renderer/src/store/preferences.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export const usePreferencesStore = defineStore('preferences', {
174174
},
175175

176176
async SET_SINGLE_PREFERENCE({ type, value }) {
177+
if (typeof this[type] === 'undefined') return
177178
this[type] = value
178179

179180
// Update i18n language if language preference changed

src/renderer/src/util/theme.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
} from './themeColor'
4444
import { isLinux } from './index'
4545

46-
const ORIGINAL_THEME = '#409EFF'
4746
const patchTheme = (css) => {
4847
return `@media not print {\n${css}\n}`
4948
}
@@ -54,41 +53,6 @@ const getEmojiPickerPatch = () => {
5453
: ''
5554
}
5655

57-
const getThemeCluster = (themeColor) => {
58-
const tintColor = (color, tint) => {
59-
let red = parseInt(color.slice(1, 3), 16)
60-
let green = parseInt(color.slice(3, 5), 16)
61-
let blue = parseInt(color.slice(5, 7), 16)
62-
if (tint === 0) {
63-
// when primary color is in its rgb space
64-
return [red, green, blue].join(',')
65-
} else {
66-
red += Math.round(tint * (255 - red))
67-
green += Math.round(tint * (255 - green))
68-
blue += Math.round(tint * (255 - blue))
69-
red = red.toString(16)
70-
green = green.toString(16)
71-
blue = blue.toString(16)
72-
return `#${red}${green}${blue}`
73-
}
74-
}
75-
76-
const clusters = [
77-
{
78-
color: themeColor,
79-
variable: 'var(--themeColor)'
80-
}
81-
]
82-
for (let i = 9; i >= 1; i--) {
83-
clusters.push({
84-
color: tintColor(themeColor, Number((i / 10).toFixed(2))),
85-
variable: `var(--themeColor${10 - i}0)`
86-
})
87-
}
88-
89-
return clusters
90-
}
91-
9256
export const addThemeStyle = (theme) => {
9357
const isCmRailscasts = railscastsThemes.includes(theme)
9458
const isCmOneDark = oneDarkThemes.includes(theme)

0 commit comments

Comments
 (0)