Add ability to toggle dark/light theme

This commit is contained in:
Jonatan Heyman 2023-01-15 02:37:12 +01:00
parent fc59c84764
commit 7595d3fc86
4 changed files with 23 additions and 7 deletions

View File

@ -47,6 +47,7 @@
:language="language" :language="language"
:languageAuto="languageAuto" :languageAuto="languageAuto"
:theme="theme" :theme="theme"
@toggleTheme="theme = theme === 'dark' ? 'light' : 'dark'"
class="status" class="status"
/> />
</template> </template>

View File

@ -17,13 +17,19 @@
}) })
}) })
const editor = new HeynoteEditor({ this.editor = new HeynoteEditor({
element: this.$refs.editor, element: this.$refs.editor,
//content: "\ntext\n", //content: "\ntext\n",
content: initialData, content: initialData,
theme: this.theme, theme: this.theme,
}) })
}, },
watch: {
theme(newTheme) {
this.editor.setTheme(newTheme)
},
},
} }
</script> </script>

View File

@ -44,7 +44,8 @@
Col <span class="num">{{ column }}</span> Col <span class="num">{{ column }}</span>
</div> </div>
<div class="spacer"></div> <div class="spacer"></div>
<div class="status-block lang"> <div class="status-block theme clickable" @click="$emit('toggleTheme')">{{ theme }}</div>
<div class="status-block lang clickable">
{{ languageName }} {{ languageName }}
<span v-if="languageAuto" class="auto">(auto)</span> <span v-if="languageAuto" class="auto">(auto)</span>
</div> </div>
@ -82,15 +83,16 @@
.status-block .status-block
padding: 2px 12px padding: 2px 12px
cursor: default cursor: default
&.clickable
cursor: pointer
&:hover
background: rgba(255,255,255, 0.1)
&.line-number &.line-number
color: rgba(255, 255, 255, 0.7) color: rgba(255, 255, 255, 0.7)
.num .num
color: rgba(255, 255, 255, 1.0) color: rgba(255, 255, 255, 1.0)
&.lang &.lang
cursor: pointer
.auto .auto
color: rgba(255, 255, 255, 0.7) color: rgba(255, 255, 255, 0.7)
&:hover
background: rgba(255,255,255, 0.1)
</style> </style>

View File

@ -14,16 +14,17 @@ import { languageDetection } from "./language-detection/autodetect.js"
export class HeynoteEditor { export class HeynoteEditor {
constructor({element, content, focus=true, theme="light"}) { constructor({element, content, focus=true, theme="light"}) {
this.theme = new Compartment
this.state = EditorState.create({ this.state = EditorState.create({
doc: content || "", doc: content || "",
extensions: [ extensions: [
heynoteKeymap, heynoteKeymap,
//minimalSetup, //minimalSetup,
customSetup, customSetup,
theme === "dark" ? heynoteDark : heynoteLight, this.theme.of("dark" ? heynoteDark : heynoteLight),
heynoteBase, heynoteBase,
indentUnit.of(" "), indentUnit.of(" "),
EditorView.scrollMargins.of(f => { EditorView.scrollMargins.of(f => {
@ -56,6 +57,12 @@ export class HeynoteEditor {
this.view.focus() this.view.focus()
} }
} }
setTheme(theme) {
this.view.dispatch({
effects: this.theme.reconfigure(theme === "dark" ? heynoteDark : heynoteLight),
})
}
} }