fix(editor): keep custom theme entry point when customThemes is empty

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-04 02:01:27 +08:00
1 parent c737fde538
commit 7aa69a0e5e
5 files changed
+199 -50

No files matched your search

@@ -106,6 +106,7 @@ import {
type CsvNullMode,
type CustomThemeColors,
type CustomTheme,
DEFAULT_CUSTOM_THEMES,
type McpConnectionPolicy,
type McpGlobalPolicy,
normalizeMcpGlobalPolicy,
@@ -649,7 +650,7 @@ const backgroundImageFileMissing = ref(false);
// Set when the draft clears a configured image; the stored copy is only
// deleted once the change is actually applied.
let pendingBackgroundImageCleanup: string | null = null;
const editCustomThemes = ref<CustomTheme[]>([...settingsStore.editorSettings.customThemes]);
const editCustomThemes = ref<CustomTheme[]>([...(settingsStore.editorSettings.customThemes.length > 0 ? settingsStore.editorSettings.customThemes : DEFAULT_CUSTOM_THEMES)]);
const editActiveCustomThemeId = ref(settingsStore.editorSettings.activeCustomThemeId);
const editDataGridTypeColorSchemes = ref<DataGridTypeColorScheme[]>(cloneDataGridTypeColorSchemes(settingsStore.editorSettings.dataGridTypeColorSchemes));
const editActiveDataGridTypeColorSchemeId = ref(settingsStore.editorSettings.activeDataGridTypeColorSchemeId);
@@ -1715,8 +1716,8 @@ function syncEditorSettingsDraftFromStore() {
editTheme.value = settingsStore.editorSettings.theme;
editBackgroundImage.value = cloneBackgroundImageDraft(settingsStore.editorSettings.backgroundImage);
pendingBackgroundImageCleanup = null;
editCustomThemes.value = [...settingsStore.editorSettings.customThemes];
editActiveCustomThemeId.value = settingsStore.editorSettings.activeCustomThemeId;
editCustomThemes.value = [...(settingsStore.editorSettings.customThemes.length > 0 ? settingsStore.editorSettings.customThemes : DEFAULT_CUSTOM_THEMES)];
editActiveCustomThemeId.value = settingsStore.editorSettings.activeCustomThemeId || editCustomThemes.value[0]?.id || "default";
editExecuteMode.value = settingsStore.editorSettings.executeMode;
editDefaultTransactionMode.value = settingsStore.editorSettings.defaultTransactionMode;
editKeepExplicitTransactionInAutoCommit.value = settingsStore.editorSettings.keepExplicitTransactionInAutoCommit;
@@ -2891,7 +2892,11 @@ function resetAiTypography() {
const themeSelectValue = computed(() => {
if (editTheme.value === "custom") {
return `custom:${editActiveCustomThemeId.value}`;
if (editCustomThemes.value.length > 0) {
const activeMatch = editCustomThemes.value.find((t) => t.id === editActiveCustomThemeId.value);
return activeMatch ? `custom:${activeMatch.id}` : `custom:${editCustomThemes.value[0].id}`;
}
return "custom";
}
return editTheme.value;
});
@@ -2903,12 +2908,21 @@ const themeSelectOptions = computed(() => [
dark: theme.dark,
isCustom: false,
})),
...editCustomThemes.value.map((theme) => ({
value: `custom:${theme.id}`,
label: theme.name,
dark: true,
isCustom: true,
})),
...(editCustomThemes.value.length === 0
? [
{
value: "custom",
label: t("settings.customTheme") || "Custom Theme",
dark: true,
isCustom: true,
},
]
: editCustomThemes.value.map((theme) => ({
value: `custom:${theme.id}`,
label: theme.name,
dark: true,
isCustom: true,
}))),
]);
function onThemeChange(v: any) {
@@ -2916,14 +2930,20 @@ function onThemeChange(v: any) {
if (v.startsWith("custom:")) {
editTheme.value = "custom";
editActiveCustomThemeId.value = v.slice(7);
} else if (v === "custom") {
editTheme.value = "custom";
if (editCustomThemes.value.length === 0) {
editCustomThemes.value = JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES));
}
editActiveCustomThemeId.value = editCustomThemes.value[0]?.id ?? "default";
} else {
editTheme.value = v as typeof DEFAULT_EDITOR_SETTINGS.theme;
}
}
function handleThemeSave(updatedThemes: CustomTheme[], activeId: string) {
editCustomThemes.value = updatedThemes;
editActiveCustomThemeId.value = activeId;
editCustomThemes.value = updatedThemes.length > 0 ? updatedThemes : JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES));
editActiveCustomThemeId.value = activeId || editCustomThemes.value[0]?.id || "default";
editTheme.value = "custom";
showThemeCustomizer.value = false;
}
@@ -5,7 +5,7 @@ import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Input } from "@/components/ui/input";
import type { CustomTheme, CustomThemeColors } from "@/stores/settingsStore";
import { DEFAULT_CUSTOM_THEME_COLORS, DEFAULT_CUSTOM_THEME_DDL_COLORS } from "@/stores/settingsStore";
import { DEFAULT_CUSTOM_THEMES, DEFAULT_CUSTOM_THEME_COLORS, DEFAULT_CUSTOM_THEME_DDL_COLORS } from "@/stores/settingsStore";
import { Plus, Trash2, Copy, Pencil, ChevronDown, Palette } from "@lucide/vue";
import { useToast } from "@/composables/useToast";
import { useI18n } from "vue-i18n";
@@ -32,12 +32,22 @@ const jsonText = ref("");
const renamingId = ref<string | null>(null);
const renamingName = ref("");
function cloneCustomThemes(themes: CustomTheme[]): CustomTheme[] {
return themes.map((theme) => ({
id: theme.id,
name: theme.name,
colors: { ...theme.colors },
ddlColors: { ...theme.ddlColors },
}));
}
watch(
() => props.open,
(isOpen) => {
if (isOpen) {
localThemes.value = JSON.parse(JSON.stringify(props.themes));
activeEditId.value = props.activeThemeId;
localThemes.value = props.themes && props.themes.length > 0 ? cloneCustomThemes(props.themes) : cloneCustomThemes(DEFAULT_CUSTOM_THEMES);
const hasActive = localThemes.value.some((t) => t.id === props.activeThemeId);
activeEditId.value = hasActive ? props.activeThemeId : (localThemes.value[0]?.id ?? "default");
syncJson();
}
},
@@ -352,11 +362,9 @@ function handleJsonChange() {
}
function handleSave() {
emit(
"save",
localThemes.value.map((t) => ({ ...t })),
activeEditId.value,
);
const themesToSave = localThemes.value.length > 0 ? localThemes.value : cloneCustomThemes(DEFAULT_CUSTOM_THEMES);
const activeId = themesToSave.some((t) => t.id === activeEditId.value) ? activeEditId.value : (themesToSave[0]?.id ?? "default");
emit("save", cloneCustomThemes(themesToSave), activeId);
emit("update:open", false);
}
@@ -0,0 +1,76 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, ref } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import ThemeCustomizerDialog from "../ThemeCustomizerDialog.vue";
import dialogSource from "../EditorSettingsDialog.vue?raw";
vi.mock("vue-i18n", async () => {
const { ref } = await import("vue");
const locale = ref("en");
const t = (key: string) => key;
return {
createI18n: () => ({ global: { t, locale }, install: () => undefined }),
useI18n: () => ({ t, locale }),
};
});
describe("ThemeCustomizerDialog with empty themes (#10994)", () => {
let app: ReturnType<typeof createApp> | null = null;
let container: HTMLDivElement | null = null;
afterEach(() => {
if (app && container) {
app.unmount();
container.remove();
}
app = null;
container = null;
});
it("seeds DEFAULT_CUSTOM_THEMES when opened with empty themes", async () => {
setActivePinia(createPinia());
container = document.createElement("div");
document.body.appendChild(container);
const onSave = vi.fn();
const isOpen = ref(false);
const Host = defineComponent({
setup() {
return () =>
h(ThemeCustomizerDialog, {
open: isOpen.value,
themes: [],
activeThemeId: "",
"onUpdate:open": (val: boolean) => {
isOpen.value = val;
},
onSave,
});
},
});
app = createApp(Host);
app.mount(container);
// Open the dialog
isOpen.value = true;
await nextTick();
// Verify dialog content is rendered into document.body via Teleport and does not crash
expect(document.body.innerHTML).toContain("Custom");
});
});
describe("EditorSettingsDialog theme select fallback (#10994)", () => {
it("includes a custom theme option even if editCustomThemes is empty", () => {
// Ensures themeSelectOptions checks editCustomThemes.value.length === 0
// and supplies a fallback custom theme entry so the entry point never disappears
expect(dialogSource).toContain("editCustomThemes.value.length === 0");
expect(dialogSource).toContain('value: "custom"');
expect(dialogSource).toContain('} else if (v === "custom")');
expect(dialogSource).toContain("editCustomThemes.value = JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES))");
});
});