feat(grid): add row/column crosshair highlight setting

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-08-27 22:23:13 +08:00
1 parent b09c6f4f21
commit ec0b6768be
18 files changed
+496 -2

No files matched your search

@@ -0,0 +1,174 @@
import { describe, expect, it } from "vitest";
import { resolveCrosshairTarget } from "@/lib/dataGrid/crosshairHighlight";
import type { CellPosition } from "@/lib/dataGrid/gridSelection";
function focus(rowIndex: number, colIndex: number): CellPosition {
return { rowIndex, colIndex };
}
describe("resolveCrosshairTarget", () => {
const visibleColumnIndexes = [3, 7, 11];
it("resolves both axes from selectionFocus for a plain cell", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(2, 1),
selectionAnchor: focus(2, 1),
hasRowSelection: false,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target).toEqual({
rowIndex: 2,
visibleColIdx: 1,
actualColIdx: 7,
rowCrosshair: true,
columnCrosshair: true,
});
});
it("keeps both axes for a multi-cell range (anchor + focus)", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(5, 2),
selectionAnchor: focus(1, 0),
hasRowSelection: false,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target).toEqual({
rowIndex: 5,
visibleColIdx: 2,
actualColIdx: 11,
rowCrosshair: true,
columnCrosshair: true,
});
});
it("only highlights the row when hasRowSelection is set alongside focus", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(4, 0),
selectionAnchor: focus(4, 0),
hasRowSelection: true,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target?.rowCrosshair).toBe(true);
expect(target?.columnCrosshair).toBe(false);
expect(target?.rowIndex).toBe(4);
});
it("only highlights the column when hasColumnSelection is set alongside focus", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(4, 0),
selectionAnchor: focus(4, 0),
hasRowSelection: false,
hasColumnSelection: true,
visibleColumnIndexes,
});
expect(target?.rowCrosshair).toBe(false);
expect(target?.columnCrosshair).toBe(true);
expect(target?.visibleColIdx).toBe(0);
});
it("derives the row from fallbackRowIndex for a whole-row selection with no focus", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: true,
hasColumnSelection: false,
visibleColumnIndexes,
fallbackRowIndex: 9,
});
expect(target).toEqual({
rowIndex: 9,
visibleColIdx: 0,
actualColIdx: 3,
rowCrosshair: true,
columnCrosshair: false,
});
});
it("returns null for a whole-row selection when fallbackRowIndex is missing", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: true,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target).toBeNull();
});
it("derives the column from fallbackColumnIndex for a whole-column selection with no focus", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: false,
hasColumnSelection: true,
visibleColumnIndexes,
fallbackColumnIndex: 2,
});
expect(target).toEqual({
rowIndex: 0,
visibleColIdx: 2,
actualColIdx: 11,
rowCrosshair: false,
columnCrosshair: true,
});
});
it("returns null for a whole-column selection when fallbackColumnIndex is missing", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: false,
hasColumnSelection: true,
visibleColumnIndexes,
});
expect(target).toBeNull();
});
it("returns null when there is no focus and no row/column selection", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: false,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target).toBeNull();
});
it("returns null when selectionFocus.colIndex is out of range for visibleColumnIndexes", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(0, 5),
selectionAnchor: focus(0, 5),
hasRowSelection: false,
hasColumnSelection: false,
visibleColumnIndexes,
});
expect(target).toBeNull();
});
it("returns null when fallbackColumnIndex is out of range for visibleColumnIndexes", () => {
const target = resolveCrosshairTarget({
selectionFocus: null,
selectionAnchor: null,
hasRowSelection: false,
hasColumnSelection: true,
visibleColumnIndexes,
fallbackColumnIndex: 9,
});
expect(target).toBeNull();
});
it("returns null when visibleColumnIndexes is empty even with a focus", () => {
const target = resolveCrosshairTarget({
selectionFocus: focus(0, 0),
selectionAnchor: focus(0, 0),
hasRowSelection: false,
hasColumnSelection: false,
visibleColumnIndexes: [],
});
expect(target).toBeNull();
});
});
@@ -1,6 +1,15 @@
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 {
DATA_GRID_DARK_ACTIVE_ROW_BG,
DATA_GRID_DARK_CROSSHAIR_COL_BG,
DATA_GRID_DARK_CROSSHAIR_ROW_BG,
DATA_GRID_LIGHT_ACTIVE_ROW_BG,
DATA_GRID_LIGHT_CROSSHAIR_COL_BG,
DATA_GRID_LIGHT_CROSSHAIR_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 {
@@ -42,6 +51,69 @@ describe("data grid paint theme", () => {
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true }).rowNumberActive).toBe(DATA_GRID_DARK_ACTIVE_ROW_BG);
});
it("resolves crosshair row/col fills in both color schemes", () => {
const emptyCssVariable = () => "";
const light = resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: false });
const dark = resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true });
// 十字底色必须比 cellActive 更明显(与 cellActive 不同、列比行更深的蓝色系)
expect(light.cellCrosshairRow).toBe(DATA_GRID_LIGHT_CROSSHAIR_ROW_BG);
expect(light.cellCrosshairCol).toBe(DATA_GRID_LIGHT_CROSSHAIR_COL_BG);
expect(light.cellCrosshairRow).not.toBe(light.cellActive);
expect(light.cellCrosshairCol).not.toBe(light.cellActive);
expect(light.cellCrosshairCol).not.toBe(light.cellCrosshairRow);
expect(dark.cellCrosshairRow).toBe(DATA_GRID_DARK_CROSSHAIR_ROW_BG);
expect(dark.cellCrosshairCol).toBe(DATA_GRID_DARK_CROSSHAIR_COL_BG);
expect(dark.cellCrosshairRow).not.toBe(dark.cellActive);
expect(dark.cellCrosshairCol).not.toBe(dark.cellActive);
expect(dark.cellCrosshairCol).not.toBe(dark.cellCrosshairRow);
// These are spatial location cues rather than text, but they must remain
// visibly distinct from the grid surface in both modes.
expect(contrastRatio(light.background, light.cellCrosshairRow)).toBeGreaterThanOrEqual(1.5);
expect(contrastRatio(light.background, light.cellCrosshairCol)).toBeGreaterThanOrEqual(2);
expect(contrastRatio(dark.background, dark.cellCrosshairRow)).toBeGreaterThanOrEqual(1.5);
expect(contrastRatio(dark.background, dark.cellCrosshairCol)).toBeGreaterThanOrEqual(2);
});
it("lets explicit crosshair CSS variables drive the canvas fills in light mode", () => {
const vars: Record<string, string> = {
"--data-grid-cell-crosshair-row-bg": "rgb(190, 220, 250)",
"--data-grid-cell-crosshair-col-bg": "rgb(160, 205, 250)",
};
const theme = resolveDataGridPaintTheme({ getVar: (name) => vars[name] ?? "", isDark: false });
expect(theme.cellCrosshairRow).toBe("rgb(190, 220, 250)");
expect(theme.cellCrosshairCol).toBe("rgb(160, 205, 250)");
});
it("uses the theme primary color at the same stronger sRGB ratios as the DOM grid", () => {
const vars: Record<string, string> = {
"--primary": "rgb(58, 123, 106)",
"--background": "rgb(248, 250, 248)",
"--data-grid-cell-crosshair-row-bg": "color-mix(in srgb, var(--primary) 34%, var(--background))",
"--data-grid-cell-crosshair-col-bg": "color-mix(in srgb, var(--primary) 50%, var(--background))",
};
const theme = resolveDataGridPaintTheme({ getVar: (name) => vars[name] ?? "", isDark: false });
expect(theme.cellCrosshairRow).toBe("rgb(183, 207, 200)");
expect(theme.cellCrosshairCol).toBe("rgb(153, 187, 177)");
});
it("propagates crosshair fills through the base cell variable so DOM and frozen cells stay visible", () => {
const gridSource = readFileSync(new URL("../../../components/grid/DataGrid.vue", import.meta.url), "utf8");
expect(gridSource).toMatch(/\.crosshair-row\s*\{\s*--data-grid-cell-bg:\s*var\(--data-grid-cell-crosshair-row-bg\)\s*!important;/);
expect(gridSource).toMatch(/\.crosshair-column\s*\{\s*--data-grid-cell-bg:\s*var\(--data-grid-cell-crosshair-col-bg\)\s*!important;/);
expect(gridSource).toContain("color-mix(in srgb, var(--primary) 34%, var(--background))");
expect(gridSource).toContain("color-mix(in srgb, var(--primary) 50%, var(--background))");
expect(gridSource).toMatch(/\.data-grid-cell--frozen\s*\{\s*background-color:\s*var\(--data-grid-cell-bg,/);
});
it("falls back to the built-in type palette for the active appearance", () => {
const emptyCssVariable = () => "";
@@ -6,6 +6,7 @@ import { dataGridFrameCoversRow, dataGridFrameIsMultiCell, dataGridSelectionFram
import type { CellSelectionRange } from "@/lib/dataGrid/gridSelection";
import type { RowStatus } from "@/lib/dataGrid/gridRowStatus";
import { DATA_GRID_DARK_SEARCH_COLORS, dataGridTypeForeground, resolveDataGridPaintTheme, type DataGridPaintTheme } from "@/lib/dataGrid/dataGridPaintTheme";
import type { CrosshairTarget } from "@/lib/dataGrid/crosshairHighlight";
export const CANVAS_DATA_GRID_ROW_HEIGHT = 26;
export const MAX_CANVAS_DATA_GRID_PIXEL_RATIO = 4;
@@ -94,6 +95,8 @@ export interface DrawCanvasDataGridOptions {
columnAligns?: readonly ("left" | "right")[];
columnTypeVisualKinds?: readonly DataGridTypeVisualKind[];
colorizeDataTypes?: boolean;
/** 行列十字高亮目标(原样传入,null 表示开关关闭或无焦点)。只画当前 viewport 内的行/列底色 */
crosshair?: CrosshairTarget | null;
rightAlignedActionCell?: CanvasRightAlignedActionCell | null;
booleanDisplayMode?: "checkbox" | "dropdown";
flatteningMultiLineEnabled: boolean;
@@ -353,6 +356,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
columnAligns,
columnTypeVisualKinds,
colorizeDataTypes = false,
crosshair,
rightAlignedActionCell,
columnIsBoolean,
booleanDisplayMode = "dropdown",
@@ -429,6 +433,13 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
ctx.fillStyle = rowFill;
ctx.fillRect(0, y, width, CANVAS_DATA_GRID_ROW_HEIGHT);
// 十字行高亮:叠在基础行色之上,但低于整行选中(rowSelectionVisual),
// 也低于后续 drawCell 的脏格/搜索/选中格填充
if (crosshair?.rowCrosshair && item.displayIndex === crosshair.rowIndex && !rowSelectionVisual && !item.isDeleted) {
ctx.fillStyle = theme.cellCrosshairRow;
ctx.fillRect(rowNumberWidth, y, width - rowNumberWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
}
// 选区覆盖指示(Navicat 风格):行落在选区范围内时行号淡色高亮;
// 优先级低于行选中/状态色/活动行,与 DOM 的级联顺序一致
const rowInSelection = !rowSelectionVisual && selectionRowCoverage && dataGridFrameCoversRow(selectionFrames, item.displayIndex);
@@ -503,6 +514,12 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
const cellPaintWidth = Math.min(width, drawX + colWidth) - clippedX;
if (cellPaintWidth <= 0) return;
// 十字列高亮:整列覆盖,叠在行底色之上;脏格/搜索/选中格填充在其后绘制,优先级更高
if (crosshair?.columnCrosshair && visibleColIdx === crosshair.visibleColIdx && !selectedFillVisual && !item.isDeleted) {
ctx.fillStyle = theme.cellCrosshairCol;
ctx.fillRect(clippedX, y, cellPaintWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
}
if (isDirtyCell && !selectedFillVisual) {
ctx.fillStyle = theme.cellDirty;
ctx.fillRect(clippedX, y, cellPaintWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
@@ -658,6 +675,11 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
ctx.fillStyle = rowFill;
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
}
// 冻结区会重绘底色遮挡第一轮溢入内容,需在此重绘十字行底色保持一致
if (crosshair?.rowCrosshair && item.displayIndex === crosshair.rowIndex && !rowSelectionVisual && !item.isDeleted) {
ctx.fillStyle = theme.cellCrosshairRow;
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
}
// 绘制冻结列的每个单元格(x 坐标不受 scrollLeft 影响)
for (let fcIdx = 0; fcIdx < frozenColumnCount; fcIdx++) {
const cellX = rowNumberWidth + (offsets[fcIdx] ?? 0);
@@ -0,0 +1,81 @@
import type { CellPosition } from "@/lib/dataGrid/gridSelection";
/**
* 行列十字高亮目标。只解析「原始选择状态」,不做转置投影(转置是纯展示层映射,
* 由 DataGrid 在渲染时把 x/y 轴互换)。供 Canvas / 常规 DOM / 转置 DOM 三个渲染分支共享。
*
* 坐标约定:
* - rowIndex 为原始行(displayIndex);
* - visibleColIdx 为可见列下标(selectionFocus.colIndex 语义,与 cellIsSelected 一致);
* - actualColIdx 为 visibleColumnIndexes[visibleColIdx] 对应的真实列下标。
*/
export interface CrosshairTarget {
rowIndex: number; // 原始行(displayIndex)
visibleColIdx: number; // selectionFocus.colIndex(可见列)
actualColIdx: number; // visibleColumnIndexes[visibleColIdx]
rowCrosshair: boolean; // 这一轴是否绘制(非"是否焦点格")
columnCrosshair: boolean;
}
export interface ResolveCrosshairTargetOptions {
selectionFocus: CellPosition | null;
selectionAnchor: CellPosition | null;
hasRowSelection: boolean;
hasColumnSelection: boolean;
visibleColumnIndexes: readonly number[];
/** 整行选中且无单元格焦点时派生行(DataGrid 从 lastClickedRowIndex / 首个 selectedRowId 推导) */
fallbackRowIndex?: number | null;
/** 整列选中且无单元格焦点时派生列(DataGrid 从首个 selectedColumnIndexes 推导) */
fallbackColumnIndex?: number | null;
}
export function resolveCrosshairTarget(options: ResolveCrosshairTargetOptions): CrosshairTarget | null {
const { selectionFocus, hasRowSelection, hasColumnSelection, visibleColumnIndexes } = options;
if (selectionFocus) {
const actualColIdx = visibleColumnIndexes[selectionFocus.colIndex];
if (actualColIdx === undefined) return null;
// 整行/整列选中会清空 focus(selectRow / selectColumns),因此普通格或多格范围
// 的 focus 必不与其他整行/整列选中并存;但防御性处理:若共存,只亮对应轴。
const rowCrosshair = !hasColumnSelection;
const columnCrosshair = !hasRowSelection;
return {
rowIndex: selectionFocus.rowIndex,
visibleColIdx: selectionFocus.colIndex,
actualColIdx,
rowCrosshair,
columnCrosshair,
};
}
// selectRow / handleRowClick 会调用 clearCellSelection() 清空 focus,整行选中时只能派生行。
if (hasRowSelection) {
if (options.fallbackRowIndex === null || options.fallbackRowIndex === undefined) return null;
const actualColIdx = visibleColumnIndexes[0];
if (actualColIdx === undefined) return null;
return {
rowIndex: options.fallbackRowIndex,
visibleColIdx: 0,
actualColIdx,
rowCrosshair: true,
columnCrosshair: false,
};
}
// selectColumns 会清空 anchor/focus,整列选中时只能派生列。
if (hasColumnSelection) {
if (options.fallbackColumnIndex === null || options.fallbackColumnIndex === undefined) return null;
const actualColIdx = visibleColumnIndexes[options.fallbackColumnIndex];
if (actualColIdx === undefined) return null;
return {
rowIndex: 0,
visibleColIdx: options.fallbackColumnIndex,
actualColIdx,
rowCrosshair: false,
columnCrosshair: true,
};
}
// 无焦点、加载中、空结果不绘制。
return null;
}
@@ -13,6 +13,8 @@ export interface DataGridPaintTheme {
rowNew: string;
rowDeleted: string;
cellActive: string;
cellCrosshairRow: string;
cellCrosshairCol: string;
cellDirty: string;
cellSelected: string;
cellSelectedDirty: string;
@@ -43,6 +45,17 @@ export const DATA_GRID_DARK_SEARCH_COLORS = {
} as const;
export const DATA_GRID_LIGHT_ACTIVE_ROW_BG = "rgb(244, 248, 255)";
export const DATA_GRID_DARK_ACTIVE_ROW_BG = "rgb(25, 34, 46)";
// 十字行/列底色:混入背景的实色(color-mix(in srgb, var(--primary) P%, var(--background))),
// 而非透明叠加——铺在浅底上的半透明色会被冲淡到几乎不可见,对比度不足正 jira #7276 的核心痛点。
// 行/列占 primary 34%/50%(CSS 方向:P% primary + 其余 background);现有 28%/40% 在浅色
// 主题的大面积网格中仍不够醒目。列更重,交点通过 Canvas 的两次绘制进一步加深;选中格仍以显式
// 填充与描边优先,故不会被十字高亮覆盖。
// 常量按默认主题 soft-light/dark(primary/background)混合得出,作为 var 未设置 / 旧浏览器
// 降级实色;color-mix 支持时网格组件的 CSS 变量会用它重新混成 —— 两处取值一致。
export const DATA_GRID_LIGHT_CROSSHAIR_ROW_BG = "rgb(174, 195, 224)";
export const DATA_GRID_LIGHT_CROSSHAIR_COL_BG = "rgb(142, 170, 210)";
export const DATA_GRID_DARK_CROSSHAIR_ROW_BG = "rgb(75, 84, 98)";
export const DATA_GRID_DARK_CROSSHAIR_COL_BG = "rgb(98, 111, 130)";
export const DATA_GRID_LIGHT_STRIPED_ROW_BG = "rgb(240, 240, 240)";
export const DATA_GRID_DARK_STRIPED_ROW_BG = "rgb(40, 40, 43)";
export const DATA_GRID_DARK_ROW_NUMBER_BG = "rgb(35, 37, 42)";
@@ -151,7 +164,8 @@ function mixRgb(first: RgbaColor, firstPercent: number, second: RgbaColor, secon
}
function parseColorMix(value: string): string | null {
const body = value.match(/^color-mix\(\s*in\s+oklab\s*,\s*(.*)\)$/i)?.[1];
// Crosshair tokens use sRGB so Canvas can reproduce the DOM blend exactly.
const body = value.match(/^color-mix\(\s*in\s+(?:srgb|oklab)\s*,\s*(.*)\)$/i)?.[1];
if (!body) return null;
const [firstRaw, secondRaw] = splitTopLevelCommas(body);
if (!firstRaw || !secondRaw) return null;
@@ -257,6 +271,10 @@ export function resolveDataGridPaintTheme(options: { getVar: (name: string) => s
const rowNew = isDark ? "rgb(51, 51, 55)" : "rgb(243, 243, 243)";
const rowDeleted = isDark ? "rgb(55, 31, 32)" : "rgb(255, 244, 244)";
const cellActive = activeSurface;
// 浅/深都读 CSS 变量(--data-grid-cell-crosshair-*-bg 从 --primary 派生),保证 DOM 与
// Canvas 在不同主题/深浅模式下取到同一色源;固定常量仅作 var 未设置时的降级默认。
const cellCrosshairRow = paintToken(getVar, "--data-grid-cell-crosshair-row-bg", isDark ? DATA_GRID_DARK_CROSSHAIR_ROW_BG : DATA_GRID_LIGHT_CROSSHAIR_ROW_BG);
const cellCrosshairCol = paintToken(getVar, "--data-grid-cell-crosshair-col-bg", isDark ? DATA_GRID_DARK_CROSSHAIR_COL_BG : DATA_GRID_LIGHT_CROSSHAIR_COL_BG);
const cellDirty = isDark ? "rgb(94, 75, 26)" : "rgb(255, 248, 230)";
const cellSelected = isDark ? "rgb(20, 40, 60)" : "rgb(239, 246, 255)";
const cellSelectedDirty = isDark ? "rgb(76, 66, 38)" : "rgb(235, 224, 184)";
@@ -288,6 +306,8 @@ export function resolveDataGridPaintTheme(options: { getVar: (name: string) => s
rowNew: isDark ? rowNew : paintToken(getVar, "--data-grid-row-new-bg", rowNew),
rowDeleted: isDark ? rowDeleted : paintToken(getVar, "--data-grid-row-deleted-bg", rowDeleted),
cellActive: isDark ? cellActive : paintToken(getVar, "--data-grid-cell-active-bg", cellActive),
cellCrosshairRow,
cellCrosshairCol,
cellDirty: paintToken(getVar, "--data-grid-cell-dirty-bg", cellDirty),
cellSelected: paintToken(getVar, "--data-grid-cell-selected-bg", cellSelected),
cellSelectedDirty: paintToken(getVar, "--data-grid-cell-selected-dirty-bg", cellSelectedDirty),
@@ -47,6 +47,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"dataGridTextFilterPanelHeight",
"dataGridAutoTransposeSingleRow",
"dataGridCellDetailButtonVisible",
"dataGridCrosshairHighlight",
"pageSize",
"tableOpenPageSize",
"queryResultMaxRowsEnabled",