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: copy-paste cell mime-type (#5789)

Myles Scolnick6464d5f4e0b8
diff --git a/frontend/src/__mocks__/common.ts b/frontend/src/__mocks__/common.ts
index bcd7284a2f..dc1a652893 100644
--- a/frontend/src/__mocks__/common.ts
+++ b/frontend/src/__mocks__/common.ts
@@ -84,6 +84,8 @@ export const SetupMocks = {
.fn()
.mockImplementation((data) => Mocks.clipboardItem(data));
+ global.ClipboardItem.supports = vi.fn().mockReturnValue(true);
+
// Mock Blob
global.Blob = vi
.fn()
diff --git a/frontend/src/components/editor/navigation/__tests__/clipboard.test.ts b/frontend/src/components/editor/navigation/__tests__/clipboard.test.ts
index 842ee84175..72f1d7fca0 100644
--- a/frontend/src/components/editor/navigation/__tests__/clipboard.test.ts
+++ b/frontend/src/components/editor/navigation/__tests__/clipboard.test.ts
@@ -75,13 +75,13 @@ describe("useCellClipboard", () => {
expect(mockClipboard.write).toHaveBeenCalledWith([
expect.objectContaining({
- types: ["application/x-marimo-cell", "text/plain"],
+ types: ["web application/x-marimo-cell", "text/plain"],
}),
]);
expect(toast).toHaveBeenCalledWith({
title: "Cell copied",
- description: "Cell has been copied to clipboard",
+ description: "Cell has been copied to clipboard.",
});
});
@@ -94,17 +94,17 @@ describe("useCellClipboard", () => {
expect(mockClipboard.write).toHaveBeenCalledWith([
expect.objectContaining({
- types: ["application/x-marimo-cell", "text/plain"],
+ types: ["web application/x-marimo-cell", "text/plain"],
}),
]);
expect(toast).toHaveBeenCalledWith({
title: "2 cells copied",
- description: "2 cells have been copied to clipboard",
+ description: "2 cells have been copied to clipboard.",
});
});
- it("should show error toast when no cells found", async () => {
+ it("should not write when no cells found", async () => {
asMock(getNotebook).mockReturnValue(MockNotebook.notebookState());
const { result } = renderHook(() => useCellClipboard());
@@ -113,25 +113,17 @@ describe("useCellClipboard", () => {
await result.current.copyCells([mockCellId1]);
});
- expect(toast).toHaveBeenCalledWith({
- title: "Error",
- description: "No cells found",
- variant: "danger",
- });
+ expect(mockClipboard.write).not.toHaveBeenCalled();
});
- it("should show error toast when empty cell list provided", async () => {
+ it("should not write when empty cell list provided", async () => {
const { result } = renderHook(() => useCellClipboard());
await act(async () => {
await result.current.copyCells([]);
});
- expect(toast).toHaveBeenCalledWith({
- title: "Error",
- description: "No cells found",
- variant: "danger",
- });
+ expect(mockClipboard.write).not.toHaveBeenCalled();
});
it("should fallback to writeText when clipboard.write fails for single cell", async () => {
@@ -148,7 +140,7 @@ describe("useCellClipboard", () => {
expect(mockClipboard.writeText).toHaveBeenCalledWith(mockCellCode1);
expect(toast).toHaveBeenCalledWith({
title: "Cell copied",
- description: "Cell code has been copied to clipboard",
+ description: "Cell has been copied to clipboard.",
});
});
@@ -168,7 +160,7 @@ describe("useCellClipboard", () => {
);
expect(toast).toHaveBeenCalledWith({
title: "2 cells copied",
- description: "2 cells code have been copied to clipboard",
+ description: "2 cells have been copied to clipboard.",
});
});
@@ -188,7 +180,7 @@ describe("useCellClipboard", () => {
);
expect(toast).toHaveBeenCalledWith({
title: "Copy failed",
- description: "Failed to copy cells to clipboard",
+ description: "Failed to copy cells to clipboard.",
variant: "danger",
});
});
@@ -204,13 +196,13 @@ describe("useCellClipboard", () => {
expect(mockClipboard.write).toHaveBeenCalledWith([
expect.objectContaining({
- types: ["application/x-marimo-cell", "text/plain"],
+ types: ["web application/x-marimo-cell", "text/plain"],
}),
]);
expect(toast).toHaveBeenCalledWith({
title: "Cell copied",
- description: "Cell has been copied to clipboard",
+ description: "Cell has been copied to clipboard.",
});
});
});
@@ -223,10 +215,10 @@ describe("useCellClipboard", () => {
};
const mockItem = {
- types: ["application/x-marimo-cell"],
+ types: ["web application/x-marimo-cell"],
getType: vi.fn().mockResolvedValue(
new Blob([JSON.stringify(clipboardData)], {
- type: "application/x-marimo-cell",
+ type: "web application/x-marimo-cell",
}),
),
};
@@ -243,7 +235,7 @@ describe("useCellClipboard", () => {
cellId: mockCellId1,
before: false,
code: "x = 42",
- autoFocus: false,
+ autoFocus: true,
});
});
@@ -258,10 +250,10 @@ describe("useCellClipboard", () => {
};
const mockItem = {
- types: ["application/x-marimo-cell"],
+ types: ["web application/x-marimo-cell"],
getType: vi.fn().mockResolvedValue(
new Blob([JSON.stringify(clipboardData)], {
- type: "application/x-marimo-cell",
+ type: "web application/x-marimo-cell",
}),
),
};
@@ -275,23 +267,23 @@ describe("useCellClipboard", () => {
});
expect(mockCreateNewCell).toHaveBeenCalledTimes(3);
- expect(mockCreateNewCell).toHaveBeenNthCalledWith(1, {
+ expect(mockCreateNewCell).toHaveBeenNthCalledWith(3, {
cellId: mockCellId1,
before: false,
code: "x = 42",
- autoFocus: false,
+ autoFocus: true,
});
expect(mockCreateNewCell).toHaveBeenNthCalledWith(2, {
cellId: mockCellId1,
before: false,
code: "y = x * 2",
- autoFocus: false,
+ autoFocus: true,
});
- expect(mockCreateNewCell).toHaveBeenNthCalledWith(3, {
+ expect(mockCreateNewCell).toHaveBeenNthCalledWith(1, {
cellId: mockCellId1,
before: false,
code: "print(y)",
- autoFocus: false,
+ autoFocus: true,
});
});
@@ -320,10 +312,10 @@ describe("useCellClipboard", () => {
it("should handle invalid clipboard data gracefully", async () => {
const mockItem = {
- types: ["application/x-marimo-cell"],
+ types: ["web application/x-marimo-cell"],
getType: vi.fn().mockResolvedValue(
new Blob(["invalid json"], {
- type: "application/x-marimo-cell",
+ type: "web application/x-marimo-cell",
}),
),
};
@@ -361,7 +353,7 @@ describe("useCellClipboard", () => {
expect(toast).toHaveBeenCalledWith({
title: "Nothing to paste",
- description: "No cell or text found in clipboard",
+ description: "No cell or text found in clipboard.",
variant: "danger",
});
});
@@ -398,7 +390,7 @@ describe("useCellClipboard", () => {
expect(toast).toHaveBeenCalledWith({
title: "Nothing to paste",
- description: "No cell or text found in clipboard",
+ description: "No cell or text found in clipboard.",
variant: "danger",
});
});
@@ -410,10 +402,10 @@ describe("useCellClipboard", () => {
};
const mockItem = {
- types: ["application/x-marimo-cell"],
+ types: ["web application/x-marimo-cell"],
getType: vi.fn().mockResolvedValue(
new Blob([JSON.stringify(clipboardData)], {
- type: "application/x-marimo-cell",
+ type: "web application/x-marimo-cell",
}),
),
};
@@ -446,10 +438,10 @@ describe("useCellClipboard", () => {
};
const mockItem = {
- types: ["application/x-marimo-cell"],
+ types: ["web application/x-marimo-cell"],
getType: vi.fn().mockResolvedValue(
new Blob([JSON.stringify(clipboardData)], {
- type: "application/x-marimo-cell",
+ type: "web application/x-marimo-cell",
}),
),
};
diff --git a/frontend/src/components/editor/navigation/clipboard.ts b/frontend/src/components/editor/navigation/clipboard.ts
index e6891c476e..d41dd77080 100644
--- a/frontend/src/components/editor/navigation/clipboard.ts
+++ b/frontend/src/components/editor/navigation/clipboard.ts
@@ -7,7 +7,8 @@ import { getNotebook, useCellActions } from "@/core/cells/cells";
import type { CellId } from "@/core/cells/ids";
import { Logger } from "@/utils/Logger";
-const MARIMO_CELL_MIMETYPE = "application/x-marimo-cell";
+// According to MDN, custom mimetypes should start with "web "
+const MARIMO_CELL_MIMETYPE = "web application/x-marimo-cell";
interface ClipboardCellData {
cells: Array<{
@@ -41,19 +42,13 @@ export function useCellClipboard() {
.filter(Boolean);
if (cells.length === 0) {
- toast({
- title: "Error",
- description: "No cells found",
- variant: "danger",
- });
+ // No cells to copy
return;
}
try {
const clipboardData: ClipboardCellData = {
- cells: cells.map((cell) => ({
- code: cell.code,
- })),
+ cells: cells.map((cell) => ({ code: cell.code })),
version: "1.0",
};
@@ -61,22 +56,14 @@ export function useCellClipboard() {
const plainText = cells.map((cell) => cell.code).join("\n\n");
// Create clipboard item with both custom mimetype and plain text
- const clipboardItem = new ClipboardItem({
- [MARIMO_CELL_MIMETYPE]: new Blob([JSON.stringify(clipboardData)], {
- type: MARIMO_CELL_MIMETYPE,
- }),
- "text/plain": new Blob([plainText], {
- type: "text/plain",
- }),
- });
+ const clipboardItem = new ClipboardItemBuilder()
+ .add(MARIMO_CELL_MIMETYPE, clipboardData)
+ .add("text/plain", plainText)
+ .build();
await navigator.clipboard.write([clipboardItem]);
- const cellText = cells.length === 1 ? "Cell" : `${cells.length} cells`;
- toast({
- title: `${cellText} copied`,
- description: `${cellText} ${cells.length === 1 ? "has" : "have"} been copied to clipboard`,
- });
+ toastSuccess(cells.length);
} catch (error) {
Logger.error("Failed to copy cells to clipboard", error);
@@ -84,17 +71,9 @@ export function useCellClipboard() {
try {
const plainText = cells.map((cell) => cell.code).join("\n\n");
await navigator.clipboard.writeText(plainText);
- const cellText = cells.length === 1 ? "Cell" : `${cells.length} cells`;
- toast({
- title: `${cellText} copied`,
- description: `${cellText} code ${cells.length === 1 ? "has" : "have"} been copied to clipboard`,
- });
+ toastSuccess(cells.length);
} catch {
- toast({
- title: "Copy failed",
- description: "Failed to copy cells to clipboard",
- variant: "danger",
- });
+ toastError();
}
}
});
@@ -126,20 +105,14 @@ export function useCellClipboard() {
// Create new cells with the copied data before/after the current cell
const currentCellId = cellId;
- for (const cell of clipboardData.cells) {
+ const reversedCells = clipboardData.cells.reverse();
+ for (const cell of reversedCells) {
actions.createNewCell({
cellId: currentCellId,
before,
code: cell.code,
- autoFocus: false,
+ autoFocus: true,
});
- // Update currentCellId to the newly created cell for chaining
- // Note: We don't have the new cell ID here, but createNewCell handles the positioning
- }
-
- // Focus the last created cell
- if (clipboardData.cells.length > 0) {
- // The focus will be handled by the cell creation logic
}
return;
@@ -159,19 +132,11 @@ export function useCellClipboard() {
autoFocus: true,
});
} else {
- toast({
- title: "Nothing to paste",
- description: "No cell or text found in clipboard",
- variant: "danger",
- });
+ toastNothingToPaste();
}
} catch (error) {
Logger.error("Failed to paste from clipboard", error);
- toast({
- title: "Paste failed",
- description: "Failed to read from clipboard",
- variant: "danger",
- });
+ toastPasteFailed();
}
});
@@ -180,3 +145,61 @@ export function useCellClipboard() {
pasteAtCell,
};
}
+
+const toastSuccess = (cellLength: number) => {
+ const cellText = cellLength === 1 ? "Cell" : `${cellLength} cells`;
+ toast({
+ title: `${cellText} copied`,
+ description: `${cellText} ${cellLength === 1 ? "has" : "have"} been copied to clipboard.`,
+ });
+};
+
+const toastError = () => {
+ toast({
+ title: "Copy failed",
+ description: "Failed to copy cells to clipboard.",
+ variant: "danger",
+ });
+};
+
+const toastNothingToPaste = () => {
+ toast({
+ title: "Nothing to paste",
+ description: "No cell or text found in clipboard.",
+ variant: "danger",
+ });
+};
+
+const toastPasteFailed = () => {
+ toast({
+ title: "Paste failed",
+ description: "Failed to read from clipboard",
+ variant: "danger",
+ });
+};
+
+class ClipboardItemBuilder {
+ private items: Record<string, string | Blob> = {};
+
+ add(mimeType: string, value: string | object) {
+ // Skip if the browser doesn't support the mime type
+ if (!ClipboardItem.supports(mimeType)) {
+ Logger.warn(`ClipboardItem does not support ${mimeType}`);
+ return this;
+ }
+
+ if (typeof value === "string") {
+ this.items[mimeType] = new Blob([value], { type: mimeType });
+ return this;
+ }
+
+ this.items[mimeType] = new Blob([JSON.stringify(value)], {
+ type: mimeType,
+ });
+ return this;
+ }
+
+ build() {
+ return new ClipboardItem(this.items);
+ }
+}