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.

improvement: copy cell output if user does not select anything (#2949)

Myles Scolnick0863f2c6e5df
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 249aab82ec..70dc7085f9 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -11,7 +11,7 @@ import { ImageOutput } from "./output/ImageOutput";
import { MarimoErrorOutput } from "./output/MarimoErrorOutput";
import { TextOutput } from "./output/TextOutput";
import { VideoOutput } from "./output/VideoOutput";
-import type { CellId } from "@/core/cells/ids";
+import { CellOutputId, type CellId } from "@/core/cells/ids";
import { cn } from "@/utils/cn";
import { ErrorBoundary } from "./boundary/ErrorBoundary";
@@ -287,7 +287,7 @@ export const OutputArea = React.memo(
<Container
title={title}
cellId={cellId}
- id={`output-${cellId}`}
+ id={CellOutputId.create(cellId)}
className={cn(stale && "marimo-output-stale", className)}
>
<OutputRenderer message={output} />
diff --git a/frontend/src/components/editor/cell/cell-context-menu.tsx b/frontend/src/components/editor/cell/cell-context-menu.tsx
index 5eac28433c..b9c6fa028f 100644
--- a/frontend/src/components/editor/cell/cell-context-menu.tsx
+++ b/frontend/src/components/editor/cell/cell-context-menu.tsx
@@ -21,6 +21,8 @@ import {
SearchIcon,
} from "lucide-react";
import { goToDefinitionAtCursorPosition } from "@/core/codemirror/go-to-definition/utils";
+import { CellOutputId } from "@/core/cells/ids";
+import { Logger } from "@/utils/Logger";
interface Props extends CellActionButtonProps {
children: React.ReactNode;
@@ -36,7 +38,23 @@ export const CellActionsContextMenu = ({ children, ...props }: Props) => {
label: "Copy",
icon: <CopyIcon size={13} strokeWidth={1.5} />,
handle: () => {
- document.execCommand("copy");
+ // Has selection, use browser copy
+ const hasSelection = window.getSelection()?.toString();
+ if (hasSelection) {
+ document.execCommand("copy");
+ return;
+ }
+
+ // No selection, copy the full cell output
+ const output = document.getElementById(
+ CellOutputId.create(props.cellId),
+ );
+ if (!output) {
+ Logger.warn("cell-context-menu: output not found");
+ return;
+ }
+ // Copy the output of the cell
+ navigator.clipboard.writeText(output.textContent ?? "");
},
},
{
diff --git a/frontend/src/components/export/export-output-button.tsx b/frontend/src/components/export/export-output-button.tsx
index 3a2e608507..632fe9f5ce 100644
--- a/frontend/src/components/export/export-output-button.tsx
+++ b/frontend/src/components/export/export-output-button.tsx
@@ -1,9 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { downloadHTMLAsImage } from "@/utils/download";
-import type { CellId } from "@/core/cells/ids";
+import { CellOutputId, type CellId } from "@/core/cells/ids";
export function downloadCellOutput(cellId: CellId) {
- const output = document.getElementById(`output-${cellId}`);
+ const output = document.getElementById(CellOutputId.create(cellId));
if (output) {
output.classList.add("printing-output");
downloadHTMLAsImage(output, "result.png").finally(() => {
diff --git a/frontend/src/core/cells/__tests__/ids.test.ts b/frontend/src/core/cells/__tests__/ids.test.ts
index c102e81964..26b3c8c8f7 100644
--- a/frontend/src/core/cells/__tests__/ids.test.ts
+++ b/frontend/src/core/cells/__tests__/ids.test.ts
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { expect, describe, test } from "vitest";
-import { CellId, HTMLCellId } from "../ids";
+import { CellId, CellOutputId, HTMLCellId } from "../ids";
describe("CellId", () => {
test("create", () => {
@@ -39,3 +39,11 @@ describe("HTMLCellId", () => {
expect(cell!.id.startsWith("cell-")).toBe(true);
});
});
+
+describe("CellOutputId", () => {
+ test("create", () => {
+ const cellId = CellId.create();
+ const cellOutputId = CellOutputId.create(cellId);
+ expect(cellOutputId).toBe(`output-${cellId}`);
+ });
+});
diff --git a/frontend/src/core/cells/ids.ts b/frontend/src/core/cells/ids.ts
index a0df6f8602..8bb33d2f3b 100644
--- a/frontend/src/core/cells/ids.ts
+++ b/frontend/src/core/cells/ids.ts
@@ -63,3 +63,10 @@ export const UIElementId = {
return id;
},
};
+
+export type CellOutputId = `output-${CellId}`;
+export const CellOutputId = {
+ create(cellId: CellId): CellOutputId {
+ return `output-${cellId}`;
+ },
+};