Skip to content

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

feat(settings): preview selected code block theme

Dryice Liu2bd9ce153869
diff --git a/web/app/(utility)/settings/appearance/page.tsx b/web/app/(utility)/settings/appearance/page.tsx
index 000446950..b885e13da 100644
--- a/web/app/(utility)/settings/appearance/page.tsx
+++ b/web/app/(utility)/settings/appearance/page.tsx
@@ -1,6 +1,7 @@
"use client";
import { useEffect, useState } from "react";
+import dynamic from "next/dynamic";
import { useTranslation } from "react-i18next";
import type { CodeBlockThemeId } from "@/components/common/code-block-themes";
@@ -23,6 +24,25 @@ import {
selectOptionClass,
} from "@/components/settings/shared";
+const CODE_BLOCK_PREVIEW_SNIPPET = `def fibonacci(n):
+ """Generate the first n Fibonacci numbers."""
+ a, b = 0, 1
+ result = []
+ for _ in range(n):
+ result.append(a)
+ a, b = b, a + b
+ return result
+
+
+# Print the first 10 numbers in the sequence
+print(f"fibonacci(10) = {fibonacci(10)}")
+`;
+
+const RichCodeBlockPreview = dynamic(
+ () => import("@/components/common/RichCodeBlock"),
+ { ssr: false },
+);
+
export default function AppearanceSettingsPage() {
const { t } = useTranslation();
const {
@@ -167,6 +187,16 @@ export default function AppearanceSettingsPage() {
"Choose how code snippets look across the app. Changes apply immediately to saved and streamed responses.",
)}
>
+ <div className="border-t border-[var(--border)]/50 px-1 py-4 first:border-t-0">
+ <div className="text-[13.5px] font-medium text-[var(--foreground)]">
+ {t("Preview")}
+ </div>
+ <p className="mb-3 mt-1 text-[12px] leading-relaxed text-[var(--muted-foreground)]">
+ {t("Updates live as you change the settings below.")}
+ </p>
+ <RichCodeBlockPreview raw={CODE_BLOCK_PREVIEW_SNIPPET} lang="python" />
+ </div>
+
<SettingRow
title={t("Syntax theme")}
description={t(
diff --git a/web/components/common/RichCodeBlock.tsx b/web/components/common/RichCodeBlock.tsx
index a01ebebe2..9b6c26d94 100644
--- a/web/components/common/RichCodeBlock.tsx
+++ b/web/components/common/RichCodeBlock.tsx
@@ -83,13 +83,13 @@ export default function RichCodeBlock({
lineHeight: "1.7",
overflowX: codeBlockWrapLongLines ? "hidden" : "auto",
whiteSpace: codeBlockWrapLongLines ? "pre-wrap" : "pre",
- overflowWrap: codeBlockWrapLongLines ? "break-word" : "normal",
+ wordWrap: codeBlockWrapLongLines ? "break-word" : "normal",
}}
codeTagProps={{
className: "md-code-block__code",
style: {
fontFamily: MONOSPACE,
- ...(codeBlockWrapLongLines ? { overflowWrap: "break-word" } : {}),
+ ...(codeBlockWrapLongLines ? { wordWrap: "break-word" } : {}),
},
}}
showLineNumbers={codeBlockShowLineNumbers}
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index dc8a3ebd2..47ac43eaf 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -597,6 +597,7 @@
"Show AI Marks": "Show AI Marks",
"Hide AI Marks": "Hide AI Marks",
"Preview": "Preview",
+ "Updates live as you change the settings below.": "Updates live as you change the settings below.",
"Process": "Process",
"Report": "Report",
"Ready to Research": "Ready to Research",
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index 60fbe1a8d..07c5fc4ea 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -597,6 +597,7 @@
"Show AI Marks": "显示 AI 批注",
"Hide AI Marks": "隐藏 AI 批注",
"Preview": "预览",
+ "Updates live as you change the settings below.": "修改下方设置时预览会实时更新。",
"Process": "过程",
"Report": "报告",
"Ready to Research": "准备开始研究",
diff --git a/web/tests/rich-code-block.test.ts b/web/tests/rich-code-block.test.ts
index 2f9b48d96..6ff34c45e 100644
--- a/web/tests/rich-code-block.test.ts
+++ b/web/tests/rich-code-block.test.ts
@@ -154,15 +154,15 @@ test("rich-code-block: wrap mode breaks long unbroken tokens instead of clipping
// overflowed with only white-space:pre-wrap on the outer <pre>, because
// pre-wrap breaks only at whitespace and a long identifier/string/URL has
// no break opportunity. The renderer must additionally emit
- // overflow-wrap:break-word so the browser is permitted to split the token
+ // word-wrap:break-word so the browser is permitted to split the token
// mid-word when it would otherwise overflow. Asserting on BOTH the outer
// <pre> and the inner <code> because the line layout uses per-line flex
// rows and inheritance must reach the token spans from both levels.
const longUnbrokenToken = "a".repeat(200);
const html = renderCodeBlock(longUnbrokenToken, "javascript");
- assert.match(html, /<pre[^>]*style="[^"]*overflow-wrap:break-word[^"]*"/);
- assert.match(html, /<code[^>]*style="[^"]*overflow-wrap:break-word[^"]*"/);
+ assert.match(html, /<pre[^>]*style="[^"]*word-wrap:break-word[^"]*"/);
+ assert.match(html, /<code[^>]*style="[^"]*word-wrap:break-word[^"]*"/);
});
test("rich-code-block: wrap + line-numbers does not let per-line flex wrappers defeat overflow-wrap", () => {
@@ -181,7 +181,7 @@ test("rich-code-block: wrap + line-numbers does not let per-line flex wrappers d
// size — so a long unbreakable token overflows the line wrapper (F3
// measured the nested flex line wrapper at clientWidth 536 /
// scrollWidth 4326 for the short-token case, and 536 / 2319 for the
- // 240-char unbroken-token case). overflow-wrap:break-word was already
+ // 240-char unbroken-token case). word-wrap:break-word was already
// correctly set on <pre> and <code> but could not propagate its break
// behavior into unshrinkable flex items, so the visible result was a
// horizontal clip. The outer-<pre> and overflow-wrap regressions above
@@ -201,7 +201,7 @@ test("rich-code-block: wrap + line-numbers does not let per-line flex wrappers d
// No span inside <code> may carry display:flex when wrap is enabled:
// that style is the proximate cause of the F3 live-browser clip. The
// per-line wrapper must lay out as a normal block instead so that
- // overflow-wrap:break-word (inherited from <pre>/<code>) can break
+ // word-wrap:break-word (inherited from <pre>/<code>) can break
// long unbreakable tokens instead of being defeated by flex items
// whose min-width:auto refuses to shrink.
assert.doesNotMatch(