Skip to content

Commit

Permalink
feat: set monaco editor theme,width,height to auto
Browse files Browse the repository at this point in the history
  • Loading branch information
VisionView committed Aug 11, 2023
1 parent dbd770b commit 439bf45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/components/AppProvider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const getTheme = computed(() => {
});
const locale = computed(() => {
let langType = appStore.languageType;
let langType = appStore.languageName;
return langType === 'zhCN' ? zhCN : enUS;
});
const dateLocale = computed(() => {
let langType = appStore.languageType;
let langType = appStore.languageName;
return langType === 'zhCN' ? dateZhCN : dateEnUS;
});
const NaiveProviderContent = defineComponent({
Expand Down
8 changes: 7 additions & 1 deletion src/store/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export const useAppStore = defineStore('app', {
state() {
return {
themeType: 0, // 0 auto, 1: dark, 2: light
languageType: 'zhCN', // zhCN || enUS
languageType: 'auto', // 0: auto, 1: zhCN, 2: enUS
languageName: 'zhCN', // zhCN || enUS
};
},
actions: {
Expand All @@ -14,6 +15,11 @@ export const useAppStore = defineStore('app', {
setLanguageType(args: string) {
this.languageType = args;
localStorage.setItem('lang', String(args));
if (args === 'auto') {
this.languageName = navigator.language === 'zh-CN' ? 'zhCN' : 'enUS';
} else {
this.languageName = args;
}
},
},
});
39 changes: 31 additions & 8 deletions src/views/editor/index.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div id="editor" ref="editor"></div>
<div id="editor" ref="editorRef"></div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import * as monaco from 'monaco-editor';
import { useAppStore } from './../../store';
const appStore = useAppStore();
/**
* refer https://github.com/wobsoriano/codeplayground
* https://github.com/wobsoriano/codeplayground/blob/master/src/components/MonacoEditor.vue
Expand Down Expand Up @@ -309,10 +309,33 @@ monaco.languages.setMonarchTokensProvider('search', {
],
},
});
const editor = ref();
// DOM
const editorRef = ref();
const editorView = ref();
const themeMedia = window.matchMedia('(prefers-color-scheme: light)');
const systemTheme = ref(themeMedia.matches);
themeMedia.addListener(e => {
systemTheme.value = e.matches;
});
// set Editoer theme name
const editorTheme = computed(() => {
// 'vs-dark',
let isDark = appStore.themeType === 0 ? !systemTheme.value : appStore.themeType === 1;
return isDark ? 'vs-dark' : 'vs-light';
});
watch(
() => editorTheme.value,
() => {
editorView.value.updateOptions({ theme: editorTheme.value });
},
);
onMounted(() => {
monaco.editor.create(editor.value, {
editorView.value = monaco.editor.create(editorRef.value, {
automaticLayout: true,
theme: editorTheme.value,
value: `// Type source code in your language here...
GET students/_search
{
Expand All @@ -331,9 +354,9 @@ GET students/_search
});
</script>

<style scoped>
<style lang="scss" scoped>
#editor {
width: 100vw;
height: 100vh;
width: 100%;
height: 100%;
}
</style>
5 changes: 1 addition & 4 deletions src/views/setting/components/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ const setThemeType = (type: number) => {
const langTypeChange = (value: string) => {
langType.value = value;
if (value === 'auto') {
value = navigator.language === 'zh-CN' ? 'zhCN' : 'enUS';
}
appStore.setLanguageType(value);
lang.global.locale.value = value;
lang.global.locale.value = appStore.languageName;
};
</script>

Expand Down

0 comments on commit 439bf45

Please sign in to comment.