Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): basic light/dark theme switcher

Elian Doranc10ba7ac5b1d
diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts
index f5a75c4634..6f40388ac4 100644
--- a/apps/client/src/services/syntax_highlight.ts
+++ b/apps/client/src/services/syntax_highlight.ts
@@ -5,10 +5,19 @@ import { copyText, copyTextWithToast } from "./clipboard_ext.js";
import { t } from "./i18n.js";
import mime_types from "./mime_types.js";
import options from "./options.js";
+import { getEffectiveThemeStyle } from "./theme.js";
import { isShare } from "./utils.js";
let highlightingLoaded = false;
+function getEffectiveCodeBlockTheme(): string {
+ if (options.get("codeBlockThemeMatchesApp") === "true") {
+ const style = getEffectiveThemeStyle();
+ return String(options.get(style === "dark" ? "codeBlockThemeDark" : "codeBlockThemeLight"));
+ }
+ return String(options.get("codeBlockTheme"));
+}
+
// Highlight.js can spend tens of milliseconds per block (php/c# are especially slow).
// The Markdown live preview replaces the entire rendered DOM on every keystroke, so the
// same unchanged code blocks would otherwise be re-highlighted continuously. Cache the
@@ -136,8 +145,7 @@ export async function ensureMimeTypesForHighlighting(mimeTypeHint?: string) {
// Load theme.
if (!highlightingLoaded) {
- const currentThemeName = String(options.get("codeBlockTheme"));
- await loadHighlightingTheme(currentThemeName);
+ await loadHighlightingTheme(getEffectiveCodeBlockTheme());
}
// Load mime types.
@@ -178,7 +186,7 @@ export async function loadHighlightingTheme(themeName: string) {
*/
export function isSyntaxHighlightEnabled() {
if (!isShare) {
- const theme = options.get("codeBlockTheme");
+ const theme = getEffectiveCodeBlockTheme();
return !!theme && theme !== "none";
}
return true;
diff --git a/apps/client/src/widgets/type_widgets/options/code_notes.tsx b/apps/client/src/widgets/type_widgets/options/code_notes.tsx
index 50d047c8d6..8992b82eba 100644
--- a/apps/client/src/widgets/type_widgets/options/code_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/code_notes.tsx
@@ -8,15 +8,14 @@ import { useEffect, useMemo, useRef } from "preact/hooks";
import { t } from "../../../services/i18n";
import mime_types from "../../../services/mime_types";
-import { ButtonGroup } from "../../react/Button";
import FormSelect from "../../react/FormSelect";
import { FormTextBoxWithUnit } from "../../react/FormTextBox";
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";
import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";
import OptionsSection from "./components/OptionsSection";
+import ThemeModeSelector from "./components/ThemeModeSelector";
import codeNoteSample from "./samples/code_note.txt?raw";
const SAMPLE_MIME = "application/typescript";
@@ -116,25 +115,8 @@ function Appearance({ wordWrapping, indentSize }: AppearanceProps) {
: codeNoteTheme;
return (
- <OptionsSection title={t("code_theme.title")}>
- <OptionsRow name="theme-mode" label={t("code_theme.theme_mode")}>
- <ButtonGroup>
- <button
- type="button"
- className={`btn btn-sm btn-secondary ${matchesApp ? "active" : ""}`}
- onClick={() => setMatchesApp(true)}
- >
- <Icon icon="bx bx-brightness-half" />{" "}{t("code_theme.match_app_appearance")}
- </button>
- <button
- type="button"
- className={`btn btn-sm btn-secondary ${!matchesApp ? "active" : ""}`}
- onClick={() => setMatchesApp(false)}
- >
- <Icon icon="bx bx-pin" />{" "}{t("code_theme.always_use_one_theme")}
- </button>
- </ButtonGroup>
- </OptionsRow>
+ <OptionsSection title={t("code_theme.title")} className="code-block-appearance">
+ <ThemeModeSelector matchesApp={matchesApp} onMatchesAppChange={setMatchesApp} />
{matchesApp ? (
<>
diff --git a/apps/client/src/widgets/type_widgets/options/components/ThemeModeSelector.tsx b/apps/client/src/widgets/type_widgets/options/components/ThemeModeSelector.tsx
new file mode 100644
index 0000000000..e04172f051
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/options/components/ThemeModeSelector.tsx
@@ -0,0 +1,32 @@
+import { t } from "../../../../services/i18n";
+import { ButtonGroup } from "../../../react/Button";
+import Icon from "../../../react/Icon";
+import OptionsRow from "./OptionsRow";
+
+interface ThemeModeSelectorProps {
+ matchesApp: boolean;
+ onMatchesAppChange: (value: boolean) => void;
+}
+
+export default function ThemeModeSelector({ matchesApp, onMatchesAppChange }: ThemeModeSelectorProps) {
+ return (
+ <OptionsRow name="theme-mode" label={t("code_theme.theme_mode")}>
+ <ButtonGroup>
+ <button
+ type="button"
+ className={`btn btn-sm btn-secondary ${matchesApp ? "active" : ""}`}
+ onClick={() => onMatchesAppChange(true)}
+ >
+ <Icon icon="bx bx-brightness-half" />{" "}{t("code_theme.match_app_appearance")}
+ </button>
+ <button
+ type="button"
+ className={`btn btn-sm btn-secondary ${!matchesApp ? "active" : ""}`}
+ onClick={() => onMatchesAppChange(false)}
+ >
+ <Icon icon="bx bx-pin" />{" "}{t("code_theme.always_use_one_theme")}
+ </button>
+ </ButtonGroup>
+ </OptionsRow>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
index 2d324de950..928d1004f7 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -1,7 +1,7 @@
import "./text_notes.css";
import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
-import { Themes } from "@triliumnext/highlightjs";
+import { getThemeVariant, Themes } from "@triliumnext/highlightjs";
import type { CSSProperties } from "preact/compat";
import { useEffect, useMemo, useState } from "preact/hooks";
@@ -12,15 +12,16 @@ import { formatDateTime, toggleBodyClass } from "../../../services/utils";
import FormGroup from "../../react/FormGroup";
import Dropdown from "../../react/Dropdown";
import { FormListItem } from "../../react/FormList";
-import { FormSelectGroup, FormSelectWithGroups } from "../../react/FormSelect";
+import FormSelect, { FormSelectGroup, FormSelectWithGroups } from "../../react/FormSelect";
import FormText from "../../react/FormText";
import FormTextBox, { FormTextBoxWithUnit } from "../../react/FormTextBox";
-import { useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";
+import { useColorScheme, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";
import { getHtml } from "../../react/RawHtml";
import CheckboxList from "./components/CheckboxList";
import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";
import OptionsSection from "./components/OptionsSection";
import RadioWithIllustration from "./components/RadioWithIllustration";
+import ThemeModeSelector from "./components/ThemeModeSelector";
const isNewLayout = isExperimentalFeatureEnabled("new-layout");
@@ -225,59 +226,97 @@ function HeadingPreview({ style }: { style: string }) {
);
}
-function CodeBlockStyle() {
- const themes = useMemo(() => {
+function useCodeBlockThemes() {
+ const allThemes = useMemo(() => {
const darkThemes: ThemeData[] = [];
const lightThemes: ThemeData[] = [];
for (const [ id, theme ] of Object.entries(Themes)) {
const data: ThemeData = {
- val: `default:${ id}`,
+ val: `default:${id}`,
title: theme.name
};
- if (theme.name.includes("Dark")) {
+ if (getThemeVariant(theme) === "dark") {
darkThemes.push(data);
} else {
lightThemes.push(data);
}
}
- const output: FormSelectGroup<ThemeData>[] = [
- {
- title: "",
- items: [{
- val: "none",
- title: t("code_block.theme_none")
- }]
- },
- {
- title: t("code_block.theme_group_light"),
- items: lightThemes
- },
- {
- title: t("code_block.theme_group_dark"),
- items: darkThemes
- }
- ];
- return output;
+ return { lightThemes, darkThemes };
}, []);
+
+ const groupedThemes = useMemo((): FormSelectGroup<ThemeData>[] => [
+ {
+ title: "",
+ items: [{
+ val: "none",
+ title: t("code_block.theme_none")
+ }]
+ },
+ {
+ title: t("code_block.theme_group_light"),
+ items: allThemes.lightThemes
+ },
+ {
+ title: t("code_block.theme_group_dark"),
+ items: allThemes.darkThemes
+ }
+ ], [allThemes]);
+
+ return { groupedThemes, lightThemes: allThemes.lightThemes, darkThemes: allThemes.darkThemes };
+}
+
+function CodeBlockStyle() {
+ const { groupedThemes, lightThemes, darkThemes } = useCodeBlockThemes();
const [ codeBlockTheme, setCodeBlockTheme ] = useTriliumOption("codeBlockTheme");
+ const [ matchesApp, setMatchesApp ] = useTriliumOptionBool("codeBlockThemeMatchesApp");
+ const [ lightTheme, setLightTheme ] = useTriliumOption("codeBlockThemeLight");
+ const [ darkTheme, setDarkTheme ] = useTriliumOption("codeBlockThemeDark");
const [ codeBlockWordWrap, setCodeBlockWordWrap ] = useTriliumOptionBool("codeBlockWordWrap");
const [ codeBlockTabWidth, setCodeBlockTabWidth ] = useTriliumOption("codeBlockTabWidth");
+ const colorScheme = useColorScheme();
+
+ const effectiveTheme = matchesApp
+ ? (colorScheme === "dark" ? darkTheme : lightTheme)
+ : codeBlockTheme;
+
+ const onThemeChange = (newTheme: string) => {
+ loadHighlightingTheme(newTheme);
+ setCodeBlockTheme(newTheme);
+ };
return (
<OptionsSection title={t("highlighting.title")}>
- <OptionsRow name="code-block-theme" label={t("highlighting.color-scheme")}>
- <FormSelectWithGroups
- values={themes}
- keyProperty="val" titleProperty="title"
- currentValue={codeBlockTheme} onChange={(newTheme) => {
- loadHighlightingTheme(newTheme);
- setCodeBlockTheme(newTheme);
- }}
- />
- </OptionsRow>
+ <ThemeModeSelector matchesApp={matchesApp} onMatchesAppChange={setMatchesApp} />
+
+ {matchesApp ? (
+ <>
+ <OptionsRow name="light-theme" label={t("code_theme.light_theme")}>
+ <FormSelect
+ values={lightThemes}
+ keyProperty="val" titleProperty="title"
+ currentValue={lightTheme} onChange={setLightTheme}
+ />
+ </OptionsRow>
+ <OptionsRow name="dark-theme" label={t("code_theme.dark_theme")}>
+ <FormSelect
+ values={darkThemes}
+ keyProperty="val" titleProperty="title"
+ currentValue={darkTheme} onChange={setDarkTheme}
+ />
+ </OptionsRow>
+ </>
+ ) : (
+ <OptionsRow name="code-block-theme" label={t("highlighting.color-scheme")}>
+ <FormSelectWithGroups
+ values={groupedThemes}
+ keyProperty="val" titleProperty="title"
+ currentValue={codeBlockTheme} onChange={onThemeChange}
+ />
+ </OptionsRow>
+ )}
<OptionsRowWithToggle
name="code-block-word-wrap"
@@ -297,7 +336,7 @@ function CodeBlockStyle() {
/>
</OptionsRow>
- <CodeBlockPreview theme={codeBlockTheme} wordWrap={codeBlockWordWrap} tabWidth={codeBlockTabWidth} />
+ <CodeBlockPreview theme={effectiveTheme} wordWrap={codeBlockWordWrap} tabWidth={codeBlockTabWidth} />
</OptionsSection>
);
}
diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts
index 7b2cd4113b..d430be9ca9 100644
--- a/packages/commons/src/lib/options_interface.ts
+++ b/packages/commons/src/lib/options_interface.ts
@@ -73,6 +73,9 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActi
locale: string;
formattingLocale: string;
codeBlockTheme: string;
+ codeBlockThemeMatchesApp: boolean;
+ codeBlockThemeLight: string;
+ codeBlockThemeDark: string;
textNoteEditorType: string;
layoutOrientation: string;
allowedHtmlTags: string;
diff --git a/packages/highlightjs/src/index.ts b/packages/highlightjs/src/index.ts
index 617aaf7ac5..8984acc4b0 100644
--- a/packages/highlightjs/src/index.ts
+++ b/packages/highlightjs/src/index.ts
@@ -5,7 +5,7 @@ import { type Theme } from "./themes.js";
import { type HighlightOptions } from "highlight.js";
export type { HighlightResult, AutoHighlightResult } from "highlight.js";
-export { default as Themes, type Theme } from "./themes.js";
+export { default as Themes, type Theme, type ThemeVariant, getThemeVariant } from "./themes.js";
const registeredMimeTypes = new Set<string>();
const unsupportedMimeTypes = new Set<string>();
diff --git a/packages/highlightjs/src/themes.ts b/packages/highlightjs/src/themes.ts
index f6d9ac00db..88cb703aa3 100644
--- a/packages/highlightjs/src/themes.ts
+++ b/packages/highlightjs/src/themes.ts
@@ -1,8 +1,14 @@
+export type ThemeVariant = "light" | "dark";
+
export interface Theme {
name: string;
load: () => Promise<{ default: typeof import("*.css", { with: { "resolution-mode": "import" } }); }>;
}
+export function getThemeVariant(theme: Theme): ThemeVariant {
+ return theme.name.includes("Dark") ? "dark" : "light";
+}
+
const themeDefinitions: Record<string, Theme> = {
"1c-light": {
name: "1C (Light)",
diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.ts
index 0802923828..c7eba5aa04 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -30,6 +30,9 @@ const ALLOWED_OPTIONS = new Set<OptionNames>([
"zoomFactor",
"theme",
"codeBlockTheme",
+ "codeBlockThemeMatchesApp",
+ "codeBlockThemeLight",
+ "codeBlockThemeDark",
"codeBlockWordWrap",
"codeBlockTabWidth",
"codeNoteTheme",
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index abde9c7293..3e19a8d44e 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -226,6 +226,9 @@ const defaultOptions: DefaultOption[] = [
},
isSynced: false
},
+ { name: "codeBlockThemeMatchesApp", value: "false", isSynced: false },
+ { name: "codeBlockThemeLight", value: "default:stackoverflow-light", isSynced: false },
+ { name: "codeBlockThemeDark", value: "default:stackoverflow-dark", isSynced: false },
{ name: "codeBlockWordWrap", value: "false", isSynced: true },
{ name: "codeBlockTabWidth", value: "4", isSynced: true },