feat(grid): paste SQL INSERT statements into blank new rows

This commit is contained in:
yangzidemuou authored and GitHub committed 2026-09-30 15:28:07 +08:00
1 parent b32ef45da1
commit dc02e3bccf
18 files changed
+530 -9

No files matched your search

@@ -713,6 +713,36 @@ describe("useDataGridEditor appendPastedRowsToNewRow", () => {
expect(editor.hasPendingChanges.value).toBe(true);
});
it("aligns pasted INSERT values by column name regardless of visible order", () => {
const editor = createEditor(["first", "hidden", "last"]);
// Visible columns are [2, 0]: "last" first, "first" second. The INSERT
// column names must map values to the real grid columns, not positions.
const result = editor.appendPastedRowsToNewRow(-1, [["Lovelace", "Ada"]], [2, 0], ["last", "first"]);
expect(result).toEqual({ ok: true, rowCount: 1 });
expect(editor.newRows.value).toEqual([["Ada", null, "Lovelace"]]);
});
it("matches INSERT column names case-insensitively and ignores unknown names", () => {
const editor = createEditor(["first", "hidden", "last"]);
const result = editor.appendPastedRowsToNewRow(-1, [["Ada", "extra", "Lovelace"]], [0, 1, 2], ["FIRST", "not_here", "Last"]);
expect(result).toEqual({ ok: true, rowCount: 1 });
expect(editor.newRows.value).toEqual([["Ada", null, "Lovelace"]]);
});
it("rejects an INSERT paste when no column names match", () => {
const editor = createEditor(["first", "hidden", "last"]);
const result = editor.appendPastedRowsToNewRow(-1, [["x"]], [0, 1, 2], ["nope"]);
expect(result).toEqual({ ok: false, reason: "no-matching-columns" });
// The pre-seeded blank new row stays untouched.
expect(editor.newRows.value).toEqual([[null, null, null]]);
});
it("clears generated key columns instead of pasting the copied value", () => {
const editor = createEditor(undefined, true, undefined, undefined, [], undefined, [
{ name: "first", data_type: "integer", extra: "autoincrement" },
@@ -737,6 +737,27 @@ describe("useDataGridExport prepared row statements", () => {
]);
});
it("copies a selected row as whole-row TSV across all visible columns on smart copy", async () => {
// Row selection only (no cell matrix): the Cmd+C smart path must copy the
// full row (#10573), not just a single cell.
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: "1\tAda", mimeType: "text/tab-separated-values", fileExtension: "tsv", rowCount: 1, columnCount: 2 });
const state = createExportState(editableTable, ["id", "name"], undefined, undefined, undefined, [[1, "Ada"]], [1]);
await expect(state.copyWithPreference("smart")).resolves.toBe(true);
expect(extractDataGridSelection).toHaveBeenCalledWith(
expect.objectContaining({
extractor: "tsv",
selectionKind: "rows",
rows: [[1, "Ada"]],
selectedColumnIndexes: [0, 1],
}),
);
expect(copyToClipboard).toHaveBeenCalledWith("1\tAda");
// The internal clipboard matrix keeps the row paste-able back into a new row.
expect(parseDataGridClipboard("1\tAda")).toEqual([["1", "Ada"]]);
});
it("uses TSV (quotes only for separator/newline) for a multi-cell smart copy without relying on clipboard metadata", async () => {
const rows = [
[1, '{"msg":"success"}'],
@@ -51,7 +51,7 @@ interface ConditionalUpdateExecution {
outcome: ConditionalUpdateOutcome;
}
export type DataGridAppendPastedRowsResult = { ok: true; rowCount: number } | { ok: false; reason: "not-editable" | "invalid-target" | "target-not-empty" | "empty-paste" | "readonly-column" };
export type DataGridAppendPastedRowsResult = { ok: true; rowCount: number } | { ok: false; reason: "not-editable" | "invalid-target" | "target-not-empty" | "empty-paste" | "readonly-column" | "no-matching-columns" };
type CommitEditResult =
| {
@@ -1240,7 +1240,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
return row.every((value) => value === null || (typeof value === "string" && value.trim() === ""));
}
function appendPastedRowsToNewRow(targetRowId: number, pastedRows: readonly (readonly (string | null)[])[], columnIndexes: readonly number[]): DataGridAppendPastedRowsResult {
function appendPastedRowsToNewRow(targetRowId: number, pastedRows: readonly (readonly (string | null)[])[], columnIndexes: readonly number[], columnNames?: readonly string[] | null): DataGridAppendPastedRowsResult {
if (!editable.value) return { ok: false, reason: "not-editable" };
if (pastedRows.every((row) => row.every((value) => value === ""))) {
return { ok: false, reason: "empty-paste" };
@@ -1260,8 +1260,26 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const pastedColumnCount = Math.max(...pastedRows.map((row) => row.length));
if (pastedColumnCount <= 0) return { ok: false, reason: "empty-paste" };
const targetColumns = columnIndexes.slice(0, pastedColumnCount);
if (targetColumns.some((columnIndex) => !canEditColumn(columnIndex))) return { ok: false, reason: "readonly-column" };
// With explicit column names (SQL INSERT), align pasted values by column
// name instead of visible position. Unknown names are ignored together
// with their values; if none of the pasted names matches this result's
// columns, reject the paste.
const valueTargets: Array<{ columnIndex: number; valueIndex: number }> = [];
if (columnNames?.length) {
const nameToColumnIndex = new Map<string, number>();
columnIndexes.forEach((columnIndex) => {
const name = sourceColumns.value?.[columnIndex] ?? result.value.columns[columnIndex];
if (name !== undefined && !nameToColumnIndex.has(name.toLowerCase())) nameToColumnIndex.set(name.toLowerCase(), columnIndex);
});
columnNames.forEach((name, valueIndex) => {
const columnIndex = nameToColumnIndex.get(name.toLowerCase());
if (columnIndex !== undefined) valueTargets.push({ columnIndex, valueIndex });
});
if (valueTargets.length === 0) return { ok: false, reason: "no-matching-columns" };
} else {
columnIndexes.slice(0, pastedColumnCount).forEach((columnIndex, valueIndex) => valueTargets.push({ columnIndex, valueIndex }));
}
if (valueTargets.some(({ columnIndex }) => !canEditColumn(columnIndex))) return { ok: false, reason: "readonly-column" };
const nextRows = newRows.value.map((row) => [...row]);
const nextMeta = cloneNewRowMeta(newRowMeta.value);
@@ -1275,14 +1293,14 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
const shouldClearColumn = clonedColumnClearPredicate();
const mappedRows = pastedRows.map((pastedRow, rowIndex) => {
const nextRow = rowIndex < reusableNewRowCount ? nextRows[targetNewIndex! + rowIndex]! : emptyDraftRow();
for (let columnOffset = 0; columnOffset < Math.min(pastedRow.length, targetColumns.length); columnOffset++) {
const columnIndex = targetColumns[columnOffset]!;
for (const { columnIndex, valueIndex } of valueTargets) {
if (valueIndex >= pastedRow.length) continue;
// Pasting a copied row into a new row must not reuse its generated key.
if (shouldClearColumn(columnIndex)) {
nextRow[columnIndex] = null;
continue;
}
const value = pastedRow[columnOffset];
const value = pastedRow[valueIndex];
nextRow[columnIndex] = value === null ? null : coerceCellValue(value, nextRow[columnIndex], columnIndex);
}
return nextRow;