Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix desktop theme toggle lag

tianma-if87705fd4935a
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index b1013347..4e6225de 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -79,7 +79,7 @@ import { EditorOutline } from "./EditorOutline";
import { useAiBubbleMenu } from "./editor/useAiBubbleMenu";
import { WeChatIcon } from "./WeChatIcon";
import { ThemeToggle } from "./ThemeToggle";
-import { useTheme } from "./ThemeProvider";
+import { useEditorTheme } from "./ThemeProvider";
import { sanitizeAndScopeCss } from "@/lib/css-sandbox";
import { RevisionHistoryDialog } from "./dialogs/RevisionHistoryDialog";
import { ExternalLinkDialog } from "./dialogs/ExternalLinkDialog";
@@ -680,7 +680,7 @@ const RichEditorPane = ({
onRequestMobileNativeEdit,
}: RichEditorPaneProps) => {
const { t, i18n } = useTranslation();
- const { customEditorTheme, editorTheme, resolvedTheme } = useTheme();
+ const { customEditorTheme, editorTheme } = useEditorTheme();
const queryClient = useQueryClient();
const isSelectionMode = Boolean(selectionActionBar);
const [title, setTitle] = useState("");
@@ -3993,7 +3993,6 @@ const RichEditorPane = ({
"--editor-body-font-size": `${MEMO_CONTENT_STYLE.body.fontSize}px`,
"--editor-body-line-height": String(MEMO_CONTENT_STYLE.body.lineHeight / MEMO_CONTENT_STYLE.body.fontSize),
"--editor-paragraph-spacing": `${MEMO_CONTENT_STYLE.body.paragraphSpacing}px`,
- "--memo-content-divider-color": MEMO_CONTENT_STYLE.divider.color[resolvedTheme],
"--memo-content-divider-spacing": `${MEMO_CONTENT_STYLE.divider.marginVertical}px`,
...(editorTheme !== "default" &&
editorTheme !== "minimal-emerald" &&
@@ -4001,13 +4000,20 @@ const RichEditorPane = ({
editorTheme !== "wechat-green" &&
editorTheme !== "modern-mint"
? {
- "--editor-theme-bg": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).background,
- "--editor-theme-text": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).text,
- "--editor-theme-muted": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).muted,
- "--editor-theme-heading": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).heading,
- "--editor-theme-accent": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).accent,
- "--editor-theme-soft": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).soft,
- "--editor-theme-border": (resolvedTheme === "dark" ? customEditorTheme.dark : customEditorTheme.light).border,
+ "--editor-theme-light-bg": customEditorTheme.light.background,
+ "--editor-theme-light-text": customEditorTheme.light.text,
+ "--editor-theme-light-muted": customEditorTheme.light.muted,
+ "--editor-theme-light-heading": customEditorTheme.light.heading,
+ "--editor-theme-light-accent": customEditorTheme.light.accent,
+ "--editor-theme-light-soft": customEditorTheme.light.soft,
+ "--editor-theme-light-border": customEditorTheme.light.border,
+ "--editor-theme-dark-bg": customEditorTheme.dark.background,
+ "--editor-theme-dark-text": customEditorTheme.dark.text,
+ "--editor-theme-dark-muted": customEditorTheme.dark.muted,
+ "--editor-theme-dark-heading": customEditorTheme.dark.heading,
+ "--editor-theme-dark-accent": customEditorTheme.dark.accent,
+ "--editor-theme-dark-soft": customEditorTheme.dark.soft,
+ "--editor-theme-dark-border": customEditorTheme.dark.border,
}
: {}),
} as CSSProperties}
diff --git a/apps/web/src/components/MermaidCodeBlock.tsx b/apps/web/src/components/MermaidCodeBlock.tsx
index 80edf2a1..bae7ea04 100644
--- a/apps/web/src/components/MermaidCodeBlock.tsx
+++ b/apps/web/src/components/MermaidCodeBlock.tsx
@@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import { NodeViewContent, NodeViewWrapper, type NodeViewProps } from "@tiptap/react";
import { useTranslation } from "react-i18next";
import { Check, CircleAlert, Code2, Copy, Maximize2, Workflow } from "lucide-react";
-import { MERMAID_THEME_PALETTES, useTheme } from "./ThemeProvider";
+import { MERMAID_THEME_PALETTES, useMermaidTheme } from "./ThemeProvider";
import { MermaidViewer } from "./MermaidViewer";
import { copyTextToClipboard } from "@/lib/clipboard";
import { renderMermaidWithFallback } from "@/lib/mermaid-renderer";
@@ -34,7 +34,7 @@ const loadBeautifulMermaid = () => {
export const MermaidCodeBlock = ({ editor, node }: NodeViewProps) => {
const { t } = useTranslation();
- const { mermaidRenderer, mermaidTheme, setMermaidRenderer } = useTheme();
+ const { mermaidRenderer, mermaidTheme, setMermaidRenderer } = useMermaidTheme();
const language = typeof node.attrs.language === "string" ? node.attrs.language.toLowerCase() : "plaintext";
const source = node.textContent.trim();
const isMermaid = language === "mermaid";
diff --git a/apps/web/src/components/ThemeProvider.tsx b/apps/web/src/components/ThemeProvider.tsx
index e2794f5d..034c1fa5 100644
--- a/apps/web/src/components/ThemeProvider.tsx
+++ b/apps/web/src/components/ThemeProvider.tsx
@@ -1,4 +1,4 @@
-import { createContext, useContext, useEffect, useMemo, useState, type ReactNode } from "react";
+import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react";
export type ThemePreference = "system" | "light" | "dark";
export type ResolvedTheme = "light" | "dark";
@@ -108,14 +108,20 @@ export const DEFAULT_CUSTOM_EDITOR_THEME: CustomEditorTheme = {
customCss: "",
};
-interface ThemeContextValue {
+interface AppearanceThemeContextValue {
preference: ThemePreference;
resolvedTheme: ResolvedTheme;
setPreference: (preference: ThemePreference) => void;
+}
+
+interface MermaidThemeContextValue {
mermaidTheme: MermaidThemeName;
setMermaidTheme: (theme: MermaidThemeName) => void;
mermaidRenderer: MermaidRenderer;
setMermaidRenderer: (renderer: MermaidRenderer) => void;
+}
+
+interface EditorThemeContextValue {
editorTheme: EditorThemeName;
setEditorTheme: (theme: EditorThemeName) => void;
customEditorThemes: CustomEditorTheme[];
@@ -136,7 +142,9 @@ const CUSTOM_EDITOR_THEME_STORAGE_KEY = "edgeever.custom-editor-theme";
const CUSTOM_EDITOR_THEMES_STORAGE_KEY = "edgeever.custom-editor-themes";
const LIGHT_THEME_COLOR = "#f8fafc";
const DARK_THEME_COLOR = "#0f172a";
-const ThemeContext = createContext<ThemeContextValue | null>(null);
+const AppearanceThemeContext = createContext<AppearanceThemeContextValue | null>(null);
+const MermaidThemeContext = createContext<MermaidThemeContextValue | null>(null);
+const EditorThemeContext = createContext<EditorThemeContextValue | null>(null);
const getSystemTheme = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
@@ -264,9 +272,6 @@ export const ThemeProvider = ({ children }: ThemeProviderProps) => {
const [customEditorThemes, setCustomEditorThemesState] = useState<CustomEditorTheme[]>(getStoredCustomEditorThemes);
useEffect(() => {
- setResolvedTheme(applyThemeToDocument(preference));
- window.localStorage.setItem(THEME_STORAGE_KEY, preference);
-
if (preference !== "system") {
return undefined;
}
@@ -283,62 +288,118 @@ export const ThemeProvider = ({ children }: ThemeProviderProps) => {
return customEditorThemes[0] || DEFAULT_CUSTOM_EDITOR_THEME;
}, [customEditorThemes, editorTheme]);
- const setCustomEditorTheme = (updatedTheme: CustomEditorTheme) => {
- const nextThemes = customEditorThemes.map((t) =>
- t.id === updatedTheme.id ? updatedTheme : t
- );
- if (!customEditorThemes.some((t) => t.id === updatedTheme.id)) {
- if (customEditorThemes.length > 0) {
- nextThemes[0] = { ...customEditorThemes[0], ...updatedTheme };
- } else {
- nextThemes.push(updatedTheme);
+ const setPreference = useCallback((nextPreference: ThemePreference) => {
+ // Apply the class before scheduling React work so a theme toggle can paint
+ // without waiting for the workspace and editor tree to render.
+ setResolvedTheme(applyThemeToDocument(nextPreference));
+ setPreferenceState(nextPreference);
+ window.localStorage.setItem(THEME_STORAGE_KEY, nextPreference);
+ }, []);
+
+ const setCustomEditorTheme = useCallback((updatedTheme: CustomEditorTheme) => {
+ setCustomEditorThemesState((currentThemes) => {
+ const nextThemes = currentThemes.map((theme) =>
+ theme.id === updatedTheme.id ? updatedTheme : theme
+ );
+ if (!currentThemes.some((theme) => theme.id === updatedTheme.id)) {
+ if (currentThemes.length > 0) {
+ nextThemes[0] = { ...currentThemes[0], ...updatedTheme };
+ } else {
+ nextThemes.push(updatedTheme);
+ }
}
- }
- setCustomEditorThemesState(nextThemes);
- window.localStorage.setItem(CUSTOM_EDITOR_THEMES_STORAGE_KEY, JSON.stringify(nextThemes));
- };
+ window.localStorage.setItem(CUSTOM_EDITOR_THEMES_STORAGE_KEY, JSON.stringify(nextThemes));
+ return nextThemes;
+ });
+ }, []);
- const setCustomEditorThemes = (nextThemes: CustomEditorTheme[]) => {
+ const setCustomEditorThemes = useCallback((nextThemes: CustomEditorTheme[]) => {
setCustomEditorThemesState(nextThemes);
window.localStorage.setItem(CUSTOM_EDITOR_THEMES_STORAGE_KEY, JSON.stringify(nextThemes));
- };
+ }, []);
+
+ const setMermaidTheme = useCallback((nextTheme: MermaidThemeName) => {
+ setMermaidThemeState(nextTheme);
+ window.localStorage.setItem(MERMAID_THEME_STORAGE_KEY, nextTheme);
+ }, []);
- const value = useMemo(
+ const setMermaidRenderer = useCallback((nextRenderer: MermaidRenderer) => {
+ setMermaidRendererState(nextRenderer);
+ window.localStorage.setItem(MERMAID_RENDERER_STORAGE_KEY, nextRenderer);
+ }, []);
+
+ const setEditorTheme = useCallback((nextTheme: string) => {
+ setEditorThemeState(nextTheme);
+ window.localStorage.setItem(EDITOR_THEME_STORAGE_KEY, nextTheme);
+ }, []);
+
+ const appearanceValue = useMemo(
() => ({
preference,
resolvedTheme,
- setPreference: (nextPreference: ThemePreference) => setPreferenceState(nextPreference),
+ setPreference,
+ }),
+ [preference, resolvedTheme, setPreference]
+ );
+
+ const mermaidValue = useMemo(
+ () => ({
mermaidTheme,
- setMermaidTheme: (nextTheme: MermaidThemeName) => {
- setMermaidThemeState(nextTheme);
- window.localStorage.setItem(MERMAID_THEME_STORAGE_KEY, nextTheme);
- },
+ setMermaidTheme,
mermaidRenderer,
- setMermaidRenderer: (nextRenderer: MermaidRenderer) => {
- setMermaidRendererState(nextRenderer);
- window.localStorage.setItem(MERMAID_RENDERER_STORAGE_KEY, nextRenderer);
- },
+ setMermaidRenderer,
+ }),
+ [mermaidRenderer, mermaidTheme, setMermaidRenderer, setMermaidTheme]
+ );
+
+ const editorValue = useMemo(
+ () => ({
editorTheme,
- setEditorTheme: (nextTheme: string) => {
- setEditorThemeState(nextTheme);
- window.localStorage.setItem(EDITOR_THEME_STORAGE_KEY, nextTheme);
- },
+ setEditorTheme,
customEditorThemes,
setCustomEditorThemes,
customEditorTheme,
setCustomEditorTheme,
}),
- [customEditorThemes, customEditorTheme, editorTheme, mermaidRenderer, mermaidTheme, preference, resolvedTheme]
+ [customEditorThemes, customEditorTheme, editorTheme, setCustomEditorTheme, setCustomEditorThemes, setEditorTheme]
);
- return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
+ return (
+ <AppearanceThemeContext.Provider value={appearanceValue}>
+ <MermaidThemeContext.Provider value={mermaidValue}>
+ <EditorThemeContext.Provider value={editorValue}>
+ {children}
+ </EditorThemeContext.Provider>
+ </MermaidThemeContext.Provider>
+ </AppearanceThemeContext.Provider>
+ );
+};
+
+export const useAppearanceTheme = () => {
+ const context = useContext(AppearanceThemeContext);
+
+ if (!context) {
+ throw new Error("useAppearanceTheme must be used within ThemeProvider");
+ }
+
+ return context;
+};
+
+export const useMermaidTheme = () => {
+ const context = useContext(MermaidThemeContext);
+
+ if (!context) {
+ throw new Error("useMermaidTheme must be used within ThemeProvider");
+ }
+
+ return context;
};
-export const useTheme = () => {
- const context = useContext(ThemeContext);
+export const useEditorTheme = () => {
+ const context = useContext(EditorThemeContext);
if (!context) {
- throw new Error("useTheme must be used within ThemeProvider");
+ throw new Error("useEditorTheme must be used within ThemeProvider");
}
return context;
diff --git a/apps/web/src/components/ThemeToggle.tsx b/apps/web/src/components/ThemeToggle.tsx
index 4956b057..85bfcc61 100644
--- a/apps/web/src/components/ThemeToggle.tsx
+++ b/apps/web/src/components/ThemeToggle.tsx
@@ -3,11 +3,11 @@ import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
-import { useTheme } from "./ThemeProvider";
+import { useAppearanceTheme } from "./ThemeProvider";
export const ThemeToggle = ({ className, showLabel = false }: { className?: string; showLabel?: boolean }) => {
const { t } = useTranslation();
- const { resolvedTheme, setPreference } = useTheme();
+ const { resolvedTheme, setPreference } = useAppearanceTheme();
const nextTheme = resolvedTheme === "dark" ? "light" : "dark";
const label = nextTheme === "dark" ? t("settings.themeToggleToDark") : t("settings.themeToggleToLight");
diff --git a/apps/web/src/components/settings/PreferenceCard.tsx b/apps/web/src/components/settings/PreferenceCard.tsx
index 692f53fd..90260da1 100644
--- a/apps/web/src/components/settings/PreferenceCard.tsx
+++ b/apps/web/src/components/settings/PreferenceCard.tsx
@@ -23,7 +23,8 @@ import { ShortcutSettingsItem } from "./ShortcutSettingsItem";
import { CustomEditorThemeDialog } from "./CustomEditorThemeDialog";
import {
MERMAID_THEME_NAMES,
- useTheme,
+ useEditorTheme,
+ useMermaidTheme,
DEFAULT_CUSTOM_LIGHT_COLORS,
DEFAULT_CUSTOM_DARK_COLORS,
type CustomEditorTheme,
@@ -49,12 +50,11 @@ export const PreferenceCard = ({
const { t } = useTranslation();
const {
editorTheme,
- mermaidTheme,
customEditorThemes,
setCustomEditorThemes,
setEditorTheme,
- setMermaidTheme,
- } = useTheme();
+ } = useEditorTheme();
+ const { mermaidTheme, setMermaidTheme } = useMermaidTheme();
const [customThemeDialogOpen, setCustomThemeDialogOpen] = useState(false);
const [editingTheme, setEditingTheme] = useState<CustomEditorTheme | null>(null);
const [activeLocalePreference, setActiveLocalePreference] = useState<AppLocalePreference>(() => getAppLocalePreference());
diff --git a/apps/web/src/lib/dark-theme.test.mjs b/apps/web/src/lib/dark-theme.test.mjs
index f572aa2a..8f0d2d07 100644
--- a/apps/web/src/lib/dark-theme.test.mjs
+++ b/apps/web/src/lib/dark-theme.test.mjs
@@ -20,4 +20,14 @@ describe("dark theme contracts", () => {
expect(css).toContain('[class~="divide-slate-100"]');
expect(css).toContain('[class~="text-emerald-700"]');
});
+
+ test("appearance changes stay out of the editor React render path", () => {
+ const editorPane = readFileSync(new URL("../components/EditorPane.tsx", import.meta.url), "utf8");
+ const editorThemeCss = readFileSync(new URL("../styles/editor-themes/base.css", import.meta.url), "utf8");
+
+ expect(editorPane).toContain("useEditorTheme()");
+ expect(editorPane).not.toContain("resolvedTheme");
+ expect(editorThemeCss).toContain(':root.dark .edgeever-editor[data-editor-theme="custom"]:not([data-editor-theme="default"])');
+ expect(editorThemeCss).toContain("--editor-theme-dark-bg");
+ });
});
diff --git a/apps/web/src/styles/editor-themes/base.css b/apps/web/src/styles/editor-themes/base.css
index c1a7fb90..92d357dd 100644
--- a/apps/web/src/styles/editor-themes/base.css
+++ b/apps/web/src/styles/editor-themes/base.css
@@ -15,6 +15,26 @@
--editor-theme-table-hover: color-mix(in srgb, var(--editor-theme-soft) 78%, var(--editor-theme-bg));
background: var(--editor-theme-bg);
}
+
+.edgeever-editor[data-editor-theme="custom"]:not([data-editor-theme="default"]) {
+ --editor-theme-bg: var(--editor-theme-light-bg);
+ --editor-theme-text: var(--editor-theme-light-text);
+ --editor-theme-muted: var(--editor-theme-light-muted);
+ --editor-theme-heading: var(--editor-theme-light-heading);
+ --editor-theme-accent: var(--editor-theme-light-accent);
+ --editor-theme-soft: var(--editor-theme-light-soft);
+ --editor-theme-border: var(--editor-theme-light-border);
+}
+
+:root.dark .edgeever-editor[data-editor-theme="custom"]:not([data-editor-theme="default"]) {
+ --editor-theme-bg: var(--editor-theme-dark-bg);
+ --editor-theme-text: var(--editor-theme-dark-text);
+ --editor-theme-muted: var(--editor-theme-dark-muted);
+ --editor-theme-heading: var(--editor-theme-dark-heading);
+ --editor-theme-accent: var(--editor-theme-dark-accent);
+ --editor-theme-soft: var(--editor-theme-dark-soft);
+ --editor-theme-border: var(--editor-theme-dark-border);
+}
.edgeever-editor[data-editor-theme]:not([data-editor-theme="default"]) .ProseMirror {
background: var(--editor-theme-bg);