Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat(web): add code block theme registry and shell distribution

Dryice Liu7ff6b23c2bcc
diff --git a/web/components/common/code-block-themes.ts b/web/components/common/code-block-themes.ts
new file mode 100644
index 000000000..40dcabb2a
--- /dev/null
+++ b/web/components/common/code-block-themes.ts
@@ -0,0 +1,197 @@
+/// <reference path="./react-syntax-highlighter-prism-themes.d.ts" />
+
+import type { CSSProperties } from "react";
+
+type PrismTheme = {
+ [key: string]: CSSProperties;
+};
+
+// Individual theme imports - using direct imports, not barrel imports
+import oneDark from "react-syntax-highlighter/dist/esm/styles/prism/one-dark";
+import a11yDark from "react-syntax-highlighter/dist/esm/styles/prism/a11y-dark";
+import a11yOneLight from "react-syntax-highlighter/dist/esm/styles/prism/a11y-one-light";
+import atomDark from "react-syntax-highlighter/dist/esm/styles/prism/atom-dark";
+import base16AteliersulphurpoolLight from "react-syntax-highlighter/dist/esm/styles/prism/base16-ateliersulphurpool.light";
+import cb from "react-syntax-highlighter/dist/esm/styles/prism/cb";
+import coldarkCold from "react-syntax-highlighter/dist/esm/styles/prism/coldark-cold";
+import coldarkDark from "react-syntax-highlighter/dist/esm/styles/prism/coldark-dark";
+import coyWithoutShadows from "react-syntax-highlighter/dist/esm/styles/prism/coy-without-shadows";
+import coy from "react-syntax-highlighter/dist/esm/styles/prism/coy";
+import darcula from "react-syntax-highlighter/dist/esm/styles/prism/darcula";
+import dark from "react-syntax-highlighter/dist/esm/styles/prism/dark";
+import dracula from "react-syntax-highlighter/dist/esm/styles/prism/dracula";
+import duotoneDark from "react-syntax-highlighter/dist/esm/styles/prism/duotone-dark";
+import duotoneEarth from "react-syntax-highlighter/dist/esm/styles/prism/duotone-earth";
+import duotoneForest from "react-syntax-highlighter/dist/esm/styles/prism/duotone-forest";
+import duotoneLight from "react-syntax-highlighter/dist/esm/styles/prism/duotone-light";
+import duotoneSea from "react-syntax-highlighter/dist/esm/styles/prism/duotone-sea";
+import duotoneSpace from "react-syntax-highlighter/dist/esm/styles/prism/duotone-space";
+import funky from "react-syntax-highlighter/dist/esm/styles/prism/funky";
+import ghcolors from "react-syntax-highlighter/dist/esm/styles/prism/ghcolors";
+import gruvboxDark from "react-syntax-highlighter/dist/esm/styles/prism/gruvbox-dark";
+import gruvboxLight from "react-syntax-highlighter/dist/esm/styles/prism/gruvbox-light";
+import holiTheme from "react-syntax-highlighter/dist/esm/styles/prism/holi-theme";
+import hopscotch from "react-syntax-highlighter/dist/esm/styles/prism/hopscotch";
+import lucario from "react-syntax-highlighter/dist/esm/styles/prism/lucario";
+import materialDark from "react-syntax-highlighter/dist/esm/styles/prism/material-dark";
+import materialLight from "react-syntax-highlighter/dist/esm/styles/prism/material-light";
+import materialOceanic from "react-syntax-highlighter/dist/esm/styles/prism/material-oceanic";
+import nightOwl from "react-syntax-highlighter/dist/esm/styles/prism/night-owl";
+import nord from "react-syntax-highlighter/dist/esm/styles/prism/nord";
+import okaidia from "react-syntax-highlighter/dist/esm/styles/prism/okaidia";
+import oneLight from "react-syntax-highlighter/dist/esm/styles/prism/one-light";
+import pojoaque from "react-syntax-highlighter/dist/esm/styles/prism/pojoaque";
+import prism from "react-syntax-highlighter/dist/esm/styles/prism/prism";
+import shadesOfPurple from "react-syntax-highlighter/dist/esm/styles/prism/shades-of-purple";
+import solarizedDarkAtom from "react-syntax-highlighter/dist/esm/styles/prism/solarized-dark-atom";
+import solarizedlight from "react-syntax-highlighter/dist/esm/styles/prism/solarizedlight";
+import synthwave84 from "react-syntax-highlighter/dist/esm/styles/prism/synthwave84";
+import tomorrow from "react-syntax-highlighter/dist/esm/styles/prism/tomorrow";
+import twilight from "react-syntax-highlighter/dist/esm/styles/prism/twilight";
+import vsDark from "react-syntax-highlighter/dist/esm/styles/prism/vs-dark";
+import vs from "react-syntax-highlighter/dist/esm/styles/prism/vs";
+import vscDarkPlus from "react-syntax-highlighter/dist/esm/styles/prism/vsc-dark-plus";
+import xonokai from "react-syntax-highlighter/dist/esm/styles/prism/xonokai";
+import zTouch from "react-syntax-highlighter/dist/esm/styles/prism/z-touch";
+
+/** Saved ID for a code block theme. Used in storage and as a stable identifier.
+ *
+ * These are camelCase versions of the Prism file names (e.g., "one-dark.js" → "oneDark").
+ * They remain stable across updates and are safe to persist.
+ */
+export type CodeBlockThemeId =
+ | "oneDark"
+ | "a11yDark"
+ | "a11yOneLight"
+ | "atomDark"
+ | "base16AteliersulphurpoolLight"
+ | "cb"
+ | "coldarkCold"
+ | "coldarkDark"
+ | "coyWithoutShadows"
+ | "coy"
+ | "darcula"
+ | "dark"
+ | "dracula"
+ | "duotoneDark"
+ | "duotoneEarth"
+ | "duotoneForest"
+ | "duotoneLight"
+ | "duotoneSea"
+ | "duotoneSpace"
+ | "funky"
+ | "ghcolors"
+ | "gruvboxDark"
+ | "gruvboxLight"
+ | "holiTheme"
+ | "hopscotch"
+ | "lucario"
+ | "materialDark"
+ | "materialLight"
+ | "materialOceanic"
+ | "nightOwl"
+ | "nord"
+ | "okaidia"
+ | "oneLight"
+ | "pojoaque"
+ | "prism"
+ | "shadesOfPurple"
+ | "solarizedDarkAtom"
+ | "solarizedlight"
+ | "synthwave84"
+ | "tomorrow"
+ | "twilight"
+ | "vsDark"
+ | "vs"
+ | "vscDarkPlus"
+ | "xonokai"
+ | "zTouch";
+
+/** A single Prism theme option with display label and the style object. */
+export interface CodeBlockThemeOption {
+ /** Stable camelCase ID used for storage and lookup. */
+ id: CodeBlockThemeId;
+ /** Human-readable label (e.g., "One Dark", "VSC Dark Plus"). */
+ label: string;
+ /** The Prism style object for syntax highlighting. */
+ style: PrismTheme;
+}
+
+/** All installed Prism themes available as selectable options. */
+export const CODE_BLOCK_THEME_OPTIONS: readonly CodeBlockThemeOption[] = [
+ { id: "a11yDark", label: "A11Y Dark", style: a11yDark },
+ { id: "a11yOneLight", label: "A11Y One Light", style: a11yOneLight },
+ { id: "atomDark", label: "Atom Dark", style: atomDark },
+ { id: "base16AteliersulphurpoolLight", label: "Base16 Atelier Sulphurpool Light", style: base16AteliersulphurpoolLight },
+ { id: "cb", label: "CB", style: cb },
+ { id: "coldarkCold", label: "Coldark Cold", style: coldarkCold },
+ { id: "coldarkDark", label: "Coldark Dark", style: coldarkDark },
+ { id: "coyWithoutShadows", label: "Coy Without Shadows", style: coyWithoutShadows },
+ { id: "coy", label: "Coy", style: coy },
+ { id: "darcula", label: "Darcula", style: darcula },
+ { id: "dark", label: "Dark", style: dark },
+ { id: "dracula", label: "Dracula", style: dracula },
+ { id: "duotoneDark", label: "Duotone Dark", style: duotoneDark },
+ { id: "duotoneEarth", label: "Duotone Earth", style: duotoneEarth },
+ { id: "duotoneForest", label: "Duotone Forest", style: duotoneForest },
+ { id: "duotoneLight", label: "Duotone Light", style: duotoneLight },
+ { id: "duotoneSea", label: "Duotone Sea", style: duotoneSea },
+ { id: "duotoneSpace", label: "Duotone Space", style: duotoneSpace },
+ { id: "funky", label: "Funky", style: funky },
+ { id: "ghcolors", label: "GH Colors", style: ghcolors },
+ { id: "gruvboxDark", label: "Gruvbox Dark", style: gruvboxDark },
+ { id: "gruvboxLight", label: "Gruvbox Light", style: gruvboxLight },
+ { id: "holiTheme", label: "Holi Theme", style: holiTheme },
+ { id: "hopscotch", label: "Hopscotch", style: hopscotch },
+ { id: "lucario", label: "Lucario", style: lucario },
+ { id: "materialDark", label: "Material Dark", style: materialDark },
+ { id: "materialLight", label: "Material Light", style: materialLight },
+ { id: "materialOceanic", label: "Material Oceanic", style: materialOceanic },
+ { id: "nightOwl", label: "Night Owl", style: nightOwl },
+ { id: "nord", label: "Nord", style: nord },
+ { id: "okaidia", label: "Okaidia", style: okaidia },
+ { id: "oneDark", label: "One Dark", style: oneDark },
+ { id: "oneLight", label: "One Light", style: oneLight },
+ { id: "pojoaque", label: "Pojoaque", style: pojoaque },
+ { id: "prism", label: "Prism", style: prism },
+ { id: "shadesOfPurple", label: "Shades of Purple", style: shadesOfPurple },
+ { id: "solarizedDarkAtom", label: "Solarized Dark Atom", style: solarizedDarkAtom },
+ { id: "solarizedlight", label: "Solarized Light", style: solarizedlight },
+ { id: "synthwave84", label: "Synthwave 84", style: synthwave84 },
+ { id: "tomorrow", label: "Tomorrow", style: tomorrow },
+ { id: "twilight", label: "Twilight", style: twilight },
+ { id: "vsDark", label: "VS Dark", style: vsDark },
+ { id: "vs", label: "VS", style: vs },
+ { id: "vscDarkPlus", label: "VSC Dark Plus", style: vscDarkPlus },
+ { id: "xonokai", label: "Xonokai", style: xonokai },
+ { id: "zTouch", label: "Z Touch", style: zTouch },
+] as const;
+
+/** The default theme ID when no user preference is set. */
+export const DEFAULT_CODE_BLOCK_THEME_ID: CodeBlockThemeId = "oneDark";
+
+/** Get a Prism theme style by ID, falling back to oneDark for invalid IDs. */
+export function getCodeBlockTheme(id: string): PrismTheme {
+ const option = CODE_BLOCK_THEME_OPTIONS.find((opt) => opt.id === id);
+ return option?.style ?? oneDark;
+}
+
+/** Extract the background color from a Prism style object.
+ *
+ * Returns the background color if available, or undefined if not.
+ * This is used for theme-derived background styling.
+ */
+export function getCodeBlockThemeBackground(style: PrismTheme): string | undefined {
+ const preStyle = style['pre[class*="language-"]'];
+ if (preStyle && typeof preStyle === "object") {
+ const backgroundColor = preStyle.backgroundColor;
+ if (typeof backgroundColor === "string") {
+ return backgroundColor;
+ }
+ const background = preStyle.background;
+ if (typeof background === "string") {
+ return background;
+ }
+ }
+ return undefined;
+}
diff --git a/web/components/common/react-syntax-highlighter-prism-themes.d.ts b/web/components/common/react-syntax-highlighter-prism-themes.d.ts
new file mode 100644
index 000000000..a73be0e8e
--- /dev/null
+++ b/web/components/common/react-syntax-highlighter-prism-themes.d.ts
@@ -0,0 +1,6 @@
+declare module "react-syntax-highlighter/dist/esm/styles/prism/*" {
+ import type { CSSProperties } from "react";
+
+ const theme: Record<string, CSSProperties>;
+ export default theme;
+}
diff --git a/web/components/settings/SettingsContext.tsx b/web/components/settings/SettingsContext.tsx
index dfccb8c3e..c4cfba0ba 100644
--- a/web/components/settings/SettingsContext.tsx
+++ b/web/components/settings/SettingsContext.tsx
@@ -13,7 +13,20 @@ import {
import { useRouter } from "next/navigation";
import { useTranslation } from "react-i18next";
-import { writeStoredLanguage } from "@/context/app-shell-storage";
+import type { CodeBlockThemeId } from "@/components/common/code-block-themes";
+import {
+ DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS,
+ DEFAULT_CODE_BLOCK_WRAP_LONG_LINES,
+ normalizeCodeBlockTheme,
+ readStoredCodeBlockShowLineNumbers,
+ readStoredCodeBlockTheme,
+ readStoredCodeBlockWrapLongLines,
+ writeStoredCodeBlockShowLineNumbers,
+ writeStoredCodeBlockTheme,
+ writeStoredCodeBlockWrapLongLines,
+ writeStoredLanguage,
+} from "@/context/app-shell-storage";
+import { useAppShell } from "@/context/AppShellContext";
import { apiFetch, apiUrl } from "@/lib/api";
import { setTheme as applyThemePreference } from "@/lib/theme";
@@ -100,8 +113,51 @@ export type Catalog = {
export type UiSettings = {
theme: "light" | "dark" | "glass" | "snow";
language: "en" | "zh";
+ code_block_theme: string;
+ code_block_show_line_numbers: boolean;
+ code_block_wrap_long_lines: boolean;
};
+type CodeBlockUiSettings = Pick<
+ UiSettings,
+ | "code_block_theme"
+ | "code_block_show_line_numbers"
+ | "code_block_wrap_long_lines"
+>;
+
+type UiSettingsPatch = Partial<UiSettings>;
+
+export function syncLoadedCodeBlockSettingsToAppShell(
+ ui: Partial<CodeBlockUiSettings>,
+): CodeBlockUiSettings {
+ const normalized = {
+ code_block_theme: normalizeCodeBlockTheme(ui.code_block_theme),
+ code_block_show_line_numbers:
+ ui.code_block_show_line_numbers === true ||
+ String(ui.code_block_show_line_numbers).toLowerCase() === "true",
+ code_block_wrap_long_lines:
+ ui.code_block_wrap_long_lines === true ||
+ String(ui.code_block_wrap_long_lines).toLowerCase() === "true",
+ };
+
+ writeStoredCodeBlockTheme(normalized.code_block_theme);
+ writeStoredCodeBlockShowLineNumbers(normalized.code_block_show_line_numbers);
+ writeStoredCodeBlockWrapLongLines(normalized.code_block_wrap_long_lines);
+
+ return normalized;
+}
+
+export async function persistUiSettingsPatch(
+ patch: UiSettingsPatch,
+ fetcher: typeof apiFetch = apiFetch,
+): Promise<void> {
+ await fetcher(apiUrl("/api/v1/settings/ui"), {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify(patch),
+ });
+}
+
export type ProviderOption = {
value: string;
label: string;
@@ -389,12 +445,18 @@ type SettingsContextValue = {
hasUnsavedChanges: boolean;
theme: UiSettings["theme"];
language: UiSettings["language"];
+ codeBlockTheme: UiSettings["code_block_theme"];
+ codeBlockShowLineNumbers: UiSettings["code_block_show_line_numbers"];
+ codeBlockWrapLongLines: UiSettings["code_block_wrap_long_lines"];
toast: string;
setToast: (value: string) => void;
// UI prefs
updateTheme: (next: UiSettings["theme"]) => Promise<void>;
updateLanguage: (next: UiSettings["language"]) => Promise<void>;
+ updateCodeBlockTheme: (next: CodeBlockThemeId) => Promise<void>;
+ updateCodeBlockShowLineNumbers: (next: boolean) => Promise<void>;
+ updateCodeBlockWrapLongLines: (next: boolean) => Promise<void>;
// Catalog mutation
mutateCatalog: (mutator: (next: Catalog) => void) => void;
@@ -466,10 +528,24 @@ export function useSettings(): SettingsContextValue {
export function SettingsProvider({ children }: { children: ReactNode }) {
const { t } = useTranslation();
const router = useRouter();
+ const {
+ setCodeBlockTheme: setAppShellCodeBlockTheme,
+ setCodeBlockShowLineNumbers: setAppShellCodeBlockShowLineNumbers,
+ setCodeBlockWrapLongLines: setAppShellCodeBlockWrapLongLines,
+ } = useAppShell();
const [status, setStatus] = useState<SystemStatus | null>(null);
const [theme, setTheme] = useState<UiSettings["theme"]>("snow");
const [language, setLanguage] = useState<UiSettings["language"]>("en");
+ const [codeBlockTheme, setCodeBlockTheme] = useState<
+ UiSettings["code_block_theme"]
+ >(() => readStoredCodeBlockTheme());
+ const [codeBlockShowLineNumbers, setCodeBlockShowLineNumbers] = useState<
+ UiSettings["code_block_show_line_numbers"]
+ >(DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS);
+ const [codeBlockWrapLongLines, setCodeBlockWrapLongLines] = useState<
+ UiSettings["code_block_wrap_long_lines"]
+ >(DEFAULT_CODE_BLOCK_WRAP_LONG_LINES);
const [catalog, setCatalog] = useState<Catalog>(defaultCatalog());
const [draft, setDraft] = useState<Catalog>(defaultCatalog());
const [catalogEditable, setCatalogEditable] = useState<boolean | null>(null);
@@ -553,6 +629,23 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
}
setTheme(payload.ui.theme);
setLanguage(payload.ui.language);
+ const syncedCodeBlockSettings = syncLoadedCodeBlockSettingsToAppShell(
+ payload.ui,
+ );
+ setAppShellCodeBlockTheme(syncedCodeBlockSettings.code_block_theme);
+ setAppShellCodeBlockShowLineNumbers(
+ syncedCodeBlockSettings.code_block_show_line_numbers,
+ );
+ setAppShellCodeBlockWrapLongLines(
+ syncedCodeBlockSettings.code_block_wrap_long_lines,
+ );
+ setCodeBlockTheme(syncedCodeBlockSettings.code_block_theme);
+ setCodeBlockShowLineNumbers(
+ syncedCodeBlockSettings.code_block_show_line_numbers,
+ );
+ setCodeBlockWrapLongLines(
+ syncedCodeBlockSettings.code_block_wrap_long_lines,
+ );
if (payload.providers) setProviders(payload.providers);
settingsLoaded = true;
} catch (err) {
@@ -584,10 +677,24 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
);
}
}
- }, [t]);
+ }, [
+ setAppShellCodeBlockShowLineNumbers,
+ setAppShellCodeBlockTheme,
+ setAppShellCodeBlockWrapLongLines,
+ t,
+ ]);
// Load settings + status once on mount. Subsequent navigations between
// settings sub-pages share this state via the layout-level provider.
+ useEffect(() => {
+ // Hydrate client-only switch state after the SSR-safe first render. Reading
+ // localStorage in the initial state can leave React with server-rendered
+ // aria-checked="false" DOM when the persisted client value is already true.
+ // eslint-disable-next-line react-hooks/set-state-in-effect
+ setCodeBlockShowLineNumbers(readStoredCodeBlockShowLineNumbers());
+ setCodeBlockWrapLongLines(readStoredCodeBlockWrapLongLines());
+ }, []);
+
useEffect(() => {
loadSettings();
return () => {
@@ -613,38 +720,45 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
}, [diagnosticsResults]);
// ── UI preferences ──────────────────────────────────────────────────────
- const persistUi = useCallback(
- async (
- nextTheme: UiSettings["theme"],
- nextLanguage: UiSettings["language"],
- ) => {
- await apiFetch(apiUrl("/api/v1/settings/ui"), {
- method: "PUT",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ theme: nextTheme, language: nextLanguage }),
- });
- },
- [],
- );
-
const updateTheme = useCallback(
async (next: UiSettings["theme"]) => {
setTheme(next);
applyThemePreference(next);
- await persistUi(next, language);
+ await persistUiSettingsPatch({ theme: next });
},
- [language, persistUi],
+ [],
);
const updateLanguage = useCallback(
async (next: UiSettings["language"]) => {
setLanguage(next);
writeStoredLanguage(next);
- await persistUi(theme, next);
+ await persistUiSettingsPatch({ language: next });
},
- [persistUi, theme],
+ [],
+ );
+
+ const updateCodeBlockTheme = useCallback(
+ async (next: CodeBlockThemeId) => {
+ setCodeBlockTheme(next);
+ writeStoredCodeBlockTheme(next);
+ await persistUiSettingsPatch({ code_block_theme: next });
+ },
+ [],
);
+ const updateCodeBlockShowLineNumbers = useCallback(async (next: boolean) => {
+ setCodeBlockShowLineNumbers(next);
+ writeStoredCodeBlockShowLineNumbers(next);
+ await persistUiSettingsPatch({ code_block_show_line_numbers: next });
+ }, []);
+
+ const updateCodeBlockWrapLongLines = useCallback(async (next: boolean) => {
+ setCodeBlockWrapLongLines(next);
+ writeStoredCodeBlockWrapLongLines(next);
+ await persistUiSettingsPatch({ code_block_wrap_long_lines: next });
+ }, []);
+
// ── Catalog mutators ────────────────────────────────────────────────────
const mutateCatalog = useCallback((mutator: (next: Catalog) => void) => {
setDraft((current) => {
@@ -1165,10 +1279,16 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
hasUnsavedChanges,
theme,
language,
+ codeBlockTheme,
+ codeBlockShowLineNumbers,
+ codeBlockWrapLongLines,
toast,
setToast,
updateTheme,
updateLanguage,
+ updateCodeBlockTheme,
+ updateCodeBlockShowLineNumbers,
+ updateCodeBlockWrapLongLines,
mutateCatalog,
addProfile,
removeActiveProfile,
@@ -1205,6 +1325,9 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
applying,
catalog,
catalogEditable,
+ codeBlockShowLineNumbers,
+ codeBlockTheme,
+ codeBlockWrapLongLines,
diagnosticsResults,
draft,
embeddingCapabilities,
@@ -1233,6 +1356,9 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
theme,
toast,
tourStepIndex,
+ updateCodeBlockShowLineNumbers,
+ updateCodeBlockTheme,
+ updateCodeBlockWrapLongLines,
updateContextWindowField,
updateLanguage,
updateModelBoolField,
diff --git a/web/context/AppShellContext.tsx b/web/context/AppShellContext.tsx
index 08d3ebc01..2c2f05726 100644
--- a/web/context/AppShellContext.tsx
+++ b/web/context/AppShellContext.tsx
@@ -18,15 +18,28 @@ import {
import {
ACTIVE_SESSION_EVENT,
ACTIVE_SESSION_STORAGE_KEY,
+ CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
+ CODE_BLOCK_SETTINGS_EVENT,
+ CODE_BLOCK_THEME_STORAGE_KEY,
+ CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
LANGUAGE_EVENT,
LANGUAGE_STORAGE_KEY,
SIDEBAR_COLLAPSED_EVENT,
SIDEBAR_COLLAPSED_STORAGE_KEY,
+ normalizeCodeBlockShowLineNumbers,
+ normalizeCodeBlockTheme,
+ normalizeCodeBlockWrapLongLines,
normalizeLanguage,
readStoredActiveSessionId,
+ readStoredCodeBlockShowLineNumbers,
+ readStoredCodeBlockTheme,
+ readStoredCodeBlockWrapLongLines,
readStoredLanguage,
readStoredSidebarCollapsed,
writeStoredActiveSessionId,
+ writeStoredCodeBlockShowLineNumbers,
+ writeStoredCodeBlockTheme,
+ writeStoredCodeBlockWrapLongLines,
writeStoredLanguage,
writeStoredSidebarCollapsed,
type AppLanguage,
@@ -41,6 +54,12 @@ interface AppShellContextValue {
setActiveSessionId: (sessionId: string | null) => void;
sidebarCollapsed: boolean;
setSidebarCollapsed: (collapsed: boolean) => void;
+ codeBlockTheme: string;
+ setCodeBlockTheme: (theme: string) => void;
+ codeBlockShowLineNumbers: boolean;
+ setCodeBlockShowLineNumbers: (show: boolean) => void;
+ codeBlockWrapLongLines: boolean;
+ setCodeBlockWrapLongLines: (wrap: boolean) => void;
}
const AppShellContext = createContext<AppShellContextValue | null>(null);
@@ -56,12 +75,23 @@ export function AppShellProvider({ children }: { children: React.ReactNode }) {
);
// Always start expanded to match SSR; hydrate from localStorage after mount
const [sidebarCollapsed, setSidebarCollapsedState] = useState<boolean>(false);
+ // Code block settings - start with defaults, hydrate from localStorage after mount
+ const [codeBlockTheme, setCodeBlockThemeState] = useState<string>(() =>
+ readStoredCodeBlockTheme(),
+ );
+ const [codeBlockShowLineNumbers, setCodeBlockShowLineNumbersState] =
+ useState<boolean>(() => readStoredCodeBlockShowLineNumbers());
+ const [codeBlockWrapLongLines, setCodeBlockWrapLongLinesState] =
+ useState<boolean>(() => readStoredCodeBlockWrapLongLines());
useEffect(() => {
// Hydrate client-only preferences after SSR-safe first render.
// eslint-disable-next-line react-hooks/set-state-in-effect
setLanguageState(readStoredLanguage());
setSidebarCollapsedState(readStoredSidebarCollapsed());
+ setCodeBlockThemeState(readStoredCodeBlockTheme());
+ setCodeBlockShowLineNumbersState(readStoredCodeBlockShowLineNumbers());
+ setCodeBlockWrapLongLinesState(readStoredCodeBlockWrapLongLines());
}, []);
useEffect(() => {
@@ -83,6 +113,19 @@ export function AppShellProvider({ children }: { children: React.ReactNode }) {
if (event.key === SIDEBAR_COLLAPSED_STORAGE_KEY) {
setSidebarCollapsedState(event.newValue === "1");
}
+ if (event.key === CODE_BLOCK_THEME_STORAGE_KEY) {
+ setCodeBlockThemeState(normalizeCodeBlockTheme(event.newValue));
+ }
+ if (event.key === CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY) {
+ setCodeBlockShowLineNumbersState(
+ normalizeCodeBlockShowLineNumbers(event.newValue),
+ );
+ }
+ if (event.key === CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY) {
+ setCodeBlockWrapLongLinesState(
+ normalizeCodeBlockWrapLongLines(event.newValue),
+ );
+ }
};
const onLanguage = (event: Event) => {
@@ -101,16 +144,38 @@ export function AppShellProvider({ children }: { children: React.ReactNode }) {
setSidebarCollapsedState(Boolean(detail?.collapsed));
};
+ const onCodeBlockSettings = (event: Event) => {
+ const detail = (
+ event as CustomEvent<{
+ codeBlockTheme?: string;
+ codeBlockShowLineNumbers?: boolean;
+ codeBlockWrapLongLines?: boolean;
+ }>
+ ).detail;
+
+ if (detail?.codeBlockTheme !== undefined) {
+ setCodeBlockThemeState(normalizeCodeBlockTheme(detail.codeBlockTheme));
+ }
+ if (detail?.codeBlockShowLineNumbers !== undefined) {
+ setCodeBlockShowLineNumbersState(Boolean(detail.codeBlockShowLineNumbers));
+ }
+ if (detail?.codeBlockWrapLongLines !== undefined) {
+ setCodeBlockWrapLongLinesState(Boolean(detail.codeBlockWrapLongLines));
+ }
+ };
+
window.addEventListener("storage", onStorage);
window.addEventListener(LANGUAGE_EVENT, onLanguage);
window.addEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
window.addEventListener(SIDEBAR_COLLAPSED_EVENT, onSidebarCollapsed);
+ window.addEventListener(CODE_BLOCK_SETTINGS_EVENT, onCodeBlockSettings);
return () => {
window.removeEventListener("storage", onStorage);
window.removeEventListener(LANGUAGE_EVENT, onLanguage);
window.removeEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
window.removeEventListener(SIDEBAR_COLLAPSED_EVENT, onSidebarCollapsed);
+ window.removeEventListener(CODE_BLOCK_SETTINGS_EVENT, onCodeBlockSettings);
};
}, []);
@@ -134,6 +199,22 @@ export function AppShellProvider({ children }: { children: React.ReactNode }) {
setSidebarCollapsedState(collapsed);
}, []);
+ const setCodeBlockTheme = useCallback((nextTheme: string) => {
+ const normalizedTheme = normalizeCodeBlockTheme(nextTheme);
+ writeStoredCodeBlockTheme(normalizedTheme);
+ setCodeBlockThemeState(normalizedTheme);
+ }, []);
+
+ const setCodeBlockShowLineNumbers = useCallback((show: boolean) => {
+ writeStoredCodeBlockShowLineNumbers(show);
+ setCodeBlockShowLineNumbersState(show);
+ }, []);
+
+ const setCodeBlockWrapLongLines = useCallback((wrap: boolean) => {
+ writeStoredCodeBlockWrapLongLines(wrap);
+ setCodeBlockWrapLongLinesState(wrap);
+ }, []);
+
const value = useMemo<AppShellContextValue>(
() => ({
theme,
@@ -144,11 +225,23 @@ export function AppShellProvider({ children }: { children: React.ReactNode }) {
setActiveSessionId,
sidebarCollapsed,
setSidebarCollapsed,
+ codeBlockTheme,
+ setCodeBlockTheme,
+ codeBlockShowLineNumbers,
+ setCodeBlockShowLineNumbers,
+ codeBlockWrapLongLines,
+ setCodeBlockWrapLongLines,
}),
[
activeSessionId,
+ codeBlockShowLineNumbers,
+ codeBlockTheme,
+ codeBlockWrapLongLines,
language,
setActiveSessionId,
+ setCodeBlockShowLineNumbers,
+ setCodeBlockTheme,
+ setCodeBlockWrapLongLines,
setLanguage,
setSidebarCollapsed,
setTheme,
diff --git a/web/context/app-shell-storage.ts b/web/context/app-shell-storage.ts
index 44e14ce9e..3f938bae9 100644
--- a/web/context/app-shell-storage.ts
+++ b/web/context/app-shell-storage.ts
@@ -7,6 +7,11 @@ export const LANGUAGE_STORAGE_KEY = "deeptutor-language";
export const SIDEBAR_COLLAPSED_STORAGE_KEY = "deeptutor.sidebarCollapsed";
export const CHAT_RESPONSE_TIMEOUT_STORAGE_KEY =
"deeptutor.chatResponseTimeout";
+export const CODE_BLOCK_THEME_STORAGE_KEY = "deeptutor.code-block-theme";
+export const CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY =
+ "deeptutor.code-block-show-line-numbers";
+export const CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY =
+ "deeptutor.code-block-wrap-long-lines";
// Mirror of the per-user ``chat_response_timeout`` UI preference. Cached in
// localStorage so the chat watchdog (a separate provider from Settings) can
@@ -52,6 +57,7 @@ export function writeStoredChatResponseTimeout(seconds: number): void {
export const ACTIVE_SESSION_EVENT = "deeptutor:active-session";
export const LANGUAGE_EVENT = "deeptutor:language";
export const SIDEBAR_COLLAPSED_EVENT = "deeptutor:sidebar-collapsed";
+export const CODE_BLOCK_SETTINGS_EVENT = "deeptutor:code-block-settings";
export function normalizeLanguage(
value: string | null | undefined,
@@ -134,3 +140,116 @@ export function writeStoredSidebarCollapsed(collapsed: boolean): void {
// localStorage may be unavailable
}
}
+
+// Code block settings defaults
+export const DEFAULT_CODE_BLOCK_THEME = "oneDark";
+export const DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS = false;
+export const DEFAULT_CODE_BLOCK_WRAP_LONG_LINES = false;
+
+export function normalizeCodeBlockTheme(
+ value: string | null | undefined,
+): string {
+ if (!value || value.trim() === "") return DEFAULT_CODE_BLOCK_THEME;
+ return value.trim();
+}
+
+export function readStoredCodeBlockTheme(): string {
+ if (typeof window === "undefined") return DEFAULT_CODE_BLOCK_THEME;
+ try {
+ const raw = window.localStorage.getItem(CODE_BLOCK_THEME_STORAGE_KEY);
+ return normalizeCodeBlockTheme(raw);
+ } catch {
+ return DEFAULT_CODE_BLOCK_THEME;
+ }
+}
+
+export function writeStoredCodeBlockTheme(theme: string): void {
+ if (typeof window === "undefined") return;
+ try {
+ window.localStorage.setItem(
+ CODE_BLOCK_THEME_STORAGE_KEY,
+ normalizeCodeBlockTheme(theme),
+ );
+ window.dispatchEvent(
+ new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
+ detail: { codeBlockTheme: normalizeCodeBlockTheme(theme) },
+ }),
+ );
+ } catch {
+ // localStorage may be unavailable
+ }
+}
+
+export function normalizeCodeBlockShowLineNumbers(
+ value: string | null | undefined,
+): boolean {
+ if (value === "true") return true;
+ if (value === "false") return false;
+ return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
+}
+
+export function readStoredCodeBlockShowLineNumbers(): boolean {
+ if (typeof window === "undefined") return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
+ try {
+ const raw = window.localStorage.getItem(
+ CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
+ );
+ return normalizeCodeBlockShowLineNumbers(raw);
+ } catch {
+ return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
+ }
+}
+
+export function writeStoredCodeBlockShowLineNumbers(show: boolean): void {
+ if (typeof window === "undefined") return;
+ try {
+ window.localStorage.setItem(
+ CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
+ String(show),
+ );
+ window.dispatchEvent(
+ new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
+ detail: { codeBlockShowLineNumbers: show },
+ }),
+ );
+ } catch {
+ // localStorage may be unavailable
+ }
+}
+
+export function normalizeCodeBlockWrapLongLines(
+ value: string | null | undefined,
+): boolean {
+ if (value === "true") return true;
+ if (value === "false") return false;
+ return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
+}
+
+export function readStoredCodeBlockWrapLongLines(): boolean {
+ if (typeof window === "undefined") return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
+ try {
+ const raw = window.localStorage.getItem(
+ CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
+ );
+ return normalizeCodeBlockWrapLongLines(raw);
+ } catch {
+ return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
+ }
+}
+
+export function writeStoredCodeBlockWrapLongLines(wrap: boolean): void {
+ if (typeof window === "undefined") return;
+ try {
+ window.localStorage.setItem(
+ CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
+ String(wrap),
+ );
+ window.dispatchEvent(
+ new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
+ detail: { codeBlockWrapLongLines: wrap },
+ }),
+ );
+ } catch {
+ // localStorage may be unavailable
+ }
+}