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

+18 -2
View File
@@ -307,6 +307,7 @@ import { useNavigationTargets } from "@/composables/useNavigationTargets";
import { useDataGridExport, type MongoCopyUpdateTarget } from "@/composables/useDataGridExport";
import { eventTargetAllowsNativeClipboard, isPlainClipboardShortcut, readTextFromClipboard } from "@/lib/common/clipboard";
import { claimDataGridPaste, claimDataGridSelectAll, clearDataGridClipboardCopy, parseDataGridClipboard, planDataGridPaste } from "@/lib/dataGrid/dataGridClipboard";
import { parseInsertStatementPaste } from "@/lib/dataGrid/dataGridInsertPaste";
import { beginDataGridNativeSelectionBlock, finishDataGridNativeSelectionBlock } from "@/lib/dataGrid/dataGridNativeSelection";
import { DATA_GRID_COPY_EXTRACTOR_DESCRIPTORS, DATA_GRID_COPY_EXTRACTOR_IDS, DATA_GRID_DEFAULT_COPY_PREFERENCES, extractorUnavailableForDatabase, type DataGridCopyExtractorId, type DataGridCopyPreference } from "@/lib/dataGrid/dataGridCopyExtractor";
import { columnNamesForCopy } from "@/lib/dataGrid/dataGridColumnNameCopy";
@@ -8481,6 +8482,7 @@ function batchAppendPasteError(reason: string): string {
"target-not-empty": "grid.batchAppendPasteTargetNotEmpty",
"empty-paste": "grid.batchAppendPasteEmpty",
"readonly-column": "grid.batchAppendPasteReadonlyColumn",
"no-matching-columns": "grid.batchAppendPasteNoMatchingColumns",
};
return t(messages[reason] ?? "grid.batchAppendPasteInvalidTarget");
}
@@ -8497,8 +8499,8 @@ function blankSelectionBatchAppendPasteTarget(pastedRows: readonly (readonly (st
return { rowId: item.id, columnIndexes: visibleColumnIndexes.value.slice(range.startCol) };
}
function appendParsedRowsToBlankTarget(targetRowId: number, rows: readonly (readonly (string | null)[])[], columnIndexes: readonly number[]): boolean {
const result = appendPastedRowsToNewRow(targetRowId, rows, columnIndexes);
function appendParsedRowsToBlankTarget(targetRowId: number, rows: readonly (readonly (string | null)[])[], columnIndexes: readonly number[], columnNames?: readonly string[] | null): boolean {
const result = appendPastedRowsToNewRow(targetRowId, rows, columnIndexes, columnNames);
if (!result.ok) {
if (result.reason === "invalid-target" || result.reason === "target-not-empty") {
batchAppendPasteRowId.value = null;
@@ -8512,6 +8514,20 @@ function appendParsedRowsToBlankTarget(targetRowId: number, rows: readonly (read
}
function pasteTextIntoGrid(text: string): boolean {
// SQL INSERT statements are only meaningful as new rows, so they are honored
// on the blank-new-row targets below; any other target pastes the parsed
// values through the regular cell path instead of treating the whole
// statement as literal text.
const insertPaste = parseInsertStatementPaste(text);
if (insertPaste) {
const insertTargetRowId = batchAppendPasteTargetRowId();
if (insertTargetRowId !== null) {
return appendParsedRowsToBlankTarget(insertTargetRowId, insertPaste.rows, visibleColumnIndexes.value, insertPaste.columnNames);
}
const insertCellTarget = blankSelectionBatchAppendPasteTarget(insertPaste.rows);
if (insertCellTarget) return appendParsedRowsToBlankTarget(insertCellTarget.rowId, insertPaste.rows, insertCellTarget.columnIndexes, insertPaste.columnNames);
return pasteRowsIntoSelection(insertPaste.rows);
}
const rows = parseDataGridClipboard(text);
const targetRowId = batchAppendPasteTargetRowId();
if (targetRowId !== null) {
@@ -516,3 +516,44 @@ describe("DataGrid multi-row paste from a blank cell", () => {
expect(updates).toEqual([{ $set: { status: "{plain text" } }, { $set: { status: "{plain text" } }]);
});
});
describe("DataGrid INSERT statement paste into blank new rows", () => {
it("fills a blank new row from a pasted INSERT statement aligned by column names", async () => {
const { host } = mountGrid();
await settle();
await addBlankRow(host);
const blankRows = pendingRows(host);
expect(blankRows).toHaveLength(1);
// Row-number selection marks the blank new row as the append target, so
// the INSERT values are aligned by column names instead of positions.
await selectRowNumber(blankRows[0]!);
// Column names in a scrambled order: c0 is the first visible column but
// receives 42 through name alignment, not position.
await paste(host, "INSERT INTO paste_target (c2, c0, hidden) VALUES ('from-insert', 42, 'h-val')");
const rows = pendingRows(host);
expect(rows).toHaveLength(1);
const cells = visibleCellTexts(rows[0]!);
expect(cells[0]).toBe("42");
expect(cells[1]).toBe("h-val");
expect(cells[2]).toBe("from-insert");
});
it("appends one row per statement when pasting multiple INSERT statements", async () => {
const { host } = mountGrid();
await settle();
await addBlankRow(host);
const blankRows = pendingRows(host);
await selectCell(visibleCells(blankRows[0]!)[0]!);
await paste(host, "INSERT INTO paste_target (c0, c2) VALUES (1, 'a');\nINSERT INTO paste_target (c0, c2) VALUES (2, 'b');");
const rows = pendingRows(host);
expect(rows).toHaveLength(2);
expect(visibleCellTexts(rows[0]!)[0]).toBe("1");
expect(visibleCellTexts(rows[0]!)[2]).toBe("a");
expect(visibleCellTexts(rows[1]!)[0]).toBe("2");
expect(visibleCellTexts(rows[1]!)[2]).toBe("b");
});
});