From 8091054d28e79a379fe3e6d80e3f43508bb9bcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Thu, 1 Oct 2026 22:55:13 +0800 Subject: [PATCH] feat(structure): copy selected field columns --- .../structure/TableStructureEditor.vue | 191 ++++++- ...tructureEditor.columnSelectionCopy.spec.ts | 484 ++++++++++++++++++ 2 files changed, 660 insertions(+), 15 deletions(-) create mode 100644 apps/desktop/src/components/structure/__tests__/TableStructureEditor.columnSelectionCopy.spec.ts diff --git a/apps/desktop/src/components/structure/TableStructureEditor.vue b/apps/desktop/src/components/structure/TableStructureEditor.vue index 075d8dea9c..b3328fc731 100644 --- a/apps/desktop/src/components/structure/TableStructureEditor.vue +++ b/apps/desktop/src/components/structure/TableStructureEditor.vue @@ -35,7 +35,8 @@ import { type SqlHighlighter, createShikiSqlHighlighter } from "@/lib/sql/sqlHig import { joinSqlStatementsForScript } from "@/lib/sql/sqlBatchScript"; import { applyDdlDatabaseQualifier, formatDdlForDisplay, formatGeneratedDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay"; import { splitSqlStatementRanges } from "@/lib/sql/sqlStatementRanges"; -import { copyToClipboard } from "@/lib/common/clipboard"; +import { copyToClipboard, eventTargetUsesNativeClipboard, isPlainClipboardShortcut } from "@/lib/common/clipboard"; +import { formatTsv } from "@/lib/export/exportFormats"; import DataGridCopyColumnNamesDialog from "@/components/grid/DataGridCopyColumnNamesDialog.vue"; import { sqlFormatDialectForDbType, type SqlFormatDialect } from "@/lib/sql/sqlFormatter"; import { queryTimeoutSecsForConcurrentIndex, queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout"; @@ -1234,6 +1235,11 @@ const selectedColumnId = ref(null); // new rows are inserted and which row copy/add operations anchor to. const selectedColumnIds = ref>(new Set()); const columnSelectionAnchorId = ref(null); +// The editable field rows have their own selection state for batch structure +// operations. Header selection is a separate mode so clipboard actions do not +// accidentally apply row copy/drop operations to the field rows. +const selectedStructureColumnKeys = ref>(new Set()); +const structureColumnSelectionAnchorKey = ref(null); const highlightedColumnId = ref(null); const indexSearchInputRef = ref>(); const indexSearchText = ref(""); @@ -1506,6 +1512,123 @@ const colLabels = computed(() => { } return labels; }); + +const copyableStructureColumnLabels = computed(() => colLabels.value.filter((label) => label.key !== "actions")); + +function structureColumnSelectionClass(key: string): string { + return key !== "actions" && selectedStructureColumnKeys.value.has(key) ? "structure-grid-column-selected" : ""; +} + +function structureColumnClipboardValue(column: EditableStructureColumn, key: string): string { + switch (key) { + case "name": + return column.name; + case "type": + return gaussdbMDataTypeDisplayName(dataTypeBaseInputValue(databaseType.value, column.dataType)); + case "length": { + if (isMysqlEnumDataType(databaseType.value, column.dataType)) return (column.enumValues ?? []).join(", "); + if (isPostgresGeometryDataType(databaseType.value, column.dataType)) { + return [postgresGeometryTypeValue(column.dataType), postgresGeometrySridValue(column.dataType)].filter(Boolean).join(", "); + } + return [dataTypeLengthInputValue(databaseType.value, column.dataType), dataTypeLengthUnitValue(databaseType.value, column.dataType)].filter(Boolean).join(" "); + } + case "nullable": + return column.isNullable ? t("structureEditor.yes") : t("structureEditor.no"); + case "primaryKey": + return column.isPrimaryKey ? t("structureEditor.yes") : t("structureEditor.no"); + case "defaultValue": + return column.defaultValue; + case "comment": + return column.comment; + case "characterSet": + return columnCharset(column); + case "collation": + return columnCollation(column); + case "extendedProperties": + return Object.entries(column.extra) + .filter(([, value]) => value !== undefined && value !== false && value !== "") + .map(([name, value]) => `${name}: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`) + .join("; "); + default: + return ""; + } +} + +function structureColumnClipboardText(): string { + const labels = copyableStructureColumnLabels.value.filter((label) => selectedStructureColumnKeys.value.has(label.key)); + if (labels.length === 0) return ""; + const rows = columns.value.filter((column) => !column.markedForDrop).map((column) => labels.map((label) => structureColumnClipboardValue(column, label.key))); + return formatTsv( + labels.map((label) => label.label), + rows, + "", + ); +} + +function setStructureColumnSelection(keys: Iterable, anchorKey: string | null) { + // Clears first: setColumnSelection also resets the header-column keys. + clearColumnSelection(); + selectedStructureColumnKeys.value = new Set(keys); + structureColumnSelectionAnchorKey.value = anchorKey; +} + +function selectStructureColumnKey(key: string, event: MouseEvent) { + if (key === "actions") return; + const labels = copyableStructureColumnLabels.value; + const index = labels.findIndex((label) => label.key === key); + if (index < 0) return; + + if (event.shiftKey && structureColumnSelectionAnchorKey.value) { + const anchorIndex = labels.findIndex((label) => label.key === structureColumnSelectionAnchorKey.value); + if (anchorIndex >= 0) { + const start = Math.min(anchorIndex, index); + const end = Math.max(anchorIndex, index); + setStructureColumnSelection( + labels.slice(start, end + 1).map((label) => label.key), + structureColumnSelectionAnchorKey.value, + ); + return; + } + } + + if (event.metaKey || event.ctrlKey) { + const next = new Set(selectedStructureColumnKeys.value); + if (next.has(key)) next.delete(key); + else next.add(key); + setStructureColumnSelection(next, key); + return; + } + + setStructureColumnSelection([key], key); +} + +function selectAllStructureColumns() { + const keys = copyableStructureColumnLabels.value.map((label) => label.key); + if (keys.length > 0) setStructureColumnSelection(keys, keys[0] ?? null); +} + +async function copySelectedStructureColumns() { + const text = structureColumnClipboardText(); + if (!text) return; + try { + await copyToClipboard(text); + toast(t("grid.copied")); + } catch (e: any) { + toast(t("grid.copyFailed", { message: e?.message || String(e) }), 5000); + } +} + +watch( + copyableStructureColumnLabels, + (labels) => { + const allowed = new Set(labels.map((label) => label.key)); + const next = [...selectedStructureColumnKeys.value].filter((key) => allowed.has(key)); + if (next.length !== selectedStructureColumnKeys.value.size) selectedStructureColumnKeys.value = new Set(next); + if (structureColumnSelectionAnchorKey.value && !allowed.has(structureColumnSelectionAnchorKey.value)) structureColumnSelectionAnchorKey.value = next[0] ?? null; + }, + { flush: "sync" }, +); + const structureColumnRowHeight = computed(() => structureDensityMetric.value.controlHeight + structureDensityMetric.value.cellPaddingY * 2 + 1); const columnTableWidth = computed(() => visibleColWidths.value.reduce((total, width) => total + width, 0)); function columnNeedsVariableGeometryHeight(column: EditableStructureColumn): boolean { @@ -3013,11 +3136,18 @@ function columnIsSelectable(column: EditableStructureColumn): boolean { return !column.markedForDrop && columns.value.some((item) => item.id === column.id); } -/** Replace the whole selection state (set + active + shift anchor) atomically. */ +/** + * Replace the whole selection state (set + active + shift anchor) atomically. + * Header-column selection and row selection are exclusive modes, so any row + * selection also clears the header-column keys, mirroring how + * `setStructureColumnSelection` clears the row selection. + */ function setColumnSelection(ids: Iterable, activeId: string | null, anchorId: string | null) { selectedColumnIds.value = new Set(ids); selectedColumnId.value = activeId; columnSelectionAnchorId.value = anchorId; + selectedStructureColumnKeys.value = new Set(); + structureColumnSelectionAnchorKey.value = null; } function clearColumnSelection() { @@ -4560,6 +4690,20 @@ function isPlainModDeleteShortcut(event: KeyboardEvent): boolean { function onStructureEditorKeydown(event: KeyboardEvent) { if (event.defaultPrevented) return; + if (activeTab.value === "columns" && !eventTargetUsesNativeClipboard(event)) { + if (isPlainClipboardShortcut(event, "a")) { + event.preventDefault(); + event.stopPropagation(); + selectAllStructureColumns(); + return; + } + if (isPlainClipboardShortcut(event, "c") && selectedStructureColumnKeys.value.size > 0) { + event.preventDefault(); + event.stopPropagation(); + void copySelectedStructureColumns(); + return; + } + } const focusedColumn = activeTab.value === "columns" ? focusedEditableColumn(event.target) : undefined; if (focusedColumn && isShiftEnterShortcut(event) && canAddColumn.value) { event.preventDefault(); @@ -5210,11 +5354,13 @@ watch( -
+
@@ -5248,7 +5400,7 @@ watch( @dragover="onColumnDragOver(index, $event)" @drop="onColumnDrop(index, $event)" > - +
{{ index + 1 }} @@ -5315,10 +5467,10 @@ watch(
- + - + - +
- + - + - +
@@ -5443,7 +5595,7 @@ watch(
- +
@@ -5471,7 +5623,7 @@ watch(
- + - + - +