settingsCodePreview.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import type { Theme } from "@/useTheme.js";
  2. import { useState } from "react";
  3. import { resolveTheme } from "@/useTheme.js";
  4. import { Card, CardContent } from "@/components/ui/card.js";
  5. import { Input } from "@/components/ui/input.js";
  6. import { Switch } from "@/components/ui/switch.js";
  7. import {
  8. Select,
  9. SelectContent,
  10. SelectItem,
  11. SelectTrigger,
  12. SelectValue,
  13. } from "@/components/ui/select.js";
  14. import {
  15. getThemeOptionLabel,
  16. SettingsRow,
  17. ThemePreviewCard,
  18. ThemeSelect,
  19. } from "@/settings/SettingsPageParts.js";
  20. import { getCodePreviewTheme } from "@/lib/codePreviewPreferences.js";
  21. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  22. import type { CodePreviewSettings } from "@/store/index.js";
  23. import { THEME_MODES } from "@/settings/settingsPageConfig.js";
  24. import { BUILTIN_SKIN_MANIFESTS, getSkinManifest } from "@/skins/registry.js";
  25. import { MAX_UI_FONT_SIZE_PX, MIN_UI_FONT_SIZE_PX } from "@/lib/uiFontSize.js";
  26. function FontSizeInput({
  27. value,
  28. min,
  29. max,
  30. ariaLabel,
  31. onChange,
  32. disabled = false,
  33. }: {
  34. value: number;
  35. min: number;
  36. max: number;
  37. ariaLabel: string;
  38. onChange: (value: number) => void;
  39. disabled?: boolean;
  40. }) {
  41. const [draft, setDraft] = useState(String(value));
  42. const commit = () => {
  43. const parsed = draft.trim() === "" ? Number.NaN : Number(draft);
  44. const nextValue = Number.isFinite(parsed)
  45. ? Math.min(max, Math.max(min, Math.round(parsed)))
  46. : value;
  47. setDraft(String(nextValue));
  48. if (nextValue !== value) {
  49. onChange(nextValue);
  50. }
  51. };
  52. return (
  53. <div className="relative w-28">
  54. <Input
  55. type="number"
  56. inputMode="numeric"
  57. min={min}
  58. max={max}
  59. step={1}
  60. value={draft}
  61. disabled={disabled}
  62. aria-label={ariaLabel}
  63. onChange={(event) => setDraft(event.currentTarget.value)}
  64. onBlur={commit}
  65. onKeyDown={(event) => {
  66. if (event.key === "Enter") {
  67. event.currentTarget.blur();
  68. } else if (event.key === "Escape") {
  69. event.preventDefault();
  70. setDraft(String(value));
  71. }
  72. }}
  73. className="pr-8 text-right tabular-nums [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
  74. />
  75. <span className="pointer-events-none absolute inset-y-0 right-2 flex items-center text-ui-lg text-foreground-subtle">
  76. px
  77. </span>
  78. </div>
  79. );
  80. }
  81. export function AppearanceSectionContent({
  82. codePreviewSettings,
  83. setCodePreviewSettings,
  84. theme,
  85. setTheme,
  86. skinId,
  87. setSkin,
  88. uiFontSizePx,
  89. setUiFontSizePx,
  90. }: {
  91. codePreviewSettings: CodePreviewSettings;
  92. setCodePreviewSettings: (settings: Partial<CodePreviewSettings>) => void;
  93. theme: Theme;
  94. setTheme: (theme: Theme) => void;
  95. skinId: string | null;
  96. setSkin: (skinId: string | null) => void;
  97. uiFontSizePx: number;
  98. setUiFontSizePx: (fontSizePx: number) => void;
  99. }) {
  100. const { intl } = useZCodeIntl();
  101. const activePreviewMode = resolveTheme(theme);
  102. // fontGrid 锁定(A4):激活皮肤声明 12px 像素网格时,字号控件禁用并说明原因。
  103. // 运行时的 inline 值钳制在 applySkin 内,这里只负责 UI 禁用与解释。
  104. const fontGridLock =
  105. getSkinManifest(skinId)?.tokens.fontGrid?.lockUiFontSize ?? null;
  106. return (
  107. <>
  108. <div className="min-w-0 space-y-3">
  109. <div>
  110. <h3 className="text-ui-lg font-semibold text-foreground">
  111. {intl.formatMessage({ id: "settings.appearance.interfaceTitle" })}
  112. </h3>
  113. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  114. {intl.formatMessage({
  115. id: "settings.appearance.interfaceDescription",
  116. })}
  117. </p>
  118. </div>
  119. <Card className="border border-border bg-card py-0 shadow-none">
  120. <CardContent className="space-y-0 px-0">
  121. <SettingsRow
  122. label={intl.formatMessage({ id: "settings.themeMode" })}
  123. description={intl.formatMessage({
  124. id: "settings.themeModeDescription",
  125. })}
  126. control={
  127. <Select value={theme} onValueChange={(value) => setTheme(value as Theme)}>
  128. <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
  129. <SelectValue />
  130. </SelectTrigger>
  131. <SelectContent>
  132. {THEME_MODES.map(({ mode, icon: Icon }) => (
  133. <SelectItem key={mode} value={mode}>
  134. <div className="flex items-center gap-2">
  135. <Icon className="size-4" />
  136. {intl.formatMessage({
  137. id: `settings.themeMode.${mode}`,
  138. })}
  139. </div>
  140. </SelectItem>
  141. ))}
  142. </SelectContent>
  143. </Select>
  144. }
  145. />
  146. {BUILTIN_SKIN_MANIFESTS.length > 0 ? (
  147. <SettingsRow
  148. label={intl.formatMessage({ id: "settings.skin.label" })}
  149. description={intl.formatMessage({
  150. id: "settings.skin.description",
  151. })}
  152. control={
  153. <Select
  154. value={skinId ?? "none"}
  155. onValueChange={(value) => setSkin(value === "none" ? null : value)}
  156. >
  157. <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
  158. <SelectValue />
  159. </SelectTrigger>
  160. <SelectContent>
  161. {/* Radix SelectItem 禁止空字符串 value(会直接抛错),用 "none" 哨兵表示无皮肤。 */}
  162. <SelectItem value="none">
  163. {intl.formatMessage({ id: "settings.skin.default" })}
  164. </SelectItem>
  165. {BUILTIN_SKIN_MANIFESTS.map((manifest) => (
  166. <SelectItem key={manifest.id} value={manifest.id}>
  167. {intl.formatMessage({ id: manifest.i18nName })}
  168. </SelectItem>
  169. ))}
  170. </SelectContent>
  171. </Select>
  172. }
  173. />
  174. ) : null}
  175. <SettingsRow
  176. label={intl.formatMessage({ id: "settings.uiFontSize" })}
  177. description={intl.formatMessage(
  178. { id: fontGridLock !== null ? "settings.skin.fontGridLock" : "settings.uiFontSizeDescription" },
  179. fontGridLock !== null ? { size: fontGridLock } : undefined,
  180. )}
  181. control={
  182. <FontSizeInput
  183. key={uiFontSizePx}
  184. disabled={fontGridLock !== null}
  185. min={MIN_UI_FONT_SIZE_PX}
  186. max={MAX_UI_FONT_SIZE_PX}
  187. value={uiFontSizePx}
  188. onChange={setUiFontSizePx}
  189. ariaLabel={intl.formatMessage({ id: "settings.uiFontSize" })}
  190. />
  191. }
  192. />
  193. </CardContent>
  194. </Card>
  195. </div>
  196. <div className="space-y-6">
  197. <div className="min-w-0 space-y-3">
  198. <div>
  199. <h3 className="text-ui-lg font-semibold text-foreground">
  200. {intl.formatMessage({ id: "settings.appearance.codeTitle" })}
  201. </h3>
  202. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  203. {intl.formatMessage({
  204. id: "settings.appearance.codeDescription",
  205. })}
  206. </p>
  207. </div>
  208. <Card className="border border-border bg-card py-0 shadow-none [&_[data-slot=select-trigger]]:w-full">
  209. <CardContent className="space-y-0 px-0">
  210. <SettingsRow
  211. label={intl.formatMessage({ id: "settings.lightTheme" })}
  212. description={intl.formatMessage({
  213. id: "settings.lightThemeDescription",
  214. })}
  215. control={
  216. <ThemeSelect
  217. value={codePreviewSettings.lightTheme}
  218. onValueChange={(value) => setCodePreviewSettings({ lightTheme: value })}
  219. />
  220. }
  221. />
  222. <SettingsRow
  223. label={intl.formatMessage({ id: "settings.darkTheme" })}
  224. description={intl.formatMessage({
  225. id: "settings.darkThemeDescription",
  226. })}
  227. control={
  228. <ThemeSelect
  229. value={codePreviewSettings.darkTheme}
  230. onValueChange={(value) => setCodePreviewSettings({ darkTheme: value })}
  231. />
  232. }
  233. />
  234. <SettingsRow
  235. label={intl.formatMessage({ id: "settings.showLineNumbers" })}
  236. description={intl.formatMessage({
  237. id: "settings.showLineNumbersDescription",
  238. })}
  239. control={
  240. <Switch
  241. checked={codePreviewSettings.showLineNumbers}
  242. onCheckedChange={(checked) =>
  243. setCodePreviewSettings({ showLineNumbers: checked })
  244. }
  245. />
  246. }
  247. />
  248. <SettingsRow
  249. label={intl.formatMessage({ id: "settings.wrapLongLines" })}
  250. description={intl.formatMessage({
  251. id: "settings.wrapLongLinesDescription",
  252. })}
  253. control={
  254. <Switch
  255. checked={codePreviewSettings.wrapLongLines}
  256. onCheckedChange={(checked) =>
  257. setCodePreviewSettings({ wrapLongLines: checked })
  258. }
  259. />
  260. }
  261. />
  262. <SettingsRow
  263. label={intl.formatMessage({ id: "settings.fontSize" })}
  264. description={intl.formatMessage({
  265. id: "settings.fontSizeDescription",
  266. })}
  267. control={
  268. <FontSizeInput
  269. key={codePreviewSettings.fontSizePx}
  270. min={12}
  271. max={20}
  272. value={codePreviewSettings.fontSizePx}
  273. onChange={(fontSizePx) => setCodePreviewSettings({ fontSizePx })}
  274. ariaLabel={intl.formatMessage({ id: "settings.fontSize" })}
  275. />
  276. }
  277. />
  278. </CardContent>
  279. </Card>
  280. </div>
  281. <div className="min-w-0 space-y-4">
  282. <div>
  283. <h3 className="text-ui-base font-semibold text-foreground">
  284. {intl.formatMessage({ id: "settings.previewSectionTitle" })}
  285. </h3>
  286. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  287. {intl.formatMessage({ id: "settings.previewDescription" })}
  288. </p>
  289. </div>
  290. <div className="grid gap-4 sm:grid-cols-2">
  291. <ThemePreviewCard
  292. mode="light"
  293. title={intl.formatMessage({ id: "settings.previewLight" })}
  294. themeName={getThemeOptionLabel(codePreviewSettings.lightTheme)}
  295. theme={getCodePreviewTheme("light", codePreviewSettings)}
  296. isActive={activePreviewMode === "light"}
  297. showLineNumbers={codePreviewSettings.showLineNumbers}
  298. wrapLongLines={codePreviewSettings.wrapLongLines}
  299. fontSizePx={codePreviewSettings.fontSizePx}
  300. />
  301. <ThemePreviewCard
  302. mode="dark"
  303. title={intl.formatMessage({ id: "settings.previewDark" })}
  304. themeName={getThemeOptionLabel(codePreviewSettings.darkTheme)}
  305. theme={getCodePreviewTheme("dark", codePreviewSettings)}
  306. isActive={activePreviewMode === "dark"}
  307. showLineNumbers={codePreviewSettings.showLineNumbers}
  308. wrapLongLines={codePreviewSettings.wrapLongLines}
  309. fontSizePx={codePreviewSettings.fontSizePx}
  310. />
  311. </div>
  312. </div>
  313. </div>
  314. </>
  315. );
  316. }