Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: allow select on table cells (#5565)

Shahmir Varqha362a7e788cf9
diff --git a/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts b/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts
index 1c98fb7c3e..1b3f243a66 100644
--- a/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts
+++ b/frontend/src/components/data-table/range-focus/__tests__/atoms.test.ts
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import type { Cell, Row, Table } from "@tanstack/react-table";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { visibleForTesting } from "../atoms";
// Mock dependencies
@@ -406,6 +406,11 @@ describe("cell selection atoms", () => {
vi.mocked(getCellValues).mockReturnValue("mocked cell values");
});
+ afterEach(() => {
+ // Clear text selection
+ window.getSelection = vi.fn().mockReturnValue(null);
+ });
+
it("should copy selected cells and call onCopyComplete", () => {
const selectedCells = new Set(["row1_col1", "row1_col2"]);
const onCopyComplete = vi.fn();
@@ -423,6 +428,29 @@ describe("cell selection atoms", () => {
expect(onCopyComplete).toHaveBeenCalledWith();
expect(state.copiedCells).toEqual(selectedCells);
});
+
+ it("should not copy if there is text selection", () => {
+ const selectedCells = new Set(["row1_col1", "row1_col2"]);
+ const onCopyComplete = vi.fn();
+
+ // Set some selected cells first
+ actions.setSelectedCells(selectedCells);
+
+ // Set text selection
+ window.getSelection = vi.fn().mockReturnValue({
+ toString: vi.fn().mockReturnValue("some text"),
+ });
+
+ actions.handleCopy({
+ table: mockTable,
+ onCopyComplete,
+ });
+
+ expect(getCellValues).not.toHaveBeenCalled();
+ expect(copyToClipboard).not.toHaveBeenCalled();
+ expect(onCopyComplete).not.toHaveBeenCalled();
+ expect(state.copiedCells).toEqual(new Set());
+ });
});
describe("navigate", () => {
diff --git a/frontend/src/components/data-table/range-focus/atoms.ts b/frontend/src/components/data-table/range-focus/atoms.ts
index c2a3a76cc9..8bca548eed 100644
--- a/frontend/src/components/data-table/range-focus/atoms.ts
+++ b/frontend/src/components/data-table/range-focus/atoms.ts
@@ -141,6 +141,11 @@ const {
selectedCell,
);
+ const isSelectingMultipleCells = cellsInRange.length > 1;
+ if (isSelectingMultipleCells) {
+ clearTextSelection();
+ }
+
return {
...state,
selectedCells: new Set(cellsInRange),
@@ -156,6 +161,12 @@ const {
onCopyComplete: () => void;
},
) => {
+ const selection = window.getSelection();
+ if (selection && selection.toString().length > 0) {
+ // Let browser handle the copy
+ return state;
+ }
+
const text = getCellValues(table, state.selectedCells);
copyToClipboard(text);
onCopyComplete();
@@ -360,3 +371,10 @@ export const createCellStateAtom = (cellId: string) =>
isCopied: copiedCells.has(cellId),
};
});
+
+// Clear browser text selection to prevent visual conflicts with cell selection indicator
+export const clearTextSelection = () => {
+ if (window.getSelection) {
+ window.getSelection()?.empty();
+ }
+};
diff --git a/frontend/src/components/data-table/renderers.tsx b/frontend/src/components/data-table/renderers.tsx
index ce82e7d956..91dabec226 100644
--- a/frontend/src/components/data-table/renderers.tsx
+++ b/frontend/src/components/data-table/renderers.tsx
@@ -101,7 +101,7 @@ export const DataTableBody = <TData,>({
tabIndex={0}
key={cell.id}
className={cn(
- "whitespace-pre truncate max-w-[300px] select-none outline-none",
+ "whitespace-pre truncate max-w-[300px] outline-none",
cell.column.getColumnWrapping &&
cell.column.getColumnWrapping() === "wrap" &&
"whitespace-pre-wrap min-w-[200px]",