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(frontend): avoid crash when creating a markdown cell (#9923)

Shahmir Varqha8a53f27a8fe8
diff --git a/frontend/src/core/cells/__tests__/focus.test.ts b/frontend/src/core/cells/__tests__/focus.test.ts
index daa6f9a9c9..cd59afce3f 100644
--- a/frontend/src/core/cells/__tests__/focus.test.ts
+++ b/frontend/src/core/cells/__tests__/focus.test.ts
@@ -1,11 +1,17 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import type { EditorView } from "@codemirror/view";
+import { createStore } from "jotai";
import { beforeEach, describe, expect, it } from "vitest";
+import { MockNotebook } from "@/__mocks__/notebook";
import { cellId } from "@/__tests__/branded";
+import type { CellHandle } from "@/components/editor/notebook-cell";
+import { notebookAtom } from "../cells";
import type { CellFocusState } from "../focus";
-import { exportedForTesting } from "../focus";
+import { exportedForTesting, lastFocusedCellAtom } from "../focus";
-const { initialState, reducer, createActions } = exportedForTesting;
+const { initialState, reducer, createActions, cellFocusAtom } =
+ exportedForTesting;
const CellIds = {
a: cellId("a"),
@@ -270,3 +276,40 @@ describe("cell focus reducer", () => {
});
});
});
+
+describe("lastFocusedCellAtom", () => {
+ it("returns null getEditorView when CodeMirror is not mounted", () => {
+ const store = createStore();
+ const markdownCellId = cellId("markdown");
+
+ const unmountedEditorHandle: CellHandle = {
+ get editorView(): EditorView {
+ throw new ReferenceError("Attempting to dereference null object.");
+ },
+ editorViewOrNull: null,
+ };
+
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [markdownCellId]: {
+ config: { hide_code: true },
+ },
+ },
+ });
+ notebook.cellHandles[markdownCellId] = {
+ current: unmountedEditorHandle,
+ };
+
+ store.set(notebookAtom, notebook);
+ store.set(cellFocusAtom, {
+ focusedCellId: markdownCellId,
+ lastFocusedCellId: markdownCellId,
+ });
+
+ const lastFocusedCell = store.get(lastFocusedCellAtom);
+
+ expect(lastFocusedCell).not.toBeNull();
+ expect(lastFocusedCell?.cellId).toBe(markdownCellId);
+ expect(lastFocusedCell?.getEditorView()).toBeNull();
+ });
+});
diff --git a/frontend/src/core/cells/focus.ts b/frontend/src/core/cells/focus.ts
index adbc6bcccf..3cd70b7c5c 100644
--- a/frontend/src/core/cells/focus.ts
+++ b/frontend/src/core/cells/focus.ts
@@ -106,7 +106,7 @@ function cellFocusDetails(cellId: CellId, notebookState: NotebookState) {
if (!data || !handle) {
return null;
}
- const getEditorView = () => handle.editorView;
+ const getEditorView = () => handle.editorViewOrNull;
return {
cellId,