Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(code): use simpler mechanism for detecting light vs dark theme
Elian Dorane224d4721de4
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsxindex e801d12120..2ad2a83a16 100644--- a/apps/client/src/widgets/type_widgets/code/Code.tsx+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx@@ -3,14 +3,13 @@ import "./code.css";import { default as VanillaCodeMirror, getThemeById } from "@triliumnext/codemirror";import { NoteType } from "@triliumnext/commons";import { Ref } from "preact";-import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { useEffect, useRef, useState } from "preact/hooks";import appContext, { CommandListenerData } from "../../../components/app_context";import FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";-import { getEffectiveThemeStyle } from "../../../services/theme";import utils from "../../../services/utils";-import { useEditorSpacedUpdate, useKeyboardShortcuts, useLegacyImperativeHandlers, useNoteBlob, useNoteLabelInt, useNoteLabelOptionalBool, useNoteProperty, useSyncedRef, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";+import { useColorScheme, useEditorSpacedUpdate, useKeyboardShortcuts, useLegacyImperativeHandlers, useNoteBlob, useNoteLabelInt, useNoteLabelOptionalBool, useNoteProperty, useSyncedRef, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";import { refToJQuerySelector } from "../../react/react_utils";import TouchBar, { TouchBarButton } from "../../react/TouchBar";import { CODE_THEME_DEFAULT_PREFIX as DEFAULT_PREFIX } from "../constants";@@ -173,23 +172,10 @@ export function CodeEditor({ parentComponent, ntxId, containerRef: externalContaconst [ matchesApp ] = useTriliumOptionBool("codeNoteThemeMatchesApp");const [ lightTheme ] = useTriliumOption("codeNoteThemeLight");const [ darkTheme ] = useTriliumOption("codeNoteThemeDark");-- // When "match app appearance" is enabled, resolve the theme based on the- // current effective app style and react to OS color-scheme changes.- const [ appStyle, setAppStyle ] = useState(getEffectiveThemeStyle);- useEffect(() => {- if (!matchesApp) return;- // Re-evaluate whenever the OS preference changes.- const mq = window.matchMedia("(prefers-color-scheme: dark)");- const handler = () => setAppStyle(getEffectiveThemeStyle());- mq.addEventListener("change", handler);- // Also recalculate immediately in case the app theme option changed.- setAppStyle(getEffectiveThemeStyle());- return () => mq.removeEventListener("change", handler);- }, [ matchesApp ]);+ const colorScheme = useColorScheme();const effectiveTheme = matchesApp- ? (appStyle === "dark" ? darkTheme : lightTheme)+ ? (colorScheme === "dark" ? darkTheme : lightTheme): codeNoteTheme;// React to background color.@@ -199,9 +185,10 @@ export function CodeEditor({ parentComponent, ntxId, containerRef: externalContaparentComponent?.$widget.closest(".scrolling-container").css("--code-background-color", backgroundColor);}, [ backgroundColor ]);- const applyTheme = useCallback((themeName: string) => {- if (codeEditorRef.current && themeName.startsWith(DEFAULT_PREFIX)) {- const theme = getThemeById(themeName.substring(DEFAULT_PREFIX.length));+ // React to theme changes.+ useEffect(() => {+ if (codeEditorRef.current && effectiveTheme.startsWith(DEFAULT_PREFIX)) {+ const theme = getThemeById(effectiveTheme.substring(DEFAULT_PREFIX.length));if (theme) {codeEditorRef.current.setTheme(theme).then(() => {const editor = containerRef.current?.querySelector(".cm-editor");@@ -211,12 +198,7 @@ export function CodeEditor({ parentComponent, ntxId, containerRef: externalConta});}}- }, []);-- // React to theme changes.- useEffect(() => {- applyTheme(effectiveTheme);- }, [ codeEditorRef, effectiveTheme, applyTheme ]);+ }, [ codeEditorRef, effectiveTheme ]);useTriliumEvent("executeWithCodeEditor", async ({ resolve, ntxId: eventNtxId }) => {if (eventNtxId !== ntxId) return;diff --git a/apps/client/src/widgets/type_widgets/options/code_notes.tsx b/apps/client/src/widgets/type_widgets/options/code_notes.tsxindex e8f3133d40..50d047c8d6 100644--- a/apps/client/src/widgets/type_widgets/options/code_notes.tsx+++ b/apps/client/src/widgets/type_widgets/options/code_notes.tsx@@ -8,11 +8,10 @@ import { useEffect, useMemo, useRef } from "preact/hooks";import { t } from "../../../services/i18n";import mime_types from "../../../services/mime_types";-import { getEffectiveThemeStyle } from "../../../services/theme";import { ButtonGroup } from "../../react/Button";import FormSelect from "../../react/FormSelect";import { FormTextBoxWithUnit } from "../../react/FormTextBox";-import { useStaticTooltip, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";+import { useColorScheme, useStaticTooltip, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";import Icon from "../../react/Icon";import { CODE_THEME_DEFAULT_PREFIX as DEFAULT_PREFIX } from "../constants";import CheckboxList from "./components/CheckboxList";@@ -107,12 +106,13 @@ function Appearance({ wordWrapping, indentSize }: AppearanceProps) {const [lightTheme, setLightTheme] = useTriliumOption("codeNoteThemeLight");const [darkTheme, setDarkTheme] = useTriliumOption("codeNoteThemeDark");+ const colorScheme = useColorScheme();const allThemes = useFilteredThemes();const lightThemes = useFilteredThemes("light");const darkThemes = useFilteredThemes("dark");const effectiveTheme = matchesApp- ? (getEffectiveThemeStyle() === "dark" ? darkTheme : lightTheme)+ ? (colorScheme === "dark" ? darkTheme : lightTheme): codeNoteTheme;return (