diff --git a/src/features/database/DataView.tsx b/src/features/database/DataView.tsx index d413e9f57..e67ba7976 100644 --- a/src/features/database/DataView.tsx +++ b/src/features/database/DataView.tsx @@ -70,6 +70,56 @@ export function ExtractorSelect({ value, onChange }: { value: ExportFormat; onCh export const copyFormatOf = (extractor: ExportFormat): CopyFormat => extractor === "csv" ? "csv" : extractor === "onerow" ? "onerow" : "tsv"; +/** The toolbar's extractor; `table` is the whole table to export (`null`: nothing, already reported). */ +export interface ExtractorMenu { + value: ExportFormat; + onChange: (f: ExportFormat) => void; + table: () => ExportData | null | Promise; +} + +const PANEL_ENTRIES: ReadonlyArray<{ panel: DataPanel; label: string }> = [ + { panel: "record", label: "Show Record View" }, + { panel: "value", label: "Open in Value Editor" }, + { panel: "aggregates", label: "Show Aggregate View" }, +]; + +/** + * The view's entries of the grid's context menu (DataGrip's): the side panels, checked while + * shown, then the toolbar's extractor and Export Table to Clipboard when the view has them. + */ +export function viewMenu( + panel: DataPanel | null, + onPanelChange: (panel: DataPanel | null) => void, + extractor?: ExtractorMenu, +): MenuItem[] { + const items: MenuItem[] = PANEL_ENTRIES.map(({ panel: p, label }) => ({ + label, + checked: panel === p, + run: () => onPanelChange(panel === p ? null : p), + })); + if (!extractor) return items; + return [ + ...items, + { kind: "separator" }, + { + label: "Data Extractor", + submenu: EXPORT_FORMATS.map((f) => ({ + label: f.label, + checked: f.id === extractor.value, + run: () => extractor.onChange(f.id), + })), + }, + { + label: "Export Table to Clipboard", + icon: "copy", + run: () => + void Promise.resolve(extractor.table()).then( + (data) => data && writeClipboardText(exportData(extractor.value, data)), + ), + }, + ]; +} + /** The Record / Value / Aggregates panel's toggle (IDEA's "Show Value Editor"). */ export function PanelToggle({ panel, onChange }: { panel: DataPanel | null; onChange: (p: DataPanel | null) => void }) { return ( @@ -117,6 +167,8 @@ export interface DataViewProps extends Pick< onPanelChange: (panel: DataPanel | null) => void; /** Plain ⌘C's copy format (the toolbar's extractor; TSV by default). */ copyFormat?: CopyFormat; + /** The toolbar's extractor, also offered in the grid's context menu with the whole table to copy. */ + extractor?: ExtractorMenu; /** The search bar is shown (the toolbar's search button, ⌘F in the grid). */ searchOpen?: boolean; onSearchOpenChange?: (open: boolean) => void; @@ -248,7 +300,11 @@ export function DataView(props: DataViewProps) { driver={props.driver} sort={sort} onSort={onSort} - rowMenu={props.rowMenu && ((rows) => props.rowMenu!(rows.map(at)))} + rowMenu={(rows) => { + const own = props.rowMenu?.(rows.map(at)) ?? []; + const view = viewMenu(panel, onPanelChange, props.extractor); + return own.length ? [...own, { kind: "separator" }, ...view] : view; + }} rowNumber={(r) => (props.firstRowNumber ?? 1) + at(r)} copyFormat={props.copyFormat} onSelectionChange={setRange} diff --git a/src/features/database/Grid.test.tsx b/src/features/database/Grid.test.tsx index 4d2aee989..92765f14e 100644 --- a/src/features/database/Grid.test.tsx +++ b/src/features/database/Grid.test.tsx @@ -1013,3 +1013,75 @@ describe("local filter (#6022)", () => { expect(cities()).toEqual(["Shanghai", "Beijing", "Shenzhen", "Hangzhou"]); }); }); + +describe("the data view's context menu (DataGrip)", () => { + const columns = [ + { name: "id", type: "int" }, + { name: "city", type: "text" }, + ]; + const data = [ + ["1", "Shanghai"], + ["2", "Beijing"], + ]; + const value = (r: number, c: number) => data[r]?.[c] ?? null; + + it("toggles the side panels, switches the extractor and exports the whole table to the clipboard", async () => { + const writeText = vi.fn(() => Promise.resolve()); + Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } }); + const onPanelChange = vi.fn(); + const onExtractor = vi.fn(); + const table = () => ({ columns, rows: data, table: "cities", driver: "postgres" as const }); + const { rerender } = render( + <> + + + , + ); + const open = () => { + fireEvent.mouseDown(screen.getAllByRole("gridcell")[0]!, { button: 0 }); + fireEvent.contextMenu(screen.getByRole("grid"), { clientX: 5, clientY: 5 }); + }; + open(); + expect(screen.getByRole("menuitemcheckbox", { name: /Open in Value Editor/ })).toHaveAttribute( + "aria-checked", + "true", + ); + fireEvent.click(screen.getByRole("menuitemcheckbox", { name: /Show Record View/ })); + expect(onPanelChange).toHaveBeenLastCalledWith("record"); + open(); + // The shown panel's entry hides it. + fireEvent.click(screen.getByRole("menuitemcheckbox", { name: /Open in Value Editor/ })); + expect(onPanelChange).toHaveBeenLastCalledWith(null); + + open(); + await act(async () => void fireEvent.click(screen.getByRole("menuitem", { name: /Export Table to Clipboard/ }))); + expect(writeText).toHaveBeenCalledWith(exportData("csv", table())); + + open(); + fireEvent.click(screen.getByRole("menuitem", { name: /Data Extractor/ })); + expect(screen.getByRole("menuitemcheckbox", { name: /^CSV/ })).toHaveAttribute("aria-checked", "true"); + fireEvent.click(screen.getByRole("menuitemcheckbox", { name: /SQL Inserts/ })); + expect(onExtractor).toHaveBeenCalledWith("sql"); + + // Without an extractor (a view with no toolbar one) only the panels are offered. + rerender( + <> + + + , + ); + open(); + expect(screen.queryByRole("menuitem", { name: /Export Table to Clipboard/ })).toBeNull(); + expect(screen.getByRole("menuitemcheckbox", { name: /Show Aggregate View/ })).toHaveAttribute( + "aria-checked", + "false", + ); + }); +}); diff --git a/src/features/database/ResultsView.tsx b/src/features/database/ResultsView.tsx index e352294f1..cecb86213 100644 --- a/src/features/database/ResultsView.tsx +++ b/src/features/database/ResultsView.tsx @@ -453,9 +453,9 @@ export function Result({ tab, onClose }: { tab: ResultTab; onClose?: () => void } }; - const showExport = async (at: { x: number; y: number }) => { - if (!r) return; - // What is exported or copied is the whole values, not the starts the page carries. + /** The result to export or copy: the whole values, not the starts the page carries. */ + const exportSource = async (): Promise => { + if (!r) return null; if (hasCutCells(r)) { try { await loadPage(tab); @@ -466,17 +466,14 @@ export function Result({ tab, onClose }: { tab: ResultTab; onClose?: () => void body: e instanceof Error ? e.message : String(e), key: "db-whole-value", }); - return; + return null; } } - showContextMenu( - at, - exportMenu( - { columns, rows: rowsOf(r), driver, table: queryTable(text, { driver }) }, - queryTable(text, { driver }), - extractor, - ), - ); + return { columns, rows: rowsOf(r), driver, table: queryTable(text, { driver }) }; + }; + const showExport = async (at: { x: number; y: number }) => { + const data = await exportSource(); + if (data) showContextMenu(at, exportMenu(data, queryTable(text, { driver }), extractor)); }; // Submit / Revert / Go to Column (⌘↩ / ⌥⌘Z / ⌘F12) reach the latest state through the handle, as in the table editor. @@ -635,6 +632,7 @@ export function Result({ tab, onClose }: { tab: ResultTab; onClose?: () => void panel={panel} onPanelChange={setPanel} copyFormat={copyFormatOf(extractor)} + extractor={{ value: extractor, onChange: setExtractor, table: exportSource }} searchOpen={searchOpen} onSearchOpenChange={setSearchOpen} firstRowNumber={(tab.page ?? 0) * (tab.pageSize ?? 0) + 1} diff --git a/src/features/database/TableEditor.tsx b/src/features/database/TableEditor.tsx index 2189cc033..838a14bf4 100644 --- a/src/features/database/TableEditor.tsx +++ b/src/features/database/TableEditor.tsx @@ -440,6 +440,16 @@ export function TableEditor({ input }: { input: EditorInput }) { panel={panel} onPanelChange={setPanel} copyFormat={copyFormatOf(extractor)} + extractor={{ + value: extractor, + onChange: setExtractor, + table: () => ({ + columns: gridColumns, + rows: page.result.rows, + table: ref.table, + driver: sourceById(ref.id)?.driver ?? "sqlite", + }), + }} searchOpen={searchOpen} onSearchOpenChange={setSearchOpen} firstRowNumber={offset + 1} diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 055608e41..739dd43d9 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -4148,5 +4148,10 @@ "Semicolons": "Semicolons", "Quotes": "Quotes", "For the strings and imports the TypeScript server adds.": "For the strings and imports the TypeScript server adds.", - "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones.": "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones." + "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones.": "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones.", + "Show Record View": "Show Record View", + "Open in Value Editor": "Open in Value Editor", + "Show Aggregate View": "Show Aggregate View", + "Data Extractor": "Data Extractor", + "Export Table to Clipboard": "Export Table to Clipboard" } diff --git a/src/platform/locales/text/ru.json b/src/platform/locales/text/ru.json index 2300d2131..8dba0c9a0 100644 --- a/src/platform/locales/text/ru.json +++ b/src/platform/locales/text/ru.json @@ -10163,5 +10163,10 @@ "Allowed command pattern": "Шаблон разрешённой команды", "Blocked command pattern": "Шаблон заблокированной команды", "Mode applies to": "Режим действует для", - "Allow all optional permissions": "Разрешить все необязательные разрешения" + "Allow all optional permissions": "Разрешить все необязательные разрешения", + "Show Record View": "Показать представление записи", + "Open in Value Editor": "Открыть в редакторе значений", + "Show Aggregate View": "Показать агрегаты", + "Data Extractor": "Экстрактор данных", + "Export Table to Clipboard": "Экспортировать таблицу в буфер обмена" } diff --git a/src/platform/locales/text/tr.json b/src/platform/locales/text/tr.json index ac11dc136..0948043be 100644 --- a/src/platform/locales/text/tr.json +++ b/src/platform/locales/text/tr.json @@ -8824,5 +8824,10 @@ "Allowed command pattern": "İzin verilen komut kalıbı", "Blocked command pattern": "Engellenen komut kalıbı", "Mode applies to": "Modun uygulandığı yer", - "Allow all optional permissions": "Tüm isteğe bağlı izinlere izin ver" + "Allow all optional permissions": "Tüm isteğe bağlı izinlere izin ver", + "Show Record View": "Kayıt Görünümünü Göster", + "Open in Value Editor": "Değer Düzenleyicide Aç", + "Show Aggregate View": "Toplam Görünümünü Göster", + "Data Extractor": "Veri Çıkarıcı", + "Export Table to Clipboard": "Tabloyu Panoya Aktar" } diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 1ef135586..bec37c238 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -9362,5 +9362,10 @@ "Allowed command pattern": "允許的命令模式", "Blocked command pattern": "封鎖的命令模式", "Mode applies to": "模式作用範圍", - "Allow all optional permissions": "允許全部可選權限" + "Allow all optional permissions": "允許全部可選權限", + "Show Record View": "顯示記錄檢視", + "Open in Value Editor": "在值編輯器中開啟", + "Show Aggregate View": "顯示彙總檢視", + "Data Extractor": "資料擷取器", + "Export Table to Clipboard": "將表格匯出到剪貼簿" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index ab7472554..b47abbc63 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -10592,5 +10592,10 @@ "Semicolons": "分号", "Quotes": "引号", "For the strings and imports the TypeScript server adds.": "用于 TypeScript 服务器添加的字符串和导入。", - "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones.": "TypeScript 服务器将空格和标点设置用于重新格式化代码,以及它添加的代码:快速修复、导入、重构和优化导入。位于 Prettier、Biome 或 oxfmt 配置之下的文件由该工具格式化。.editorconfig 的 quote_type 和 ij_* 属性优先。TSX 文件使用 TypeScript 设置,JSX 文件使用 JavaScript 设置。" + "The TypeScript server applies Spaces and Punctuation to Reformat Code and to the code it adds: quick fixes, imports, refactorings and organize imports. Files below a Prettier, Biome or oxfmt configuration are formatted by that tool. A .editorconfig's quote_type and ij_* properties come first. TSX files use the TypeScript settings, JSX files the JavaScript ones.": "TypeScript 服务器将空格和标点设置用于重新格式化代码,以及它添加的代码:快速修复、导入、重构和优化导入。位于 Prettier、Biome 或 oxfmt 配置之下的文件由该工具格式化。.editorconfig 的 quote_type 和 ij_* 属性优先。TSX 文件使用 TypeScript 设置,JSX 文件使用 JavaScript 设置。", + "Show Record View": "显示记录视图", + "Open in Value Editor": "在值编辑器中打开", + "Show Aggregate View": "显示聚合视图", + "Data Extractor": "数据提取器", + "Export Table to Clipboard": "将表导出到剪贴板" }