feat(editor): allow customizing active line and editor UI colors

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 02:30:51 +08:00
1 parent 951f0a21a0
commit 252f8a0d1f
19 files changed
+204 -9

No files matched your search

+27 -2
View File
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { buildEditorFontThemeRules, buildSqlCompletionThemeRules, editorDiagnosticColors, editorThemeAppearanceFor, IDE_EDITOR_THEMES, resolveCustomThemeBackgrounds, resolveEditorTheme, SQL_BUILTIN_HIGHLIGHT_TAG } from "@/lib/editor/editorThemes";
import { buildEditorFontThemeRules, buildSqlCompletionThemeRules, editorDiagnosticColors, editorThemeAppearanceFor, IDE_EDITOR_THEMES, loadEditorTheme, resolveCustomThemeBackgrounds, resolveEditorTheme, SQL_BUILTIN_HIGHLIGHT_TAG } from "@/lib/editor/editorThemes";
import { DEFAULT_APP_CUSTOM_UI_COLORS, wcagContrastRatio, type AppThemePalette } from "@/lib/app/appTheme";
import type { EditorTheme } from "@/stores/settingsStore";
import { DEFAULT_CUSTOM_THEME_COLORS, type CustomThemeColors, type EditorTheme } from "@/stores/settingsStore";
import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect";
import * as langSql from "@codemirror/lang-sql";
@@ -61,6 +61,31 @@ describe("custom editor theme backgrounds", () => {
});
});
it("uses an explicit gutter background when provided", () => {
expect(resolveCustomThemeBackgrounds({ background: "#10131a", gutterBackground: "#0b0c10" }, true)).toEqual({
background: "#10131a",
gutterBackground: "#0b0c10",
});
expect(resolveCustomThemeBackgrounds({ gutterBackground: "#0b0c10" }, false)).toEqual({
background: "#fafafa",
gutterBackground: "#0b0c10",
});
});
it("loads custom editor theme with custom UI colors", async () => {
const customColors: CustomThemeColors = {
...DEFAULT_CUSTOM_THEME_COLORS,
activeLine: "#2a2a3a",
selection: "#3a3a4a",
cursor: "#ff00ff",
gutterBackground: "#111122",
lineNumber: "#777788",
matchingBracket: "#444455",
};
const extension = await loadEditorTheme("custom", "dark", customColors);
expect(extension).toBeDefined();
});
it("keeps the existing custom defaults when background is omitted", () => {
expect(resolveCustomThemeBackgrounds(undefined, true)).toEqual({
background: "#1e1e2e",
+2 -2
View File
@@ -92,10 +92,10 @@ const customThemeColors = {
invalid: "#f38ba8", // 无效字符
};
export function resolveCustomThemeBackgrounds(colors?: Pick<CustomThemeColors, "background">, isDark: boolean = true): { background: string; gutterBackground: string } {
export function resolveCustomThemeBackgrounds(colors?: Pick<CustomThemeColors, "background" | "gutterBackground">, isDark: boolean = true): { background: string; gutterBackground: string } {
return {
background: colors?.background ?? (isDark ? "#1e1e2e" : "#fafafa"),
gutterBackground: colors?.background ?? customThemeColors.gutterBackground,
gutterBackground: colors?.gutterBackground ?? colors?.background ?? customThemeColors.gutterBackground,
};
}
@@ -350,6 +350,42 @@ describe("settingsTransfer", () => {
expect(result.value.editorSettings.customThemes).toEqual([validTheme]);
});
it("accepts custom theme items with optional UI colors", () => {
const base = DEFAULT_EDITOR_SETTINGS.customThemes[0];
const validTheme = {
...base,
id: "t1",
name: "T1",
colors: {
...base.colors,
background: "#282c34",
foreground: "#abb2bf",
activeLine: "#2c313a",
selection: "#3e4451",
cursor: "#528bff",
gutterBackground: "#21252b",
lineNumber: "#4b5263",
matchingBracket: "#515a6b",
},
};
const result = parseSettingsTransferFile(fileWith({ customThemes: [validTheme] }));
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.editorSettings.customThemes).toEqual([validTheme]);
});
it("rejects custom theme items with wrong-typed optional UI colors", () => {
const base = DEFAULT_EDITOR_SETTINGS.customThemes[0];
const result = parseSettingsTransferFile(
fileWith({
customThemes: [{ ...base, id: "t1", name: "T1", colors: { ...base.colors, activeLine: 123 } }],
}),
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.detail).toContain("customThemes");
});
it("rejects null custom theme items instead of crashing", () => {
const result = parseSettingsTransferFile(fileWith({ customThemes: [null] }));
expect(result.ok).toBe(false);
@@ -349,7 +349,7 @@ const CUSTOM_THEME_DDL_COLORS_SHAPE = valueKindShape(DEFAULT_CUSTOM_THEME_DDL_CO
* cover them and a wrong-typed value (e.g. `background: {}`) would otherwise
* pass validation and reach the persisted settings.
*/
const CUSTOM_THEME_OPTIONAL_COLOR_KEYS = ["background", "foreground"] as const;
const CUSTOM_THEME_OPTIONAL_COLOR_KEYS = ["background", "foreground", "activeLine", "selection", "cursor", "gutterBackground", "lineNumber", "matchingBracket"] as const;
function hasValidOptionalColorKinds(value: unknown): boolean {
if (!isPlainObject(value)) return false;