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: Preserve language in cell actions undo/redo stack (#2511)

Wasim Sandhuacbd57416d0e
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index 50850a9267..3fb79c4a46 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -173,7 +173,7 @@ describe("cell reducer", () => {
`);
});
- it("can delete a cell", () => {
+ it("can delete a Python cell and undo delete", () => {
actions.createNewCell({
cellId: firstCellId,
before: false,
@@ -199,6 +199,63 @@ describe("cell reducer", () => {
`);
});
+ it("can delete a SQL cell and undo delete", () => {
+ actions.createNewCell({
+ cellId: firstCellId,
+ before: false,
+ code: `df = mo.sql("""SELECT * FROM table""")`,
+ });
+ const newCellId = state.cellIds.atOrThrow(1);
+ actions.deleteCell({
+ cellId: newCellId,
+ });
+ expect(formatCells(state)).toMatchInlineSnapshot(`
+ "
+ key: 0
+ code: ''"
+ `);
+
+ // undo
+ actions.undoDeleteCell();
+ expect(formatCells(state)).toMatchInlineSnapshot(`
+ "
+ key: 0
+ code: ''
+
+ key: 2
+ code: 'df = mo.sql("""SELECT * FROM table""")'"
+ `);
+ });
+
+ it("can delete a Markdown cell and undo delete", () => {
+ const text = "The quick brown fox jumps over the lazy dog.";
+ actions.createNewCell({
+ cellId: firstCellId,
+ before: false,
+ code: `mo.md(r"""${text}""")`,
+ });
+ const newCellId = state.cellIds.atOrThrow(1);
+ actions.deleteCell({
+ cellId: newCellId,
+ });
+ expect(formatCells(state)).toMatchInlineSnapshot(`
+ "
+ key: 0
+ code: ''"
+ `);
+
+ // undo
+ actions.undoDeleteCell();
+ expect(formatCells(state)).toMatchInlineSnapshot(`
+ "
+ key: 0
+ code: ''
+
+ key: 2
+ code: 'mo.md(r"""${text}""")'"
+ `);
+ });
+
it("can update a cell", () => {
actions.updateCellCode({
cellId: firstCellId,
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 1e887f996a..7fb5b688f9 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -392,9 +392,12 @@ const {
const focusIndex = index === 0 ? 1 : index - 1;
const scrollKey = state.cellIds.atOrThrow(focusIndex);
- const serializedEditorState = state.cellHandles[
- cellId
- ].current?.editorView.state.toJSON({ history: historyField });
+ const editorView = state.cellHandles[cellId].current?.editorView;
+ const serializedEditorState = editorView?.state.toJSON({
+ history: historyField,
+ });
+ serializedEditorState.doc = state.cellData[cellId].code;
+
return {
...state,
cellIds: state.cellIds.delete(index),
@@ -421,6 +424,7 @@ const {
serializedEditorState = { doc: "" },
index,
} = mostRecentlyDeleted;
+
const cellId = CellId.create();
const undoCell = createCell({
id: cellId,
@@ -429,6 +433,7 @@ const {
edited: serializedEditorState.doc.trim().length > 0,
serializedEditorState,
});
+
return {
...state,
cellIds: state.cellIds.insert(cellId, index),