feat(grid): add find and replace for query results
This commit is contained in:
1 parent
d17ce46626
commit
8567e50887
19 files changed
+747
-81
No files matched your search
@@ -49,6 +49,7 @@ function createEditorWithResult(
|
||||
onCellValueChanged?: (rowId: number, columnIndex: number) => void,
|
||||
tableColumns?: Array<{ name: string; data_type: string; extra?: string; column_default?: string }>,
|
||||
mongoCollectionGrid = false,
|
||||
quickEntry = false,
|
||||
) {
|
||||
let editor: ReturnType<typeof useDataGridEditor>;
|
||||
const result = ref<{ columns: string[]; rows: CellValue[][] }>({
|
||||
@@ -77,6 +78,7 @@ function createEditorWithResult(
|
||||
onExecuteSql: computed(() => undefined),
|
||||
sql: computed(() => undefined),
|
||||
searchText: ref(""),
|
||||
dataGridQuickEntryEnabled: computed(() => quickEntry),
|
||||
whereFilterInput: ref(""),
|
||||
currentWhereInput: computed(() => undefined),
|
||||
orderByInput: ref(""),
|
||||
@@ -136,6 +138,122 @@ function createEditor(...args: Parameters<typeof createEditorWithResult>) {
|
||||
return createEditorWithResult(...args).editor;
|
||||
}
|
||||
|
||||
describe("useDataGridEditor searched replacements", () => {
|
||||
beforeEach(() => {
|
||||
mocks.prepareDataGridSave.mockReset();
|
||||
mocks.executeBatch.mockReset();
|
||||
mocks.getConfig.mockReturnValue(undefined);
|
||||
});
|
||||
|
||||
it("stages one undoable batch, keeps empty strings and preserves prior pending edits", () => {
|
||||
const editor = createEditor(undefined, true, undefined, undefined, [["hit", "keep", "hit"]]);
|
||||
editor.newRows.value = [];
|
||||
editor.applyCellValue(0, 1, "draft");
|
||||
expect(
|
||||
editor.stageCellReplacements([
|
||||
{ rowId: 0, col: 0, previousValue: "hit", value: "" },
|
||||
{ rowId: 0, col: 2, previousValue: "hit", value: "new" },
|
||||
]),
|
||||
).toBe(2);
|
||||
expect(editor.dirtyRows.value.get(0)).toEqual(
|
||||
new Map([
|
||||
[1, "draft"],
|
||||
[0, ""],
|
||||
[2, "new"],
|
||||
]),
|
||||
);
|
||||
expect(editor.manualSaveRequired.value).toBe(true);
|
||||
editor.undoPendingChange();
|
||||
expect(editor.dirtyRows.value.get(0)).toEqual(new Map([[1, "draft"]]));
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
editor.redoPendingChange();
|
||||
expect(editor.dirtyRows.value.get(0)?.get(0)).toBe("");
|
||||
expect(editor.manualSaveRequired.value).toBe(true);
|
||||
expect(mocks.executeBatch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects readonly, stale, deleted, non-string and out-of-range targets", () => {
|
||||
const editor = createEditor(
|
||||
["first", undefined, "last"],
|
||||
true,
|
||||
undefined,
|
||||
[2],
|
||||
[
|
||||
["hit", "hit", "hit"],
|
||||
[123, null, false],
|
||||
],
|
||||
);
|
||||
editor.newRows.value = [];
|
||||
editor.deletedRows.value.add(0);
|
||||
const changes = [
|
||||
{ rowId: 0, col: 0, previousValue: "hit", value: "x" },
|
||||
{ rowId: 1, col: 0, previousValue: "123", value: "x" },
|
||||
{ rowId: 9, col: 0, previousValue: "hit", value: "x" },
|
||||
];
|
||||
expect(editor.stageCellReplacements(changes)).toBe(0);
|
||||
editor.deletedRows.value.clear();
|
||||
expect(
|
||||
editor.stageCellReplacements([
|
||||
{ rowId: 0, col: 0, previousValue: "stale", value: "x" },
|
||||
{ rowId: 0, col: 1, previousValue: "hit", value: "x" },
|
||||
{ rowId: 0, col: 2, previousValue: "hit", value: "x" },
|
||||
{ rowId: -1, col: 0, previousValue: "hit", value: "x" },
|
||||
]),
|
||||
).toBe(0);
|
||||
expect(editor.dirtyRows.value.size).toBe(0);
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
});
|
||||
|
||||
it("blocks later quick-entry autosaves until explicit save or discard", async () => {
|
||||
const editor = createEditor(undefined, true, undefined, undefined, [["hit", "keep", "hit"]], undefined, undefined, false, true);
|
||||
editor.newRows.value = [];
|
||||
editor.stageCellReplacements([{ rowId: 0, col: 0, previousValue: "hit", value: "new" }]);
|
||||
editor.applyCellValue(0, 1, "later edit");
|
||||
await editor.saveChanges({ autoSave: true });
|
||||
expect(mocks.prepareDataGridSave).not.toHaveBeenCalled();
|
||||
expect(mocks.executeBatch).not.toHaveBeenCalled();
|
||||
expect(editor.hasPendingChanges.value).toBe(true);
|
||||
mocks.prepareDataGridSave.mockResolvedValue({ statements: ["UPDATE people SET first='new',hidden='later edit' WHERE last='hit'"], rollbackStatements: [] });
|
||||
mocks.executeBatch.mockResolvedValue([]);
|
||||
await editor.saveChanges();
|
||||
expect(mocks.executeBatch).toHaveBeenCalledOnce();
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
editor.stageCellReplacements([{ rowId: 0, col: 0, previousValue: "new", value: "other" }]);
|
||||
editor.discardChanges();
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
});
|
||||
|
||||
it("persists the manual-save interlock across a tab remount", async () => {
|
||||
const key = "replace-remount";
|
||||
const rows: CellValue[][] = [["hit", "keep", "hit"]];
|
||||
const first = createEditor(undefined, true, key, undefined, rows);
|
||||
first.newRows.value = [];
|
||||
first.stageCellReplacements([{ rowId: 0, col: 0, previousValue: "hit", value: "new" }]);
|
||||
first.savePendingSnapshot();
|
||||
const second = createEditor(undefined, true, key, undefined, rows);
|
||||
second.newRows.value = [];
|
||||
expect(second.manualSaveRequired.value).toBe(true);
|
||||
await second.saveChanges({ autoSave: true });
|
||||
expect(mocks.executeBatch).not.toHaveBeenCalled();
|
||||
second.discardChanges();
|
||||
clearDataGridPendingSnapshot(key);
|
||||
});
|
||||
|
||||
it("clears the manual-save interlock when replacement restores all original values", () => {
|
||||
const editor = createEditor(undefined, true, undefined, undefined, [["hit", "keep", "hit"]]);
|
||||
editor.newRows.value = [];
|
||||
editor.stageCellReplacements([{ rowId: 0, col: 0, previousValue: "hit", value: "new" }]);
|
||||
editor.stageCellReplacements([{ rowId: 0, col: 0, previousValue: "new", value: "hit" }]);
|
||||
expect(editor.hasPendingChanges.value).toBe(false);
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
editor.undoPendingChange();
|
||||
expect(editor.dirtyRows.value.get(0)?.get(0)).toBe("new");
|
||||
expect(editor.manualSaveRequired.value).toBe(true);
|
||||
editor.redoPendingChange();
|
||||
expect(editor.manualSaveRequired.value).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
function beforeTabSwitchEvent(fromTabId: string, tabId = "next-tab") {
|
||||
return new CustomEvent("dbx:before-tab-switch", { detail: { tabId, fromTabId } });
|
||||
}
|
||||
|
||||
@@ -15,6 +15,49 @@ async function flushSearchDebounce() {
|
||||
}
|
||||
|
||||
describe("useDataGridSearch", () => {
|
||||
it("supports literal case-sensitive replacement searches without column-name matches", async () => {
|
||||
vi.useFakeTimers();
|
||||
const sensitive = ref(true);
|
||||
const search = useDataGridSearch({
|
||||
columns: ["HIT", "value"],
|
||||
rows: [
|
||||
["HIT", "hit hit"],
|
||||
[" hit ", "hit"],
|
||||
],
|
||||
getCellSearchText: (row, col) => row[col].toLowerCase(),
|
||||
getCellRawSearchText: (row, col) => row[col],
|
||||
caseSensitive: sensitive,
|
||||
literalQuery: true,
|
||||
includeColumnMatches: false,
|
||||
isCellSearchable: (_row, col) => col === 0,
|
||||
});
|
||||
search.searchText.value = "HIT";
|
||||
await flushSearchDebounce();
|
||||
expect(search.matches.value).toEqual([{ kind: "cell", displayRow: 0, col: 0 }]);
|
||||
sensitive.value = false;
|
||||
await flushSearchDebounce();
|
||||
expect(search.matchCount.value).toBe(2);
|
||||
search.searchText.value = " hit ";
|
||||
await flushSearchDebounce();
|
||||
expect(search.matches.value).toEqual([{ kind: "cell", displayRow: 1, col: 0 }]);
|
||||
});
|
||||
it("degrades case-sensitive matching to case-insensitive without a raw-text provider", async () => {
|
||||
vi.useFakeTimers();
|
||||
const search = useDataGridSearch({
|
||||
columns: ["name"],
|
||||
rows: [["Alice"]],
|
||||
// No getCellRawSearchText: only the lowercase cache exists, so an
|
||||
// uppercase query must still match instead of never finding the cell.
|
||||
getCellSearchText: (row, col) => row[col].toLowerCase(),
|
||||
caseSensitive: true,
|
||||
literalQuery: false,
|
||||
includeColumnMatches: false,
|
||||
});
|
||||
search.searchText.value = "ALICE";
|
||||
await flushSearchDebounce();
|
||||
expect(search.matchCount.value).toBe(1);
|
||||
});
|
||||
|
||||
it("debounces matching across columns and cells", async () => {
|
||||
vi.useFakeTimers();
|
||||
// getCellSearchText 契约:返回小写文本(调用方负责缓存小写副本)
|
||||
|
||||
Reference in new issue
Block a user