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.

improvement: bind meta-z to undo toast (#2021)

Myles Scolnicke69434014292
diff --git a/frontend/src/components/buttons/undo-button.tsx b/frontend/src/components/buttons/undo-button.tsx
new file mode 100644
index 0000000000..f022e7b23b
--- /dev/null
+++ b/frontend/src/components/buttons/undo-button.tsx
@@ -0,0 +1,35 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { useEventListener } from "@/hooks/useEventListener";
+import { MinimalHotkeys } from "../shortcuts/renderShortcut";
+import { Button, type ButtonProps } from "../ui/button";
+
+/* Copyright 2024 Marimo. All rights reserved. */
+interface UndoButtonProps extends Omit<ButtonProps, "onClick"> {
+ onClick?: (event: Pick<Event, "preventDefault" | "stopPropagation">) => void;
+}
+
+export const UndoButton = (props: UndoButtonProps) => {
+ // Add ctrl-z or meta-z event listener
+ useEventListener(
+ window,
+ "keydown",
+ (event) => {
+ if ((event.ctrlKey || event.metaKey) && event.key === "z") {
+ event.preventDefault();
+ event.stopPropagation();
+ props.onClick?.(event);
+ }
+ },
+ {
+ capture: true,
+ },
+ );
+
+ const children = props.children ?? "Undo";
+
+ return (
+ <Button data-testid="undo-button" size="sm" variant="outline" {...props}>
+ {children} <MinimalHotkeys className="ml-2" shortcut="cmd-z" />
+ </Button>
+ );
+};
diff --git a/frontend/src/components/editor/cell/useDeleteCell.tsx b/frontend/src/components/editor/cell/useDeleteCell.tsx
index bdd7354728..844863d8b1 100644
--- a/frontend/src/components/editor/cell/useDeleteCell.tsx
+++ b/frontend/src/components/editor/cell/useDeleteCell.tsx
@@ -1,8 +1,8 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { Button } from "@/components/ui/button";
+import { UndoButton } from "@/components/buttons/undo-button";
import { toast } from "@/components/ui/use-toast";
import { getCells, useCellActions } from "@/core/cells/cells";
-import { CellId } from "@/core/cells/ids";
+import type { CellId } from "@/core/cells/ids";
import { sendDeleteCell } from "@/core/network/requests";
import useEvent from "react-use-event-hook";
@@ -28,17 +28,13 @@ export function useDeleteCellCallback() {
description:
"You can bring it back by clicking undo or through the command palette.",
action: (
- <Button
+ <UndoButton
data-testid="undo-delete-button"
- size="sm"
- variant="outline"
onClick={() => {
undoDeleteCell();
dismiss();
}}
- >
- Undo
- </Button>
+ />
),
});
});
diff --git a/frontend/src/components/find-replace/find-replace.tsx b/frontend/src/components/find-replace/find-replace.tsx
index a489e9eef2..63e892eff3 100644
--- a/frontend/src/components/find-replace/find-replace.tsx
+++ b/frontend/src/components/find-replace/find-replace.tsx
@@ -35,6 +35,7 @@ import {
} from "@/core/codemirror/find-replace/search-highlight";
import { KeyboardHotkeys } from "../shortcuts/renderShortcut";
import { hotkeysAtom } from "@/core/config/config";
+import { UndoButton } from "../buttons/undo-button";
export const FindReplace: React.FC = () => {
const [isFocused, setIsFocused] = useState(false);
@@ -224,17 +225,13 @@ export const FindReplace: React.FC = () => {
const { dismiss } = toast({
title: "Replaced all occurrences",
action: (
- <Button
+ <UndoButton
data-testid="undo-replace-all-button"
- size="sm"
- variant="outline"
onClick={() => {
undo();
dismiss();
}}
- >
- Undo
- </Button>
+ />
),
});
}}
diff --git a/frontend/src/components/shortcuts/renderShortcut.tsx b/frontend/src/components/shortcuts/renderShortcut.tsx
index 2b7dd26da4..52c6c13622 100644
--- a/frontend/src/components/shortcuts/renderShortcut.tsx
+++ b/frontend/src/components/shortcuts/renderShortcut.tsx
@@ -62,8 +62,15 @@ export const MinimalShortcut: React.FC<{
}> = ({ className, shortcut }) => {
const hotkeys = useAtomValue(hotkeysAtom);
const hotkey = hotkeys.getHotkey(shortcut);
- const keys = hotkey.key.split("-");
+ return <MinimalHotkeys shortcut={hotkey.key} className={className} />;
+};
+
+export const MinimalHotkeys: React.FC<{
+ className?: string;
+ shortcut: string;
+}> = ({ shortcut, className }) => {
+ const keys = shortcut.split("-");
return (
<DropdownMenuShortcut className={cn("flex gap-1 items-center", className)}>
{keys.map(prettyPrintHotkey).map(([label, symbol]) => {