feat(data-grid): add multi-format copy extractors
This commit is contained in:
1 parent
166027600f
commit
0b7a886cfd
60 files changed
+4615
-1766
No files matched your search
@@ -125,8 +125,15 @@ describe("parseDataGridClipboard", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves embedded tabs and newlines even without null values", () => {
|
||||
const text = '"left\tinside"\t"line 1\nline 2"';
|
||||
rememberDataGridClipboardCopy(text, [["left\tinside", "line 1\nline 2"]]);
|
||||
|
||||
expect(parseDataGridClipboard(text)).toEqual([["left\tinside", "line 1\nline 2"]]);
|
||||
});
|
||||
|
||||
it("restores null positions after copied headers", () => {
|
||||
rememberDataGridClipboardCopy("name\tnote\nAda\tNULL", [["Ada", null]], true);
|
||||
rememberDataGridClipboardCopy("name\tnote\nAda\tNULL", [["Ada", null]], ["name", "note"]);
|
||||
|
||||
expect(parseDataGridClipboard("name\tnote\nAda\tNULL")).toEqual([
|
||||
["name", "note"],
|
||||
|
||||
@@ -64,7 +64,7 @@ describe("dataGridContextMenu", () => {
|
||||
icons: { cellDetails: icon, columnDetails: icon, rowDetails: icon, setNull: icon, bulkEdit: icon, transpose: icon },
|
||||
actions: { cellDetails: action, columnDetails: action, rowDetails: action, setNull: action, bulkEdit: action, transpose: action },
|
||||
copySubmenu: { label: "copy" },
|
||||
selectionSubmenu: { label: "selection" },
|
||||
clearSelectionItem: { label: "clear" },
|
||||
});
|
||||
expect(cellItems.find((item) => item.label === "bulk")?.disabled).toBe(true);
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS, normalizeDataGridExtractorOptions, validateDataGridExtractorOptions } from "@/lib/dataGrid/dataGridCopyExtractor";
|
||||
|
||||
describe("data-grid extractor options", () => {
|
||||
it("normalizes persisted values without sharing the default object", () => {
|
||||
const normalized = normalizeDataGridExtractorOptions(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
|
||||
expect(normalized).toEqual(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
expect(normalized).not.toBe(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
expect(normalized.dsv).not.toBe(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv);
|
||||
});
|
||||
|
||||
it("rejects overlapping effective row and column separators", () => {
|
||||
const options = normalizeDataGridExtractorOptions(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
options.dsv.rowSeparator = ",";
|
||||
|
||||
expect(validateDataGridExtractorOptions("csv", options)).toBe("separators-overlap");
|
||||
});
|
||||
|
||||
it("rejects an empty custom separator before normalization can hide it", () => {
|
||||
const options = normalizeDataGridExtractorOptions(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
options.dsv.columnSeparator = "";
|
||||
|
||||
expect(validateDataGridExtractorOptions("dsv", options)).toBe("column-separator-empty");
|
||||
});
|
||||
|
||||
it("requires exactly one Unicode quote character", () => {
|
||||
const options = normalizeDataGridExtractorOptions(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
options.dsv.quote = "🙂";
|
||||
expect(validateDataGridExtractorOptions("one-row", options)).toBeNull();
|
||||
|
||||
options.dsv.quote = "''";
|
||||
expect(validateDataGridExtractorOptions("one-row", options)).toBe("invalid-quote");
|
||||
});
|
||||
|
||||
it("normalizes and validates separator and null-text limits by Unicode code point", () => {
|
||||
const columnSeparator = "🙂".repeat(5);
|
||||
const nullText = "🧊".repeat(40);
|
||||
const normalized = normalizeDataGridExtractorOptions({
|
||||
...DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS,
|
||||
dsv: { ...DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv, columnSeparator, nullText },
|
||||
});
|
||||
|
||||
expect(normalized.dsv.columnSeparator).toBe(columnSeparator);
|
||||
expect(normalized.dsv.nullText).toBe(nullText);
|
||||
expect(validateDataGridExtractorOptions("dsv", normalized)).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects quote conflicts, control quotes, and oversized values", () => {
|
||||
const options = normalizeDataGridExtractorOptions(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
|
||||
options.dsv.quote = ",";
|
||||
expect(validateDataGridExtractorOptions("csv", options)).toBe("quote-conflicts");
|
||||
|
||||
options.dsv.quote = "\n";
|
||||
expect(validateDataGridExtractorOptions("one-row", options)).toBe("invalid-quote");
|
||||
|
||||
options.dsv.quote = '"';
|
||||
options.dsv.nullText = "x".repeat(65);
|
||||
expect(validateDataGridExtractorOptions("dsv", options)).toBe("null-text-too-long");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { DATA_GRID_DARK_ACTIVE_ROW_BG, DATA_GRID_LIGHT_ACTIVE_ROW_BG, dataGridActiveRowBackground, resolveDataGridPaintTheme } from "@/lib/dataGrid/dataGridPaintTheme";
|
||||
|
||||
describe("data grid paint theme", () => {
|
||||
it("uses a subtle blue active-row surface in both color schemes", () => {
|
||||
expect(dataGridActiveRowBackground(false)).toBe(DATA_GRID_LIGHT_ACTIVE_ROW_BG);
|
||||
expect(dataGridActiveRowBackground(true)).toBe(DATA_GRID_DARK_ACTIVE_ROW_BG);
|
||||
|
||||
const emptyCssVariable = () => "";
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: false }).cellActive).toBe(DATA_GRID_LIGHT_ACTIVE_ROW_BG);
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: false }).rowNumberActive).toBe(DATA_GRID_LIGHT_ACTIVE_ROW_BG);
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true }).cellActive).toBe(DATA_GRID_DARK_ACTIVE_ROW_BG);
|
||||
expect(resolveDataGridPaintTheme({ getVar: emptyCssVariable, isDark: true }).rowNumberActive).toBe(DATA_GRID_DARK_ACTIVE_ROW_BG);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,15 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { dataGridToolbarIntervalOptions, selectDataGridToolbarAutoRefreshInterval, selectDataGridToolbarExportItem, toggleDataGridToolbarAutoRefresh, triggerDataGridToolbarAction, type DataGridToolbarAutoRefreshCapability } from "@/lib/dataGrid/dataGridToolbar";
|
||||
import {
|
||||
dataGridToolbarIntervalOptions,
|
||||
selectDataGridToolbarAutoRefreshInterval,
|
||||
selectDataGridToolbarCopyItem,
|
||||
selectDataGridToolbarExportItem,
|
||||
toggleDataGridToolbarAutoRefresh,
|
||||
triggerDataGridToolbarAction,
|
||||
triggerDataGridToolbarCopy,
|
||||
type DataGridToolbarAutoRefreshCapability,
|
||||
type DataGridToolbarCopyCapability,
|
||||
} from "@/lib/dataGrid/dataGridToolbar";
|
||||
|
||||
function autoRefreshCapability(overrides: Partial<DataGridToolbarAutoRefreshCapability> = {}): DataGridToolbarAutoRefreshCapability {
|
||||
return {
|
||||
@@ -81,4 +91,26 @@ describe("data grid toolbar capabilities", () => {
|
||||
expect(onSelect).toHaveBeenCalledOnce();
|
||||
expect(onSelect).toHaveBeenCalledWith("csv");
|
||||
});
|
||||
|
||||
it("keeps copy format selection available when the current copy action is disabled", async () => {
|
||||
const onCopy = vi.fn();
|
||||
const onSelect = vi.fn();
|
||||
const capability: DataGridToolbarCopyCapability = {
|
||||
label: "SQL Updates",
|
||||
disabled: true,
|
||||
currentValue: "sql-updates",
|
||||
items: [
|
||||
{ value: "tsv", label: "TSV" },
|
||||
{ value: "sql-updates", label: "SQL Updates", disabled: true },
|
||||
],
|
||||
onCopy,
|
||||
onSelect,
|
||||
};
|
||||
|
||||
await expect(triggerDataGridToolbarCopy(capability)).resolves.toBe(false);
|
||||
await expect(selectDataGridToolbarCopyItem(capability, "tsv")).resolves.toBe(true);
|
||||
await expect(selectDataGridToolbarCopyItem(capability, "sql-updates")).resolves.toBe(false);
|
||||
expect(onCopy).not.toHaveBeenCalled();
|
||||
expect(onSelect).toHaveBeenCalledWith("tsv");
|
||||
});
|
||||
});
|
||||
+11
-28
@@ -1,34 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { formatSelectionAsTsv, summarizeSelection } from "@/lib/dataGrid/gridSelection";
|
||||
import { dedupeColumnIndexes, summarizeSelection } from "@/lib/dataGrid/gridSelection";
|
||||
|
||||
describe("gridSelection", () => {
|
||||
it("formats TSV selections without headers by default", () => {
|
||||
expect(
|
||||
formatSelectionAsTsv({
|
||||
columns: ["id", "name"],
|
||||
rows: [
|
||||
[1, "Ada"],
|
||||
[2, "Lin"],
|
||||
],
|
||||
}),
|
||||
).toBe("1\tAda\n2\tLin");
|
||||
});
|
||||
|
||||
it("can include column headers in TSV selections", () => {
|
||||
expect(
|
||||
formatSelectionAsTsv(
|
||||
{
|
||||
columns: ["id", "name"],
|
||||
rows: [
|
||||
[1, "Ada"],
|
||||
[2, "Lin"],
|
||||
],
|
||||
},
|
||||
true,
|
||||
),
|
||||
).toBe("id\tname\n1\tAda\n2\tLin");
|
||||
});
|
||||
|
||||
it("summarizes empty selections", () => {
|
||||
expect(summarizeSelection({ columns: [], rows: [] })).toEqual({
|
||||
cellCount: 0,
|
||||
@@ -71,4 +44,14 @@ describe("gridSelection", () => {
|
||||
sum: 105.5,
|
||||
});
|
||||
});
|
||||
|
||||
it("dedupeColumnIndexes preserves the first visible occurrence instead of sorting", () => {
|
||||
// After drag-reordering, visible order is [2, 0, 1]; extraction must keep it,
|
||||
// not revert to numeric [0, 1, 2].
|
||||
expect(dedupeColumnIndexes([2, 0, 1])).toEqual([2, 0, 1]);
|
||||
});
|
||||
|
||||
it("dedupeColumnIndexes dedups keeping the first occurrence and drops negatives", () => {
|
||||
expect(dedupeColumnIndexes([2, 0, 2, -1, 1, 0])).toEqual([2, 0, 1]);
|
||||
});
|
||||
});
|
||||
@@ -225,6 +225,7 @@ export const buildCreateTableSql = forward("buildCreateTableSql");
|
||||
export const buildSingleColumnAlterSql = forward("buildSingleColumnAlterSql");
|
||||
export const analyzeEditableQueryEditability = forward("analyzeEditableQueryEditability");
|
||||
export const prepareDataGridSave = forward("prepareDataGridSave");
|
||||
export const extractDataGridSelection = forward("extractDataGridSelection");
|
||||
export const buildDataGridCopyUpdateStatements = forward("buildDataGridCopyUpdateStatements");
|
||||
export const buildDataGridCopyInsertStatement = forward("buildDataGridCopyInsertStatement");
|
||||
export const buildDataGridContextFilterCondition = forward("buildDataGridContextFilterCondition");
|
||||
|
||||
@@ -135,6 +135,7 @@ import type {
|
||||
DataGridSaveStatementOptions,
|
||||
HiveTablePropertiesSqlOptions,
|
||||
} from "@/lib/dataGrid/dataGridSql";
|
||||
import type { DataGridExtractRequest, DataGridExtractResult } from "@/lib/dataGrid/dataGridCopyExtractor";
|
||||
import type { BuildTableStructureChangeSqlOptions, BuildSingleColumnAlterSqlOptions, SqliteTableStructureChangePreview, TableStructureChangeSql } from "@/lib/table/tableStructureEditorSql";
|
||||
import type { BuildTableSelectSqlOptions } from "@/lib/table/tableSelectSql";
|
||||
import type { DatabaseSearchSql, DatabaseSearchSqlOptions, SearchResultWhereOptions } from "@/lib/database/databaseSearch";
|
||||
@@ -1001,6 +1002,10 @@ export async function prepareDataGridSave(options: DataGridSaveStatementOptions)
|
||||
return post("/api/query/prepare-data-grid-save", { options });
|
||||
}
|
||||
|
||||
export async function extractDataGridSelection(request: DataGridExtractRequest): Promise<DataGridExtractResult> {
|
||||
return post("/api/query/extract-data-grid-selection", { request });
|
||||
}
|
||||
|
||||
export async function buildDataGridCopyUpdateStatements(options: DataGridCopyUpdateStatementOptions): Promise<string[]> {
|
||||
return post("/api/query/build-data-grid-copy-update-statements", { options });
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ import type {
|
||||
DataGridSaveStatementOptions,
|
||||
HiveTablePropertiesSqlOptions,
|
||||
} from "@/lib/dataGrid/dataGridSql";
|
||||
import type { DataGridExtractRequest, DataGridExtractResult } from "@/lib/dataGrid/dataGridCopyExtractor";
|
||||
import type { DataCompareFromTablesOptions, DataCompareFromTablesPreparation, DataCompareSyncPlan, DataCompareSyncPlanOptions, DataComparePreparation, DataComparePreparationOptions } from "@/lib/dataGrid/dataCompare";
|
||||
import type { SchemaDiffPreparation, SchemaDiffPreparationOptions, TableDiff, FunctionDiff, SequenceDiff, RuleDiff, OwnerDiff } from "@/lib/schema/schemaDiff";
|
||||
import type { BuildTableStructureChangeSqlOptions, BuildSingleColumnAlterSqlOptions, SqliteTableStructureChangePreview, TableStructureChangeSql } from "@/lib/table/tableStructureEditorSql";
|
||||
@@ -1105,6 +1106,10 @@ export async function prepareDataGridSave(options: DataGridSaveStatementOptions)
|
||||
return invoke("prepare_data_grid_save", { options });
|
||||
}
|
||||
|
||||
export async function extractDataGridSelection(request: DataGridExtractRequest): Promise<DataGridExtractResult> {
|
||||
return invoke("extract_data_grid_selection", { request });
|
||||
}
|
||||
|
||||
export async function buildDataGridCopyUpdateStatements(options: DataGridCopyUpdateStatementOptions): Promise<string[]> {
|
||||
return invoke("build_data_grid_copy_update_statements", { options });
|
||||
}
|
||||
|
||||
@@ -87,6 +87,12 @@ interface CanvasRenderState {
|
||||
currentSearchBorder: string;
|
||||
}
|
||||
|
||||
export function resolveCanvasDataGridRowFill(theme: Pick<DataGridPaintTheme, "cellActive" | "cellSelected">, rowBase: string, options: { isActive: boolean; isDeleted: boolean; isSelected: boolean }): string {
|
||||
if (options.isSelected) return theme.cellSelected;
|
||||
if (options.isActive && !options.isDeleted) return theme.cellActive;
|
||||
return rowBase;
|
||||
}
|
||||
|
||||
const canvasRenderStateCache = new WeakMap<HTMLCanvasElement, CanvasRenderState>();
|
||||
|
||||
function setCanvasNumericVariant(ctx: CanvasRenderingContext2D, value: "normal" | "tabular-nums") {
|
||||
@@ -296,14 +302,32 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
|
||||
if (!item) continue;
|
||||
const y = rowIndex * CANVAS_DATA_GRID_ROW_HEIGHT - scrollTop;
|
||||
const rowIsActive = isRowActive(item.displayIndex);
|
||||
const rowSelectionVisual = rowCellsUseSelectionVisual(item.id);
|
||||
|
||||
const rowBase = item.isDeleted ? theme.rowDeleted : item.isNew && !rowIsActive ? theme.rowNew : item.isDraft && !rowIsActive ? theme.rowMuted : item.displayIndex % 2 === 1 && !rowIsActive ? theme.rowMuted : theme.background;
|
||||
const rowFill = resolveCanvasDataGridRowFill(theme, rowBase, {
|
||||
isActive: rowIsActive,
|
||||
isDeleted: item.isDeleted,
|
||||
isSelected: rowSelectionVisual,
|
||||
});
|
||||
const rowBorderY = crispCanvasLine(y + CANVAS_DATA_GRID_ROW_HEIGHT - 1, dpr);
|
||||
ctx.globalAlpha = item.isDeleted ? 0.7 : 1;
|
||||
ctx.fillStyle = rowIsActive && !item.isDeleted ? theme.cellSelectedSingle : rowBase;
|
||||
ctx.fillStyle = rowFill;
|
||||
ctx.fillRect(0, y, width, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
|
||||
const rowNumberFill = item.status === "draft" ? theme.rowNumberDefault : item.status === "new" ? theme.rowNumberNew : item.status === "edited" ? theme.rowNumberEdited : item.status === "deleted" ? theme.rowNumberDeleted : theme.rowNumberDefault;
|
||||
const rowNumberFill = rowSelectionVisual
|
||||
? theme.rowNumberSelected
|
||||
: item.status === "draft"
|
||||
? theme.rowNumberDefault
|
||||
: item.status === "new"
|
||||
? theme.rowNumberNew
|
||||
: item.status === "edited"
|
||||
? theme.rowNumberEdited
|
||||
: item.status === "deleted"
|
||||
? theme.rowNumberDeleted
|
||||
: rowIsActive
|
||||
? theme.rowNumberActive
|
||||
: theme.rowNumberDefault;
|
||||
ctx.fillStyle = rowNumberFill;
|
||||
ctx.fillRect(0, y, rowNumberWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
ctx.strokeStyle = theme.border;
|
||||
@@ -339,10 +363,9 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
|
||||
if (drawX + colWidth < rowNumberWidth || drawX >= width) return;
|
||||
|
||||
const selectedCell = cellIsSelected(item.displayIndex, visibleColIdx);
|
||||
const rowSelectionVisual = rowCellsUseSelectionVisual(item.id);
|
||||
const isDirtyCell = item.isDirtyCol[actualColIdx];
|
||||
const selectedFillVisual = rowSelectionVisual || selectedCell;
|
||||
const selectedBorderVisual = rowSelectionVisual || selectedCell;
|
||||
const selectedBorderVisual = selectedCell;
|
||||
const isSearchMatch = paintSearchMatches && searchMatchKeys.has(dataGridSearchMatchKey(item.displayIndex, actualColIdx));
|
||||
const isCurrentSearchMatch = paintSearchMatches && currentSearchMatch?.displayRow === item.displayIndex && currentSearchMatch.col === actualColIdx;
|
||||
const clippedX = Math.max(drawX, rowNumberWidth);
|
||||
@@ -357,7 +380,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
|
||||
ctx.fillStyle = theme.cellHover;
|
||||
ctx.fillRect(clippedX, y, cellPaintWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
}
|
||||
if ((rowIsActive || selectedCell) && !item.isDeleted && !isDirtyCell) {
|
||||
if (selectedCell && !item.isDeleted && !isDirtyCell) {
|
||||
ctx.fillStyle = theme.cellSelectedSingle;
|
||||
ctx.fillRect(clippedX, y, cellPaintWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
}
|
||||
@@ -463,16 +486,11 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
|
||||
// 用不透明底色覆盖冻结区域(遮挡第一轮溢入的非冻结列内容)
|
||||
// 注意:rowBase 在浅色主题下可能是 color-mix(..., transparent) 半透明色,
|
||||
// 单次半透明填充无法遮挡文字,需先填不透明底再叠加半透明色调
|
||||
if (rowIsActive && !item.isDeleted) {
|
||||
ctx.fillStyle = theme.cellSelectedSingle;
|
||||
ctx.fillStyle = theme.background;
|
||||
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
if (rowFill !== theme.background) {
|
||||
ctx.fillStyle = rowFill;
|
||||
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
} else {
|
||||
ctx.fillStyle = theme.background;
|
||||
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
if (rowBase !== theme.background) {
|
||||
ctx.fillStyle = rowBase;
|
||||
ctx.fillRect(rowNumberWidth, y, frozenWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
}
|
||||
}
|
||||
// 绘制冻结列的每个单元格(x 坐标不受 scrollLeft 影响)
|
||||
for (let fcIdx = 0; fcIdx < frozenColumnCount; fcIdx++) {
|
||||
|
||||
@@ -35,14 +35,9 @@ export function clearDataGridClipboardCopy(): void {
|
||||
internalClipboardCopy = null;
|
||||
}
|
||||
|
||||
export function rememberDataGridClipboardCopy(text: string, rows: readonly (readonly unknown[])[], includeHeader = false): void {
|
||||
if (!rows.some((row) => row.some((value) => value === null))) {
|
||||
internalClipboardCopy = null;
|
||||
return;
|
||||
}
|
||||
|
||||
export function rememberDataGridClipboardCopy(text: string, rows: readonly (readonly unknown[])[], header?: readonly unknown[]): void {
|
||||
// Preserve the logical grid matrix because plain TSV cannot escape embedded tabs or newlines.
|
||||
const headerRows = includeHeader ? parseClipboardTable(text).slice(0, 1) : [];
|
||||
const headerRows = header ? [header.map((value) => displayCellValue(value as CellValue))] : [];
|
||||
const copiedRows = rows.map((row) => row.map((value) => (value === null ? null : displayCellValue(value as CellValue))));
|
||||
internalClipboardCopy = { text, rows: [...headerRows, ...copiedRows], writeRevision: getClipboardWriteRevision() };
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ export function createDataGridCellContextMenuItems(options: {
|
||||
actions: Record<"cellDetails" | "columnDetails" | "rowDetails" | "setNull" | "bulkEdit" | "transpose", () => void>;
|
||||
downloadItem?: DataGridContextMenuItem | null;
|
||||
copySubmenu: DataGridContextMenuItem;
|
||||
selectionSubmenu: DataGridContextMenuItem;
|
||||
clearSelectionItem?: DataGridContextMenuItem;
|
||||
generateSubmenu?: DataGridContextMenuItem;
|
||||
}): DataGridContextMenuItem[] {
|
||||
const items: DataGridContextMenuItem[] = [];
|
||||
@@ -148,7 +148,7 @@ export function createDataGridCellContextMenuItems(options: {
|
||||
if (options.generateSubmenu) items.push(options.generateSubmenu);
|
||||
}
|
||||
if (options.hasCell) items.push({ label: options.labels.transpose, action: options.actions.transpose, icon: options.icons.transpose });
|
||||
if (options.hasSelection) items.push(options.selectionSubmenu);
|
||||
if (options.hasSelection && options.clearSelectionItem) items.push(options.clearSelectionItem);
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
import type { DataGridCopyInsertMode, DataGridTableMeta } from "@/lib/dataGrid/dataGridSql";
|
||||
|
||||
export const DATA_GRID_COPY_EXTRACTOR_IDS = ["tsv", "tsv-with-headers", "csv", "csv-with-headers", "pipe-separated", "dsv", "json", "json-lines", "one-row", "sql-in-list", "sql-inserts", "sql-updates", "where-clause", "markdown", "html", "xml", "pretty"] as const;
|
||||
|
||||
export type DataGridCopyExtractorId = (typeof DATA_GRID_COPY_EXTRACTOR_IDS)[number];
|
||||
export type DataGridExtractorCategory = "delimited" | "json" | "sql" | "document";
|
||||
|
||||
export const DATA_GRID_EXTRACTOR_CONTRACT_VERSION = 1 as const;
|
||||
|
||||
export const DATA_GRID_COPY_EXTRACTOR_DESCRIPTORS: Record<DataGridCopyExtractorId, { category: DataGridExtractorCategory; separatorBefore?: boolean }> = {
|
||||
tsv: { category: "delimited" },
|
||||
"tsv-with-headers": { category: "delimited" },
|
||||
csv: { category: "delimited" },
|
||||
"csv-with-headers": { category: "delimited" },
|
||||
"pipe-separated": { category: "delimited" },
|
||||
dsv: { category: "delimited" },
|
||||
"one-row": { category: "delimited" },
|
||||
json: { category: "json", separatorBefore: true },
|
||||
"json-lines": { category: "json" },
|
||||
"sql-in-list": { category: "sql", separatorBefore: true },
|
||||
"sql-inserts": { category: "sql" },
|
||||
"sql-updates": { category: "sql" },
|
||||
"where-clause": { category: "sql" },
|
||||
markdown: { category: "document", separatorBefore: true },
|
||||
html: { category: "document" },
|
||||
xml: { category: "document" },
|
||||
pretty: { category: "document" },
|
||||
};
|
||||
|
||||
export type DataGridSelectionKind = "cells" | "rows" | "columns";
|
||||
export type DataGridQuotePolicy = "always" | "minimal" | "never";
|
||||
export type DataGridExtractorOptionsError = "column-separator-empty" | "row-separator-empty" | "separator-too-long" | "null-text-too-long" | "separators-overlap" | "invalid-quote" | "quote-conflicts";
|
||||
|
||||
/** Extractors that emit relational SQL predicates not supported by some stores.
|
||||
* sql-updates has a Mongo-specific updateOne path, so Mongo is NOT disabled;
|
||||
* where-clause has no Mongo equivalent, so Mongo IS disabled. */
|
||||
const NO_SQL_UPDATE_DATABASE_TYPES: ReadonlyArray<string> = ["neo4j", "tdengine"];
|
||||
const NO_WHERE_CLAUSE_DATABASE_TYPES: ReadonlyArray<string> = ["neo4j", "tdengine", "mongodb"];
|
||||
|
||||
/** True when the extractor emits relational SQL the database type cannot run. */
|
||||
export function extractorUnavailableForDatabase(extractor: DataGridCopyExtractorId, databaseType: string | undefined | null): boolean {
|
||||
const db = databaseType ?? "";
|
||||
if (extractor === "sql-updates") return NO_SQL_UPDATE_DATABASE_TYPES.includes(db);
|
||||
if (extractor === "where-clause") return NO_WHERE_CLAUSE_DATABASE_TYPES.includes(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
export interface DataGridDsvOptions {
|
||||
columnSeparator: string;
|
||||
rowSeparator: string;
|
||||
nullText: string;
|
||||
quote: string;
|
||||
quotePolicy: DataGridQuotePolicy;
|
||||
includeColumnHeader: boolean;
|
||||
includeRowHeader: boolean;
|
||||
}
|
||||
|
||||
export interface DataGridExtractorOptions {
|
||||
dsv: DataGridDsvOptions;
|
||||
sql: {
|
||||
skipComputedColumns: boolean;
|
||||
skipGeneratedColumns: boolean;
|
||||
insertMode: DataGridCopyInsertMode;
|
||||
excludePrimaryKeysFromInsert: boolean;
|
||||
};
|
||||
json: { pretty: boolean };
|
||||
}
|
||||
|
||||
export interface DataGridExtractColumn {
|
||||
displayName: string;
|
||||
sourceName?: string;
|
||||
sourceIndex: number;
|
||||
}
|
||||
|
||||
export interface DataGridExtractRequest {
|
||||
version: typeof DATA_GRID_EXTRACTOR_CONTRACT_VERSION;
|
||||
extractor: DataGridCopyExtractorId;
|
||||
databaseType?: DatabaseType;
|
||||
tableMeta?: DataGridTableMeta;
|
||||
columns: DataGridExtractColumn[];
|
||||
selectedColumnIndexes: number[];
|
||||
rows: unknown[][];
|
||||
selectionKind: DataGridSelectionKind;
|
||||
options: DataGridExtractorOptions;
|
||||
}
|
||||
|
||||
export type DataGridExtractWarningCode = "omitted-columns" | "duplicate-json-column-names";
|
||||
|
||||
export interface DataGridExtractResult {
|
||||
text: string;
|
||||
mimeType: string;
|
||||
fileExtension: string;
|
||||
rowCount: number;
|
||||
columnCount: number;
|
||||
omittedColumns?: string[];
|
||||
warnings?: Array<{ code: DataGridExtractWarningCode; message: string }>;
|
||||
}
|
||||
|
||||
export interface DataGridExtractPreview extends DataGridExtractResult {
|
||||
sourceRowCount: number;
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
export const DATA_GRID_EXTRACTOR_PREVIEW_MAX_ROWS = 100;
|
||||
|
||||
export const DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS: DataGridExtractorOptions = {
|
||||
dsv: {
|
||||
columnSeparator: ",",
|
||||
rowSeparator: "\n",
|
||||
nullText: "NULL",
|
||||
quote: '"',
|
||||
quotePolicy: "minimal",
|
||||
includeColumnHeader: false,
|
||||
includeRowHeader: false,
|
||||
},
|
||||
sql: {
|
||||
skipComputedColumns: true,
|
||||
skipGeneratedColumns: true,
|
||||
insertMode: "merged",
|
||||
excludePrimaryKeysFromInsert: false,
|
||||
},
|
||||
json: { pretty: true },
|
||||
};
|
||||
|
||||
function unicodeCodePointLength(value: string): number {
|
||||
return [...value].length;
|
||||
}
|
||||
|
||||
export function normalizeDataGridExtractorOptions(value: unknown): DataGridExtractorOptions {
|
||||
const source = typeof value === "object" && value !== null ? (value as Partial<DataGridExtractorOptions>) : {};
|
||||
const dsv: Partial<DataGridDsvOptions> = typeof source.dsv === "object" && source.dsv !== null ? source.dsv : {};
|
||||
const sql: Partial<DataGridExtractorOptions["sql"]> = typeof source.sql === "object" && source.sql !== null ? source.sql : {};
|
||||
const json: Partial<DataGridExtractorOptions["json"]> = typeof source.json === "object" && source.json !== null ? source.json : {};
|
||||
const separator = (candidate: unknown, fallback: string) => (typeof candidate === "string" && unicodeCodePointLength(candidate) > 0 && unicodeCodePointLength(candidate) <= 8 ? candidate : fallback);
|
||||
const quote = typeof dsv.quote === "string" && unicodeCodePointLength(dsv.quote) === 1 ? dsv.quote : DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv.quote;
|
||||
return {
|
||||
dsv: {
|
||||
columnSeparator: separator(dsv.columnSeparator, DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv.columnSeparator),
|
||||
rowSeparator: separator(dsv.rowSeparator, DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv.rowSeparator),
|
||||
nullText: typeof dsv.nullText === "string" && unicodeCodePointLength(dsv.nullText) <= 64 ? dsv.nullText : DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS.dsv.nullText,
|
||||
quote,
|
||||
quotePolicy: dsv.quotePolicy === "always" || dsv.quotePolicy === "never" ? dsv.quotePolicy : "minimal",
|
||||
includeColumnHeader: dsv.includeColumnHeader === true,
|
||||
includeRowHeader: dsv.includeRowHeader === true,
|
||||
},
|
||||
sql: {
|
||||
skipComputedColumns: sql.skipComputedColumns !== false,
|
||||
skipGeneratedColumns: sql.skipGeneratedColumns !== false,
|
||||
insertMode: sql.insertMode === "row-by-row" ? "row-by-row" : "merged",
|
||||
excludePrimaryKeysFromInsert: sql.excludePrimaryKeysFromInsert === true,
|
||||
},
|
||||
json: { pretty: json.pretty !== false },
|
||||
};
|
||||
}
|
||||
|
||||
const DATA_GRID_COPY_EXTRACTOR_ID_SET = new Set<string>(DATA_GRID_COPY_EXTRACTOR_IDS);
|
||||
|
||||
export function normalizeDataGridCopyExtractorId(value: unknown): DataGridCopyExtractorId {
|
||||
return typeof value === "string" && DATA_GRID_COPY_EXTRACTOR_ID_SET.has(value) ? (value as DataGridCopyExtractorId) : "tsv";
|
||||
}
|
||||
|
||||
export function validateDataGridExtractorOptions(extractor: DataGridCopyExtractorId, options: DataGridExtractorOptions): DataGridExtractorOptionsError | null {
|
||||
const usesDsv = DATA_GRID_COPY_EXTRACTOR_DESCRIPTORS[extractor].category === "delimited";
|
||||
if (!usesDsv) return null;
|
||||
const quoteCodePoint = options.dsv.quote.codePointAt(0);
|
||||
if (unicodeCodePointLength(options.dsv.quote) !== 1 || quoteCodePoint === undefined || quoteCodePoint <= 0x1f || quoteCodePoint === 0x7f) return "invalid-quote";
|
||||
if (unicodeCodePointLength(options.dsv.nullText) > 64) return "null-text-too-long";
|
||||
|
||||
const columnSeparator = extractor === "tsv" || extractor === "tsv-with-headers" ? "\t" : extractor === "csv" || extractor === "csv-with-headers" ? "," : extractor === "pipe-separated" ? "|" : options.dsv.columnSeparator;
|
||||
if (!columnSeparator) return "column-separator-empty";
|
||||
const usesRowSeparator = extractor !== "one-row";
|
||||
if (usesRowSeparator && !options.dsv.rowSeparator) return "row-separator-empty";
|
||||
if (unicodeCodePointLength(columnSeparator) > 8 || unicodeCodePointLength(options.dsv.rowSeparator) > 8) return "separator-too-long";
|
||||
if (usesRowSeparator && (columnSeparator.includes(options.dsv.rowSeparator) || options.dsv.rowSeparator.includes(columnSeparator))) return "separators-overlap";
|
||||
if (columnSeparator.includes(options.dsv.quote) || (usesRowSeparator && options.dsv.rowSeparator.includes(options.dsv.quote))) return "quote-conflicts";
|
||||
return null;
|
||||
}
|
||||
@@ -35,6 +35,8 @@ export const DATA_GRID_DARK_SEARCH_COLORS = {
|
||||
current: "rgb(116, 87, 0)",
|
||||
currentBorder: "rgb(239, 177, 0)",
|
||||
} as const;
|
||||
export const DATA_GRID_LIGHT_ACTIVE_ROW_BG = "rgb(244, 248, 255)";
|
||||
export const DATA_GRID_DARK_ACTIVE_ROW_BG = "rgb(25, 34, 46)";
|
||||
export const DATA_GRID_LIGHT_STRIPED_ROW_BG = "rgb(240, 240, 240)";
|
||||
export const DATA_GRID_DARK_STRIPED_ROW_BG = "rgb(40, 40, 43)";
|
||||
export const DATA_GRID_DARK_ROW_NUMBER_BG = "rgb(35, 37, 42)";
|
||||
@@ -218,6 +220,10 @@ export function cssVarColor(getVar: (name: string) => string, name: string, fall
|
||||
return toCanvasSafeColor(getVar(name), fallback);
|
||||
}
|
||||
|
||||
export function dataGridActiveRowBackground(isDark: boolean): string {
|
||||
return isDark ? DATA_GRID_DARK_ACTIVE_ROW_BG : DATA_GRID_LIGHT_ACTIVE_ROW_BG;
|
||||
}
|
||||
|
||||
function resolveCssVarReferences(value: string, getVar: (name: string) => string, depth = 0): string {
|
||||
if (depth > 6 || !value.includes("var(")) return value;
|
||||
return value.replace(/var\(\s*(--[\w-]+)(?:\s*,\s*([^)]+))?\s*\)/g, (_match, name: string, fallback?: string) => {
|
||||
@@ -240,16 +246,16 @@ export function resolveDataGridPaintTheme(options: { getVar: (name: string) => s
|
||||
const primary = cssVarColor(getVar, "--primary", isDark ? "rgb(208, 208, 214)" : "rgb(23, 23, 23)");
|
||||
const destructive = cssVarColor(getVar, "--destructive", isDark ? "rgb(243, 98, 95)" : "rgb(231, 0, 11)");
|
||||
const accent = cssVarColor(getVar, "--accent", isDark ? "rgb(46, 47, 51)" : "rgb(245, 245, 245)");
|
||||
const activeSurface = isDark ? "rgb(64, 64, 64)" : "rgb(232, 232, 232)";
|
||||
const activeSurface = dataGridActiveRowBackground(isDark);
|
||||
const rowMuted = isDark ? DATA_GRID_DARK_STRIPED_ROW_BG : DATA_GRID_LIGHT_STRIPED_ROW_BG;
|
||||
const rowNew = isDark ? "rgb(51, 51, 55)" : "rgb(243, 243, 243)";
|
||||
const rowDeleted = isDark ? "rgb(55, 31, 32)" : "rgb(255, 244, 244)";
|
||||
const cellActive = activeSurface;
|
||||
const cellDirty = isDark ? "rgb(94, 75, 26)" : "rgb(255, 248, 230)";
|
||||
const cellSelected = isDark ? "rgb(66, 67, 70)" : "rgb(226, 226, 226)";
|
||||
const cellSelectedDirty = isDark ? "rgb(94, 75, 26)" : "rgb(244, 229, 186)";
|
||||
const cellSelectedBorder = isDark ? "rgb(170, 170, 175)" : "rgb(90, 90, 90)";
|
||||
const cellSelectedSingle = isDark ? "rgb(17, 24, 39)" : "rgb(209, 213, 219)";
|
||||
const cellSelected = isDark ? "rgb(20, 40, 60)" : "rgb(239, 246, 255)";
|
||||
const cellSelectedDirty = isDark ? "rgb(76, 66, 38)" : "rgb(235, 224, 184)";
|
||||
const cellSelectedBorder = isDark ? "rgb(96, 165, 250)" : "rgb(59, 130, 246)";
|
||||
const cellSelectedSingle = isDark ? "rgb(30, 64, 96)" : "rgb(191, 219, 254)";
|
||||
const cellHover = accent;
|
||||
const cellSearch = isDark ? DATA_GRID_DARK_SEARCH_COLORS.match : "rgb(253, 245, 184)";
|
||||
const cellCurrentSearch = isDark ? DATA_GRID_DARK_SEARCH_COLORS.current : "rgba(253, 224, 71, 0.52)";
|
||||
@@ -259,7 +265,7 @@ export function resolveDataGridPaintTheme(options: { getVar: (name: string) => s
|
||||
const rowNumberEdited = isDark ? DATA_GRID_DARK_ROW_NUMBER_EDITED_BG : DATA_GRID_LIGHT_ROW_NUMBER_EDITED_BG;
|
||||
const rowNumberDeleted = isDark ? DATA_GRID_DARK_ROW_NUMBER_DELETED_BG : DATA_GRID_LIGHT_ROW_NUMBER_DELETED_BG;
|
||||
const rowNumberActive = activeSurface;
|
||||
const rowNumberSelected = isDark ? "rgb(31, 41, 55)" : "rgb(209, 213, 219)";
|
||||
const rowNumberSelected = isDark ? "rgb(30, 64, 96)" : "rgb(191, 219, 254)";
|
||||
|
||||
return {
|
||||
background,
|
||||
|
||||
@@ -51,6 +51,7 @@ export interface DataGridCopyInsertStatementOptions {
|
||||
sourceColumns?: Array<string | undefined>;
|
||||
rows: GridCellValue[][];
|
||||
excludePrimaryKeys?: boolean;
|
||||
includeComputedColumns?: boolean;
|
||||
insertMode?: DataGridCopyInsertMode;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,17 @@ export interface DataGridToolbarExportCapability {
|
||||
onSelect: (value: string) => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface DataGridToolbarCopyCapability {
|
||||
label: string;
|
||||
tooltip?: string;
|
||||
visible?: boolean;
|
||||
disabled?: boolean;
|
||||
currentValue: string;
|
||||
items: readonly DataGridToolbarMenuItem[];
|
||||
onCopy: () => void | Promise<void>;
|
||||
onSelect: (value: string) => void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface DataGridToolbarAutoRefreshCapability {
|
||||
label: string;
|
||||
shortLabel: string;
|
||||
@@ -86,3 +97,17 @@ export async function selectDataGridToolbarExportItem(capability: DataGridToolba
|
||||
await capability.onSelect(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function triggerDataGridToolbarCopy(capability: DataGridToolbarCopyCapability | undefined): Promise<boolean> {
|
||||
if (!capability || !isDataGridToolbarCapabilityVisible(capability) || capability.disabled) return false;
|
||||
await capability.onCopy();
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function selectDataGridToolbarCopyItem(capability: DataGridToolbarCopyCapability | undefined, value: string): Promise<boolean> {
|
||||
if (!capability || !isDataGridToolbarCapabilityVisible(capability)) return false;
|
||||
const item = capability.items.find((candidate) => candidate.value === value);
|
||||
if (!item || item.disabled) return false;
|
||||
await capability.onSelect(value);
|
||||
return true;
|
||||
}
|
||||
@@ -39,6 +39,13 @@ export function normalizeSelectedColumnIndexes(columnIndexes: Iterable<number>):
|
||||
return [...new Set(columnIndexes)].filter((index) => index >= 0).sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
export function dedupeColumnIndexes(columnIndexes: Iterable<number>): number[] {
|
||||
// Deduplicate while preserving the first visible occurrence (insertion order)
|
||||
// instead of numeric order. Used by the copy/extractor path so drag-reordered
|
||||
// columns stay in the order the user sees them.
|
||||
return [...new Set(columnIndexes)].filter((index) => index >= 0);
|
||||
}
|
||||
|
||||
export function normalizeSelectionRange(anchor: CellPosition, focus: CellPosition): CellSelectionRange {
|
||||
return {
|
||||
startRow: Math.min(anchor.rowIndex, focus.rowIndex),
|
||||
@@ -118,50 +125,3 @@ export function summarizeSelection(selection: SelectionData): SelectionSummary {
|
||||
sum,
|
||||
};
|
||||
}
|
||||
|
||||
function displayValue(value: GridCellValue): string {
|
||||
if (value === null) return "NULL";
|
||||
if (typeof value === "boolean") return value ? "true" : "false";
|
||||
if (typeof value === "object") return JSON.stringify(value);
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function csvValue(value: GridCellValue | string): string {
|
||||
const text = typeof value === "string" ? value : displayValue(value);
|
||||
return `"${text.replace(/"/g, '""')}"`;
|
||||
}
|
||||
|
||||
function sqlValue(value: GridCellValue): string {
|
||||
if (value === null) return "NULL";
|
||||
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
||||
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
||||
return `'${String(value).replace(/'/g, "''")}'`;
|
||||
}
|
||||
|
||||
export function formatSelectionAsTsv(selection: SelectionData, includeHeader = false): string {
|
||||
const body = selection.rows.map((row) => row.map(displayValue).join("\t")).join("\n");
|
||||
if (!includeHeader) return body;
|
||||
return [selection.columns.join("\t"), body].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
export function formatSelectionAsCsv(selection: SelectionData): string {
|
||||
const header = selection.columns.map(csvValue).join(",");
|
||||
const body = selection.rows.map((row) => row.map(csvValue).join(",")).join("\n");
|
||||
return [header, body].filter(Boolean).join("\n");
|
||||
}
|
||||
|
||||
export function formatSelectionAsJson(selection: SelectionData): string {
|
||||
const objects = selection.rows.map((row) => {
|
||||
const item: Record<string, GridCellValue> = {};
|
||||
selection.columns.forEach((column, index) => {
|
||||
item[column] = row[index] ?? null;
|
||||
});
|
||||
return item;
|
||||
});
|
||||
return JSON.stringify(objects, null, 2);
|
||||
}
|
||||
|
||||
export function formatSelectionAsSqlInList(selection: SelectionData): string {
|
||||
const values = selection.rows.flat().map(sqlValue);
|
||||
return `(${values.join(", ")})`;
|
||||
}
|
||||
Reference in new issue
Block a user