From 5803df3f73b2a818ddfca30d742240a4862d9d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E4=B8=AB=E8=AE=B2=E6=A2=B5?= Date: Mon, 20 Jul 2026 13:59:49 +0800 Subject: [PATCH] feat(context-menu): copy INSERT from selected columns --- apps/desktop/src/components/grid/DataGrid.vue | 22 ++ .../src/components/ui/CustomContextMenu.vue | 16 +- .../__tests__/useDataGridExport.spec.ts | 130 +++++++++- .../__tests__/useDataGridSelection.spec.ts | 41 ++++ .../src/composables/useDataGridExport.ts | 225 +++++++++++++++--- .../src/composables/useDataGridSelection.ts | 50 +++- apps/desktop/src/i18n/locales/en.ts | 3 + apps/desktop/src/i18n/locales/es.ts | 3 + apps/desktop/src/i18n/locales/it.ts | 3 + apps/desktop/src/i18n/locales/ja.ts | 3 + apps/desktop/src/i18n/locales/pt-BR.ts | 3 + apps/desktop/src/i18n/locales/zh-CN.ts | 3 + apps/desktop/src/i18n/locales/zh-TW.ts | 3 + apps/desktop/src/lib/common/clipboard.ts | 2 +- .../src/lib/dataGrid/dataGridContextMenu.ts | 2 +- .../desktop/src/lib/dataGrid/gridSelection.ts | 5 + packages/app-tests/clipboard.test.ts | 80 ++++++- .../app-tests/dataGridContextMenu.test.ts | 6 + 18 files changed, 551 insertions(+), 49 deletions(-) diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index 68f8f1615..18b40f620 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -3345,6 +3345,7 @@ const { isSelectingAll, selectedRange, selectedCells, + selectedCellMatrix, selectedCellCount, hasCellSelection, clearCellSelection, @@ -4979,6 +4980,11 @@ const { copySelectionCsv, copySelectionJson, copySelectionSqlInList, + copySelectionAsInsert, + prefetchSelectionAsInsertStatement, + canCopyPreparedSelectionInsert, + canCopySelectionAsInsert, + selectionInsertRowCount, copySelectedRowsTsv, copySelectedRowsTsvWithHeaders, copyColumnNames, @@ -5018,6 +5024,7 @@ const { exportBatchSize: computed(() => settingsStore.editorSettings.exportBatchSize), hasCellSelection, selectedCells, + selectedCellMatrix, selectedRange, contextCell: exportContextCell, getRowItem: (rowId: number) => visibleDisplayItems.value.find((item) => item.id === rowId), @@ -6356,6 +6363,7 @@ function onHeaderContext(col: string, columnIndex: number) { } contextHeaderColumn.value = col; contextHeaderColumnIndex.value = columnIndex; + void prefetchCopyStatements(); } async function copyHeaderColumn() { if (!contextHeaderColumn.value) return; @@ -6568,6 +6576,12 @@ function onRowContext(rowId: number, rowIndex: number) { } async function prefetchCopyStatements() { + if (canCopySelectionAsInsert.value) { + await prefetchSelectionAsInsertStatement(); + if (selectionInsertRowCount.value > 1 && canCopyPreparedSelectionInsert()) { + await prefetchSelectionAsInsertStatement("row-by-row"); + } + } await prefetchRowAsInsertStatement(false); if (isMultiRow.value) { await prefetchRowAsInsertStatement(false, "row-by-row"); @@ -7262,6 +7276,13 @@ function copySubmenu(): ContextMenuItem { } function selectionSubmenu(): ContextMenuItem { + const insertItems: ContextMenuItem[] = + selectedCells.value.rows.length > 1 + ? [ + { label: t("grid.copySelectionInsertMerged"), action: () => copySelectionAsInsert("merged"), disabled: () => !canCopySelectionAsInsert.value || !canCopyPreparedSelectionInsert("merged") }, + { label: t("grid.copySelectionInsertRowByRow"), action: () => copySelectionAsInsert("row-by-row"), disabled: () => !canCopySelectionAsInsert.value || !canCopyPreparedSelectionInsert("row-by-row") }, + ] + : [{ label: t("grid.copySelectionInsert"), action: () => copySelectionAsInsert(), disabled: () => !canCopySelectionAsInsert.value || !canCopyPreparedSelectionInsert() }]; return { label: t("grid.selection"), icon: SquareDashed, @@ -7271,6 +7292,7 @@ function selectionSubmenu(): ContextMenuItem { { label: t("grid.copySelectionCsv"), action: copySelectionCsv }, { label: t("grid.copySelectionJson"), action: copySelectionJson }, { label: t("grid.copySelectionSql"), action: copySelectionSqlInList }, + ...insertItems, { label: "", separator: true }, { label: t("grid.clearSelection"), action: clearCellSelection }, ], diff --git a/apps/desktop/src/components/ui/CustomContextMenu.vue b/apps/desktop/src/components/ui/CustomContextMenu.vue index 5ac83ccee..06967052d 100644 --- a/apps/desktop/src/components/ui/CustomContextMenu.vue +++ b/apps/desktop/src/components/ui/CustomContextMenu.vue @@ -7,7 +7,7 @@ import { registerGlobalContextMenu, type ContextMenuRegistration } from "@/compo export interface ContextMenuItem { label: string; action?: () => void; - disabled?: boolean; + disabled?: boolean | (() => boolean); separator?: boolean; icon?: Component; iconClass?: string; @@ -102,14 +102,14 @@ onMounted(() => { }); function handleItemClick(item: ContextMenuItem) { - if (item.disabled) return; + if (itemIsDisabled(item)) return; if (item.children?.length) return; // submenu trigger — do nothing on click close(); item.action?.(); } function handleSubItemClick(item: ContextMenuItem) { - if (item.disabled) return; + if (itemIsDisabled(item)) return; close(); item.action?.(); } @@ -143,7 +143,7 @@ function onItemMouseEnter(index: number, event: MouseEvent) { lastMouseX = event.clientX; lastMouseY = event.clientY; const item = activeItems.value[index]; - if (!item?.children?.length || item.disabled) { + if (!item?.children?.length || itemIsDisabled(item)) { // Moving to an item without children — close submenu immediately, no delay needed activeSubIndex.value = null; return; @@ -240,6 +240,10 @@ function itemButtonClass(variant?: "default" | "destructive") { ]; } +function itemIsDisabled(item: ContextMenuItem): boolean { + return typeof item.disabled === "function" ? item.disabled() : !!item.disabled; +} + function shortcutKeys(shortcut?: string): string[] { return shortcutDisplayKeys(shortcut); } @@ -264,7 +268,7 @@ onBeforeUnmount(() => {
-