feat(grid): add find and replace for query results

This commit is contained in:
caichangqing1120 authored and GitHub committed 2026-09-17 14:21:41 +08:00
1 parent d17ce46626
commit 8567e50887
19 files changed
+747 -81

No files matched your search

@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { findDataGridReplacementMatches, replaceDataGridText } from "@/lib/dataGrid/dataGridReplace";
describe("data grid searched text replacement", () => {
it("replaces repeated JSON fragments without changing the rest of the value", () => {
const value = '[{"backend":"svc.test-test:80","path":"/api/**"},{"backend":"svc2.test-test:81"}]';
expect(replaceDataGridText(value, "test-test", "prod", false)).toBe('[{"backend":"svc.prod:80","path":"/api/**"},{"backend":"svc2.prod:81"}]');
});
it("treats both search and replacement as literals", () => {
expect(replaceDataGridText("a.* a.*", ".*", "$&$1", false)).toBe("a$&$1 a$&$1");
expect(replaceDataGridText("one\\two", "\\", "$'", false)).toBe("one$'two");
});
it("supports case sensitivity, whitespace searches and empty replacement strings", () => {
expect(replaceDataGridText("TEST test", "test", "x", true)).toBe("TEST x");
expect(replaceDataGridText("TEST test", "test", "x", false)).toBe("x x");
expect(replaceDataGridText(" a ", " ", "", false)).toBe("a");
expect(replaceDataGridText("hit", "hit", "", false)).toBe("");
expect(replaceDataGridText("hit", "", "x", false)).toBe("hit");
});
const rows = [
{ rowId: 7, data: ["hit", null, 123, true, "HIT"] },
{ rowId: 2, data: ["hit", "hit", "none", false, "hit"] },
];
it("matches only string cells in loaded rows using stable source IDs", () => {
expect(findDataGridReplacementMatches({ rows, search: "hit" })).toEqual([
{ rowId: 7, col: 0, value: "hit" },
{ rowId: 7, col: 4, value: "HIT" },
{ rowId: 2, col: 0, value: "hit" },
{ rowId: 2, col: 1, value: "hit" },
{ rowId: 2, col: 4, value: "hit" },
]);
});
it("applies column, selection, case, readonly and truncation restrictions before matching", () => {
expect(findDataGridReplacementMatches({ rows, search: "hit", caseSensitive: true, column: 4 })).toEqual([{ rowId: 2, col: 4, value: "hit" }]);
expect(findDataGridReplacementMatches({ rows, search: "hit", includesCell: (rowId, col) => rowId === 2 && col === 1 })).toEqual([{ rowId: 2, col: 1, value: "hit" }]);
expect(findDataGridReplacementMatches({ rows, search: "hit", canReplaceCell: (rowId, col) => rowId !== 7 && col !== 1, isTruncated: (rowId, col) => rowId === 2 && col === 4 })).toEqual([{ rowId: 2, col: 0, value: "hit" }]);
expect(findDataGridReplacementMatches({ rows, search: "hit", includesCell: () => false })).toEqual([]);
expect(findDataGridReplacementMatches({ rows, search: "" })).toEqual([]);
});
});
@@ -0,0 +1,42 @@
import type { CellValue } from "@/lib/dataGrid/cellValue";
export type DataGridReplacementMatch = { rowId: number; col: number; value: string };
export type DataGridCellReplacement = DataGridReplacementMatch & { previousValue: string };
export type DataGridReplaceScope = "loaded" | "column" | "selection";
type ReplacementMatchOptions = {
rows: readonly { rowId: number; data: readonly CellValue[] }[];
search: string;
caseSensitive?: boolean;
column?: number;
includesCell?: (rowId: number, col: number) => boolean;
canReplaceCell?: (rowId: number, col: number) => boolean;
isTruncated?: (rowId: number, col: number) => boolean;
};
export function dataGridReplacementPattern(search: string, caseSensitive: boolean): RegExp {
return new RegExp(search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), caseSensitive ? "gu" : "giu");
}
export function replaceDataGridText(value: string, search: string, replacement: string, caseSensitive: boolean): string {
if (!search) return value;
// A callback keeps $&, $1 and other replacement tokens literal.
return value.replace(dataGridReplacementPattern(search, caseSensitive), () => replacement);
}
export function findDataGridReplacementMatches(options: ReplacementMatchOptions): DataGridReplacementMatch[] {
if (!options.search) return [];
const pattern = dataGridReplacementPattern(options.search, options.caseSensitive ?? false);
const matches: DataGridReplacementMatch[] = [];
for (const row of options.rows) {
row.data.forEach((value, col) => {
if (typeof value !== "string" || (options.column !== undefined && col !== options.column)) return;
if (options.includesCell && !options.includesCell(row.rowId, col)) return;
if (options.canReplaceCell && !options.canReplaceCell(row.rowId, col)) return;
if (options.isTruncated?.(row.rowId, col)) return;
pattern.lastIndex = 0;
if (pattern.test(value)) matches.push({ rowId: row.rowId, col, value });
});
}
return matches;
}