Skip to content

Commit

Permalink
set the monaco editor theme globally
Browse files Browse the repository at this point in the history
It is impossible, by the feature of monaco editor, to set different themes
for different instances of monaco editor.
  • Loading branch information
TaiSakuma committed Sep 5, 2023
1 parent fd82fbb commit 838bbbe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
14 changes: 0 additions & 14 deletions src/components/History/RunCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { ref, toRefs, watch } from "vue";
import * as monaco from "monaco-editor";
import { useDarkMode } from "@/utils/color-theme";
type Run = {
runNo: number;
state?: string | null | undefined;
Expand All @@ -19,8 +17,6 @@ interface Props {
const props = defineProps<Props>();
const { isDark } = useDarkMode();
const { run } = toRefs(props);
const stateChipColor = ref({
Expand Down Expand Up @@ -85,21 +81,11 @@ watch(
selectionHighlight: false,
occurrencesHighlight: false,
renderLineHighlight: "none",
theme: isDark.value ? "nextline-dark" : "nextline-light",
});
val.style.height = `${editor.getContentHeight()}px`;
},
{ immediate: true }
);
watch(
isDark,
(val) => {
monaco.editor.setTheme(
val ? "nextline-dark" : "nextline-light"
);
},
{ immediate: true }
);
</script>

<template>
Expand Down
16 changes: 1 addition & 15 deletions src/components/main/trace-frame/code-col/monaco-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ import {
import type { Ref, MaybeRefOrGetter } from "vue";
import * as monaco from "monaco-editor";

import { useDarkMode } from "@/utils/color-theme";

export function useMonacoEditor(
element: MaybeRefOrGetter<HTMLElement | undefined>,
source: Ref<string>
) {
const { isDark } = useDarkMode();
// const { isDark } = useDarkMode();
const model = monaco.editor.createModel(toValue(source), "python");
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>();
const theme = computed(() =>
toValue(isDark) ? "nextline-dark" : "nextline-light"
);

onMounted(() => {
const ele = toValue(element);
Expand All @@ -46,17 +41,8 @@ export function useMonacoEditor(
selectionHighlight: false,
occurrencesHighlight: false,
renderLineHighlight: "none",
theme: toValue(theme),
});
});

// Note: All instances of Monaco Editor share the same theme.
// It is not possible to have different themes for different instances.
// https://github.com/Microsoft/monaco-editor/issues/338

watchEffect(() => {
monaco.editor.setTheme(toValue(theme));
});

return { editor, model };
}
Expand Down
12 changes: 1 addition & 11 deletions src/components/main/trace-frame/script-editor/monaco-editor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ref, watch, watchEffect, toValue, onMounted } from "vue";
import { ref, watch, toValue, onMounted } from "vue";
import type { Ref, MaybeRefOrGetter } from "vue";
import { watchDebounced } from "@vueuse/core";

import * as monaco from "monaco-editor";

import { useDarkMode } from "@/utils/color-theme";

export function useMonacoEditor(
element: MaybeRefOrGetter<HTMLElement | undefined>,
source: Ref<string>
Expand All @@ -14,7 +12,6 @@ export function useMonacoEditor(
useUpdateSource(model, source);

const editor = ref<monaco.editor.IStandaloneCodeEditor>();
const { isDark } = useDarkMode();

onMounted(() => {
const ele = toValue(element);
Expand All @@ -37,16 +34,9 @@ export function useMonacoEditor(
selectionHighlight: true,
occurrencesHighlight: true,
renderLineHighlight: "line",
theme: isDark.value ? "nextline-dark" : "nextline-light",
});
});

watchEffect(() => {
monaco.editor.setTheme(
toValue(isDark) ? "nextline-dark" : "nextline-light"
);
});

return { editor, model };
}

Expand Down
15 changes: 15 additions & 0 deletions src/utils/color-theme/color-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ref, computed, watchEffect, toValue } from "vue";
import type { MaybeRefOrGetter, UnwrapRef } from "vue";
import { useTheme } from "vuetify";
import type { ThemeDefinition } from "vuetify";
import * as monaco from "monaco-editor";
import type { OmitIndexSignature } from "type-fest";

import { useDynamicColors } from "@/utils/dynamic-color";
Expand Down Expand Up @@ -37,6 +38,7 @@ export function useColorTheme() {

useDarkModeOnVuetify();
useMonacoEditorTheme();
useDarkModeOnMonacoEditor();
}

function useDarkModeOnVuetify() {
Expand All @@ -48,6 +50,19 @@ function useDarkModeOnVuetify() {
});
}

function useDarkModeOnMonacoEditor() {
// Note: All instances of Monaco Editor share the same theme.
// It is not possible to have different themes for different instances.
// https://github.com/Microsoft/monaco-editor/issues/338
const { isDark } = useDarkMode();
const themeName = computed(() =>
isDark.value ? "nextline-dark" : "nextline-light"
);
watchEffect(() => {
monaco.editor.setTheme(themeName.value);
});
}

function useDynamicColorsOnVuetify(
dynamicColors: MaybeRefOrGetter<DynamicColors>,
isDark: MaybeRefOrGetter<boolean>
Expand Down

0 comments on commit 838bbbe

Please sign in to comment.