@adecore/database 0.0.1 → 0.16.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -1
- package/dist/DatabaseProvider.d.ts +13 -0
- package/dist/DatabaseProvider.js +17 -0
- package/dist/actions.d.ts +40 -0
- package/dist/actions.js +1 -0
- package/dist/client/client.d.ts +5 -0
- package/dist/client/client.js +245 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +2 -0
- package/dist/client/types.d.ts +97 -0
- package/dist/client/types.js +13 -0
- package/dist/client-context.d.ts +16 -0
- package/dist/client-context.js +17 -0
- package/dist/connections/ConnectionForm.d.ts +10 -0
- package/dist/connections/ConnectionForm.js +145 -0
- package/dist/connections/ConnectionManager.d.ts +12 -0
- package/dist/connections/ConnectionManager.js +93 -0
- package/dist/connections/EngineIcon.d.ts +9 -0
- package/dist/connections/EngineIcon.js +7 -0
- package/dist/connections/connection-config.d.ts +16 -0
- package/dist/connections/connection-config.js +107 -0
- package/dist/connections/docker.d.ts +9 -0
- package/dist/connections/docker.js +38 -0
- package/dist/connections/engine-icons.d.ts +3 -0
- package/dist/connections/engine-icons.js +3 -0
- package/dist/connections/use-docker-containers.d.ts +17 -0
- package/dist/connections/use-docker-containers.js +26 -0
- package/dist/console/DestructiveDialog.d.ts +9 -0
- package/dist/console/DestructiveDialog.js +9 -0
- package/dist/console/HistoryPanel.d.ts +10 -0
- package/dist/console/HistoryPanel.js +15 -0
- package/dist/console/QueryConsole.d.ts +14 -0
- package/dist/console/QueryConsole.js +261 -0
- package/dist/console/SchemaPicker.d.ts +11 -0
- package/dist/console/SchemaPicker.js +8 -0
- package/dist/console/SqlEditor.d.ts +12 -0
- package/dist/console/SqlEditor.js +61 -0
- package/dist/console/StatementResultView.d.ts +18 -0
- package/dist/console/StatementResultView.js +32 -0
- package/dist/console/TransactionControls.d.ts +13 -0
- package/dist/console/TransactionControls.js +12 -0
- package/dist/console/destructive.d.ts +7 -0
- package/dist/console/destructive.js +43 -0
- package/dist/console/editing.d.ts +12 -0
- package/dist/console/editing.js +60 -0
- package/dist/console/history.d.ts +13 -0
- package/dist/console/history.js +43 -0
- package/dist/console/labels.d.ts +1 -0
- package/dist/console/labels.js +6 -0
- package/dist/console/outcome.d.ts +6 -0
- package/dist/console/outcome.js +11 -0
- package/dist/console/schemas.d.ts +2 -0
- package/dist/console/schemas.js +5 -0
- package/dist/console/shortcuts.d.ts +2 -0
- package/dist/console/shortcuts.js +3 -0
- package/dist/console/useConsoleHistory.d.ts +7 -0
- package/dist/console/useConsoleHistory.js +22 -0
- package/dist/ddl/alter-mysql.d.ts +4 -0
- package/dist/ddl/alter-mysql.js +79 -0
- package/dist/ddl/alter-sqlite.d.ts +4 -0
- package/dist/ddl/alter-sqlite.js +80 -0
- package/dist/ddl/alter.d.ts +4 -0
- package/dist/ddl/alter.js +24 -0
- package/dist/ddl/column.d.ts +9 -0
- package/dist/ddl/column.js +62 -0
- package/dist/ddl/create.d.ts +12 -0
- package/dist/ddl/create.js +74 -0
- package/dist/ddl/dialect.d.ts +12 -0
- package/dist/ddl/dialect.js +34 -0
- package/dist/ddl/diff.d.ts +34 -0
- package/dist/ddl/diff.js +111 -0
- package/dist/ddl/draft.d.ts +46 -0
- package/dist/ddl/draft.js +70 -0
- package/dist/ddl/index.d.ts +10 -0
- package/dist/ddl/index.js +10 -0
- package/dist/ddl/parse.d.ts +17 -0
- package/dist/ddl/parse.js +131 -0
- package/dist/ddl/statements.d.ts +6 -0
- package/dist/ddl/statements.js +12 -0
- package/dist/ddl/types.d.ts +6 -0
- package/dist/ddl/types.js +52 -0
- package/dist/ddl/validate.d.ts +7 -0
- package/dist/ddl/validate.js +38 -0
- package/dist/designer/ColumnsEditor.d.ts +2 -0
- package/dist/designer/ColumnsEditor.js +24 -0
- package/dist/designer/DesignerView.d.ts +25 -0
- package/dist/designer/DesignerView.js +33 -0
- package/dist/designer/ForeignKeysEditor.d.ts +7 -0
- package/dist/designer/ForeignKeysEditor.js +24 -0
- package/dist/designer/IndexesEditor.d.ts +2 -0
- package/dist/designer/IndexesEditor.js +13 -0
- package/dist/designer/NamePicker.d.ts +11 -0
- package/dist/designer/NamePicker.js +13 -0
- package/dist/designer/OptionsEditor.d.ts +2 -0
- package/dist/designer/OptionsEditor.js +17 -0
- package/dist/designer/SqlPreview.d.ts +12 -0
- package/dist/designer/SqlPreview.js +17 -0
- package/dist/designer/TableDesigner.d.ts +10 -0
- package/dist/designer/TableDesigner.js +103 -0
- package/dist/designer/edit.d.ts +17 -0
- package/dist/designer/edit.js +115 -0
- package/dist/designer/editor-props.d.ts +10 -0
- package/dist/designer/editor-props.js +1 -0
- package/dist/designer/plan.d.ts +9 -0
- package/dist/designer/plan.js +13 -0
- package/dist/designer/recover.d.ts +2 -0
- package/dist/designer/recover.js +18 -0
- package/dist/designer/useReferenceColumns.d.ts +3 -0
- package/dist/designer/useReferenceColumns.js +36 -0
- package/dist/explorer/DatabaseExplorer.d.ts +13 -0
- package/dist/explorer/DatabaseExplorer.js +264 -0
- package/dist/explorer/RowMenu.d.ts +11 -0
- package/dist/explorer/RowMenu.js +89 -0
- package/dist/explorer/TableDialog.d.ts +15 -0
- package/dist/explorer/TableDialog.js +50 -0
- package/dist/explorer/expansion.d.ts +7 -0
- package/dist/explorer/expansion.js +31 -0
- package/dist/explorer/table-statements.d.ts +5 -0
- package/dist/explorer/table-statements.js +12 -0
- package/dist/explorer/tree.d.ts +125 -0
- package/dist/explorer/tree.js +295 -0
- package/dist/explorer/use-explorer-loads.d.ts +12 -0
- package/dist/explorer/use-explorer-loads.js +109 -0
- package/dist/explorer/use-stored-expansion.d.ts +9 -0
- package/dist/explorer/use-stored-expansion.js +47 -0
- package/dist/grid/CellEditor.d.ts +9 -0
- package/dist/grid/CellEditor.js +29 -0
- package/dist/grid/CopyAsMenu.d.ts +7 -0
- package/dist/grid/CopyAsMenu.js +10 -0
- package/dist/grid/DataGrid.d.ts +36 -0
- package/dist/grid/DataGrid.js +512 -0
- package/dist/grid/EnumPicker.d.ts +17 -0
- package/dist/grid/EnumPicker.js +53 -0
- package/dist/grid/GridHeaderCell.d.ts +36 -0
- package/dist/grid/GridHeaderCell.js +69 -0
- package/dist/grid/RecordView.d.ts +20 -0
- package/dist/grid/RecordView.js +126 -0
- package/dist/grid/ValueDock.d.ts +11 -0
- package/dist/grid/ValueDock.js +28 -0
- package/dist/grid/aggregates.d.ts +22 -0
- package/dist/grid/aggregates.js +62 -0
- package/dist/grid/cell-range.d.ts +14 -0
- package/dist/grid/cell-range.js +11 -0
- package/dist/grid/column-layout.d.ts +18 -0
- package/dist/grid/column-layout.js +49 -0
- package/dist/grid/column-selection.d.ts +11 -0
- package/dist/grid/column-selection.js +28 -0
- package/dist/grid/column-view.d.ts +10 -0
- package/dist/grid/column-view.js +23 -0
- package/dist/grid/copy-formats.d.ts +15 -0
- package/dist/grid/copy-formats.js +80 -0
- package/dist/grid/display.d.ts +20 -0
- package/dist/grid/display.js +81 -0
- package/dist/grid/edit-value.d.ts +5 -0
- package/dist/grid/edit-value.js +55 -0
- package/dist/grid/enum-type.d.ts +7 -0
- package/dist/grid/enum-type.js +52 -0
- package/dist/grid/focused-value.d.ts +4 -0
- package/dist/grid/focused-value.js +19 -0
- package/dist/grid/layout.d.ts +22 -0
- package/dist/grid/layout.js +74 -0
- package/dist/grid/navigation.d.ts +15 -0
- package/dist/grid/navigation.js +52 -0
- package/dist/grid/row-selection.d.ts +9 -0
- package/dist/grid/row-selection.js +24 -0
- package/dist/grid/sort.d.ts +7 -0
- package/dist/grid/sort.js +7 -0
- package/dist/grid/types.d.ts +30 -0
- package/dist/grid/types.js +9 -0
- package/dist/host/helper-path.d.ts +7 -0
- package/dist/host/helper-path.js +15 -0
- package/dist/host/host.d.ts +2 -0
- package/dist/host/host.js +343 -0
- package/dist/host/index.d.ts +5 -0
- package/dist/host/index.js +4 -0
- package/dist/host/spawn.d.ts +7 -0
- package/dist/host/spawn.js +98 -0
- package/dist/host/types.d.ts +19 -0
- package/dist/host/types.js +1 -0
- package/dist/host/validate.d.ts +10 -0
- package/dist/host/validate.js +339 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +13 -0
- package/dist/locales/en.json +577 -0
- package/dist/locales/nl.json +577 -0
- package/dist/locales.d.ts +4 -0
- package/dist/locales.js +14 -0
- package/dist/protocol/connection.d.ts +56 -0
- package/dist/protocol/connection.js +1 -0
- package/dist/protocol/errors.d.ts +7 -0
- package/dist/protocol/errors.js +1 -0
- package/dist/protocol/index.d.ts +5 -0
- package/dist/protocol/index.js +3 -0
- package/dist/protocol/messages.d.ts +250 -0
- package/dist/protocol/messages.js +2 -0
- package/dist/protocol/schema.d.ts +53 -0
- package/dist/protocol/schema.js +1 -0
- package/dist/protocol/values.d.ts +21 -0
- package/dist/protocol/values.js +15 -0
- package/dist/sql-split.d.ts +15 -0
- package/dist/sql-split.js +201 -0
- package/dist/sql.d.ts +29 -0
- package/dist/sql.js +104 -0
- package/dist/structure/StructureView.d.ts +14 -0
- package/dist/structure/StructureView.js +99 -0
- package/dist/structure/structure-text.d.ts +2 -0
- package/dist/structure/structure-text.js +2 -0
- package/dist/table/CommandField.d.ts +14 -0
- package/dist/table/CommandField.js +157 -0
- package/dist/table/ImportDialog.d.ts +27 -0
- package/dist/table/ImportDialog.js +28 -0
- package/dist/table/TableStatusBar.d.ts +19 -0
- package/dist/table/TableStatusBar.js +22 -0
- package/dist/table/TableToolbar.d.ts +43 -0
- package/dist/table/TableToolbar.js +16 -0
- package/dist/table/TableView.d.ts +13 -0
- package/dist/table/TableView.js +508 -0
- package/dist/table/command-field.d.ts +57 -0
- package/dist/table/command-field.js +203 -0
- package/dist/table/grid-rows.d.ts +23 -0
- package/dist/table/grid-rows.js +55 -0
- package/dist/table/import-mapping.d.ts +7 -0
- package/dist/table/import-mapping.js +28 -0
- package/dist/table/layout-store.d.ts +13 -0
- package/dist/table/layout-store.js +43 -0
- package/dist/table/paging.d.ts +12 -0
- package/dist/table/paging.js +20 -0
- package/dist/table/pending.d.ts +32 -0
- package/dist/table/pending.js +94 -0
- package/dist/table/references.d.ts +12 -0
- package/dist/table/references.js +21 -0
- package/dist/table/useLoaded.d.ts +7 -0
- package/dist/table/useLoaded.js +36 -0
- package/dist/table/useStoredLayout.d.ts +8 -0
- package/dist/table/useStoredLayout.js +39 -0
- package/dist/table/useTableTransfer.d.ts +51 -0
- package/dist/table/useTableTransfer.js +125 -0
- package/dist/testing/fake.d.ts +21 -0
- package/dist/testing/fake.js +372 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +1 -0
- package/dist/use-popup-press.d.ts +1 -0
- package/dist/use-popup-press.js +24 -0
- package/dist/use-schema-change.d.ts +1 -0
- package/dist/use-schema-change.js +13 -0
- package/dist/use-stable-callback.d.ts +1 -0
- package/dist/use-stable-callback.js +9 -0
- package/dist/value/ValuePanel.d.ts +14 -0
- package/dist/value/ValuePanel.js +103 -0
- package/dist/value/draft.d.ts +33 -0
- package/dist/value/draft.js +44 -0
- package/dist/value/hex.d.ts +14 -0
- package/dist/value/hex.js +46 -0
- package/dist/value/json.d.ts +9 -0
- package/dist/value/json.js +71 -0
- package/dist/value/views.d.ts +22 -0
- package/dist/value/views.js +54 -0
- package/dist/workbench/DatabaseWorkbench.d.ts +9 -0
- package/dist/workbench/DatabaseWorkbench.js +179 -0
- package/dist/workbench/DesignerTab.d.ts +10 -0
- package/dist/workbench/DesignerTab.js +22 -0
- package/dist/workbench/TableTab.d.ts +10 -0
- package/dist/workbench/TableTab.js +8 -0
- package/dist/workbench/tabs.d.ts +42 -0
- package/dist/workbench/tabs.js +139 -0
- package/package.json +79 -3
- package/src/DatabaseProvider.tsx +37 -0
- package/src/actions.ts +34 -0
- package/src/client/client.ts +324 -0
- package/src/client/index.ts +18 -0
- package/src/client/types.ts +161 -0
- package/src/client-context.ts +35 -0
- package/src/connections/ConnectionForm.tsx +474 -0
- package/src/connections/ConnectionManager.tsx +224 -0
- package/src/connections/EngineIcon.tsx +18 -0
- package/src/connections/connection-config.ts +143 -0
- package/src/connections/docker.ts +50 -0
- package/src/connections/engine-icons.ts +5 -0
- package/src/connections/use-docker-containers.ts +41 -0
- package/src/console/DestructiveDialog.tsx +40 -0
- package/src/console/HistoryPanel.tsx +67 -0
- package/src/console/QueryConsole.tsx +439 -0
- package/src/console/SchemaPicker.tsx +32 -0
- package/src/console/SqlEditor.tsx +95 -0
- package/src/console/StatementResultView.tsx +176 -0
- package/src/console/TransactionControls.tsx +53 -0
- package/src/console/destructive.ts +60 -0
- package/src/console/editing.ts +77 -0
- package/src/console/history.ts +62 -0
- package/src/console/labels.ts +7 -0
- package/src/console/outcome.ts +14 -0
- package/src/console/schemas.ts +7 -0
- package/src/console/shortcuts.ts +4 -0
- package/src/console/useConsoleHistory.ts +39 -0
- package/src/ddl/alter-mysql.ts +90 -0
- package/src/ddl/alter-sqlite.ts +93 -0
- package/src/ddl/alter.ts +29 -0
- package/src/ddl/column.ts +74 -0
- package/src/ddl/create.ts +92 -0
- package/src/ddl/dialect.ts +53 -0
- package/src/ddl/diff.ts +164 -0
- package/src/ddl/draft.ts +136 -0
- package/src/ddl/index.ts +10 -0
- package/src/ddl/parse.ts +161 -0
- package/src/ddl/statements.ts +20 -0
- package/src/ddl/types.ts +60 -0
- package/src/ddl/validate.ts +47 -0
- package/src/designer/ColumnsEditor.tsx +204 -0
- package/src/designer/DesignerView.tsx +184 -0
- package/src/designer/ForeignKeysEditor.tsx +132 -0
- package/src/designer/IndexesEditor.tsx +63 -0
- package/src/designer/NamePicker.tsx +44 -0
- package/src/designer/OptionsEditor.tsx +101 -0
- package/src/designer/SqlPreview.tsx +56 -0
- package/src/designer/TableDesigner.tsx +186 -0
- package/src/designer/edit.ts +137 -0
- package/src/designer/editor-props.ts +13 -0
- package/src/designer/plan.ts +25 -0
- package/src/designer/recover.ts +20 -0
- package/src/designer/useReferenceColumns.ts +54 -0
- package/src/explorer/DatabaseExplorer.tsx +450 -0
- package/src/explorer/RowMenu.tsx +134 -0
- package/src/explorer/TableDialog.tsx +80 -0
- package/src/explorer/expansion.ts +47 -0
- package/src/explorer/table-statements.ts +18 -0
- package/src/explorer/tree.ts +453 -0
- package/src/explorer/use-explorer-loads.ts +157 -0
- package/src/explorer/use-stored-expansion.ts +66 -0
- package/src/grid/CellEditor.tsx +53 -0
- package/src/grid/CopyAsMenu.tsx +33 -0
- package/src/grid/DataGrid.tsx +871 -0
- package/src/grid/EnumPicker.tsx +108 -0
- package/src/grid/GridHeaderCell.tsx +243 -0
- package/src/grid/RecordView.tsx +293 -0
- package/src/grid/ValueDock.tsx +52 -0
- package/src/grid/aggregates.ts +94 -0
- package/src/grid/cell-range.ts +26 -0
- package/src/grid/column-layout.ts +71 -0
- package/src/grid/column-selection.ts +42 -0
- package/src/grid/column-view.ts +35 -0
- package/src/grid/copy-formats.ts +108 -0
- package/src/grid/display.ts +109 -0
- package/src/grid/edit-value.ts +60 -0
- package/src/grid/enum-type.ts +62 -0
- package/src/grid/focused-value.ts +23 -0
- package/src/grid/layout.ts +91 -0
- package/src/grid/navigation.ts +74 -0
- package/src/grid/row-selection.ts +35 -0
- package/src/grid/sort.ts +12 -0
- package/src/grid/types.ts +59 -0
- package/src/host/helper-path.ts +41 -0
- package/src/host/host.ts +456 -0
- package/src/host/index.ts +5 -0
- package/src/host/spawn.ts +124 -0
- package/src/host/types.ts +46 -0
- package/src/host/validate.ts +402 -0
- package/src/index.ts +37 -0
- package/src/locales/en.json +577 -0
- package/src/locales/nl.json +577 -0
- package/src/locales.ts +18 -0
- package/src/protocol/connection.ts +86 -0
- package/src/protocol/errors.ts +38 -0
- package/src/protocol/index.ts +33 -0
- package/src/protocol/messages.ts +168 -0
- package/src/protocol/schema.ts +76 -0
- package/src/protocol/values.ts +52 -0
- package/src/sql-split.ts +220 -0
- package/src/sql.ts +152 -0
- package/src/structure/StructureView.tsx +269 -0
- package/src/structure/structure-text.ts +4 -0
- package/src/table/CommandField.tsx +318 -0
- package/src/table/ImportDialog.tsx +168 -0
- package/src/table/TableStatusBar.tsx +91 -0
- package/src/table/TableToolbar.tsx +217 -0
- package/src/table/TableView.tsx +880 -0
- package/src/table/command-field.ts +285 -0
- package/src/table/grid-rows.ts +80 -0
- package/src/table/import-mapping.ts +37 -0
- package/src/table/layout-store.ts +62 -0
- package/src/table/paging.ts +33 -0
- package/src/table/pending.ts +143 -0
- package/src/table/references.ts +38 -0
- package/src/table/useLoaded.ts +58 -0
- package/src/table/useStoredLayout.ts +53 -0
- package/src/table/useTableTransfer.ts +187 -0
- package/src/testing/fake.ts +533 -0
- package/src/testing/index.ts +1 -0
- package/src/use-popup-press.ts +27 -0
- package/src/use-schema-change.ts +19 -0
- package/src/use-stable-callback.ts +12 -0
- package/src/value/ValuePanel.tsx +254 -0
- package/src/value/draft.ts +72 -0
- package/src/value/hex.ts +62 -0
- package/src/value/json.ts +74 -0
- package/src/value/views.ts +68 -0
- package/src/workbench/DatabaseWorkbench.tsx +348 -0
- package/src/workbench/DesignerTab.tsx +37 -0
- package/src/workbench/TableTab.tsx +27 -0
- package/src/workbench/tabs.ts +184 -0
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import clsx from 'clsx';
|
|
4
|
+
import { ArrowUpRight, Ban, Copy, RotateCcw } from 'lucide-react';
|
|
5
|
+
import { useTranslation } from 'react-i18next';
|
|
6
|
+
import { ContextMenu, copyText, EDIT_SHORTCUTS, Icon, IconButton, isApplePlatform, isModHeld, Kbd, Spinner, useContentSize } from '@adecore/ui';
|
|
7
|
+
import { formatNumber } from '@adecore/ui/format';
|
|
8
|
+
import { fullRect, rectBetween, rectContains } from './cell-range.js';
|
|
9
|
+
import { CellEditor } from './CellEditor.js';
|
|
10
|
+
import { captureLayout, restoreLayout, sameLayout } from './column-layout.js';
|
|
11
|
+
import { NO_COLUMN_SELECTION, selectColumn, shownSelection } from './column-selection.js';
|
|
12
|
+
import { displayOrder, hideColumn, pinnedCount, showAllColumns, togglePin } from './column-view.js';
|
|
13
|
+
import { COPY_FORMATS, formatCopy } from './copy-formats.js';
|
|
14
|
+
import { CopyAsMenu } from './CopyAsMenu.js';
|
|
15
|
+
import { cellView, copyTextOf, isPreview } from './display.js';
|
|
16
|
+
import { draftOf, parseDraft } from './edit-value.js';
|
|
17
|
+
import { parseEnumType } from './enum-type.js';
|
|
18
|
+
import { EnumPicker } from './EnumPicker.js';
|
|
19
|
+
import { GridHeaderCell } from './GridHeaderCell.js';
|
|
20
|
+
import { clampColumnWidth, columnOffsets, estimateColumnWidth, fitColumnWidth, FALLBACK_VIEWPORT_HEIGHT, growWidths, gutterWidth, HEADER_HEIGHT, OVERSCAN_ROWS, ROW_HEIGHT, scrollToReveal, visibleRange } from './layout.js';
|
|
21
|
+
import { moveFocus } from './navigation.js';
|
|
22
|
+
import { selectRow } from './row-selection.js';
|
|
23
|
+
import { sortOnly, sortStateOf } from './sort.js';
|
|
24
|
+
import { useNumberNotation } from '../client-context.js';
|
|
25
|
+
import { usePopupPress } from '../use-popup-press.js';
|
|
26
|
+
const NO_KEYS = new Set();
|
|
27
|
+
const NO_SORTS = [];
|
|
28
|
+
const signatureOf = (columns) => columns.map((column) => column.name).join('\u0000');
|
|
29
|
+
const textOf = (view) => (view.suffix === undefined ? view.text : `${view.text} ${view.suffix}`);
|
|
30
|
+
const estimateWidths = (columns, rows, notation) => columns.map((column, index) => estimateColumnWidth(column.name, rows.slice(0, 50).map((row) => textOf(cellView(row.cells[index] ?? null, column.kind, notation))), column.primaryKey === true));
|
|
31
|
+
const TONE = {
|
|
32
|
+
value: '',
|
|
33
|
+
null: 'text-text-faint',
|
|
34
|
+
default: 'text-text-faint italic'
|
|
35
|
+
};
|
|
36
|
+
const ROW_STATE = {
|
|
37
|
+
inserted: 'bg-positive/10',
|
|
38
|
+
deleted: 'bg-status-error/10 text-text-faint'
|
|
39
|
+
};
|
|
40
|
+
/*
|
|
41
|
+
* The table of the table view and of the console. Only the rows in view are drawn, at a fixed
|
|
42
|
+
* height, inside a box as tall as all of them; the header and the row-number gutter stick to the edges
|
|
43
|
+
* of the scroll box. Focus stays on that box and points at the cell with `aria-activedescendant`, so
|
|
44
|
+
* a cell scrolled out of the window loses nothing.
|
|
45
|
+
*/
|
|
46
|
+
export function DataGrid({ columns, rows, label, editable = false, selectedKeys, onSelectedKeysChange, onCommit, loadValue, sorts, onSortsChange, onFocusedCellChange, onDeleteSelected, sqlTarget, queryMenu, menu, onDuplicateRows, onFollow, canFollow, onRangeChange, initialLayout, onLayoutChange, focusRequest, columnRequest, empty, className, ref }) {
|
|
47
|
+
const { t } = useTranslation('database');
|
|
48
|
+
const gridId = useId();
|
|
49
|
+
const scroller = useRef(null);
|
|
50
|
+
const anchor = useRef(-1);
|
|
51
|
+
const pressedInPopup = usePopupPress();
|
|
52
|
+
const notation = useNumberNotation();
|
|
53
|
+
/* Set once an edit commits or cancels, so the blur that follows the input's removal does not commit it again. */
|
|
54
|
+
const settled = useRef(true);
|
|
55
|
+
const dragging = useRef(false);
|
|
56
|
+
const dragOrigin = useRef(null);
|
|
57
|
+
const reportFocus = useRef(onFocusedCellChange);
|
|
58
|
+
const reportRange = useRef(onRangeChange);
|
|
59
|
+
const reportLayout = useRef(onLayoutChange);
|
|
60
|
+
const [measure, size] = useContentSize();
|
|
61
|
+
const [scrollTop, setScrollTop] = useState(0);
|
|
62
|
+
const [focused, setFocused] = useState(null);
|
|
63
|
+
const [rangeAnchor, setRangeAnchor] = useState(null);
|
|
64
|
+
const [columnSelection, setColumnSelection] = useState(NO_COLUMN_SELECTION);
|
|
65
|
+
const [menuTarget, setMenuTarget] = useState(null);
|
|
66
|
+
const [editing, setEditing] = useState(null);
|
|
67
|
+
const [ownSelection, setOwnSelection] = useState(NO_KEYS);
|
|
68
|
+
const [layout, setLayout] = useState(() => ({
|
|
69
|
+
signature: signatureOf(columns),
|
|
70
|
+
rows,
|
|
71
|
+
notation,
|
|
72
|
+
estimated: estimateWidths(columns, rows, notation),
|
|
73
|
+
...restoreLayout(columns, initialLayout)
|
|
74
|
+
}));
|
|
75
|
+
const reportedLayout = useRef(captureLayout(columns, layout));
|
|
76
|
+
const signature = signatureOf(columns);
|
|
77
|
+
let current = layout;
|
|
78
|
+
if (layout.signature !== signature || layout.rows !== rows || layout.notation !== notation) {
|
|
79
|
+
const same = layout.signature === signature;
|
|
80
|
+
const estimated = estimateWidths(columns, rows, notation);
|
|
81
|
+
current = {
|
|
82
|
+
signature,
|
|
83
|
+
rows,
|
|
84
|
+
notation,
|
|
85
|
+
estimated: same ? growWidths(layout.estimated, estimated) : estimated,
|
|
86
|
+
...(same ? { resized: layout.resized, view: layout.view } : restoreLayout(columns, initialLayout))
|
|
87
|
+
};
|
|
88
|
+
setLayout(current);
|
|
89
|
+
}
|
|
90
|
+
const order = displayOrder(columns.length, current.view);
|
|
91
|
+
const pinnedShown = pinnedCount(columns.length, current.view);
|
|
92
|
+
const displayOf = new Map(order.map((index, position) => [index, position]));
|
|
93
|
+
const widths = columns.map((_, index) => current.resized[index] ?? current.estimated[index] ?? clampColumnWidth(0));
|
|
94
|
+
const shownWidths = order.map((index) => widths[index]);
|
|
95
|
+
const offsets = columnOffsets(shownWidths);
|
|
96
|
+
const gutter = gutterWidth(rows.length);
|
|
97
|
+
const totalWidth = gutter + shownWidths.reduce((sum, width) => sum + width, 0);
|
|
98
|
+
const pinnedWidth = shownWidths.slice(0, pinnedShown).reduce((sum, width) => sum + width, 0);
|
|
99
|
+
const viewportHeight = size.height > 0 ? size.height : FALLBACK_VIEWPORT_HEIGHT;
|
|
100
|
+
const range = visibleRange(scrollTop - HEADER_HEIGHT, viewportHeight, ROW_HEIGHT, rows.length, OVERSCAN_ROWS);
|
|
101
|
+
const selection = selectedKeys ?? ownSelection;
|
|
102
|
+
const isShown = (position) => position !== null && position.row < rows.length && displayOf.has(position.column);
|
|
103
|
+
const focusCell = isShown(focused) ? focused : null;
|
|
104
|
+
const anchorCell = isShown(rangeAnchor) ? rangeAnchor : null;
|
|
105
|
+
const toDisplay = (position) => ({ row: position.row, column: displayOf.get(position.column) });
|
|
106
|
+
const fromDisplay = (position) => ({ row: position.row, column: order[position.column] });
|
|
107
|
+
const rect = focusCell === null ? null : rectBetween(toDisplay(anchorCell ?? focusCell), toDisplay(focusCell));
|
|
108
|
+
const page = Math.max(1, Math.floor((viewportHeight - HEADER_HEIGHT) / ROW_HEIGHT));
|
|
109
|
+
const sortable = onSortsChange !== undefined;
|
|
110
|
+
const sortList = sorts ?? NO_SORTS;
|
|
111
|
+
const focusedRowKey = focusCell === null ? null : (rows[focusCell.row]?.key ?? null);
|
|
112
|
+
const focusedColumn = focusCell?.column ?? -1;
|
|
113
|
+
const pickedColumns = shownSelection(columnSelection, order);
|
|
114
|
+
const pickedSet = new Set(pickedColumns);
|
|
115
|
+
const columnBlock = pickedColumns.length > 0 && rows.length > 0 ? { top: 0, bottom: rows.length - 1, columns: pickedColumns } : null;
|
|
116
|
+
const cellBlock = rect === null ? null : { top: rect.top, bottom: rect.bottom, columns: order.slice(rect.left, rect.right + 1) };
|
|
117
|
+
const block = columnBlock ?? cellBlock;
|
|
118
|
+
const blockKey = block === null ? '' : `${block.top}:${block.bottom}:${block.columns.join(',')}`;
|
|
119
|
+
const headerId = (column) => `${gridId}-h-${column}`;
|
|
120
|
+
const enumTypes = useMemo(() => columns.map((column) => parseEnumType(column.type)), [columns]);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
reportFocus.current = onFocusedCellChange;
|
|
123
|
+
});
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
reportRange.current = onRangeChange;
|
|
126
|
+
reportLayout.current = onLayoutChange;
|
|
127
|
+
});
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
const captured = captureLayout(columns, current);
|
|
130
|
+
if (!sameLayout(captured, reportedLayout.current)) {
|
|
131
|
+
reportedLayout.current = captured;
|
|
132
|
+
reportLayout.current?.(captured);
|
|
133
|
+
}
|
|
134
|
+
}, [columns, current]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
reportFocus.current?.(focusedRowKey === null ? null : { rowKey: focusedRowKey, column: focusedColumn });
|
|
137
|
+
}, [focusedRowKey, focusedColumn]);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (blockKey === '') {
|
|
140
|
+
reportRange.current?.(null);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const [top, bottom, list] = blockKey.split(':');
|
|
144
|
+
reportRange.current?.({ top: Number(top), bottom: Number(bottom), columns: list === '' ? [] : list.split(',').map(Number) });
|
|
145
|
+
}, [blockKey]);
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
const index = focusRequest === null || focusRequest === undefined ? -1 : rows.findIndex((row) => row.key === focusRequest.rowKey);
|
|
148
|
+
if (focusRequest !== null && focusRequest !== undefined && index >= 0) {
|
|
149
|
+
focusAt({ row: index, column: focusRequest.column });
|
|
150
|
+
scroller.current?.focus();
|
|
151
|
+
}
|
|
152
|
+
// Only a new request moves the focus; the rows changing under a request already handled must not.
|
|
153
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
154
|
+
}, [focusRequest]);
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (columnRequest !== null && columnRequest !== undefined && displayOf.has(columnRequest.column)) {
|
|
157
|
+
pickColumn(columnRequest.column);
|
|
158
|
+
revealColumn(columnRequest.column);
|
|
159
|
+
scroller.current?.focus();
|
|
160
|
+
}
|
|
161
|
+
// Only a new request moves the pick; the columns changing under a request already handled must not.
|
|
162
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
163
|
+
}, [columnRequest]);
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
const stop = () => {
|
|
166
|
+
dragging.current = false;
|
|
167
|
+
};
|
|
168
|
+
window.addEventListener('pointerup', stop);
|
|
169
|
+
window.addEventListener('pointercancel', stop);
|
|
170
|
+
return () => {
|
|
171
|
+
window.removeEventListener('pointerup', stop);
|
|
172
|
+
window.removeEventListener('pointercancel', stop);
|
|
173
|
+
};
|
|
174
|
+
}, []);
|
|
175
|
+
const attach = useCallback((node) => {
|
|
176
|
+
scroller.current = node;
|
|
177
|
+
measure(node);
|
|
178
|
+
if (typeof ref === 'function') {
|
|
179
|
+
ref(node);
|
|
180
|
+
}
|
|
181
|
+
else if (ref) {
|
|
182
|
+
ref.current = node;
|
|
183
|
+
}
|
|
184
|
+
}, [measure, ref]);
|
|
185
|
+
const cellId = (row, column) => `${gridId}-${row}-${column}`;
|
|
186
|
+
const canEdit = (position) => {
|
|
187
|
+
const row = rows[position.row];
|
|
188
|
+
const column = columns[position.column];
|
|
189
|
+
if (!editable || row === undefined || column === undefined || row.locked === true || row.state === 'deleted' || column.readOnly === true) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
return !isPreview(row.cells[position.column] ?? null) || loadValue !== undefined;
|
|
193
|
+
};
|
|
194
|
+
const revealColumn = (column) => {
|
|
195
|
+
const element = scroller.current;
|
|
196
|
+
const at = displayOf.get(column);
|
|
197
|
+
if (element === null || at === undefined || at < pinnedShown) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const left = scrollToReveal(gutter + (offsets[at] ?? 0), shownWidths[at] ?? 0, element.scrollLeft, element.clientWidth, gutter + pinnedWidth);
|
|
201
|
+
if (left !== element.scrollLeft) {
|
|
202
|
+
element.scrollLeft = left;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const reveal = (position) => {
|
|
206
|
+
const element = scroller.current;
|
|
207
|
+
if (element === null) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const top = scrollToReveal(HEADER_HEIGHT + position.row * ROW_HEIGHT, ROW_HEIGHT, element.scrollTop, element.clientHeight, HEADER_HEIGHT);
|
|
211
|
+
if (top !== element.scrollTop) {
|
|
212
|
+
element.scrollTop = top;
|
|
213
|
+
}
|
|
214
|
+
revealColumn(position.column);
|
|
215
|
+
};
|
|
216
|
+
const pickColumn = (column, click = { shiftKey: false, mod: false }) => {
|
|
217
|
+
setColumnSelection((now) => selectColumn(now, order, column, click));
|
|
218
|
+
setRangeAnchor(null);
|
|
219
|
+
};
|
|
220
|
+
/* With `extend` the range grows from where it started; otherwise the cell stands alone. */
|
|
221
|
+
const focusAt = (position, extend = false) => {
|
|
222
|
+
setColumnSelection(NO_COLUMN_SELECTION);
|
|
223
|
+
setRangeAnchor(extend && focusCell !== null ? (anchorCell ?? focusCell) : null);
|
|
224
|
+
setFocused(position);
|
|
225
|
+
reveal(position);
|
|
226
|
+
};
|
|
227
|
+
const step = (from, input) => {
|
|
228
|
+
const next = moveFocus(isShown(from) ? toDisplay(from) : null, input, { rows: rows.length, columns: order.length, page });
|
|
229
|
+
return next === null ? null : fromDisplay(next);
|
|
230
|
+
};
|
|
231
|
+
const setSelection = (next) => {
|
|
232
|
+
if (selectedKeys === undefined) {
|
|
233
|
+
setOwnSelection(next);
|
|
234
|
+
}
|
|
235
|
+
onSelectedKeysChange?.(next);
|
|
236
|
+
};
|
|
237
|
+
const beginEdit = async (position) => {
|
|
238
|
+
const row = rows[position.row];
|
|
239
|
+
if (row === undefined || !canEdit(position)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const cell = row.cells[position.column] ?? null;
|
|
243
|
+
settled.current = false;
|
|
244
|
+
if (!isPreview(cell)) {
|
|
245
|
+
const text = draftOf(cell);
|
|
246
|
+
setEditing({ ...position, draft: text, initial: text, loading: false });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
setEditing({ ...position, draft: '', initial: '', loading: true });
|
|
250
|
+
const value = await loadValue?.(row.key, position.column);
|
|
251
|
+
setEditing((now) => {
|
|
252
|
+
if (now === null || !now.loading || now.row !== position.row || now.column !== position.column) {
|
|
253
|
+
return now;
|
|
254
|
+
}
|
|
255
|
+
if (value === undefined) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
const text = draftOf(value);
|
|
259
|
+
return { ...now, draft: text, initial: text, loading: false };
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
const finishEdit = (commit, move) => {
|
|
263
|
+
if (editing === null || settled.current) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
settled.current = true;
|
|
267
|
+
const column = columns[editing.column];
|
|
268
|
+
const row = rows[editing.row];
|
|
269
|
+
if (commit && !editing.loading && editing.draft !== editing.initial && column !== undefined && row !== undefined) {
|
|
270
|
+
onCommit?.(row.key, editing.column, parseDraft(editing.draft, column.kind));
|
|
271
|
+
}
|
|
272
|
+
setEditing(null);
|
|
273
|
+
scroller.current?.focus();
|
|
274
|
+
if (move !== undefined) {
|
|
275
|
+
const next = step(editing, { key: 'Tab', shiftKey: move === 'previous', mod: false });
|
|
276
|
+
if (next !== null) {
|
|
277
|
+
focusAt(next);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const finishPick = (result) => {
|
|
282
|
+
if (editing === null || settled.current) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
settled.current = true;
|
|
286
|
+
const row = rows[editing.row];
|
|
287
|
+
const before = row?.cells[editing.column] ?? null;
|
|
288
|
+
if (result !== undefined && row !== undefined && result.value !== (typeof before === 'string' ? before : null)) {
|
|
289
|
+
onCommit?.(row.key, editing.column, result.value);
|
|
290
|
+
}
|
|
291
|
+
setEditing(null);
|
|
292
|
+
scroller.current?.focus();
|
|
293
|
+
};
|
|
294
|
+
const followCell = (cell) => onFollow !== undefined && (canFollow?.(cell) ?? true);
|
|
295
|
+
const commitValue = (value) => {
|
|
296
|
+
const position = menuTarget === null || menuTarget.column === null ? null : { row: menuTarget.row, column: menuTarget.column };
|
|
297
|
+
const row = position === null ? undefined : rows[position.row];
|
|
298
|
+
if (position !== null && row !== undefined && canEdit(position)) {
|
|
299
|
+
onCommit?.(row.key, position.column, value);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
/* The cells of the range or of the picked columns, as the columns and rows a copy writes. */
|
|
303
|
+
const blockOf = () => {
|
|
304
|
+
if (block === null) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
columns: block.columns.map((index) => ({ name: columns[index].name, kind: columns[index].kind })),
|
|
309
|
+
rows: rows.slice(block.top, block.bottom + 1).map((row) => block.columns.map((index) => row.cells[index] ?? null)),
|
|
310
|
+
target: sqlTarget
|
|
311
|
+
};
|
|
312
|
+
};
|
|
313
|
+
const copyAs = (format) => {
|
|
314
|
+
const block = blockOf();
|
|
315
|
+
if (block !== null) {
|
|
316
|
+
copyText(formatCopy(format, block));
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
/* One cell copies as it reads; a block copies as TSV, which a spreadsheet pastes into cells. */
|
|
320
|
+
const copyRange = () => {
|
|
321
|
+
if (block !== null && block.top === block.bottom && block.columns.length === 1) {
|
|
322
|
+
copyText(copyTextOf(rows[block.top]?.cells[block.columns[0]] ?? null));
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
copyAs('tsv');
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const copyCell = () => {
|
|
329
|
+
const row = menuTarget === null ? undefined : rows[menuTarget.row];
|
|
330
|
+
if (row !== undefined && menuTarget?.column != null) {
|
|
331
|
+
copyText(copyTextOf(row.cells[menuTarget.column] ?? null));
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const selectAll = () => {
|
|
335
|
+
const all = fullRect(rows.length, order.length);
|
|
336
|
+
if (all !== null) {
|
|
337
|
+
setColumnSelection(NO_COLUMN_SELECTION);
|
|
338
|
+
setRangeAnchor(fromDisplay({ row: all.top, column: all.left }));
|
|
339
|
+
setFocused(fromDisplay({ row: all.bottom, column: all.right }));
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
const selectRowAt = (index, click) => {
|
|
343
|
+
const result = selectRow(rows.map((row) => row.key), selection, anchor.current, index, click);
|
|
344
|
+
anchor.current = result.anchor;
|
|
345
|
+
setSelection(result.selected);
|
|
346
|
+
if (!click.mod && order.length > 0) {
|
|
347
|
+
setColumnSelection(NO_COLUMN_SELECTION);
|
|
348
|
+
setRangeAnchor({ row: result.anchor, column: order[order.length - 1] });
|
|
349
|
+
setFocused({ row: index, column: order[0] });
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const handleKeyDown = (event) => {
|
|
353
|
+
if (event.target !== event.currentTarget || event.altKey) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (editing !== null) {
|
|
357
|
+
if (event.key === 'Escape') {
|
|
358
|
+
finishEdit(false);
|
|
359
|
+
}
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const mod = isModHeld(event, isApplePlatform());
|
|
363
|
+
const key = event.key.toLowerCase();
|
|
364
|
+
if (mod && key === 'c') {
|
|
365
|
+
event.preventDefault();
|
|
366
|
+
copyRange();
|
|
367
|
+
}
|
|
368
|
+
else if (mod && key === 'a') {
|
|
369
|
+
event.preventDefault();
|
|
370
|
+
selectAll();
|
|
371
|
+
}
|
|
372
|
+
else if (mod && key === 'd' && onDuplicateRows !== undefined) {
|
|
373
|
+
event.preventDefault();
|
|
374
|
+
const keys = selection.size > 0 ? rows.filter((row) => selection.has(row.key)).map((row) => row.key) : focusedRowKey === null ? [] : [focusedRowKey];
|
|
375
|
+
if (keys.length > 0) {
|
|
376
|
+
onDuplicateRows(keys);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
else if ((event.key === 'Enter' || event.key === 'F2') && focusCell !== null) {
|
|
380
|
+
event.preventDefault();
|
|
381
|
+
void beginEdit(focusCell);
|
|
382
|
+
}
|
|
383
|
+
else if (event.key === ' ' && event.shiftKey && focusCell !== null) {
|
|
384
|
+
event.preventDefault();
|
|
385
|
+
selectRowAt(focusCell.row, { shiftKey: false, mod });
|
|
386
|
+
}
|
|
387
|
+
else if ((event.key === 'Delete' || event.key === 'Backspace') && onDeleteSelected !== undefined && selection.size > 0) {
|
|
388
|
+
event.preventDefault();
|
|
389
|
+
onDeleteSelected();
|
|
390
|
+
}
|
|
391
|
+
else if (event.key === 'Escape' && (anchorCell !== null || columnSelection.columns.size > 0)) {
|
|
392
|
+
setRangeAnchor(null);
|
|
393
|
+
setColumnSelection(NO_COLUMN_SELECTION);
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
const next = step(focusCell, { key: event.key, shiftKey: event.shiftKey, mod });
|
|
397
|
+
if (next !== null) {
|
|
398
|
+
event.preventDefault();
|
|
399
|
+
focusAt(next, event.shiftKey && event.key !== 'Tab');
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
const pressCell = (event, position, isEditing) => {
|
|
404
|
+
if (event.button !== 0 || isEditing) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
const row = rows[position.row];
|
|
408
|
+
if (row !== undefined && isModHeld(event, isApplePlatform()) && followCell({ rowKey: row.key, column: position.column })) {
|
|
409
|
+
focusAt(position);
|
|
410
|
+
onFollow?.({ rowKey: row.key, column: position.column });
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const extend = event.shiftKey && focusCell !== null;
|
|
414
|
+
dragOrigin.current = extend ? (anchorCell ?? focusCell) : position;
|
|
415
|
+
dragging.current = true;
|
|
416
|
+
focusAt(position, extend);
|
|
417
|
+
};
|
|
418
|
+
const dragTo = (position) => {
|
|
419
|
+
if (dragging.current && dragOrigin.current !== null) {
|
|
420
|
+
setRangeAnchor(dragOrigin.current);
|
|
421
|
+
setFocused(position);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
const openCellMenu = (position) => {
|
|
425
|
+
if (!rectContains(rect, position.row, displayOf.get(position.column))) {
|
|
426
|
+
focusAt(position);
|
|
427
|
+
}
|
|
428
|
+
setMenuTarget(position);
|
|
429
|
+
};
|
|
430
|
+
const openRowMenu = (index) => {
|
|
431
|
+
const row = rows[index];
|
|
432
|
+
if (row !== undefined && !selection.has(row.key)) {
|
|
433
|
+
selectRowAt(index, { shiftKey: false, mod: false });
|
|
434
|
+
}
|
|
435
|
+
setMenuTarget({ row: index, column: null });
|
|
436
|
+
};
|
|
437
|
+
const resizeColumn = (index, width) => {
|
|
438
|
+
setLayout((now) => ({ ...now, resized: { ...now.resized, [index]: width } }));
|
|
439
|
+
};
|
|
440
|
+
const fitColumn = (index) => {
|
|
441
|
+
const column = columns[index];
|
|
442
|
+
if (column !== undefined) {
|
|
443
|
+
resizeColumn(index, fitColumnWidth(column.name, rows.map((row) => textOf(cellView(row.cells[index] ?? null, column.kind, notation))), column.primaryKey === true));
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
const changeView = (change) => {
|
|
447
|
+
setLayout((now) => ({ ...now, view: change(now.view) }));
|
|
448
|
+
};
|
|
449
|
+
const headerActions = {
|
|
450
|
+
onSelect: (index, click) => {
|
|
451
|
+
if (!pressedInPopup()) {
|
|
452
|
+
pickColumn(index, click);
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
onSortDirection: (index, direction) => onSortsChange?.(sortOnly(columns[index].name, direction)),
|
|
456
|
+
onClearSort: () => onSortsChange?.([]),
|
|
457
|
+
onResize: resizeColumn,
|
|
458
|
+
onFit: fitColumn,
|
|
459
|
+
onHide: (index) => changeView((view) => hideColumn(view, index)),
|
|
460
|
+
onShowAll: () => changeView(showAllColumns),
|
|
461
|
+
onTogglePin: (index) => changeView((view) => togglePin(view, index))
|
|
462
|
+
};
|
|
463
|
+
const renderRow = (index) => {
|
|
464
|
+
const row = rows[index];
|
|
465
|
+
const selected = selection.has(row.key);
|
|
466
|
+
const tint = row.state === undefined ? (selected ? 'bg-accent-soft/40' : '') : ROW_STATE[row.state];
|
|
467
|
+
return (_jsxs("div", { role: "row", "aria-rowindex": index + 2, "aria-selected": selected, className: clsx('group/row absolute inset-x-0 flex border-b border-border-soft', tint, tint === '' && 'hover:bg-surface-hover'), style: { top: index * ROW_HEIGHT, height: ROW_HEIGHT }, children: [_jsx("div", { role: "rowheader", "data-selected": selected ? '' : undefined, className: clsx('sticky left-0 z-10 flex shrink-0 cursor-default items-center justify-end border-r border-border bg-surface pr-2 pl-1 font-mono text-xs text-text-faint tabular-nums select-none data-[selected]:bg-accent-soft data-[selected]:text-text', row.state === 'inserted' && 'text-positive'), style: { width: gutter }, onClick: (event) => {
|
|
468
|
+
if (!pressedInPopup()) {
|
|
469
|
+
selectRowAt(index, { shiftKey: event.shiftKey, mod: isModHeld(event, isApplePlatform()) });
|
|
470
|
+
}
|
|
471
|
+
}, onContextMenu: () => openRowMenu(index), children: row.number === null ? '+' : formatNumber(row.number) }), order.map((columnIndex, position) => {
|
|
472
|
+
const column = columns[columnIndex];
|
|
473
|
+
const cell = row.cells[columnIndex] ?? null;
|
|
474
|
+
const view = cellView(cell, column.kind, notation);
|
|
475
|
+
const cellPosition = { row: index, column: columnIndex };
|
|
476
|
+
const isFocused = focusCell !== null && focusCell.row === index && focusCell.column === columnIndex;
|
|
477
|
+
const ranged = pickedSet.has(columnIndex) || (columnBlock === null && rectContains(rect, index, position));
|
|
478
|
+
const edited = row.edited?.has(columnIndex) === true;
|
|
479
|
+
const isEditing = editing !== null && editing.row === index && editing.column === columnIndex;
|
|
480
|
+
const pinned = position < pinnedShown;
|
|
481
|
+
const enumType = enumTypes[columnIndex] ?? null;
|
|
482
|
+
const followable = !isEditing && followCell({ rowKey: row.key, column: columnIndex });
|
|
483
|
+
return (_jsxs("div", { id: cellId(index, columnIndex), role: "gridcell", "aria-colindex": position + 2, "aria-selected": ranged, "data-focused": isFocused ? '' : undefined, "data-ranged": ranged ? '' : undefined, className: clsx('group/cell flex h-full shrink-0 items-center overflow-hidden px-3 font-mono text-code whitespace-nowrap outline-0 select-none data-[focused]:outline-1 data-[focused]:-outline-offset-1 data-[focused]:outline-border-strong group-focus-within/grid:data-[focused]:outline-accent', position === order.length - 1 ? 'border-r-0' : 'border-r', view.align === 'end' ? 'justify-end tabular-nums' : 'justify-start', pinned
|
|
484
|
+
? [
|
|
485
|
+
'sticky z-5',
|
|
486
|
+
position === pinnedShown - 1 ? 'border-border-strong' : 'border-border-soft',
|
|
487
|
+
ranged ? 'bg-accent-soft' : ['bg-surface', tint === '' && 'group-hover/row:bg-surface-hover']
|
|
488
|
+
]
|
|
489
|
+
: ['relative border-border-soft', ranged ? 'bg-accent-soft' : edited && 'bg-accent/10']), style: { width: shownWidths[position], left: pinned ? gutter + (offsets[position] ?? 0) : undefined }, onPointerDown: (event) => pressCell(event, cellPosition, isEditing), onPointerEnter: () => dragTo(cellPosition), onDoubleClick: () => void beginEdit(cellPosition), onContextMenu: () => openCellMenu(cellPosition), children: [pinned && !ranged && _jsx("span", { "aria-hidden": true, className: clsx('pointer-events-none absolute inset-0', tint, edited && 'bg-accent/10') }), isEditing && editing.loading && _jsx(Spinner, { size: 12, label: t('grid.loadingValue') }), isEditing && !editing.loading && enumType !== null && (_jsx(EnumPicker, { autoOpen: true, type: enumType, value: typeof cell === 'string' ? cell : null, nullable: column.nullable !== false, label: column.name, onDone: finishPick })), isEditing && !editing.loading && enumType === null && (_jsx(CellEditor, { value: editing.draft, label: column.name, onValueChange: (draft) => setEditing({ ...editing, draft }), onCommit: (move) => finishEdit(true, move), onCancel: () => finishEdit(false) })), !isEditing && (_jsxs(_Fragment, { children: [_jsx("span", { className: clsx('min-w-0 truncate', TONE[view.tone], row.state === 'deleted' && 'line-through'), children: view.text }), view.suffix !== undefined && _jsx("span", { className: "ml-2 shrink-0 text-text-faint", children: view.suffix })] })), followable && (_jsx(IconButton, { icon: ArrowUpRight, size: "2xs", label: t('grid.followReference'), tabIndex: -1, className: "absolute top-1/2 right-1 -translate-y-1/2 bg-surface-raised opacity-0 group-hover/cell:opacity-100 group-data-[focused]/cell:opacity-100 focus-visible:opacity-100", onPointerDown: (event) => event.stopPropagation(), onClick: () => {
|
|
490
|
+
if (!pressedInPopup()) {
|
|
491
|
+
onFollow?.({ rowKey: row.key, column: columnIndex });
|
|
492
|
+
}
|
|
493
|
+
} }))] }, columnIndex));
|
|
494
|
+
})] }, row.key));
|
|
495
|
+
};
|
|
496
|
+
const menuRow = menuTarget === null ? undefined : rows[menuTarget.row];
|
|
497
|
+
const menuCell = menuTarget === null || menuTarget.column === null ? null : { row: menuTarget.row, column: menuTarget.column };
|
|
498
|
+
const settable = menuCell !== null && canEdit(menuCell);
|
|
499
|
+
const nullable = menuCell !== null && columns[menuCell.column]?.nullable !== false;
|
|
500
|
+
const menuColumn = menuCell === null ? undefined : columns[menuCell.column];
|
|
501
|
+
const hasDefault = menuColumn !== undefined && ((menuColumn.defaultValue ?? null) !== null || menuColumn.autoIncrement === true);
|
|
502
|
+
const menuContext = menuRow === undefined || menuTarget === null
|
|
503
|
+
? null
|
|
504
|
+
: {
|
|
505
|
+
rowKeys: selection.has(menuRow.key) ? rows.filter((row) => selection.has(row.key)).map((row) => row.key) : [menuRow.key],
|
|
506
|
+
cell: menuTarget.column === null ? null : { rowKey: menuRow.key, column: menuTarget.column }
|
|
507
|
+
};
|
|
508
|
+
return (_jsx("div", { ref: attach, role: "grid", "aria-label": label, "aria-rowcount": rows.length + 1, "aria-colcount": order.length + 1, "aria-activedescendant": pickedColumns.length > 0 ? headerId(pickedColumns[0]) : focusCell === null ? undefined : cellId(focusCell.row, focusCell.column), tabIndex: 0, className: clsx('group/grid relative min-h-0 flex-1 overflow-auto bg-surface text-text outline-none', className), onKeyDown: handleKeyDown, onScroll: (event) => setScrollTop(Math.floor(event.currentTarget.scrollTop / ROW_HEIGHT) * ROW_HEIGHT), children: _jsxs("div", { className: "relative min-w-full", style: { width: totalWidth }, children: [_jsxs("div", { role: "row", "aria-rowindex": 1, className: "sticky top-0 z-20 flex w-full border-b border-border bg-surface", style: { height: HEADER_HEIGHT }, children: [_jsx("div", { className: "sticky left-0 z-10 shrink-0 border-r border-border bg-surface", style: { width: gutter } }), order.map((columnIndex, position) => {
|
|
509
|
+
const column = columns[columnIndex];
|
|
510
|
+
return (_jsx(GridHeaderCell, { column: column, index: columnIndex, position: position, width: shownWidths[position], sortable: sortable, selected: pickedSet.has(columnIndex), id: headerId(columnIndex), sort: sortable ? sortStateOf(sortList, column.name) : null, multipleSorts: sortList.length > 1, hasSorts: sortList.length > 0, pinned: position < pinnedShown, stickyLeft: gutter + (offsets[position] ?? 0), lastPinned: position === pinnedShown - 1, last: position === order.length - 1, hasHidden: current.view.hidden.size > 0, canHide: order.length > 1, actions: headerActions }, columnIndex));
|
|
511
|
+
})] }), _jsxs(ContextMenu.Root, { children: [_jsx(ContextMenu.Trigger, { role: "rowgroup", className: "relative", style: { height: rows.length * ROW_HEIGHT }, children: Array.from({ length: range.end - range.start }, (_, offset) => renderRow(range.start + offset)) }), _jsxs(ContextMenu.Popup, { children: [_jsxs(ContextMenu.Item, { disabled: menuCell === null, onClick: copyCell, children: [_jsx(Icon, { icon: Copy, size: 14 }), t('grid.copyValue'), _jsx(Kbd, { shortcut: EDIT_SHORTCUTS.copy })] }), _jsx(CopyAsMenu, { disabled: block === null, formats: sqlTarget === undefined ? COPY_FORMATS.filter((format) => format !== 'sql') : COPY_FORMATS, onCopy: copyAs }), menuContext !== null && menuCell !== null && queryMenu !== undefined && (_jsxs(_Fragment, { children: [_jsx(ContextMenu.Separator, {}), queryMenu(menuContext)] })), _jsx(ContextMenu.Separator, {}), editable && menuCell !== null && (_jsxs(_Fragment, { children: [_jsxs(ContextMenu.Item, { disabled: !settable || !nullable, onClick: () => commitValue(null), children: [_jsx(Icon, { icon: Ban, size: 14 }), t('grid.setNull')] }), hasDefault && (_jsxs(ContextMenu.Item, { disabled: !settable, onClick: () => commitValue({ kind: 'default' }), children: [_jsx(Icon, { icon: RotateCcw, size: 14 }), t('grid.setDefault')] }))] })), menuContext !== null && menu?.(menuContext)] })] }), rows.length === 0 && empty !== undefined && _jsx("div", { className: "sticky left-0 w-fit px-4 py-6 text-xs text-text-muted", children: empty })] }) }));
|
|
512
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Ref } from 'react';
|
|
2
|
+
import { type EnumType } from './enum-type.ts';
|
|
3
|
+
export interface EnumPickerProps {
|
|
4
|
+
type: EnumType;
|
|
5
|
+
value: string | null;
|
|
6
|
+
nullable: boolean;
|
|
7
|
+
label: string;
|
|
8
|
+
autoOpen?: boolean;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
onDone(result: {
|
|
11
|
+
readonly value: string | null;
|
|
12
|
+
} | undefined): void;
|
|
13
|
+
look?: 'cell' | 'field';
|
|
14
|
+
className?: string;
|
|
15
|
+
ref?: Ref<HTMLButtonElement>;
|
|
16
|
+
}
|
|
17
|
+
export declare function EnumPicker({ type, value, nullable, label, autoOpen, disabled, onDone, look, className, ref }: EnumPickerProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from 'react';
|
|
3
|
+
import clsx from 'clsx';
|
|
4
|
+
import { Ban, ChevronDown } from 'lucide-react';
|
|
5
|
+
import { useTranslation } from 'react-i18next';
|
|
6
|
+
import { Icon, Menu } from '@adecore/ui';
|
|
7
|
+
import { joinMembers, membersOf } from './enum-type.js';
|
|
8
|
+
const LOOK = {
|
|
9
|
+
cell: 'absolute inset-0 z-10 flex items-center gap-2 border border-accent bg-surface-raised px-3 font-mono text-code outline-0',
|
|
10
|
+
field: 'field field-sm flex w-full items-center gap-2 font-mono text-code data-disabled:opacity-50'
|
|
11
|
+
};
|
|
12
|
+
/*
|
|
13
|
+
* The editor of a MySQL ENUM or SET column: a menu of the values the type declares, plus NULL where
|
|
14
|
+
* the column allows it. An enum picks one value and is done; a set toggles its members and is done
|
|
15
|
+
* when the menu closes. It is a menu rather than a `Select` since a set takes several values and a
|
|
16
|
+
* cell has to open it by itself as the edit starts.
|
|
17
|
+
*/
|
|
18
|
+
export function EnumPicker({ type, value, nullable, label, autoOpen = false, disabled = false, onDone, look = 'cell', className, ref }) {
|
|
19
|
+
const { t } = useTranslation('database');
|
|
20
|
+
const settled = useRef(false);
|
|
21
|
+
const [members, setMembers] = useState(() => new Set(type.kind === 'set' && value !== null ? membersOf(value) : []));
|
|
22
|
+
const set = type.kind === 'set';
|
|
23
|
+
const settle = (result) => {
|
|
24
|
+
if (!settled.current) {
|
|
25
|
+
settled.current = true;
|
|
26
|
+
onDone(result);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const handleOpenChange = (open) => {
|
|
30
|
+
if (open) {
|
|
31
|
+
settled.current = false;
|
|
32
|
+
setMembers(new Set(set && value !== null ? membersOf(value) : []));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const joined = joinMembers(type, members);
|
|
36
|
+
settle(set && joined !== value ? { value: joined } : undefined);
|
|
37
|
+
};
|
|
38
|
+
const toggle = (member, checked) => {
|
|
39
|
+
setMembers((now) => {
|
|
40
|
+
const next = new Set(now);
|
|
41
|
+
if (checked) {
|
|
42
|
+
next.add(member);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
next.delete(member);
|
|
46
|
+
}
|
|
47
|
+
return next;
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
return (_jsxs(Menu.Root, { defaultOpen: autoOpen, onOpenChange: handleOpenChange, children: [_jsxs(Menu.Trigger, { ref: ref, disabled: disabled, "aria-label": label, className: clsx(LOOK[look], className), children: [_jsx("span", { className: clsx('min-w-0 flex-1 truncate text-left', value === null && 'text-text-faint'), children: value ?? 'NULL' }), _jsx(Icon, { icon: ChevronDown, size: 14, className: "shrink-0 text-text-faint" })] }), _jsxs(Menu.Popup, { className: "max-h-80 overflow-auto", children: [nullable && (_jsxs(_Fragment, { children: [_jsxs(Menu.Item, { onClick: () => settle({ value: null }), children: [_jsx(Icon, { icon: Ban, size: 14 }), t('grid.setNull')] }), _jsx(Menu.Separator, {})] })), set
|
|
51
|
+
? type.values.map((member) => (_jsx(Menu.CheckboxItem, { checked: members.has(member), closeOnClick: false, onCheckedChange: (checked) => toggle(member, checked), children: member === '' ? _jsx("span", { className: "text-text-faint", children: t('grid.emptyValue') }) : member }, member)))
|
|
52
|
+
: type.values.map((option) => (_jsxs(Menu.Item, { onClick: () => settle({ value: option }), children: [_jsx(Menu.Check, { kind: "radio", checked: option === value }), option === '' ? _jsx("span", { className: "text-text-faint", children: t('grid.emptyValue') }) : option] }, option)))] })] }));
|
|
53
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { SortDirection } from '../sql.ts';
|
|
2
|
+
import type { ColumnClick } from './column-selection.ts';
|
|
3
|
+
import type { GridColumn } from './types.ts';
|
|
4
|
+
export interface HeaderActions {
|
|
5
|
+
onSelect(index: number, click: ColumnClick): void;
|
|
6
|
+
onSortDirection(index: number, direction: SortDirection): void;
|
|
7
|
+
onClearSort(): void;
|
|
8
|
+
onResize(index: number, width: number): void;
|
|
9
|
+
onFit(index: number): void;
|
|
10
|
+
onHide(index: number): void;
|
|
11
|
+
onShowAll(): void;
|
|
12
|
+
onTogglePin(index: number): void;
|
|
13
|
+
}
|
|
14
|
+
export interface GridHeaderCellProps {
|
|
15
|
+
column: GridColumn;
|
|
16
|
+
index: number;
|
|
17
|
+
position: number;
|
|
18
|
+
width: number;
|
|
19
|
+
sortable: boolean;
|
|
20
|
+
selected: boolean;
|
|
21
|
+
id: string;
|
|
22
|
+
sort: {
|
|
23
|
+
direction: SortDirection;
|
|
24
|
+
position: number;
|
|
25
|
+
} | null;
|
|
26
|
+
multipleSorts: boolean;
|
|
27
|
+
hasSorts: boolean;
|
|
28
|
+
pinned: boolean;
|
|
29
|
+
stickyLeft: number;
|
|
30
|
+
lastPinned: boolean;
|
|
31
|
+
last: boolean;
|
|
32
|
+
hasHidden: boolean;
|
|
33
|
+
canHide: boolean;
|
|
34
|
+
actions: HeaderActions;
|
|
35
|
+
}
|
|
36
|
+
export declare function GridHeaderCell({ column, index, position, width, sortable, selected, id, sort, multipleSorts, hasSorts, pinned, stickyLeft, lastPinned, last, hasHidden, canHide, actions }: GridHeaderCellProps): import("react").JSX.Element;
|