Skip to content

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

fix(web): keep phone preview in sync when switching editor themes

tianma-if82efbe4a3c2f
diff --git a/apps/web/src/components/EditorPhonePreview.tsx b/apps/web/src/components/EditorPhonePreview.tsx
index 54f0806f..49aa3e6f 100644
--- a/apps/web/src/components/EditorPhonePreview.tsx
+++ b/apps/web/src/components/EditorPhonePreview.tsx
@@ -80,7 +80,11 @@ export const EditorPhonePreview = ({ editor, title, scrollContainer, className }
const sync = () => {
if (editor.isDestroyed) return;
const generation = ++previewSyncGeneration.current;
- const root = preparePublishArticle(editor.getHTML(), editor.view.dom);
+ const root = preparePublishArticle(editor.getHTML(), editor.view.dom, editorTheme);
+ setMarkup({
+ html: root.innerHTML,
+ style: root.getAttribute("style") ?? "",
+ });
void embedMermaidForPreview(root, editor, mermaidTheme).then(() => {
if (generation !== previewSyncGeneration.current) return;
setMarkup({
@@ -99,6 +103,10 @@ export const EditorPhonePreview = ({ editor, title, scrollContainer, className }
};
}, [editor, editorTheme, mermaidTheme]);
+ useEffect(() => {
+ previewRef.current?.setAttribute("style", markup.style);
+ }, [markup.style]);
+
const handleFollowChange = (enabled: boolean) => {
setFollow(enabled);
writeEditorPhonePreviewFollowPreference(enabled);
@@ -198,16 +206,14 @@ export const EditorPhonePreview = ({ editor, title, scrollContainer, className }
<div className="edgeever-phone-device__island" aria-hidden="true">
<span className="edgeever-phone-device__lens" />
</div>
- <div
- className="edgeever-phone-preview"
- ref={(node) => {
- previewRef.current = node;
- if (node) node.setAttribute("style", markup.style);
- }}
- >
+ <div className="edgeever-phone-preview" ref={previewRef}>
{title?.trim() ? <p className="edgeever-phone-shell-title">{title.trim()}</p> : null}
{markup.html ? (
- <div className="edgeever-phone-article" dangerouslySetInnerHTML={{ __html: markup.html }} />
+ <div
+ key={editorTheme}
+ className="edgeever-phone-article"
+ dangerouslySetInnerHTML={{ __html: markup.html }}
+ />
) : (
<p className="pt-10 text-center text-sm text-slate-400">{t("editor.phonePreviewEmpty")}</p>
)}
diff --git a/apps/web/src/lib/publish-layout.test.mjs b/apps/web/src/lib/publish-layout.test.mjs
index 0e553c95..3926867a 100644
--- a/apps/web/src/lib/publish-layout.test.mjs
+++ b/apps/web/src/lib/publish-layout.test.mjs
@@ -207,7 +207,12 @@ describe("copy pipeline wiring", () => {
test("phone preview markup applies the paper layout instead of raw editor chrome", () => {
const source = readFileSync(new URL("./wechat-copy.ts", import.meta.url), "utf8");
+ const preview = readFileSync(new URL("../components/EditorPhonePreview.tsx", import.meta.url), "utf8");
expect(source).toContain("export const preparePublishArticle");
expect(source).toContain('applyPublishLayout(root, paperTheme.layout, paperTheme.palette, "phone")');
+ expect(source).toContain("editorThemeOverride");
+ expect(preview).toContain("preparePublishArticle(editor.getHTML(), editor.view.dom, editorTheme)");
+ expect(preview).toContain("previewRef.current?.setAttribute(\"style\", markup.style)");
+ expect(preview).toContain("key={editorTheme}");
});
});
diff --git a/apps/web/src/lib/wechat-copy.ts b/apps/web/src/lib/wechat-copy.ts
index 5759090f..48d4ef6d 100644
--- a/apps/web/src/lib/wechat-copy.ts
+++ b/apps/web/src/lib/wechat-copy.ts
@@ -473,11 +473,20 @@ export const readEditorCopyContext = (from?: HTMLElement | null) => {
};
};
-export const preparePublishArticle = (html: string, from?: HTMLElement | null) => {
+export const preparePublishArticle = (
+ html: string,
+ from?: HTMLElement | null,
+ editorThemeOverride?: string,
+) => {
const root = document.createElement("div");
root.innerHTML = html;
- const { editorTheme, customColors, customCss } = readEditorCopyContext(from);
- applyInlineStyles(root, editorTheme, customColors, customCss);
+ const context = readEditorCopyContext(from);
+ applyInlineStyles(
+ root,
+ editorThemeOverride ?? context.editorTheme,
+ context.customColors,
+ context.customCss,
+ );
convertImageGalleriesForWeChat(root);
return root;
};