feat(data-grid): add customizable data type colors

This commit is contained in:
kirito authored and GitHub committed 2026-08-18 23:05:17 +08:00
1 parent 95760e320a
commit e0448ac87e
25 files changed
+1209 -15

No files matched your search

@@ -0,0 +1,87 @@
import { createPinia, setActivePinia } from "pinia";
import { readFileSync } from "node:fs";
import { beforeEach, describe, expect, it, vi } from "vitest";
const saved: unknown[] = [];
vi.mock("@/lib/backend/api", () => ({
saveEditorSettings: vi.fn(async (settings: unknown) => {
saved.push(JSON.parse(JSON.stringify(settings)));
}),
loadEditorSettings: vi.fn(async () => null),
loadDesktopSettings: vi.fn(async () => null),
saveDesktopSettings: vi.fn(async () => {}),
}));
const SCHEME = {
id: "type-colors-1",
name: "配色方案 1",
colors: { integer: "#254fce", numeric: "#0e7490", string: "#fdc9c9", boolean: "#100cc2", temporal: "#7e22ce", structured: "#be185d", identifier: "#92400e", binary: "#b91c1c", spatial: "#047857" },
};
describe("settings store type color scheme persistence", () => {
beforeEach(() => {
saved.length = 0;
vi.stubGlobal("localStorage", {
getItem: vi.fn(() => null),
setItem: vi.fn(),
removeItem: vi.fn(),
});
setActivePinia(createPinia());
});
it("keeps a committed scheme in memory", async () => {
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useSettingsStore();
store.updateEditorSettings({ dataGridTypeColorSchemes: [SCHEME], activeDataGridTypeColorSchemeId: SCHEME.id });
expect(store.editorSettings.dataGridTypeColorSchemes).toHaveLength(1);
expect(store.editorSettings.activeDataGridTypeColorSchemeId).toBe(SCHEME.id);
});
it("writes the scheme through to the persistence layer", async () => {
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useSettingsStore();
await store.persistEditorSettings();
saved.length = 0;
store.updateEditorSettings({ dataGridTypeColorSchemes: [SCHEME], activeDataGridTypeColorSchemeId: SCHEME.id });
await store.persistEditorSettings();
const last = saved.at(-1) as Record<string, unknown> | undefined;
expect(last?.dataGridTypeColorSchemes).toEqual([SCHEME]);
expect(last?.activeDataGridTypeColorSchemeId).toBe(SCHEME.id);
});
it("persists a scheme committed before the store finished loading", async () => {
// The scheme dialog can commit before anything has awaited initEditorSettings.
// A plain updateEditorSettings only lands in memory there, so the colors show
// up and then vanish on the next reload; the persisting variant must be used.
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useSettingsStore();
await store.updateEditorSettingsAndPersist({ dataGridTypeColorSchemes: [SCHEME], activeDataGridTypeColorSchemeId: SCHEME.id });
const last = saved.at(-1) as Record<string, unknown> | undefined;
expect(last?.dataGridTypeColorSchemes).toEqual([SCHEME]);
expect(last?.activeDataGridTypeColorSchemeId).toBe(SCHEME.id);
});
it("stages scheme edits in the settings draft", () => {
const dialogSource = readFileSync(new URL("../../components/editor/EditorSettingsDialog.vue", import.meta.url), "utf8");
expect(dialogSource).toContain("editDataGridTypeColorSchemes.value = structuredClone(schemes)");
expect(dialogSource).toContain("editActiveDataGridTypeColorSchemeId.value = activeId");
expect(dialogSource).not.toMatch(/settingsStore\.updateEditorSettings\w*\(\{[^}]*DataGridTypeColorScheme/);
});
it("restores a persisted scheme through normalization", async () => {
const { normalizeEditorSettings } = await import("@/stores/settingsStore");
const restored = normalizeEditorSettings({ dataGridTypeColorSchemes: [SCHEME], activeDataGridTypeColorSchemeId: SCHEME.id });
expect(restored.dataGridTypeColorSchemes).toEqual([SCHEME]);
expect(restored.activeDataGridTypeColorSchemeId).toBe(SCHEME.id);
});
});
+17
View File
@@ -8,6 +8,7 @@ import { safeLocalStorageGet, safeLocalStorageRemove } from "@/lib/backend/safeS
import { type ColumnFormatterConfig, type CustomColumnFormatterConfig, normalizeColumnFormatter, normalizeCustomColumnFormatter, normalizeGlobalDateTimePattern } from "@/lib/dataGrid/columnFormatter";
import { type DataGridCopyPreference, type DataGridExtractorOptions, DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS, normalizeDataGridCopyPreference, normalizeDataGridExtractorOptions } from "@/lib/dataGrid/dataGridCopyExtractor";
import { DATA_GRID_TEXT_FILTER_PANEL_HEIGHT_DEFAULT, normalizeDataGridTextFilterPanelHeight } from "@/lib/dataGrid/dataGridTextFilterPanel";
import { DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID, type DataGridTypeColorScheme, normalizeActiveDataGridTypeColorSchemeId, normalizeDataGridTypeColorSchemes } from "@/lib/dataGrid/dataGridTypeColorScheme";
import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
import { DEFAULT_QUERY_RESULT_MAX_ROWS, normalizeQueryResultMaxRows } from "@/lib/dataGrid/queryResultRowLimit";
import { normalizeConnectTimeoutSecs, normalizeQueryTimeoutSecs } from "@/lib/connection/timeoutLimits";
@@ -551,6 +552,8 @@ export interface EditorSettings {
showColumnTypesInHeader: boolean;
dataGridShowTransposeFieldMetadata: boolean;
colorizeDataGridCellTypes: boolean;
dataGridTypeColorSchemes: DataGridTypeColorScheme[];
activeDataGridTypeColorSchemeId: string;
showIndexIndicatorsInHeader: boolean;
compactColumnHeaderActions: boolean;
columnWidthDensity: ColumnWidthDensity;
@@ -754,6 +757,8 @@ export const DEFAULT_EDITOR_SETTINGS: EditorSettings = {
showColumnTypesInHeader: true,
dataGridShowTransposeFieldMetadata: false,
colorizeDataGridCellTypes: false,
dataGridTypeColorSchemes: [],
activeDataGridTypeColorSchemeId: DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID,
showIndexIndicatorsInHeader: true,
compactColumnHeaderActions: true,
columnWidthDensity: "standard",
@@ -1031,6 +1036,8 @@ export function normalizeEditorSettings(settings: Partial<EditorSettings>, exist
const savedExecuteModeDefaultVersion = settings.executeModeDefaultVersion;
const executeModeDefaultVersion = typeof savedExecuteModeDefaultVersion === "number" && savedExecuteModeDefaultVersion >= EXECUTE_MODE_CURRENT_DEFAULT_VERSION ? savedExecuteModeDefaultVersion : EXECUTE_MODE_CURRENT_DEFAULT_VERSION;
const hasCurrentExecuteModeDefault = executeModeDefaultVersion === savedExecuteModeDefaultVersion;
// The active id can only be validated once the scheme list it points into is known.
const dataGridTypeColorSchemes = normalizeDataGridTypeColorSchemes(settings.dataGridTypeColorSchemes);
return {
fontFamily: normalizeFontFamily(settings.fontFamily, DEFAULT_EDITOR_SETTINGS.fontFamily),
fontSize: settings.fontSize ?? DEFAULT_EDITOR_SETTINGS.fontSize,
@@ -1116,6 +1123,8 @@ export function normalizeEditorSettings(settings: Partial<EditorSettings>, exist
showColumnTypesInHeader: settings.showColumnTypesInHeader ?? DEFAULT_EDITOR_SETTINGS.showColumnTypesInHeader,
dataGridShowTransposeFieldMetadata: settings.dataGridShowTransposeFieldMetadata === true,
colorizeDataGridCellTypes: settings.colorizeDataGridCellTypes ?? DEFAULT_EDITOR_SETTINGS.colorizeDataGridCellTypes,
dataGridTypeColorSchemes,
activeDataGridTypeColorSchemeId: normalizeActiveDataGridTypeColorSchemeId(dataGridTypeColorSchemes, settings.activeDataGridTypeColorSchemeId),
showIndexIndicatorsInHeader: settings.showIndexIndicatorsInHeader ?? DEFAULT_EDITOR_SETTINGS.showIndexIndicatorsInHeader,
compactColumnHeaderActions: settings.compactColumnHeaderActions ?? DEFAULT_EDITOR_SETTINGS.compactColumnHeaderActions,
columnWidthDensity: normalizeColumnWidthDensity(settings.columnWidthDensity),
@@ -1702,6 +1711,14 @@ export const useSettingsStore = defineStore("settings", () => {
if (partial.showColumnTypesInHeader !== undefined) editorSettings.value.showColumnTypesInHeader = partial.showColumnTypesInHeader;
if (partial.dataGridShowTransposeFieldMetadata !== undefined) editorSettings.value.dataGridShowTransposeFieldMetadata = partial.dataGridShowTransposeFieldMetadata === true;
if (partial.colorizeDataGridCellTypes !== undefined) editorSettings.value.colorizeDataGridCellTypes = partial.colorizeDataGridCellTypes === true;
if (partial.dataGridTypeColorSchemes !== undefined) {
editorSettings.value.dataGridTypeColorSchemes = normalizeDataGridTypeColorSchemes(partial.dataGridTypeColorSchemes);
// Dropping the scheme the grid is painting with has to fall back to the theme defaults.
editorSettings.value.activeDataGridTypeColorSchemeId = normalizeActiveDataGridTypeColorSchemeId(editorSettings.value.dataGridTypeColorSchemes, editorSettings.value.activeDataGridTypeColorSchemeId);
}
if (partial.activeDataGridTypeColorSchemeId !== undefined) {
editorSettings.value.activeDataGridTypeColorSchemeId = normalizeActiveDataGridTypeColorSchemeId(editorSettings.value.dataGridTypeColorSchemes, partial.activeDataGridTypeColorSchemeId);
}
if (partial.showIndexIndicatorsInHeader !== undefined) editorSettings.value.showIndexIndicatorsInHeader = partial.showIndexIndicatorsInHeader;
if (partial.compactColumnHeaderActions !== undefined) editorSettings.value.compactColumnHeaderActions = partial.compactColumnHeaderActions;
if (partial.columnWidthDensity !== undefined) editorSettings.value.columnWidthDensity = normalizeColumnWidthDensity(partial.columnWidthDensity);