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.

feat: add undo to Replace All (#48)

Myles Scolnick3558f3da5715
diff --git a/frontend/src/components/find-replace/find-replace.tsx b/frontend/src/components/find-replace/find-replace.tsx
index 59dcf0ef9c..dc4bdfe1ee 100644
--- a/frontend/src/components/find-replace/find-replace.tsx
+++ b/frontend/src/components/find-replace/find-replace.tsx
@@ -26,6 +26,7 @@ import {
import { useHotkey } from "@/hooks/useHotkey";
import { EditorView } from "codemirror";
import { FocusScope } from "@react-aria/focus";
+import { toast } from "../ui/use-toast";
export const FindReplace: React.FC = () => {
const [isFocused, setIsFocused] = useState(false);
@@ -168,7 +169,29 @@ export const FindReplace: React.FC = () => {
size="xs"
variant="outline"
className="h-6 text-xs"
- onClick={() => replaceAll()}
+ onClick={() => {
+ const undo = replaceAll();
+ if (!undo) {
+ return;
+ }
+
+ // Show toast with undo button
+ const { dismiss } = toast({
+ title: "Replaced all occurrences",
+ action: (
+ <Button
+ size="sm"
+ variant="outline"
+ onClick={() => {
+ undo();
+ dismiss();
+ }}
+ >
+ Undo
+ </Button>
+ ),
+ });
+ }}
disabled={state.findText === "" || state.replaceText === ""}
>
Replace All
diff --git a/frontend/src/components/ui/button.tsx b/frontend/src/components/ui/button.tsx
index 9af1ccd4af..843ff8c578 100644
--- a/frontend/src/components/ui/button.tsx
+++ b/frontend/src/components/ui/button.tsx
@@ -44,8 +44,8 @@ const buttonVariants = cva(
),
outline: cn(
"border border-slate-500 shadow-smSolid",
- "hover:bg-accent/90 hover:text-accent-foreground",
- "hover:border-transparent",
+ "hover:bg-secondary/90 hover:text-secondary-foreground",
+ "hover:border-input",
activeCommon
),
secondary: cn(
diff --git a/frontend/src/components/ui/use-toast.ts b/frontend/src/components/ui/use-toast.ts
index 510b1fa9b0..24e9808ab2 100644
--- a/frontend/src/components/ui/use-toast.ts
+++ b/frontend/src/components/ui/use-toast.ts
@@ -4,7 +4,7 @@ import * as React from "react";
import { ToastActionElement, type ToastProps } from "@/components/ui/toast";
const TOAST_LIMIT = 1;
-const TOAST_REMOVE_DELAY = 1_000_000;
+const TOAST_REMOVE_DELAY = 10_000; // 10 seconds
type ToasterToast = ToastProps & {
id: string;
diff --git a/frontend/src/core/codemirror/find-replace/navigate.ts b/frontend/src/core/codemirror/find-replace/navigate.ts
index 76eaadf438..0f594be347 100644
--- a/frontend/src/core/codemirror/find-replace/navigate.ts
+++ b/frontend/src/core/codemirror/find-replace/navigate.ts
@@ -126,6 +126,7 @@ export const findPrev = findInDirection("prev");
*/
export const replaceAll = searchCommand(({ query }) => {
const views = getAllEditorViews();
+ const undoHandlers: Array<() => void> = [];
for (const view of views) {
if (view.state.readOnly) {
continue;
@@ -136,17 +137,31 @@ export const replaceAll = searchCommand(({ query }) => {
return { from, to, insert: query.getReplacement(match) };
});
- if (changes && changes.length === 0) {
+ if (!changes || changes.length === 0) {
continue;
}
+ const prevDoc = view.state.doc.toString();
+ undoHandlers.push(() => {
+ view.dispatch({
+ changes: [{ from: 0, to: view.state.doc.length, insert: prevDoc }],
+ userEvent: "input.replace.all",
+ });
+ });
+
view.dispatch({
changes,
userEvent: "input.replace.all",
});
}
- return true;
+ const handleUndo = () => {
+ for (const undoHandler of undoHandlers) {
+ undoHandler();
+ }
+ };
+
+ return handleUndo;
});
/**
diff --git a/frontend/src/editor/Cell.tsx b/frontend/src/editor/Cell.tsx
index a37aa77c49..82fed04ef9 100644
--- a/frontend/src/editor/Cell.tsx
+++ b/frontend/src/editor/Cell.tsx
@@ -3,7 +3,7 @@ import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
import { historyField } from "@codemirror/commands";
import { foldAll, unfoldAll } from "@codemirror/language";
import { EditorState, Prec, StateEffect } from "@codemirror/state";
-import { keymap, EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";
+import { keymap, EditorView, KeyBinding } from "@codemirror/view";
import {
memo,
FocusEvent,
@@ -232,7 +232,7 @@ const CellComponent = (
);
useEffect(() => {
- const runKeymap = keymap.of([
+ const cellEditingHotkeys: KeyBinding[] = [
{
key: HOTKEYS.getHotkey("cell.run").key,
preventDefault: true,
@@ -241,8 +241,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const runAndNewCellBelowKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.runAndNewBelow").key,
preventDefault: true,
@@ -253,8 +251,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const runAndNewCellAboveKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.runAndNewAbove").key,
preventDefault: true,
@@ -265,8 +261,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const deleteCellKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.delete").key,
preventDefault: true,
@@ -284,8 +278,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const moveDownKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.moveDown").key,
preventDefault: true,
@@ -294,8 +286,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const moveUpKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.moveUp").key,
preventDefault: true,
@@ -304,8 +294,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const focusDownKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.focusDown").key,
preventDefault: true,
@@ -314,8 +302,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const focusUpKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.focusUp").key,
preventDefault: true,
@@ -324,8 +310,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const sendToBottomKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.sendToBottom").key,
preventDefault: true,
@@ -334,8 +318,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const sendToTopKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.sendToTop").key,
preventDefault: true,
@@ -344,8 +326,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const createAboveKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.createAbove").key,
preventDefault: true,
@@ -355,8 +335,6 @@ const CellComponent = (
return true;
},
},
- ]);
- const createBelowKeymap = keymap.of([
{
key: HOTKEYS.getHotkey("cell.createBelow").key,
preventDefault: true,
@@ -366,34 +344,18 @@ const CellComponent = (
return true;
},
},
- ]);
+ ];
- const onChangePlugin = ViewPlugin.fromClass(
- class {
- constructor(private readonly view: EditorView) {}
- update(update: ViewUpdate) {
- if (update.docChanged) {
- const nextCode = this.view.state.doc.toString();
- updateCellCode(cellId, nextCode);
- }
- }
+ const onChangePlugin = EditorView.updateListener.of((update) => {
+ if (update.docChanged) {
+ const nextCode = update.state.doc.toString();
+ updateCellCode(cellId, nextCode);
}
- );
+ });
const extensions = [
...setup(userConfig.completion, theme),
- Prec.highest(runKeymap),
- Prec.highest(runAndNewCellBelowKeymap),
- Prec.highest(runAndNewCellAboveKeymap),
- Prec.highest(deleteCellKeymap),
- Prec.highest(moveDownKeymap),
- Prec.highest(moveUpKeymap),
- Prec.highest(focusDownKeymap),
- Prec.highest(focusUpKeymap),
- Prec.highest(sendToBottomKeymap),
- Prec.highest(sendToTopKeymap),
- Prec.highest(createAboveKeymap),
- Prec.highest(createBelowKeymap),
+ Prec.highest(keymap.of(cellEditingHotkeys)),
Prec.highest(formatKeymapExtension(cellId, updateCellCode)),
scrollActiveLineIntoView(),
onChangePlugin,