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: Undo split cell with code snapshot (#2030)

Wasim Sandhu60b3e5e5c315
diff --git a/frontend/src/components/editor/cell/useSplitCell.tsx b/frontend/src/components/editor/cell/useSplitCell.tsx
index 91f2eec82b..ec45521fb7 100644
--- a/frontend/src/components/editor/cell/useSplitCell.tsx
+++ b/frontend/src/components/editor/cell/useSplitCell.tsx
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { UndoButton } from "@/components/buttons/undo-button";
import { toast } from "@/components/ui/use-toast";
-import { useCellActions } from "@/core/cells/cells";
+import { getCellEditorView, useCellActions } from "@/core/cells/cells";
import { CellId } from "@/core/cells/ids";
import useEvent from "react-use-event-hook";
@@ -9,6 +9,10 @@ export function useSplitCellCallback() {
const { splitCell, undoSplitCell } = useCellActions();
return useEvent((opts: { cellId: CellId }) => {
+ // Save snapshot of code for undo
+ const cellEditorView = getCellEditorView(opts.cellId);
+ const code = cellEditorView?.state.doc.toString() ?? "";
+
// Optimistic update
splitCell(opts);
@@ -20,7 +24,7 @@ export function useSplitCellCallback() {
size="sm"
variant="outline"
onClick={() => {
- undoSplitCell(opts);
+ undoSplitCell({ ...opts, snapshot: code });
dismiss();
}}
>
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 624b33e73a..6d9900211a 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -26,7 +26,7 @@ import { splitAtom, selectAtom } from "jotai/utils";
import { isStaticNotebook, parseStaticState } from "../static/static-state";
import { type CellLog, getCellLogsForMessage } from "./logs";
import { deserializeBase64, deserializeJson } from "@/utils/json/base64";
-import { historyField, undo } from "@codemirror/commands";
+import { historyField } from "@codemirror/commands";
import { clamp } from "@/utils/math";
import type { LayoutState } from "../layout/layout";
import { notebookIsRunning } from "./utils";
@@ -818,8 +818,8 @@ const {
scrollKey: newCellId,
};
},
- undoSplitCell: (state, action: { cellId: CellId }) => {
- const { cellId } = action;
+ undoSplitCell: (state, action: { cellId: CellId; snapshot: string }) => {
+ const { cellId, snapshot } = action;
const cell = state.cellData[cellId];
const newCellIndex = state.cellIds.indexOfOrThrow(cellId) + 1;
@@ -829,8 +829,7 @@ const {
return state;
}
- undo(cellHandle.editorView);
- const code = cellHandle.editorView.state.doc.toString();
+ updateEditorCodeFromPython(cellHandle.editorView, snapshot);
return {
...state,
@@ -839,8 +838,9 @@ const {
...state.cellData,
[cellId]: {
...cell,
- code: code,
- edited: Boolean(code) && code.trim() !== cell.lastCodeRun?.trim(),
+ code: snapshot,
+ edited:
+ Boolean(snapshot) && snapshot?.trim() !== cell.lastCodeRun?.trim(),
},
},
cellRuntime: {