feat(grid): 新增原始数据复制模式并设为默认

* ✨ feat(grid): 新增原始数据复制模式并设为默认

- 原样输出单元格内容,不对引号、制表符和换行进行 DSV 转义
- 支持原始数据复制后的网格粘贴还原
- 补充多语言复制选项文案及前后端提取器实现与测试

* ✨ feat(grid): 优化新增智能复制选项

- 单个单元格复制保留原始单元格文本,多单元格自动使用转义 TSV
- 默认复制设置由格式选择升级为复制行为选择,并支持预览
- 限制原始文本格式仅用于单单元格复制
- 更新多语言文案及相关测试
This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-05 23:58:01 +08:00
1 parent e749bdd42c
commit 930aa48c20
24 files changed
+333 -91

No files matched your search

@@ -370,6 +370,87 @@ describe("useDataGridExport prepared row statements", () => {
]);
});
it("uses escaped TSV for a multi-cell smart copy without relying on clipboard metadata", async () => {
const rows = [
[1, '{"msg":"success"}'],
[2, "inside\ttab\nnext line"],
];
const matrix: CellSelectionMatrix = {
rowIndexes: [0, 1],
columnIndexes: [0, 1],
columns: ["id", "name"],
rows,
};
const text = '1\t"{""msg"":""success""}"\n2\t"inside\ttab\nnext line"';
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text, mimeType: "text/tab-separated-values", fileExtension: "tsv", rowCount: 2, columnCount: 2 });
const state = createExportState(editableTable, ["id", "name"], matrix, undefined, undefined, rows);
await expect(state.copyWithPreference("smart")).resolves.toBe(true);
expect(extractDataGridSelection).toHaveBeenCalledWith(expect.objectContaining({ extractor: "tsv" }));
expect(copyToClipboard).toHaveBeenCalledWith(text);
});
it("preserves JSON-column text in a single-cell smart copy", async () => {
const tableMeta: DataGridTableMeta = {
tableName: "events",
primaryKeys: ["id"],
columns: [
{ name: "id", data_type: "int", is_nullable: false, is_primary_key: true },
{ name: "payload", data_type: "json", is_nullable: true },
],
};
const payload = '{ "id": 9007199254740993, "items": [ 1, 2 ] }';
const matrix: CellSelectionMatrix = {
rowIndexes: [0],
columnIndexes: [1],
columns: ["payload"],
rows: [[payload]],
};
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: payload, mimeType: "text/plain", fileExtension: "txt", rowCount: 1, columnCount: 1 });
const state = createExportState(tableMeta, ["id", "payload"], matrix, [7, payload]);
await expect(state.copyWithPreference("smart")).resolves.toBe(true);
expect(extractDataGridSelection).toHaveBeenCalledWith(expect.objectContaining({ extractor: "raw", rows: [[payload]] }));
expect(copyToClipboard).toHaveBeenCalledWith(payload);
});
it("does not allow raw output for a multi-cell selection", async () => {
const matrix: CellSelectionMatrix = {
rowIndexes: [0, 1],
columnIndexes: [0],
columns: ["id"],
rows: [[1], [2]],
};
const state = createExportState(editableTable, ["id", "name"], matrix, undefined, undefined, [
[1, "Ada"],
[2, "Linus"],
]);
expect(state.canCopyWithExtractor("raw")).toBe(false);
await expect(state.copyWithExtractor("raw")).resolves.toBe(false);
expect(extractDataGridSelection).not.toHaveBeenCalled();
});
it("uses the same TSV extractor for smart copy previews of multiple cells", async () => {
const matrix: CellSelectionMatrix = {
rowIndexes: [0, 1],
columnIndexes: [0],
columns: ["id"],
rows: [[1], [2]],
};
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: "1\n2", mimeType: "text/tab-separated-values", fileExtension: "tsv", rowCount: 2, columnCount: 1 });
const state = createExportState(editableTable, ["id", "name"], matrix, undefined, undefined, [
[1, "Ada"],
[2, "Linus"],
]);
await expect(state.previewWithPreference("smart", DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS)).resolves.toEqual(expect.objectContaining({ text: "1\n2" }));
expect(extractDataGridSelection).toHaveBeenCalledWith(expect.objectContaining({ extractor: "tsv" }));
});
it("rejects SQL UPDATE when the selection contains no writable non-key column", () => {
const matrix: CellSelectionMatrix = {
rowIndexes: [0],
@@ -37,7 +37,9 @@ vi.mock("@/composables/useToast", () => ({
vi.mock("@/composables/useDataGridExtractor", () => ({
useDataGridExtractor: () => ({
copyWithExtractor: vi.fn(),
copyWithPreference: vi.fn(),
previewWithExtractor: vi.fn(),
previewWithPreference: vi.fn(),
canCopyWithExtractor: vi.fn(),
}),
}));
@@ -558,7 +558,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
});
}
const { copyWithExtractor, previewWithExtractor, canCopyWithExtractor } = useDataGridExtractor({
const { copyWithExtractor, copyWithPreference, previewWithExtractor, previewWithPreference, canCopyWithExtractor } = useDataGridExtractor({
columns,
displayItems,
allColumns,
@@ -1291,7 +1291,9 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
canCopyRow,
copyAll,
copyWithExtractor,
copyWithPreference,
previewWithExtractor,
previewWithPreference,
canCopyWithExtractor,
exportCsv,
exportCurrentPageCsv,
@@ -10,7 +10,9 @@ import {
DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS,
extractorUnavailableForDatabase,
normalizeDataGridExtractorOptions,
resolveDataGridCopyPreference,
type DataGridCopyExtractorId,
type DataGridCopyPreference,
type DataGridExtractPreview,
type DataGridExtractRequest,
type DataGridExtractorOptions,
@@ -133,9 +135,9 @@ export function useDataGridExtractor(options: UseDataGridExtractorOptions) {
sourceName: sourceNames?.[sourceIndex],
sourceIndex: compactIndex,
}));
const selectedColumnIndexes = selectedSourceIndexes.map((sourceIndex) => compactIndexBySource.get(sourceIndex)).filter((index): index is number => index !== undefined);
const rows = sourceRows.map((row) => requiredSourceIndexes.map((sourceIndex) => normalizeCellValue(row[sourceIndex], columnTypesBySource.get(sourceIndex))));
const descriptor = DATA_GRID_COPY_EXTRACTOR_DESCRIPTORS[extractor];
const selectedColumnIndexes = selectedSourceIndexes.map((sourceIndex) => compactIndexBySource.get(sourceIndex)).filter((index): index is number => index !== undefined);
const rows = sourceRows.map((row) => requiredSourceIndexes.map((sourceIndex) => (descriptor.category === "json" || descriptor.category === "sql" ? normalizeCellValue(row[sourceIndex], columnTypesBySource.get(sourceIndex)) : row[sourceIndex])));
const tableMeta =
descriptor.category === "sql"
? compactTableMeta(
@@ -190,9 +192,19 @@ export function useDataGridExtractor(options: UseDataGridExtractorOptions) {
}
return canBuildSqlUpdateRequest();
}
if (extractor === "raw") {
const request = buildRequest(extractor, extractorOptions);
return request !== null && request.rows.length * request.selectedColumnIndexes.length === 1;
}
return selectionData() !== null;
}
function resolveCopyPreference(preference: DataGridCopyPreference, extractorOptions: DataGridExtractorOptions): DataGridCopyExtractorId | null {
const request = buildRequest("raw", extractorOptions);
if (!request) return null;
return resolveDataGridCopyPreference(preference, request.rows.length * request.selectedColumnIndexes.length);
}
async function resolveMongoExtractorResult(extractor: DataGridCopyExtractorId, request: DataGridExtractRequest, rowLimit?: number) {
if (options.databaseType.value !== "mongodb") return undefined;
if (extractor !== "sql-inserts" && extractor !== "sql-updates") return undefined;
@@ -214,11 +226,11 @@ export function useDataGridExtractor(options: UseDataGridExtractorOptions) {
if (!result.text) return false;
// Derive the grid paste-back payload from the effective request schema so
// hidden support columns, row headers, NULLs, tabs, and newlines keep the
// same shape and values as the rendered TSV.
const isTsv = extractor === "tsv" || extractor === "tsv-with-headers";
const gridCopy = isTsv
// same shape and values as the rendered raw text or TSV.
const isGridTabularCopy = extractor === "raw" || extractor === "tsv" || extractor === "tsv-with-headers";
const gridCopy = isGridTabularCopy
? (() => {
const includeRowHeader = request.options.dsv.includeRowHeader;
const includeRowHeader = extractor !== "raw" && request.options.dsv.includeRowHeader;
const rows = request.rows.map((row, rowIndex) => {
const selectedRow = request.selectedColumnIndexes.map((index) => row[index]);
return includeRowHeader ? [rowIndex + 1, ...selectedRow] : selectedRow;
@@ -250,6 +262,22 @@ export function useDataGridExtractor(options: UseDataGridExtractorOptions) {
return { ...result, sourceRowCount, truncated: sourceRowCount > result.rowCount };
}
async function copyWithPreference(preference: DataGridCopyPreference, extractorOptions: DataGridExtractorOptions = options.extractorOptions?.value ?? DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS): Promise<boolean> {
if (hasUnsupportedDiscreteSelection.value) {
toast(t("grid.copyExtractorUnsupportedSelection"), 5000);
return false;
}
const extractor = resolveCopyPreference(preference, extractorOptions);
return extractor ? copyWithExtractor(extractor, extractorOptions) : false;
}
async function previewWithPreference(preference: DataGridCopyPreference, extractorOptions: DataGridExtractorOptions): Promise<DataGridExtractPreview> {
if (hasUnsupportedDiscreteSelection.value) throw new Error(t("grid.copyExtractorUnsupportedSelection"));
const extractor = resolveCopyPreference(preference, extractorOptions);
if (!extractor) throw new Error(t("grid.copyExtractorEmptySelection"));
return previewWithExtractor(extractor, extractorOptions);
}
function showWarnings(warnings: Array<{ code: DataGridExtractWarningCode; message: string }> | undefined, omittedColumns: string[] | undefined) {
if (!warnings?.length) return;
for (const warning of warnings) {
@@ -258,7 +286,7 @@ export function useDataGridExtractor(options: UseDataGridExtractorOptions) {
}
}
return { copyWithExtractor, previewWithExtractor, canCopyWithExtractor };
return { copyWithExtractor, copyWithPreference, previewWithExtractor, previewWithPreference, canCopyWithExtractor };
}
function compactTableMeta(tableMeta: DataGridTableMeta | undefined, requiredColumns: string[]): DataGridTableMeta | undefined {