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.

improve: Undo split cells via toast (#2018)

Wasim Sandhu98134ea042c6
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 02974e1f08..87a2de9ffe 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -36,6 +36,7 @@ import type { LanguageAdapterType } from "@/core/codemirror/language/types";
import { autoInstantiateAtom } from "@/core/config/config";
import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";
import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
+import { useSplitCellCallback } from "../useSplitCell";
export interface CellEditorProps
extends Pick<CellRuntimeState, "status">,
@@ -92,7 +93,8 @@ const CellEditorInternal = ({
const editorViewParentRef = useRef<HTMLDivElement>(null);
const loading = status === "running" || status === "queued";
- const { splitCell, sendToTop, sendToBottom } = useCellActions();
+ const { sendToTop, sendToBottom } = useCellActions();
+ const splitCell = useSplitCellCallback();
const handleDelete = useEvent(() => {
// Cannot delete running cells, since we're waiting for their output.
diff --git a/frontend/src/components/editor/cell/useSplitCell.tsx b/frontend/src/components/editor/cell/useSplitCell.tsx
new file mode 100644
index 0000000000..91f2eec82b
--- /dev/null
+++ b/frontend/src/components/editor/cell/useSplitCell.tsx
@@ -0,0 +1,32 @@
+/* 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 { CellId } from "@/core/cells/ids";
+import useEvent from "react-use-event-hook";
+
+export function useSplitCellCallback() {
+ const { splitCell, undoSplitCell } = useCellActions();
+
+ return useEvent((opts: { cellId: CellId }) => {
+ // Optimistic update
+ splitCell(opts);
+
+ const { dismiss } = toast({
+ title: "Cell split",
+ action: (
+ <UndoButton
+ data-testid="undo-split-button"
+ size="sm"
+ variant="outline"
+ onClick={() => {
+ undoSplitCell(opts);
+ dismiss();
+ }}
+ >
+ Undo
+ </UndoButton>
+ ),
+ });
+ });
+}
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index b83740b87c..624b33e73a 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 } from "@codemirror/commands";
+import { historyField, undo } from "@codemirror/commands";
import { clamp } from "@/utils/math";
import type { LayoutState } from "../layout/layout";
import { notebookIsRunning } from "./utils";
@@ -818,6 +818,44 @@ const {
scrollKey: newCellId,
};
},
+ undoSplitCell: (state, action: { cellId: CellId }) => {
+ const { cellId } = action;
+
+ const cell = state.cellData[cellId];
+ const newCellIndex = state.cellIds.indexOfOrThrow(cellId) + 1;
+ const cellHandle = state.cellHandles[cellId].current;
+
+ if (cellHandle?.editorView == null) {
+ return state;
+ }
+
+ undo(cellHandle.editorView);
+ const code = cellHandle.editorView.state.doc.toString();
+
+ return {
+ ...state,
+ cellIds: state.cellIds.delete(newCellIndex),
+ cellData: {
+ ...state.cellData,
+ [cellId]: {
+ ...cell,
+ code: code,
+ edited: Boolean(code) && code.trim() !== cell.lastCodeRun?.trim(),
+ },
+ },
+ cellRuntime: {
+ ...state.cellRuntime,
+ [cellId]: {
+ ...state.cellRuntime[cellId],
+ output: null,
+ consoleOutputs: [],
+ },
+ },
+ cellHandles: {
+ ...state.cellHandles,
+ },
+ };
+ },
});
// Helper function to update a cell in the array
diff --git a/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py
index fef04b8480..728c492cc6 100644
--- a/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py
+++ b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py
@@ -1,21 +1,21 @@
# Copyright 2024 Marimo. All rights reserved.
+
import marimo
-__generated_with = "0.6.19"
+__generated_with = "0.7.19"
app = marimo.App(app_title="1654 - Virtualize Multiselect")
@app.cell
def __():
import marimo as mo
- mo.md("[improve: Virtualize mo.ui.multiselect to handle large lists #1654](https://github.com/marimo-team/marimo/pull/1654)")
return mo,
@app.cell
def __(mo):
fuzzy_match_test = ["foo bar", "bar foo", "foob", "foobar", "barfoo"]
- mo.ui.multiselect(fuzzy_match_test, label="Fuzzy match search")
+ mo.ui.multiselect(fuzzy_match_test, label="Fuzzy match test")
return fuzzy_match_test,