From b04ff5bbee117675644fae6e70f4a37d6b3215b7 Mon Sep 17 00:00:00 2001 From: Teffen Ellis <592134+GirlBossRush@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:10:12 -0500 Subject: [PATCH] web: Fix issue where Codemirror partially applies OneDark theme. (#12811) * web: Fix issue where code mirror partially applies OneDark theme. - Reported in #4622 - Partially fixed via fd9ce53 * update syntax highlight color when theme is changed Signed-off-by: Jens Langhammer * we dont really need to initialise these in the constructor tbh Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer Co-authored-by: Jens Langhammer --- web/src/elements/CodeMirror.ts | 51 +++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/web/src/elements/CodeMirror.ts b/web/src/elements/CodeMirror.ts index 503896ff4b..99d4b63f44 100644 --- a/web/src/elements/CodeMirror.ts +++ b/web/src/elements/CodeMirror.ts @@ -11,7 +11,7 @@ import { } from "@codemirror/language"; import * as yamlMode from "@codemirror/legacy-modes/mode/yaml"; import { Compartment, EditorState, Extension } from "@codemirror/state"; -import { oneDark } from "@codemirror/theme-one-dark"; +import { oneDark, oneDarkHighlightStyle } from "@codemirror/theme-one-dark"; import { ViewUpdate } from "@codemirror/view"; import { EditorView, drawSelection, keymap, lineNumbers } from "@codemirror/view"; import { EVENT_THEME_CHANGE } from "@goauthentik/common/constants"; @@ -49,10 +49,20 @@ export class CodeMirrorTextarea extends AKElement { _value?: string; - theme: Compartment; + theme: Compartment = new Compartment(); + syntaxHighlighting: Compartment = new Compartment(); - themeLight: Extension; - themeDark: Extension; + themeLight = EditorView.theme( + { + "&": { + backgroundColor: "var(--pf-global--BackgroundColor--light-300)", + }, + }, + { dark: false }, + ); + themeDark = oneDark; + syntaxHighlightingLight = syntaxHighlighting(defaultHighlightStyle); + syntaxHighlightingDark = syntaxHighlighting(oneDarkHighlightStyle); static get styles(): CSSResult[] { return [ @@ -120,20 +130,6 @@ export class CodeMirrorTextarea extends AKElement { } } - constructor() { - super(); - this.theme = new Compartment(); - this.themeLight = EditorView.theme( - { - "&": { - backgroundColor: "var(--pf-global--BackgroundColor--light-300)", - }, - }, - { dark: false }, - ); - this.themeDark = oneDark; - } - private getInnerValue(): string { if (!this.editor) { return ""; @@ -161,18 +157,26 @@ export class CodeMirrorTextarea extends AKElement { this.addEventListener(EVENT_THEME_CHANGE, ((ev: CustomEvent) => { if (ev.detail === UiThemeEnum.Dark) { this.editor?.dispatch({ - effects: this.theme.reconfigure(this.themeDark), + effects: [ + this.theme.reconfigure(this.themeDark), + this.syntaxHighlighting.reconfigure(this.syntaxHighlightingDark), + ], }); } else { this.editor?.dispatch({ - effects: this.theme.reconfigure(this.themeLight), + effects: [ + this.theme.reconfigure(this.themeLight), + this.syntaxHighlighting.reconfigure(this.syntaxHighlightingLight), + ], }); } }) as EventListener); + + const dark = this.activeTheme === UiThemeEnum.Dark; + const extensions = [ history(), keymap.of([...defaultKeymap, ...historyKeymap]), - syntaxHighlighting(defaultHighlightStyle), this.getLanguageExtension(), lineNumbers(), drawSelection(), @@ -189,7 +193,10 @@ export class CodeMirrorTextarea extends AKElement { }), EditorState.readOnly.of(this.readOnly), EditorState.tabSize.of(2), - this.theme.of(this.activeTheme === UiThemeEnum.Dark ? this.themeDark : this.themeLight), + this.syntaxHighlighting.of( + dark ? this.syntaxHighlightingDark : this.syntaxHighlightingLight, + ), + this.theme.of(dark ? this.themeDark : this.themeLight), ]; this.editor = new EditorView({ extensions: extensions.filter((p) => p) as Extension[],