Skip to content

Commit 8be7ba3

Browse files
committed
Add a workaround for an oddity / bug in monaco-editor/react: The launch of each editor sets the theme to a default of 'light'.
1 parent e2d16e9 commit 8be7ba3

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/main.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ import App from "./view/App";
44
import "./view/styles.css";
55
import * as TextMate from "./monaco/TextMate";
66
import * as Themes from "./monaco/Themes";
7+
import * as monaco from 'monaco-editor';
8+
import { loader } from '@monaco-editor/react';
9+
10+
loader.config({ monaco });
711

812
await TextMate.setup();
913
export const customThemes = await Themes.load();
14+
export let currentTheme = "vs-dark-modern";
15+
16+
export function onUpdateTheme(theme: string, editor: monaco.editor.ICodeEditor) {
17+
document.body.style.backgroundColor = Themes.getBackgroundColor(editor);
18+
currentTheme = theme;
19+
}
1020

1121
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);
1222
root.render(

src/monaco/Themes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ export async function load(): Promise<string[]> {
8989
return addedThemes;
9090
}
9191

92-
export function addSetThemeActions(editor: monaco.editor.IStandaloneCodeEditor, themes: string[], callback?: () => void) {
92+
export function addSetThemeActions(editor: monaco.editor.IStandaloneCodeEditor, themes: string[], callback?: (theme: string, editor: monaco.editor.ICodeEditor) => void) {
9393
for (let themeID of themes) {
9494
editor.addAction({
9595
id: `setTheme-${themeID}`,
9696
label: `Set theme to ${themeID}`,
9797
run: (editor: monaco.editor.ICodeEditor, arg: string) => {
9898
monaco.editor.setTheme(themeID);
99-
callback?.();
99+
callback?.(themeID, editor);
100100
}
101101
});
102102
}
103103
}
104104

105-
export function getBackgroundColor(editor: monaco.editor.IStandaloneCodeEditor): string {
105+
export function getBackgroundColor(editor: monaco.editor.ICodeEditor): string {
106106
return getComputedStyle(editor.getDomNode()!)
107107
.getPropertyValue('--vscode-editor-background')
108108
}

src/view/App.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import React, { createRef, useRef } from 'react';
1+
import React from 'react';
22
import ReactDOM from "react-dom/client";
33
import './App.css';
44

55
import * as monaco from 'monaco-editor';
6-
import { loader } from '@monaco-editor/react';
76
import { MonacoLogController, FileInfo } from '../monaco/MonacoLogController';
87
import { EventCallback, emit, listen } from '@tauri-apps/api/event'
98
import { useEffect } from 'react';
109
import Editor from "@monaco-editor/react";
1110
import { TextZone } from '../monaco/ViewZones';
1211
import { addSetLanguageActions } from '../monaco/SetLanguage';
1312
import { addSetThemeActions, getBackgroundColor } from '../monaco/Themes';
14-
import { customThemes } from '../main';
15-
16-
loader.config({ monaco });
13+
import { currentTheme, customThemes, onUpdateTheme } from '../main';
1714

1815
function listenReact<T>(name: string, fun: EventCallback<T>) {
1916
useEffect(() => {
@@ -39,7 +36,8 @@ function createPatchEditor(language: string, textZone: TextZone) {
3936

4037
const secondEditor = <Editor
4138
onMount={handleEditorDidMount}
42-
theme="vs-dark-modern"
39+
40+
theme={currentTheme}
4341

4442
// language / value inputs are tracked, "default" versions are only for the initial value
4543
defaultLanguage={language}
@@ -93,20 +91,23 @@ function App() {
9391
});
9492

9593
async function handleEditorDidMount(editor: monaco.editor.IStandaloneCodeEditor, monaco: any) {
96-
document.body.style.backgroundColor = getBackgroundColor(editor);
94+
// This shouldnt be needed, but it needs to be run once to update the background color for the first time.
95+
onUpdateTheme(currentTheme, editor);
9796

9897
let logController_ = new MonacoLogController(editor, createPatchEditor);
9998
logController = logController_;
10099

101100
addSetLanguageActions(editor);
102-
addSetThemeActions(editor, customThemes, () => document.body.style.backgroundColor = getBackgroundColor(editor));
101+
addSetThemeActions(editor, customThemes, onUpdateTheme);
103102
}
104103

105104
return <Editor
106105
height="calc(100vh - 30px)" // TODO This shouldn't be here but otherwise the size is 5px
107106
width="calc(100vw - 30px)"
107+
108+
theme={currentTheme}
109+
108110
onMount={handleEditorDidMount}
109-
theme="vs-dark-modern"
110111
options={{
111112
minimap: { enabled: false },
112113
renderLineHighlight: "none",

0 commit comments

Comments
 (0)