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: deleteing a cell that has code marked hidden (#2575)

Myles Scolnick5f9039045bed
diff --git a/frontend/src/components/editor/cell/DeleteButton.tsx b/frontend/src/components/editor/cell/DeleteButton.tsx
index 8fb65af59e..24563c4231 100644
--- a/frontend/src/components/editor/cell/DeleteButton.tsx
+++ b/frontend/src/components/editor/cell/DeleteButton.tsx
@@ -32,17 +32,17 @@ export const DeleteButton = (props: {
size="icon"
onClick={onClick}
data-testid="delete-button"
+ onMouseDown={(evt) => {
+ // Prevent stealing focus
+ // This is needed to delete cells when the cell editor
+ // is shown temporarily (e.g. when set to `hidden`)
+ evt.preventDefault();
+ }}
className={cn(
"hover:bg-transparent text-destructive/60 hover:text-destructive",
- {
- DeleteButton: true,
- "inactive-button": appClosed || loading,
- running: loading,
- },
+ (appClosed || loading) && "inactive-button",
)}
- style={{
- boxShadow: "none",
- }}
+ style={{ boxShadow: "none" }}
>
<Trash2Icon size={14} />
</Button>
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 5f5be951ed..dc1dd1a8e1 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -330,10 +330,25 @@ const CellEditorInternal = ({
if (hidden) {
updateCellConfig({ cellId, config: { hide_code: false } });
editorViewRef.current?.focus();
- editorViewParentRef.current?.addEventListener(
+ // Reach one parent up
+ const parent = editorViewParentRef.current?.parentElement;
+ const abortController = new AbortController();
+ parent?.addEventListener(
"focusout",
- () => updateCellConfig({ cellId, config: { hide_code: true } }),
- { once: true },
+ () => {
+ requestAnimationFrame(() => {
+ // Skip closing if the focus is still in the parent element
+ const focusedElement = document.activeElement;
+ if (parent?.contains(focusedElement)) {
+ return;
+ }
+ // Hide the code editor
+ updateCellConfig({ cellId, config: { hide_code: true } });
+ editorViewRef.current?.dom.blur();
+ abortController.abort();
+ });
+ },
+ { signal: abortController.signal },
);
}
}, [hidden, cellId, updateCellConfig, editorViewRef]);