fix(grid): ignore unchanged cell blur commits

This commit is contained in:
jearhe authored and GitHub committed 2026-08-24 20:28:24 +08:00
1 parent ff31f44b42
commit ba60ffb954
2 files changed
+107 -6

No files matched your search

@@ -5,7 +5,7 @@ import { coerceDataGridCellValue, dataGridCellEditorText } from "@/lib/dataGrid/
import { focusDataGridEditorWithoutScrolling, preserveDataGridScrollPosition } from "@/lib/dataGrid/dataGridEditorFocus"; import { focusDataGridEditorWithoutScrolling, preserveDataGridScrollPosition } from "@/lib/dataGrid/dataGridEditorFocus";
import { normalizeDataGridSaveError } from "@/lib/dataGrid/dataGridSql"; import { normalizeDataGridSaveError } from "@/lib/dataGrid/dataGridSql";
import { rowStatusFilterAfterAddingRow, type RowStatusFilter } from "@/lib/dataGrid/gridRowStatus"; import { rowStatusFilterAfterAddingRow, type RowStatusFilter } from "@/lib/dataGrid/gridRowStatus";
import { type GridNewRowMeta, type GridNewRowPlacement } from "@/lib/dataGrid/gridNewRowPlacement"; import type { GridNewRowMeta, GridNewRowPlacement } from "@/lib/dataGrid/gridNewRowPlacement";
import { supportsDataGridTransaction } from "@/lib/table/tableEditing"; import { supportsDataGridTransaction } from "@/lib/table/tableEditing";
import { useConnectionStore } from "@/stores/connectionStore"; import { useConnectionStore } from "@/stores/connectionStore";
import { useHistoryStore } from "@/stores/historyStore"; import { useHistoryStore } from "@/stores/historyStore";
@@ -119,9 +119,7 @@ export interface UseDataGridEditorOptions {
onResultPayloadMutated?: () => void; onResultPayloadMutated?: () => void;
onCellValueChanged?: (rowId: number, columnIndex: number) => void; onCellValueChanged?: (rowId: number, columnIndex: number) => void;
prepareFullReload?: () => void; prepareFullReload?: () => void;
emit: { emit: (event: "reload", sql?: string, searchText?: string, whereInput?: string, orderBy?: string, limit?: number, offset?: number) => void;
(event: "reload", sql?: string, searchText?: string, whereInput?: string, orderBy?: string, limit?: number, offset?: number): void;
};
} }
interface PendingChangesSnapshot { interface PendingChangesSnapshot {
@@ -601,6 +599,19 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
}) as CellValue; }) as CellValue;
} }
function coerceCommittedCellValue(value: string, currentValue: CellValue | undefined, oldValue: CellValue | undefined, columnIndex: number): CellValue {
const editorText = dataGridCellEditorText({
value: currentValue,
databaseType: resolvedDatabaseType.value,
columnInfo: tableColumnForGridColumn(columnIndex),
});
// Keep the original CellValue when the editor text was not changed. This
// avoids turning a displayed value such as number 1 into string "1" when
// result and table metadata use different representations.
if (value === editorText) return currentValue ?? null;
return coerceCellValue(value, oldValue, columnIndex);
}
let isBatching = false; let isBatching = false;
let batchUndoSnapshotPushed = false; let batchUndoSnapshotPushed = false;
let batchMutated = false; let batchMutated = false;
@@ -833,8 +844,14 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
} }
const oldVal = result.value.rows[item.sourceIndex]?.[col]; const oldVal = result.value.rows[item.sourceIndex]?.[col];
const newVal = options.explicitValue !== undefined ? options.explicitValue : coerceCellValue(editValue.value, oldVal, col); const currentVal = item.data[col] ?? null;
const changed = newVal !== item.data[col]; const newVal = options.explicitValue !== undefined ? options.explicitValue : coerceCommittedCellValue(editValue.value, currentVal, oldVal, col);
const changed = newVal !== currentVal;
if (!changed) {
editingCell.value = null;
isCommitting = false;
return { changed: false, rowKind: "existing" };
}
if (newVal !== oldVal) { if (newVal !== oldVal) {
if (changed) pushUndoSnapshot(); if (changed) pushUndoSnapshot();
if (!dirtyRows.value.has(item.sourceIndex)) dirtyRows.value.set(item.sourceIndex, new Map()); if (!dirtyRows.value.has(item.sourceIndex)) dirtyRows.value.set(item.sourceIndex, new Map());
+84
View File
@@ -113,6 +113,7 @@ function createQuickEntryEditor(options: {
filterRowsInGetRowItem?: boolean; filterRowsInGetRowItem?: boolean;
supportsInsert?: boolean; supportsInsert?: boolean;
save?: (changes: { dirtyRows: Map<number, Map<number, CellValue>>; newRows: CellValue[][]; newRowMeta: Array<{ sourceIndex?: number; editedColumns?: number[] }> }) => Promise<void>; save?: (changes: { dirtyRows: Map<number, Map<number, CellValue>>; newRows: CellValue[][]; newRowMeta: Array<{ sourceIndex?: number; editedColumns?: number[] }> }) => Promise<void>;
onCellValueChanged?: (rowId: number, columnIndex: number) => void;
}) { }) {
const result = computed(() => ({ const result = computed(() => ({
columns: ["id", "name"], columns: ["id", "name"],
@@ -143,6 +144,7 @@ function createQuickEntryEditor(options: {
currentWhereInput: computed(() => undefined), currentWhereInput: computed(() => undefined),
rowStatusFilter, rowStatusFilter,
dataGridQuickEntryEnabled: computed(() => options.quickEntryEnabled), dataGridQuickEntryEnabled: computed(() => options.quickEntryEnabled),
onCellValueChanged: options.onCellValueChanged,
pageSize: ref(50), pageSize: ref(50),
currentPage: ref(1), currentPage: ref(1),
cacheKey: options.cacheKey ? computed(() => options.cacheKey) : undefined, cacheKey: options.cacheKey ? computed(() => options.cacheKey) : undefined,
@@ -1663,6 +1665,88 @@ test("quick entry off keeps blur edits pending without saving", async () => {
assert.equal(editor.dirtyRows.value.get(0)?.get(1), "Ada Lovelace"); assert.equal(editor.dirtyRows.value.get(0)?.get(1), "Ada Lovelace");
}); });
test("unchanged cell blur commits do not create pending changes", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
let callbackCount = 0;
const editor = createQuickEntryEditor({
quickEntryEnabled: false,
onCellValueChanged: () => {
callbackCount += 1;
},
});
const version = editor.pendingChangesVersion.value;
const transactionActive = editor.transactionActive.value;
editor.startEdit(0, 1);
await editor.commitEditFromBlur();
assert.equal(editor.dirtyRows.value.size, 0);
assert.equal(editor.hasPendingChanges.value, false);
assert.equal(editor.transactionActive.value, transactionActive);
assert.equal(editor.pendingChangesVersion.value, version);
assert.equal(callbackCount, 0);
assert.equal(editor.rowDataWithChanges([1, "Ada"], 0)[1], "Ada");
});
test("restoring the original cell value before blur is a no-op", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const editor = createQuickEntryEditor({ quickEntryEnabled: false });
editor.startEdit(0, 1);
editor.editValue.value = "Bob";
editor.editValue.value = "Ada";
await editor.commitEditFromBlur();
assert.equal(editor.dirtyRows.value.size, 0);
assert.equal(editor.hasPendingChanges.value, false);
assert.equal(editor.canUndoPendingChange.value, false);
assert.equal(editor.pendingChangesVersion.value, 0);
assert.equal(editor.rowDataWithChanges([1, "Ada"], 0)[1], "Ada");
});
test("unchanged cell blur commits preserve other dirty cells", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const editor = createQuickEntryEditor({ quickEntryEnabled: false });
editor.applyCellValue(0, 0, "2");
editor.startEdit(0, 1);
await editor.commitEditFromBlur();
assert.deepEqual([...(editor.dirtyRows.value.get(0)?.entries() ?? [])], [[0, 2]]);
assert.equal(editor.hasPendingChanges.value, true);
assert.equal(editor.rowDataWithChanges([1, "Ada"], 0)[1], "Ada");
});
test("changed cell blur commits remain pending", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const editor = createQuickEntryEditor({ quickEntryEnabled: false });
editor.startEdit(0, 1);
editor.editValue.value = "Bob";
await editor.commitEditFromBlur();
assert.equal(editor.dirtyRows.value.get(0)?.get(1), "Bob");
assert.equal(editor.hasPendingChanges.value, true);
assert.equal(editor.rowDataWithChanges([1, "Ada"], 0)[1], "Bob");
});
test("unchanged numeric cell blur commits keep the numeric baseline", async () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const editor = createPeopleGridEditor();
editor.startEdit(0, 0);
await editor.commitEditFromBlur();
assert.equal(editor.dirtyRows.value.size, 0);
assert.equal(editor.hasPendingChanges.value, false);
assert.equal(editor.rowDataWithChanges([1, "Ada"], 0)[0], 1);
});
test("explicit enum commits distinguish NULL, empty string, and the literal NULL", async () => { test("explicit enum commits distinguish NULL, empty string, and the literal NULL", async () => {
setActivePinia(createPinia()); setActivePinia(createPinia());
installBrowserTestGlobals(); installBrowserTestGlobals();