Skip to content

Commit

Permalink
base soltution
Browse files Browse the repository at this point in the history
  • Loading branch information
alextaing committed Oct 19, 2023
1 parent b3b57a7 commit 7804a6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/studio-ui/src/components/UndoRedo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useTemporalStore from "../store/useTemporalStore";
import { ReactComponent as Undo } from "../icons/undo.svg";
import { useCallback } from "react";
import { useCallback, useEffect } from "react";
import classNames from "classnames";

/**
Expand All @@ -22,6 +22,17 @@ export default function UndoRedo(): JSX.Element {
redo();
}, [redo]);

const handleUndoKeydown = useCallback((event: KeyboardEvent) => {
event.preventDefault()
if ((event.ctrlKey || event.metaKey) && event.key === "z") {
undo()
}
}, [undo])

useEffect(() => {
document.addEventListener("keydown", handleUndoKeydown)
}, [handleUndoKeydown]);

const disableUndo = pastStates.length === 0;
const disableRedo = futureStates.length === 0;
const undoClasses = classNames("w-4", {
Expand Down
18 changes: 18 additions & 0 deletions packages/studio-ui/tests/components/UndoRedo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ describe("Undo/redo", () => {
expect(useStudioStore.getState().pages.activeComponentUUID).toBeUndefined();
});

it("undoes last state update using control + z", async () => {
render(<UndoRedo />);
expect(useStudioStore.getState().pages.activeComponentUUID).toBe(
"searchbar-uuid"
);
await userEvent.keyboard('{Control>}z{/Control}')
expect(useStudioStore.getState().pages.activeComponentUUID).toBeUndefined();
});

it("undoes last state update using command + z", async () => {
render(<UndoRedo />);
expect(useStudioStore.getState().pages.activeComponentUUID).toBe(
"searchbar-uuid"
);
await userEvent.keyboard('{Meta>}z{/Meta}')
expect(useStudioStore.getState().pages.activeComponentUUID).toBeUndefined();
});

it("redoes single state update when redo is clicked", async () => {
const undo = useTemporalStore((store) => store.undo);
undo();
Expand Down

0 comments on commit 7804a6d

Please sign in to comment.