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: add cell deletion toast to undo, and add to command pallete and notebook actions (#1209)

Myles Scolnickb6fc2a8b7d35
diff --git a/frontend/islands/__demo__/output.html b/frontend/islands/__demo__/output.html
index 0a1bdae537..0185ed3383 100644
--- a/frontend/islands/__demo__/output.html
+++ b/frontend/islands/__demo__/output.html
@@ -8,9 +8,9 @@
<meta name="description" content="a marimo app" />
<title>🏝️</title>
- <script type="module" src="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/main.js"></script>
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/main.js"></script>
<link
- href="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/style.css"
+ href="https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist/style.css"
rel="stylesheet"
crossorigin="anonymous"
/>
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index 4fda36f888..ba051537aa 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -1,5 +1,4 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { sendDeleteCell } from "@/core/network/requests";
import { downloadCellOutput } from "@/components/export/export-output-button";
import { Switch } from "@/components/ui/switch";
import {
@@ -45,6 +44,7 @@ import {
import { Label } from "@/components/ui/label";
import { MarkdownIcon, PythonIcon } from "../cell/code/icons";
import { aiEnabledAtom } from "@/core/config/config";
+import { useDeleteCellCallback } from "../cell/useDeleteCell";
export interface CellActionButtonProps
extends Pick<CellData, "name" | "config"> {
@@ -64,12 +64,12 @@ export function useCellActionButtons({ cell }: Props) {
updateCellConfig,
updateCellCode,
updateCellName,
- deleteCell,
moveCell,
sendToTop,
sendToBottom,
} = useCellActions();
const runCell = useRunCell(cell?.cellId);
+ const deleteCell = useDeleteCellCallback();
const { openModal } = useImperativeModal();
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
const aiEnabled = useAtomValue(aiEnabledAtom);
@@ -292,8 +292,7 @@ export function useCellActionButtons({ cell }: Props) {
label: "Delete",
variant: "danger",
icon: <Trash2Icon size={13} strokeWidth={1.5} />,
- handle: async () => {
- await sendDeleteCell(cellId);
+ handle: () => {
deleteCell({ cellId });
},
},
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index fc9761cc9e..e63e994ab4 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -19,9 +19,11 @@ import {
PanelLeftIcon,
CheckIcon,
KeyboardIcon,
+ Undo2Icon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import {
+ canUndoDeletes,
disabledCellIds,
enabledCellIds,
useCellActions,
@@ -55,7 +57,7 @@ export function useNotebookActions() {
const { selectedPanel } = useChromeState();
const notebook = useNotebook();
- const { updateCellConfig } = useCellActions();
+ const { updateCellConfig, undoDeleteCell } = useCellActions();
const restartKernel = useRestartKernel();
const setCommandPaletteOpen = useSetAtom(commandPaletteAtom);
const setKeyboardShortcutsOpen = useSetAtom(keyboardShortcutsAtom);
@@ -217,6 +219,14 @@ export function useNotebookActions() {
);
},
},
+ {
+ icon: <Undo2Icon size={14} strokeWidth={1.5} />,
+ label: "Undo cell deletion",
+ hidden: !canUndoDeletes(notebook),
+ handle: () => {
+ undoDeleteCell();
+ },
+ },
{
divider: true,
diff --git a/frontend/src/components/editor/cell/useDeleteCell.tsx b/frontend/src/components/editor/cell/useDeleteCell.tsx
new file mode 100644
index 0000000000..46baf408ab
--- /dev/null
+++ b/frontend/src/components/editor/cell/useDeleteCell.tsx
@@ -0,0 +1,39 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { Button } from "@/components/ui/button";
+import { toast } from "@/components/ui/use-toast";
+import { useCellActions } from "@/core/cells/cells";
+import { CellId } from "@/core/cells/ids";
+import { sendDeleteCell } from "@/core/network/requests";
+import useEvent from "react-use-event-hook";
+
+export function useDeleteCellCallback() {
+ const { deleteCell, undoDeleteCell } = useCellActions();
+
+ return useEvent((opts: { cellId: CellId }) => {
+ const { cellId } = opts;
+ // Optimistic update
+ deleteCell({ cellId });
+ sendDeleteCell(cellId).catch(() => {
+ // Fall back on failure
+ undoDeleteCell();
+ });
+ const { dismiss } = toast({
+ title: "Cell deleted",
+ description:
+ "You can bring it back by clicking undo or through the command palette.",
+ action: (
+ <Button
+ data-testid="undo-delete-button"
+ size="sm"
+ variant="outline"
+ onClick={() => {
+ undoDeleteCell();
+ dismiss();
+ }}
+ >
+ Undo
+ </Button>
+ ),
+ });
+ });
+}
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index 7090f1ccf8..7c8ef8e704 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -1,6 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { useEffect } from "react";
-import { sendDeleteCell } from "@/core/network/requests";
import { Cell } from "@/components/editor/Cell";
import {
ConnectionStatus,
@@ -14,7 +13,6 @@ import {
import { AppConfig, UserConfig } from "../../../core/config/config-schema";
import { AppMode } from "../../../core/mode";
import { useHotkey } from "../../../hooks/useHotkey";
-import { useEvent } from "../../../hooks/useEvent";
import { formatAll } from "../../../core/codemirror/format";
import { useTheme } from "../../../theme/useTheme";
import { VerticalLayoutWrapper } from "./vertical-layout/vertical-layout-wrapper";
@@ -23,6 +21,7 @@ import { useChromeActions } from "../chrome/state";
import { Functions } from "@/utils/functions";
import { NotebookBanner } from "../notebook-banner";
import { PackageAlert } from "@/components/editor/package-alert";
+import { useDeleteCellCallback } from "../cell/useDeleteCell";
interface CellArrayProps {
notebook: NotebookState;
@@ -42,7 +41,6 @@ export const CellArray: React.FC<CellArrayProps> = ({
const {
updateCellCode,
prepareForRun,
- deleteCell,
moveCell,
moveToNextCell,
updateCellConfig,
@@ -77,10 +75,7 @@ export const CellArray: React.FC<CellArrayProps> = ({
useHotkey("cell.hideCode", Functions.NOOP);
useHotkey("cell.format", Functions.NOOP);
- const onDeleteCell: typeof deleteCell = useEvent((payload) => {
- sendDeleteCell(payload.cellId);
- deleteCell(payload);
- });
+ const onDeleteCell = useDeleteCellCallback();
// Scroll to a cell targeted by a previous action
useEffect(() => {
diff --git a/frontend/src/core/App.tsx b/frontend/src/core/App.tsx
index 55fefbfe70..4b841b85ed 100644
--- a/frontend/src/core/App.tsx
+++ b/frontend/src/core/App.tsx
@@ -13,6 +13,7 @@ import { WebSocketState } from "./websocket/types";
import { useMarimoWebSocket } from "./websocket/useMarimoWebSocket";
import {
LastSavedNotebook,
+ canUndoDeletes,
notebookCells,
notebookIsRunning,
notebookNeedsRun,
@@ -360,7 +361,7 @@ export const App: React.FC<AppProps> = ({ userConfig, appConfig }) => {
closed={connStatus.state === WebSocketState.CLOSED}
running={isRunning}
needsRun={notebookNeedsRun(notebook)}
- undoAvailable={notebook.history.length > 0}
+ undoAvailable={canUndoDeletes(notebook)}
appWidth={appConfig.width}
/>
)}
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 35fefb8a8e..540aacbfc2 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -847,6 +847,10 @@ export function enabledCellIds(state: NotebookState) {
.filter((cell) => !cell.config.disabled);
}
+export function canUndoDeletes(state: NotebookState) {
+ return state.history.length > 0;
+}
+
/**
* Cells that are stale and can be run.
*/