Skip to content

Commit 0caf592

Browse files
committed
fix(theme): changing monaco editor theme programatically on theme change
1 parent 51c2882 commit 0caf592

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

components/v2/navbar.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ export default Vue.extend({
176176
mounted() {
177177
if (darkMode === null) {
178178
this.darkMode = darkMode = this.$cookies.isDarkMode
179-
if (this.$cookies.isDarkMode) {
179+
if (darkMode) {
180180
document.documentElement.classList.add('dark')
181181
document.cookie = `darkMode=${darkMode}; Secure; max-age=31536000; path=/;`
182+
this.$store.commit('theme/set', darkMode)
182183
}
183184
}
184185
document.documentElement.classList.remove('hidden')
@@ -190,6 +191,7 @@ export default Vue.extend({
190191
'dark'
191192
)
192193
document.cookie = `darkMode=${!currentDarkMode}; Secure; max-age=31536000; path=/;`
194+
this.$store.commit('theme/set', !currentDarkMode)
193195
this.darkMode = !currentDarkMode
194196
},
195197
},

pages/v2/diff.vue

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,21 @@
11
<template>
22
<div class="page-contents">
3+
<!-- Following hidden input is hacky way to update monaco editor theme when user changes theme manually -->
4+
<input type="hidden" inert :value="onThemeChange" />
35
<Navbar :show-back-button="true" />
46
<main class="outline-none" tabindex="0">
57
<DiffActionBar :diff-navigator="diffNavigator" />
68
<section
7-
class="
8-
flex
9-
items-stretch
10-
flex-wrap
11-
w-full
12-
gap-4
13-
font-mono
14-
text-gray-800
15-
dark:text-gray-50
16-
"
9+
class="flex flex-wrap items-stretch w-full gap-4 font-mono text-gray-800 dark:text-gray-50"
1710
>
18-
<div class="flex space-around w-full gap-4">
11+
<div class="flex w-full gap-4 space-around">
1912
<p
20-
class="
21-
flex-grow-0 flex-shrink-0
22-
w-1/2
23-
text-lg
24-
font-bold
25-
text-center
26-
capitalize
27-
break-all
28-
"
13+
class="flex-grow-0 flex-shrink-0 w-1/2 text-lg font-bold text-center capitalize break-all "
2914
>
3015
<span class="inline-block w-4/5">{{ lhsLabel }}</span>
3116
</p>
3217
<p
33-
class="
34-
flex-grow-0 flex-shrink-0
35-
w-1/2
36-
text-lg
37-
font-bold
38-
text-center
39-
capitalize
40-
break-all
41-
"
18+
class="flex-grow-0 flex-shrink-0 w-1/2 text-lg font-bold text-center capitalize break-all "
4219
>
4320
<span class="inline-block w-4/5">{{ rhsLabel }}</span>
4421
</p>
@@ -76,6 +53,13 @@ export default Vue.extend({
7653
diffNavigator: {},
7754
}
7855
},
56+
computed: {
57+
onThemeChange() {
58+
const theme = this.$store.state.theme.darkMode ? 'vs-dark' : 'light'
59+
this.monacoDiffEditor?.updateOptions?.({ theme })
60+
return this.$store.state.theme.darkMode
61+
},
62+
},
7963
beforeMount() {
8064
const _diff = this.$route.hash
8165
if (_diff) {

pages/v2/index.vue

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
n
12
<template>
23
<div class="page-contents">
34
<Navbar />
5+
<!-- Following hidden input is hacky way to update monaco editor theme when user changes theme manually -->
6+
<input type="hidden" inert :value="onThemeChange" />
47
<main class="text-gray-800 outline-none dark:text-gray-50" tabindex="0">
58
<section>
69
<header>
@@ -54,23 +57,7 @@
5457
<div class="self-end flex-grow-0 w-full mt-4 text-center">
5558
<button
5659
id="submitButton"
57-
class="
58-
inline-flex
59-
items-center
60-
justify-center
61-
w-48
62-
px-4
63-
py-2
64-
transition-transform
65-
transform
66-
bg-blue-600
67-
rounded-md
68-
shadow-lg
69-
outline-none
70-
text-gray-50
71-
focus:ring-4
72-
active:scale-y-75
73-
"
60+
class="inline-flex items-center justify-center w-48 px-4 py-2 transition-transform transform bg-blue-600 rounded-md shadow-lg outline-none text-gray-50 focus:ring-4 active:scale-y-75"
7461
aria-label="Click here to compare the inputted text blocks"
7562
>
7663
Compare
@@ -101,6 +88,14 @@ export default Vue.extend({
10188
rhsEditor: null,
10289
}
10390
},
91+
computed: {
92+
onThemeChange() {
93+
const theme = this.$store.state.theme.darkMode ? 'vs-dark' : 'light'
94+
this.lhsEditor?.updateOptions({ theme })
95+
this.rhsEditor?.updateOptions({ theme })
96+
return this.$store.state.theme.darkMode
97+
},
98+
},
10499
mounted() {
105100
showTutorials(this.$cookies, this.$route.path, this.$cookies.isDarkMode)
106101
document.addEventListener('keydown', this.handleCtrlEnter)
@@ -121,7 +116,7 @@ export default Vue.extend({
121116
this.rhsEditor = monaco.editor.create(rhs, {
122117
...monacoEditorOptions,
123118
value: this.rhs || '',
124-
wordWrap: 'on'
119+
wordWrap: 'on',
125120
})
126121
}
127122
})

store/theme.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { GetterTree, MutationTree } from 'vuex/types'
2+
3+
export const state = () => ({
4+
darkMode: false
5+
})
6+
7+
export type themeStore = ReturnType<typeof state>
8+
9+
export const getters: GetterTree<themeStore, themeStore> = {
10+
isEnabled: (state) => state.darkMode,
11+
}
12+
13+
export const mutations: MutationTree<themeStore> = {
14+
toggle(state) {
15+
state.darkMode = !state.darkMode
16+
},
17+
set(state, value) {
18+
state.darkMode = value
19+
}
20+
}

0 commit comments

Comments
 (0)