feat(ai): add independent conversation font settings

Closes #9584
This commit is contained in:
t8y2 committed 2026-09-27 22:37:49 +00:00
1 parent 43ddfce93d
commit e9998e2806
21 files changed
+563 -7

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
import { describe, expect, it } from "vitest";
import {
AI_CONVERSATION_FONT_FAMILY_DEFAULT,
AI_CONVERSATION_FONT_SIZE_DEFAULT,
AI_CONVERSATION_FONT_SIZE_MAX,
AI_CONVERSATION_FONT_SIZE_MIN,
aiConversationFontFamilyForName,
aiConversationTypographyCssVariables,
normalizeAiConversationFontFamily,
normalizeAiConversationFontFamilyInput,
normalizeAiConversationFontSize,
normalizeAiConversationTypography,
} from "@/lib/ai/aiTypography";
describe("AI conversation typography", () => {
it("preserves the legacy inherited 12px appearance for sparse settings", () => {
expect(normalizeAiConversationTypography(undefined)).toEqual({
fontFamily: AI_CONVERSATION_FONT_FAMILY_DEFAULT,
fontSize: AI_CONVERSATION_FONT_SIZE_DEFAULT,
});
expect(aiConversationTypographyCssVariables(undefined)).toEqual({
"--dbx-ai-content-font-size": "12px",
"--dbx-ai-code-font-size": "12px",
"--dbx-ai-inline-code-font-size": "11px",
});
});
it("sanitizes font families and clamps invalid sizes", () => {
expect(normalizeAiConversationFontFamily(null)).toBe("");
expect(normalizeAiConversationFontFamily(" 'Atkinson Hyperlegible', sans-serif\n")).toBe("'Atkinson Hyperlegible', sans-serif");
expect(aiConversationFontFamilyForName("Reader's Choice")).toBe("'Reader\\'s Choice', sans-serif");
expect(normalizeAiConversationFontFamilyInput("Atkinson Hyperlegible")).toBe("'Atkinson Hyperlegible', sans-serif");
expect(normalizeAiConversationFontFamilyInput("Georgia, serif")).toBe("Georgia, serif");
expect(normalizeAiConversationFontSize(Number.NaN)).toBe(AI_CONVERSATION_FONT_SIZE_DEFAULT);
expect(normalizeAiConversationFontSize("18")).toBe(AI_CONVERSATION_FONT_SIZE_DEFAULT);
expect(normalizeAiConversationFontSize(1)).toBe(AI_CONVERSATION_FONT_SIZE_MIN);
expect(normalizeAiConversationFontSize(99)).toBe(AI_CONVERSATION_FONT_SIZE_MAX);
expect(normalizeAiConversationFontSize(15.6)).toBe(16);
});
it("creates scoped prose and monospace code sizes from a custom preference", () => {
expect(aiConversationTypographyCssVariables({ fontFamily: "Georgia, serif", fontSize: 18 })).toEqual({
"--dbx-ai-content-font-family": "Georgia, serif",
"--dbx-ai-content-font-size": "18px",
"--dbx-ai-code-font-size": "18px",
"--dbx-ai-inline-code-font-size": "17px",
});
});
});
+50
View File
@@ -0,0 +1,50 @@
export const AI_CONVERSATION_FONT_FAMILY_DEFAULT = "";
export const AI_CONVERSATION_FONT_SIZE_DEFAULT = 12;
export const AI_CONVERSATION_FONT_SIZE_MIN = 10;
export const AI_CONVERSATION_FONT_SIZE_MAX = 24;
const AI_CONVERSATION_FONT_FAMILY_MAX_LENGTH = 512;
const AI_CODE_FONT_SIZE_MIN = 11;
export interface AiConversationTypography {
fontFamily: string;
fontSize: number;
}
export function normalizeAiConversationFontFamily(value: unknown): string {
if (typeof value !== "string") return AI_CONVERSATION_FONT_FAMILY_DEFAULT;
return value.trim().slice(0, AI_CONVERSATION_FONT_FAMILY_MAX_LENGTH);
}
export function aiConversationFontFamilyForName(name: string): string {
return `'${name.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}', sans-serif`;
}
export function normalizeAiConversationFontFamilyInput(value: string): string {
const normalized = normalizeAiConversationFontFamily(value);
if (!normalized || normalized.includes(",") || normalized.includes("'") || normalized.includes('"')) return normalized;
return aiConversationFontFamilyForName(normalized);
}
export function normalizeAiConversationFontSize(value: unknown): number {
if (typeof value !== "number" || !Number.isFinite(value)) return AI_CONVERSATION_FONT_SIZE_DEFAULT;
return Math.min(AI_CONVERSATION_FONT_SIZE_MAX, Math.max(AI_CONVERSATION_FONT_SIZE_MIN, Math.round(value)));
}
export function normalizeAiConversationTypography(value: Partial<AiConversationTypography> | null | undefined): AiConversationTypography {
return {
fontFamily: normalizeAiConversationFontFamily(value?.fontFamily),
fontSize: normalizeAiConversationFontSize(value?.fontSize),
};
}
export function aiConversationTypographyCssVariables(value: Partial<AiConversationTypography> | null | undefined): Record<string, string> {
const typography = normalizeAiConversationTypography(value);
const variables: Record<string, string> = {
"--dbx-ai-content-font-size": `${typography.fontSize}px`,
"--dbx-ai-code-font-size": `${Math.max(AI_CODE_FONT_SIZE_MIN, typography.fontSize)}px`,
"--dbx-ai-inline-code-font-size": `${Math.max(AI_CODE_FONT_SIZE_MIN, typography.fontSize - 1)}px`,
};
if (typography.fontFamily) variables["--dbx-ai-content-font-family"] = typography.fontFamily;
return variables;
}