feat(data-grid): add customizable data type colors
This commit is contained in:
1 parent
95760e320a
commit
e0448ac87e
25 files changed
+1209
-15
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { hexToHsv, hexToRgba, hsvToHex } from "@/lib/common/color";
|
||||
|
||||
describe("hexToHsv", () => {
|
||||
it.each([
|
||||
["#ff0000", 0, 1, 1],
|
||||
["#00ff00", 120, 1, 1],
|
||||
["#0000ff", 240, 1, 1],
|
||||
["#ffffff", 0, 0, 1],
|
||||
["#000000", 0, 0, 0],
|
||||
["#808080", 0, 0, 0.502],
|
||||
])("maps %s to its hue, saturation and value", (color, h, s, v) => {
|
||||
const hsv = hexToHsv(color);
|
||||
|
||||
expect(hsv?.h).toBeCloseTo(h, 1);
|
||||
expect(hsv?.s).toBeCloseTo(s, 2);
|
||||
expect(hsv?.v).toBeCloseTo(v, 2);
|
||||
});
|
||||
|
||||
it("accepts shorthand and hash-less input", () => {
|
||||
expect(hexToHsv("#f00")).toEqual(hexToHsv("#ff0000"));
|
||||
expect(hexToHsv("ff0000")).toEqual(hexToHsv("#ff0000"));
|
||||
});
|
||||
|
||||
it.each([["#12345g"], ["rgb(0,0,0)"], ["red"], [""], ["#ff00"]])("rejects the non-hex input %p", (color) => {
|
||||
expect(hexToHsv(color)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("hsvToHex", () => {
|
||||
it.each([
|
||||
[{ h: 0, s: 1, v: 1 }, "#ff0000"],
|
||||
[{ h: 120, s: 1, v: 1 }, "#00ff00"],
|
||||
[{ h: 240, s: 1, v: 1 }, "#0000ff"],
|
||||
[{ h: 0, s: 0, v: 1 }, "#ffffff"],
|
||||
[{ h: 0, s: 0, v: 0 }, "#000000"],
|
||||
[{ h: 60, s: 1, v: 1 }, "#ffff00"],
|
||||
])("renders %o as %s", (hsv, expected) => {
|
||||
expect(hsvToHex(hsv)).toBe(expected);
|
||||
});
|
||||
|
||||
it("wraps the hue and clamps the fractions", () => {
|
||||
expect(hsvToHex({ h: 360, s: 1, v: 1 })).toBe("#ff0000");
|
||||
expect(hsvToHex({ h: -120, s: 1, v: 1 })).toBe("#0000ff");
|
||||
expect(hsvToHex({ h: 0, s: 4, v: 4 })).toBe("#ff0000");
|
||||
expect(hsvToHex({ h: 0, s: -1, v: -1 })).toBe("#000000");
|
||||
});
|
||||
|
||||
it("always emits a six-digit lowercase hex", () => {
|
||||
for (let hue = 0; hue < 360; hue += 7) {
|
||||
expect(hsvToHex({ h: hue, s: 0.42, v: 0.73 })).toMatch(/^#[0-9a-f]{6}$/);
|
||||
}
|
||||
});
|
||||
|
||||
it("round-trips a color through HSV without drift", () => {
|
||||
// The spectrum picker converts on every pointer move, so drift would creep.
|
||||
for (const color of ["#1d4ed8", "#0e7490", "#166534", "#c2410c", "#7e22ce", "#93c5fd", "#6ee7b7"]) {
|
||||
expect(hsvToHex(hexToHsv(color)!)).toBe(color);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("hexToRgba", () => {
|
||||
it("keeps working alongside the HSV helpers", () => {
|
||||
expect(hexToRgba("#ff0000", 0.5)).toBe("rgba(255, 0, 0, 0.5)");
|
||||
expect(hexToRgba("nonsense", 1)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { DATA_GRID_DARK_ACTIVE_ROW_BG, DATA_GRID_LIGHT_ACTIVE_ROW_BG, dataGridActiveRowBackground, resolveDataGridPaintTheme } from "@/lib/dataGrid/dataGridPaintTheme";
|
||||
import { DEFAULT_DATA_GRID_TYPE_COLORS_DARK, DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT, dataGridTypeColorCssVar } from "@/lib/dataGrid/dataGridTypeColorScheme";
|
||||
|
||||
function parseRgb(value: string): { r: number; g: number; b: number } | null {
|
||||
const hex = value.match(/^#([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i);
|
||||
@@ -41,6 +42,30 @@ describe("data grid paint theme", () => {
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true }).rowNumberActive).toBe(DATA_GRID_DARK_ACTIVE_ROW_BG);
|
||||
});
|
||||
|
||||
it("falls back to the built-in type palette for the active appearance", () => {
|
||||
const emptyCssVariable = () => "";
|
||||
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: false }).typeForegrounds.integer).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT.integer);
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true }).typeForegrounds.integer).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_DARK.integer);
|
||||
});
|
||||
|
||||
it("lets an overridden type variable drive the canvas foreground", () => {
|
||||
// A custom scheme is applied by writing these variables onto the document,
|
||||
// so the canvas paint theme has to read them rather than its own defaults.
|
||||
const overrides: Record<string, string> = { [dataGridTypeColorCssVar("integer")]: "#123456" };
|
||||
const theme = resolveDataGridPaintTheme({ getVar: (name) => overrides[name] ?? "", isDark: false });
|
||||
|
||||
// A resolved variable is normalized to a canvas-safe rgb(); an absent one keeps the raw fallback.
|
||||
expect(theme.typeForegrounds.integer).toBe("rgb(18, 52, 86)");
|
||||
expect(theme.typeForegrounds.string).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT.string);
|
||||
});
|
||||
|
||||
it("keeps unknown-typed values on the neutral foreground", () => {
|
||||
const theme = resolveDataGridPaintTheme({ getVar: () => "", isDark: false });
|
||||
|
||||
expect(theme.typeForegrounds.unknown).toBe(theme.foreground);
|
||||
});
|
||||
|
||||
it("keeps the classic blue selection palette instead of theme accent/ring mixing", () => {
|
||||
const vars: Record<string, string> = {
|
||||
"--background": "rgb(255, 255, 255)",
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
DATA_GRID_TYPE_COLOR_KEYS,
|
||||
DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID,
|
||||
DEFAULT_DATA_GRID_TYPE_COLORS_DARK,
|
||||
DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT,
|
||||
dataGridTypeColorCssVar,
|
||||
defaultDataGridTypeColors,
|
||||
normalizeActiveDataGridTypeColorSchemeId,
|
||||
normalizeDataGridTypeColorSchemes,
|
||||
normalizeDataGridTypeColors,
|
||||
resolveActiveDataGridTypeColors,
|
||||
type DataGridTypeColorScheme,
|
||||
} from "@/lib/dataGrid/dataGridTypeColorScheme";
|
||||
|
||||
const globalStylesSource = readFileSync(new URL("../../../styles/globals.css", import.meta.url), "utf8");
|
||||
|
||||
function cssBlockVariables(selector: string): Record<string, string> {
|
||||
const start = globalStylesSource.indexOf(`${selector} {`);
|
||||
expect(start).toBeGreaterThanOrEqual(0);
|
||||
const block = globalStylesSource.slice(start, globalStylesSource.indexOf("}", start));
|
||||
const found: Record<string, string> = {};
|
||||
for (const [, name, value] of block.matchAll(/(--data-grid-type-[\w-]+):\s*([^;]+);/g)) {
|
||||
found[name] = value.trim();
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function scheme(id: string, overrides: Partial<Record<string, string>> = {}): DataGridTypeColorScheme {
|
||||
return { id, name: id, colors: { ...DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT, ...overrides } };
|
||||
}
|
||||
|
||||
describe("data grid type color keys", () => {
|
||||
it("covers every visual kind except the neutral unknown", () => {
|
||||
expect(DATA_GRID_TYPE_COLOR_KEYS).toEqual(["integer", "numeric", "string", "boolean", "temporal", "structured", "identifier", "binary", "spatial"]);
|
||||
expect(DATA_GRID_TYPE_COLOR_KEYS).not.toContain("unknown");
|
||||
});
|
||||
|
||||
it("keeps the built-in palettes in sync with globals.css", () => {
|
||||
// The stylesheet cannot import the TS defaults, so drift has to be caught here.
|
||||
const light = cssBlockVariables(":root");
|
||||
const dark = cssBlockVariables(":root.dark");
|
||||
|
||||
for (const key of DATA_GRID_TYPE_COLOR_KEYS) {
|
||||
expect(light[dataGridTypeColorCssVar(key)]).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT[key]);
|
||||
expect(dark[dataGridTypeColorCssVar(key)]).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_DARK[key]);
|
||||
}
|
||||
});
|
||||
|
||||
it("selects the palette matching the appearance", () => {
|
||||
expect(defaultDataGridTypeColors(false)).toEqual(DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT);
|
||||
expect(defaultDataGridTypeColors(true)).toEqual(DEFAULT_DATA_GRID_TYPE_COLORS_DARK);
|
||||
});
|
||||
});
|
||||
|
||||
describe("data grid type color normalization", () => {
|
||||
it("keeps valid six-digit hex values", () => {
|
||||
expect(normalizeDataGridTypeColors({ ...DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT, integer: "#ABCDEF" }).integer).toBe("#ABCDEF");
|
||||
});
|
||||
|
||||
it.each([["#fff"], ["red"], ["rgb(0,0,0)"], ["#12345g"], [""], [42], [null]])("falls back for the unusable value %p", (value) => {
|
||||
expect(normalizeDataGridTypeColors({ integer: value }).integer).toBe(DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT.integer);
|
||||
});
|
||||
|
||||
it("fills every missing key from the supplied fallback palette", () => {
|
||||
expect(normalizeDataGridTypeColors({}, DEFAULT_DATA_GRID_TYPE_COLORS_DARK)).toEqual(DEFAULT_DATA_GRID_TYPE_COLORS_DARK);
|
||||
});
|
||||
|
||||
it.each([[undefined], [null], ["nope"], [{}]])("treats the non-array scheme list %p as empty", (value) => {
|
||||
expect(normalizeDataGridTypeColorSchemes(value)).toEqual([]);
|
||||
});
|
||||
|
||||
it("drops entries without a usable id and de-duplicates the rest", () => {
|
||||
const result = normalizeDataGridTypeColorSchemes([scheme("keep"), { id: " " }, { name: "no id" }, scheme("keep"), null]);
|
||||
|
||||
expect(result.map((entry) => entry.id)).toEqual(["keep"]);
|
||||
});
|
||||
|
||||
it("refuses a persisted scheme claiming the reserved auto id", () => {
|
||||
expect(normalizeDataGridTypeColorSchemes([scheme(DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID)])).toEqual([]);
|
||||
});
|
||||
|
||||
it("falls back to the id when a scheme has no usable name", () => {
|
||||
expect(normalizeDataGridTypeColorSchemes([{ id: "abc", colors: DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT }])[0].name).toBe("abc");
|
||||
});
|
||||
});
|
||||
|
||||
describe("active data grid type color scheme", () => {
|
||||
const schemes = [scheme("mine", { integer: "#123456" })];
|
||||
|
||||
it("keeps an id that resolves to a scheme", () => {
|
||||
expect(normalizeActiveDataGridTypeColorSchemeId(schemes, "mine")).toBe("mine");
|
||||
});
|
||||
|
||||
it.each([["deleted"], [undefined], [null], [7]])("falls back to auto for the unresolvable id %p", (value) => {
|
||||
expect(normalizeActiveDataGridTypeColorSchemeId(schemes, value)).toBe(DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID);
|
||||
});
|
||||
|
||||
it("returns no override under auto so the stylesheet keeps following the theme", () => {
|
||||
expect(resolveActiveDataGridTypeColors(schemes, DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns the selected palette", () => {
|
||||
expect(resolveActiveDataGridTypeColors(schemes, "mine")?.integer).toBe("#123456");
|
||||
});
|
||||
|
||||
it("returns no override when the active scheme is missing", () => {
|
||||
expect(resolveActiveDataGridTypeColors(schemes, "gone")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -28,3 +28,78 @@ export function hexToRgba(color: string | undefined, alpha: number): string | un
|
||||
const b = value & 255;
|
||||
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
||||
}
|
||||
|
||||
/** Hue in degrees (0-360); saturation and value as 0-1 fractions. */
|
||||
export interface HsvColor {
|
||||
h: number;
|
||||
s: number;
|
||||
v: number;
|
||||
}
|
||||
|
||||
function clamp01(value: number): number {
|
||||
if (Number.isNaN(value)) return 0;
|
||||
return Math.min(1, Math.max(0, value));
|
||||
}
|
||||
|
||||
function parseHexChannels(color: string): [number, number, number] | null {
|
||||
const hex = color.trim().replace(/^#/, "");
|
||||
const normalized =
|
||||
hex.length === 3
|
||||
? hex
|
||||
.split("")
|
||||
.map((char) => char + char)
|
||||
.join("")
|
||||
: hex;
|
||||
if (!/^[0-9a-fA-F]{6}$/.test(normalized)) return null;
|
||||
const value = Number.parseInt(normalized, 16);
|
||||
return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param color Hex color, with or without the leading hash
|
||||
* @return Matching HSV coordinates, or null when the input is not a hex color
|
||||
*/
|
||||
export function hexToHsv(color: string): HsvColor | null {
|
||||
const channels = parseHexChannels(color);
|
||||
if (!channels) return null;
|
||||
const [r, g, b] = channels.map((channel) => channel / 255);
|
||||
const max = Math.max(r, g, b);
|
||||
const delta = max - Math.min(r, g, b);
|
||||
|
||||
let h = 0;
|
||||
if (delta !== 0) {
|
||||
if (max === r) h = 60 * (((g - b) / delta) % 6);
|
||||
else if (max === g) h = 60 * ((b - r) / delta + 2);
|
||||
else h = 60 * ((r - g) / delta + 4);
|
||||
}
|
||||
|
||||
return { h: (h + 360) % 360, s: max === 0 ? 0 : delta / max, v: max };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param hsv HSV coordinates; hue wraps and the fractions are clamped
|
||||
* @return Lowercase `#rrggbb` string
|
||||
*/
|
||||
export function hsvToHex(hsv: HsvColor): string {
|
||||
const hue = (((Number.isNaN(hsv.h) ? 0 : hsv.h) % 360) + 360) % 360;
|
||||
const chroma = clamp01(hsv.v) * clamp01(hsv.s);
|
||||
const second = chroma * (1 - Math.abs(((hue / 60) % 2) - 1));
|
||||
const offset = clamp01(hsv.v) - chroma;
|
||||
const sector = Math.floor(hue / 60) % 6;
|
||||
const rgb = [
|
||||
[chroma, second, 0],
|
||||
[second, chroma, 0],
|
||||
[0, chroma, second],
|
||||
[0, second, chroma],
|
||||
[second, 0, chroma],
|
||||
[chroma, 0, second],
|
||||
][sector];
|
||||
|
||||
return `#${rgb
|
||||
.map((channel) =>
|
||||
Math.round((channel + offset) * 255)
|
||||
.toString(16)
|
||||
.padStart(2, "0"),
|
||||
)
|
||||
.join("")}`;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { DataGridTypeVisualKind } from "@/lib/dataGrid/dataGridColumnType";
|
||||
import { DATA_GRID_TYPE_COLOR_KEYS, dataGridTypeColorCssVar, defaultDataGridTypeColors } from "@/lib/dataGrid/dataGridTypeColorScheme";
|
||||
|
||||
export type DataGridTypeForegrounds = Record<DataGridTypeVisualKind, string>;
|
||||
|
||||
@@ -271,18 +272,11 @@ export function resolveDataGridPaintTheme(options: { getVar: (name: string) => s
|
||||
const rowNumberDeleted = isDark ? DATA_GRID_DARK_ROW_NUMBER_DELETED_BG : DATA_GRID_LIGHT_ROW_NUMBER_DELETED_BG;
|
||||
const rowNumberActive = activeSurface;
|
||||
const rowNumberSelected = isDark ? "rgb(30, 64, 96)" : "rgb(191, 219, 254)";
|
||||
const typeForegrounds: DataGridTypeForegrounds = {
|
||||
integer: paintToken(getVar, "--data-grid-type-integer-fg", isDark ? "#93c5fd" : "#1d4ed8"),
|
||||
numeric: paintToken(getVar, "--data-grid-type-numeric-fg", isDark ? "#67e8f9" : "#0e7490"),
|
||||
string: paintToken(getVar, "--data-grid-type-string-fg", isDark ? "#86efac" : "#166534"),
|
||||
boolean: paintToken(getVar, "--data-grid-type-boolean-fg", isDark ? "#fdba74" : "#c2410c"),
|
||||
temporal: paintToken(getVar, "--data-grid-type-temporal-fg", isDark ? "#d8b4fe" : "#7e22ce"),
|
||||
structured: paintToken(getVar, "--data-grid-type-structured-fg", isDark ? "#f9a8d4" : "#be185d"),
|
||||
identifier: paintToken(getVar, "--data-grid-type-identifier-fg", isDark ? "#fcd34d" : "#92400e"),
|
||||
binary: paintToken(getVar, "--data-grid-type-binary-fg", isDark ? "#fca5a5" : "#b91c1c"),
|
||||
spatial: paintToken(getVar, "--data-grid-type-spatial-fg", isDark ? "#6ee7b7" : "#047857"),
|
||||
unknown: foreground,
|
||||
};
|
||||
const typeDefaults = defaultDataGridTypeColors(isDark);
|
||||
const typeForegrounds = { unknown: foreground } as DataGridTypeForegrounds;
|
||||
for (const key of DATA_GRID_TYPE_COLOR_KEYS) {
|
||||
typeForegrounds[key] = paintToken(getVar, dataGridTypeColorCssVar(key), typeDefaults[key]);
|
||||
}
|
||||
|
||||
return {
|
||||
background,
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import { DATA_GRID_TYPE_VISUAL_KINDS, type DataGridTypeVisualKind } from "@/lib/dataGrid/dataGridColumnType";
|
||||
|
||||
/** Every visual kind except `unknown`, which deliberately stays on the neutral foreground. */
|
||||
export type DataGridTypeColorKey = Exclude<DataGridTypeVisualKind, "unknown">;
|
||||
|
||||
export type DataGridTypeColors = Record<DataGridTypeColorKey, string>;
|
||||
|
||||
export interface DataGridTypeColorScheme {
|
||||
id: string;
|
||||
name: string;
|
||||
colors: DataGridTypeColors;
|
||||
}
|
||||
|
||||
export const DATA_GRID_TYPE_COLOR_KEYS = DATA_GRID_TYPE_VISUAL_KINDS.filter((kind): kind is DataGridTypeColorKey => kind !== "unknown");
|
||||
|
||||
/** Sentinel scheme id meaning "leave the stylesheet defaults alone and follow light/dark". */
|
||||
export const DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID = "auto";
|
||||
|
||||
// Keep these in sync with the :root / :root.dark blocks in styles/globals.css.
|
||||
// dataGridTypeColorScheme.spec.ts asserts the two stay identical.
|
||||
export const DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT: DataGridTypeColors = {
|
||||
integer: "#1d4ed8",
|
||||
numeric: "#0e7490",
|
||||
string: "#166534",
|
||||
boolean: "#c2410c",
|
||||
temporal: "#7e22ce",
|
||||
structured: "#be185d",
|
||||
identifier: "#92400e",
|
||||
binary: "#b91c1c",
|
||||
spatial: "#047857",
|
||||
};
|
||||
|
||||
export const DEFAULT_DATA_GRID_TYPE_COLORS_DARK: DataGridTypeColors = {
|
||||
integer: "#93c5fd",
|
||||
numeric: "#67e8f9",
|
||||
string: "#86efac",
|
||||
boolean: "#fdba74",
|
||||
temporal: "#d8b4fe",
|
||||
structured: "#f9a8d4",
|
||||
identifier: "#fcd34d",
|
||||
binary: "#fca5a5",
|
||||
spatial: "#6ee7b7",
|
||||
};
|
||||
|
||||
const HEX_COLOR_RE = /^#[0-9a-fA-F]{6}$/;
|
||||
|
||||
/**
|
||||
* @param key Type visual kind driving the cue
|
||||
* @return CSS custom property backing both the DOM class and the canvas paint theme
|
||||
*/
|
||||
export function dataGridTypeColorCssVar(key: DataGridTypeColorKey): string {
|
||||
return `--data-grid-type-${key}-fg`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param isDark Whether the dark appearance is active
|
||||
* @return Built-in palette for that appearance
|
||||
*/
|
||||
export function defaultDataGridTypeColors(isDark: boolean): DataGridTypeColors {
|
||||
return isDark ? DEFAULT_DATA_GRID_TYPE_COLORS_DARK : DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value Untrusted persisted palette
|
||||
* @param fallback Palette supplying every key the input fails to provide
|
||||
* @return Palette with all nine keys set to a valid `#rrggbb` value
|
||||
*/
|
||||
export function normalizeDataGridTypeColors(value: unknown, fallback: DataGridTypeColors = DEFAULT_DATA_GRID_TYPE_COLORS_LIGHT): DataGridTypeColors {
|
||||
const source = (value && typeof value === "object" ? value : {}) as Record<string, unknown>;
|
||||
const out = {} as DataGridTypeColors;
|
||||
for (const key of DATA_GRID_TYPE_COLOR_KEYS) {
|
||||
const candidate = source[key];
|
||||
out[key] = typeof candidate === "string" && HEX_COLOR_RE.test(candidate) ? candidate : fallback[key];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value Untrusted persisted scheme list
|
||||
* @return Schemes with usable ids, names, and palettes; unusable entries are dropped
|
||||
*/
|
||||
export function normalizeDataGridTypeColorSchemes(value: unknown): DataGridTypeColorScheme[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
const seen = new Set<string>();
|
||||
const out: DataGridTypeColorScheme[] = [];
|
||||
for (const entry of value) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const { id, name, colors } = entry as Record<string, unknown>;
|
||||
// The auto id is reserved, so a persisted scheme may never claim it.
|
||||
if (typeof id !== "string" || !id.trim() || id === DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
out.push({
|
||||
id,
|
||||
name: typeof name === "string" && name.trim() ? name : id,
|
||||
colors: normalizeDataGridTypeColors(colors),
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param schemes Available custom schemes
|
||||
* @param activeId Selected scheme id
|
||||
* @return Selected id, or the auto sentinel when it does not resolve to a scheme
|
||||
*/
|
||||
export function normalizeActiveDataGridTypeColorSchemeId(schemes: DataGridTypeColorScheme[], activeId: unknown): string {
|
||||
if (typeof activeId !== "string" || !schemes.some((scheme) => scheme.id === activeId)) return DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID;
|
||||
return activeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param schemes Available custom schemes
|
||||
* @param activeId Selected scheme id
|
||||
* @return Palette to force onto the CSS variables, or null to keep following the theme
|
||||
*/
|
||||
export function resolveActiveDataGridTypeColors(schemes: DataGridTypeColorScheme[], activeId: string): DataGridTypeColors | null {
|
||||
if (activeId === DATA_GRID_TYPE_COLOR_SCHEME_AUTO_ID) return null;
|
||||
const scheme = schemes.find((entry) => entry.id === activeId);
|
||||
return scheme ? scheme.colors : null;
|
||||
}
|
||||
@@ -69,6 +69,25 @@ describe("EDITOR_SETTINGS_DRAFT_KEYS", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("colorizeDataGridCellTypes");
|
||||
});
|
||||
|
||||
it("includes the type color scheme in the apply-footer draft", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridTypeColorSchemes");
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("activeDataGridTypeColorSchemeId");
|
||||
|
||||
const base = editorSettingsDraftFromSettings(makeSettings({ dataGridTypeColorSchemes: [], activeDataGridTypeColorSchemeId: "auto" }));
|
||||
const withScheme = editorSettingsDraftFromSettings(
|
||||
makeSettings({
|
||||
dataGridTypeColorSchemes: [{ 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" } }],
|
||||
activeDataGridTypeColorSchemeId: "type-colors-1",
|
||||
}),
|
||||
);
|
||||
|
||||
expect(editorSettingsDraftChanged(withScheme, base)).toBe(true);
|
||||
expect(editorSettingsPatchFromDraft(withScheme, base)).toEqual({
|
||||
dataGridTypeColorSchemes: [{ 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" } }],
|
||||
activeDataGridTypeColorSchemeId: "type-colors-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("includes the data grid filter view", () => {
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridFilterEditorView");
|
||||
expect(EDITOR_SETTINGS_DRAFT_KEYS).toContain("dataGridTextFilterPanelHeight");
|
||||
|
||||
@@ -34,6 +34,8 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"showColumnTypesInHeader",
|
||||
"dataGridShowTransposeFieldMetadata",
|
||||
"colorizeDataGridCellTypes",
|
||||
"dataGridTypeColorSchemes",
|
||||
"activeDataGridTypeColorSchemeId",
|
||||
"showIndexIndicatorsInHeader",
|
||||
"compactColumnHeaderActions",
|
||||
"dataGridQuickEntry",
|
||||
|
||||
@@ -179,6 +179,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
|
||||
{ id: "data-grid-header-types", category: "data", titleKey: "settings.showColumnTypesInHeader", descriptionKey: "settings.showColumnTypesInHeaderDescription", targetId: "data" },
|
||||
{ id: "data-grid-transpose-field-metadata", category: "data", titleKey: "settings.dataGridShowTransposeFieldMetadata", descriptionKey: "settings.dataGridShowTransposeFieldMetadataDescription", targetId: "data" },
|
||||
{ id: "data-grid-cell-type-colors", category: "data", titleKey: "settings.colorizeDataGridCellTypes", descriptionKey: "settings.colorizeDataGridCellTypesDescription", targetId: "data" },
|
||||
{ id: "data-grid-type-colors", category: "data", titleKey: "settings.dataGridTypeColorScheme", descriptionKey: "settings.dataGridTypeColorSchemeDescription", targetId: "data-grid-type-colors" },
|
||||
{ id: "data-grid-index-indicators", category: "data", titleKey: "settings.showIndexIndicatorsInHeader", descriptionKey: "settings.showIndexIndicatorsInHeaderDescription", targetId: "data" },
|
||||
{ id: "data-grid-compact-header-actions", category: "data", titleKey: "settings.compactColumnHeaderActions", descriptionKey: "settings.compactColumnHeaderActionsDescription", targetId: "data" },
|
||||
{ id: "data-grid-auto-total", category: "data", titleKey: "settings.autoCalculateTotalRows", descriptionKey: "settings.autoCalculateTotalRowsDescription", targetId: "data" },
|
||||
|
||||
Reference in new issue
Block a user