perf(grid): optimize bulk edit performance

This commit is contained in:
GIWTO authored and GitHub committed 2026-07-25 20:06:25 +08:00
1 parent e6da0a6298
commit 1e21a5ad52
3 files changed
+151 -39

No files matched your search

+41 -24
View File
@@ -2769,6 +2769,8 @@ const {
isSaving, isSaving,
saveError, saveError,
useTransaction, useTransaction,
beginBatch,
commitBatch,
exitTransaction, exitTransaction,
startEdit, startEdit,
commitEdit, commitEdit,
@@ -5474,28 +5476,33 @@ function fillSelectionWithValue(value: string | null): boolean {
const range = selectedRange.value; const range = selectedRange.value;
let applied = false; let applied = false;
const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow(); const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow();
if (range) { beginBatch();
for (let rowIndex = range.startRow; rowIndex <= range.endRow; rowIndex++) { try {
if (range) {
for (let rowIndex = range.startRow; rowIndex <= range.endRow; rowIndex++) {
const item = displayItemAt(rowIndex);
if (!item) continue;
for (let visibleCol = range.startCol; visibleCol <= range.endCol; visibleCol++) {
applied = applyVisibleSelectedCellValue(item, visibleCol, value, allowDraftSelectionValue) || applied;
}
}
return applied;
}
if (!hasColumnSelection.value) return false;
const visibleColumnIndexes = selectedVisibleColumnIndexes();
if (!visibleColumnIndexes.length) return false;
for (let rowIndex = 0; rowIndex < displayRowCount.value; rowIndex++) {
const item = displayItemAt(rowIndex); const item = displayItemAt(rowIndex);
if (!item) continue; if (!item) continue;
for (let visibleCol = range.startCol; visibleCol <= range.endCol; visibleCol++) { for (const visibleCol of visibleColumnIndexes) {
applied = applyVisibleSelectedCellValue(item, visibleCol, value, allowDraftSelectionValue) || applied; applied = applyVisibleSelectedCellValue(item, visibleCol, value) || applied;
} }
} }
return applied; return applied;
} finally {
commitBatch();
} }
if (!hasColumnSelection.value) return false;
const visibleColumnIndexes = selectedVisibleColumnIndexes();
if (!visibleColumnIndexes.length) return false;
for (let rowIndex = 0; rowIndex < displayRowCount.value; rowIndex++) {
const item = displayItemAt(rowIndex);
if (!item) continue;
for (const visibleCol of visibleColumnIndexes) {
applied = applyVisibleSelectedCellValue(item, visibleCol, value) || applied;
}
}
return applied;
} }
function selectionHasEditableCells(): boolean { function selectionHasEditableCells(): boolean {
@@ -5578,9 +5585,14 @@ function applyGeneratedSelectionValue(kind: CellValueGenerationKind, startValue
const values = generateCellValues(kind, cells.length, { startValue }); const values = generateCellValues(kind, cells.length, { startValue });
const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow(); const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow();
let applied = false; let applied = false;
cells.forEach((cell, index) => { beginBatch();
applied = applyVisibleSelectedCellValue(cell.item, cell.visibleCol, values[index] ?? null, allowDraftSelectionValue, { preserveEmptyString: kind === "empty" }) || applied; try {
}); cells.forEach((cell, index) => {
applied = applyVisibleSelectedCellValue(cell.item, cell.visibleCol, values[index] ?? null, allowDraftSelectionValue, { preserveEmptyString: kind === "empty" }) || applied;
});
} finally {
commitBatch();
}
if (applied) toast(t("grid.generatedValuesApplied", { count: cells.length })); if (applied) toast(t("grid.generatedValuesApplied", { count: cells.length }));
return applied; return applied;
} }
@@ -5634,12 +5646,17 @@ function cutSelection() {
copySelectionTsv(); copySelectionTsv();
const range = selectedRange.value; const range = selectedRange.value;
const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow(); const allowDraftSelectionValue = selectedRangeTargetsOnlyDraftRow();
for (let rowIndex = range.startRow; rowIndex <= range.endRow; rowIndex++) { beginBatch();
const item = displayItemAt(rowIndex); try {
if (!item) continue; for (let rowIndex = range.startRow; rowIndex <= range.endRow; rowIndex++) {
for (let visibleCol = range.startCol; visibleCol <= range.endCol; visibleCol++) { const item = displayItemAt(rowIndex);
applyVisibleSelectedCellValue(item, visibleCol, null, allowDraftSelectionValue); if (!item) continue;
for (let visibleCol = range.startCol; visibleCol <= range.endCol; visibleCol++) {
applyVisibleSelectedCellValue(item, visibleCol, null, allowDraftSelectionValue);
}
} }
} finally {
commitBatch();
} }
} }
@@ -1,4 +1,4 @@
import { ref, computed, nextTick, watch, getCurrentInstance, onActivated, onBeforeUnmount, onDeactivated, onMounted, toRaw, type ComputedRef, type Ref } from "vue"; import { ref, shallowRef, computed, nextTick, watch, getCurrentInstance, onActivated, onBeforeUnmount, onDeactivated, onMounted, toRaw, type ComputedRef, type Ref } from "vue";
import * as api from "@/lib/backend/api"; import * as api from "@/lib/backend/api";
import type { CellValue } from "@/lib/dataGrid/cellValue"; import type { CellValue } from "@/lib/dataGrid/cellValue";
import { coerceDataGridCellValue, dataGridCellEditorText } from "@/lib/dataGrid/dataGridCellCoercion"; import { coerceDataGridCellValue, dataGridCellEditorText } from "@/lib/dataGrid/dataGridCellCoercion";
@@ -206,7 +206,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const editingCell = ref<{ rowId: number; col: number } | null>(null); const editingCell = ref<{ rowId: number; col: number } | null>(null);
const editValue = ref(""); const editValue = ref("");
const scrollerRef = ref<GridScrollerRef | null>(null); const scrollerRef = ref<GridScrollerRef | null>(null);
const dirtyRows = ref<Map<number, Map<number, CellValue>>>(new Map()); const dirtyRows = shallowRef<Map<number, Map<number, CellValue>>>(new Map());
const newRows = ref<CellValue[][]>([]); const newRows = ref<CellValue[][]>([]);
const deletedRows = ref<Set<number>>(new Set()); const deletedRows = ref<Set<number>>(new Set());
const quickEntryDraftRow = ref<CellValue[]>([]); const quickEntryDraftRow = ref<CellValue[]>([]);
@@ -500,10 +500,45 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
}) as CellValue; }) as CellValue;
} }
let isBatching = false;
let batchUndoSnapshotPushed = false;
let batchMutated = false;
let batchColumnInfoCache: Map<number, ColumnInfo | undefined> | null = null;
function beginBatch() {
if (isBatching) return;
isBatching = true;
batchUndoSnapshotPushed = false;
batchMutated = false;
batchColumnInfoCache = new Map();
}
function commitBatch() {
if (!isBatching) return;
isBatching = false;
batchColumnInfoCache = null;
if (!batchMutated) return;
dirtyRows.value = new Map(dirtyRows.value);
newRows.value = [...newRows.value];
touchPendingChanges();
}
function markBatchMutated() {
if (isBatching) batchMutated = true;
}
function tableColumnForGridColumn(columnIndex: number): ColumnInfo | undefined { function tableColumnForGridColumn(columnIndex: number): ColumnInfo | undefined {
if (isBatching && batchColumnInfoCache) {
if (batchColumnInfoCache.has(columnIndex)) {
return batchColumnInfoCache.get(columnIndex);
}
}
const columnName = sourceColumns.value?.[columnIndex] ?? result.value.columns[columnIndex]; const columnName = sourceColumns.value?.[columnIndex] ?? result.value.columns[columnIndex];
if (!columnName) return undefined; const info = columnName ? tableMeta.value?.columns.find((column) => column.name.toLowerCase() === columnName.toLowerCase()) : undefined;
return tableMeta.value?.columns.find((column) => column.name.toLowerCase() === columnName.toLowerCase()); if (isBatching && batchColumnInfoCache) {
batchColumnInfoCache.set(columnIndex, info);
}
return info;
} }
function canEditColumn(columnIndex: number): boolean { function canEditColumn(columnIndex: number): boolean {
@@ -747,9 +782,19 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const nextDraftRow = [...quickEntryDraftRow.value]; const nextDraftRow = [...quickEntryDraftRow.value];
nextDraftRow[col] = value === null ? null : coerceCellValue(value, oldVal, col, options); nextDraftRow[col] = value === null ? null : coerceCellValue(value, oldVal, col, options);
if (nextDraftRow[col] === oldVal) return; if (nextDraftRow[col] === oldVal) return;
pushUndoSnapshot(); if (isBatching) {
if (!batchUndoSnapshotPushed) {
pushUndoSnapshot();
batchUndoSnapshotPushed = true;
}
} else {
pushUndoSnapshot();
}
quickEntryDraftRow.value = draftRowHasValue(nextDraftRow) ? nextDraftRow : emptyDraftRow(); quickEntryDraftRow.value = draftRowHasValue(nextDraftRow) ? nextDraftRow : emptyDraftRow();
touchPendingChanges(); markBatchMutated();
if (!isBatching) {
touchPendingChanges();
}
scheduleQuickEntryDraftPromotion(); scheduleQuickEntryDraftPromotion();
return; return;
} }
@@ -761,10 +806,20 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const oldVal = row[col]; const oldVal = row[col];
const newVal = value === null ? null : coerceCellValue(value, oldVal, col, options); const newVal = value === null ? null : coerceCellValue(value, oldVal, col, options);
if (newVal === oldVal) return; if (newVal === oldVal) return;
pushUndoSnapshot(); if (isBatching) {
if (!batchUndoSnapshotPushed) {
pushUndoSnapshot();
batchUndoSnapshotPushed = true;
}
} else {
pushUndoSnapshot();
}
row[col] = newVal; row[col] = newVal;
newRows.value = [...newRows.value]; markBatchMutated();
touchPendingChanges(); if (!isBatching) {
newRows.value = [...newRows.value];
touchPendingChanges();
}
return; return;
} }
@@ -778,19 +833,39 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const newVal = value === null ? null : coerceCellValue(value, oldVal, col, options); const newVal = value === null ? null : coerceCellValue(value, oldVal, col, options);
if (newVal === currentVal) return; if (newVal === currentVal) return;
if (newVal !== oldVal) { if (newVal !== oldVal) {
pushUndoSnapshot(); if (isBatching) {
if (!batchUndoSnapshotPushed) {
pushUndoSnapshot();
batchUndoSnapshotPushed = true;
}
} else {
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());
dirtyRows.value.get(item.sourceIndex)!.set(col, newVal); dirtyRows.value.get(item.sourceIndex)!.set(col, newVal);
markBatchMutated();
if (useTransaction.value && !transactionActive.value) { if (useTransaction.value && !transactionActive.value) {
enterTransaction(); enterTransaction();
} }
} else { } else {
if (hasPendingCellChange) pushUndoSnapshot(); if (hasPendingCellChange) {
if (isBatching) {
if (!batchUndoSnapshotPushed) {
pushUndoSnapshot();
batchUndoSnapshotPushed = true;
}
} else {
pushUndoSnapshot();
}
}
rowChanges?.delete(col); rowChanges?.delete(col);
if (rowChanges?.size === 0) dirtyRows.value.delete(item.sourceIndex); if (rowChanges?.size === 0) dirtyRows.value.delete(item.sourceIndex);
if (hasPendingCellChange) markBatchMutated();
}
if (!isBatching) {
dirtyRows.value = new Map(dirtyRows.value);
touchPendingChanges();
} }
dirtyRows.value = new Map(dirtyRows.value);
touchPendingChanges();
} }
function restoreCellValue(rowId: number, col: number) { function restoreCellValue(rowId: number, col: number) {
@@ -1330,9 +1405,9 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
} }
function discardChanges() { function discardChanges() {
dirtyRows.value.clear(); dirtyRows.value = new Map();
newRows.value = []; newRows.value = [];
deletedRows.value.clear(); deletedRows.value = new Set();
quickEntryDraftRow.value = emptyDraftRow(); quickEntryDraftRow.value = emptyDraftRow();
queuedAutoSaveChanges.clear(); queuedAutoSaveChanges.clear();
editingCell.value = null; editingCell.value = null;
@@ -1477,6 +1552,8 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
isSaving, isSaving,
saveError, saveError,
useTransaction, useTransaction,
beginBatch,
commitBatch,
enterTransaction, enterTransaction,
exitTransaction, exitTransaction,
startEdit, startEdit,
+18
View File
@@ -773,6 +773,24 @@ test("a failed NULL save keeps the pending cell edit", async () => {
assert.equal(editor.hasPendingChanges.value, true); assert.equal(editor.hasPendingChanges.value, true);
}); });
test("committing a no-op edit batch keeps the existing save error", () => {
setActivePinia(createPinia());
installBrowserTestGlobals();
const editor = createPeopleGridEditor();
editor.applyCellValue(0, 1, "Ada Lovelace");
editor.saveError.value = "Previous save failed";
const version = editor.pendingChangesVersion.value;
editor.beginBatch();
editor.applyCellValue(0, 1, "Ada Lovelace");
editor.commitBatch();
assert.equal(editor.saveError.value, "Previous save failed");
assert.equal(editor.pendingChangesVersion.value, version);
assert.equal(editor.dirtyRows.value.get(0)?.get(1), "Ada Lovelace");
});
test("a NOT NULL validation failure keeps the pending NULL cell edit recoverable", async () => { test("a NOT NULL validation failure keeps the pending NULL cell edit recoverable", async () => {
setActivePinia(createPinia()); setActivePinia(createPinia());
installBrowserTestGlobals(); installBrowserTestGlobals();