Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Undo Keyboard Shortcut #417

Merged
merged 20 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/studio-ui/.size-limit.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = [
{
path: "lib/**/*.js",
limit: "850 kB",
limit: "870 kB",
nmanu1 marked this conversation as resolved.
Show resolved Hide resolved
gzip: false,
},
];
3 changes: 2 additions & 1 deletion packages/studio-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@
"immer": "^9.0.21",
"lodash": "^4.17.21",
"path-browserify": "^1.0.1",
"platform": "^1.3.6",
"postcss": "^8.4.27",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-modal": "3.16.1",
"react-select": "^5.7.4",
"react-toastify": "^9.1.1",
"react-tooltip": "^5.18.0",
"true-myth": "^6.2.0",
"tailwind-merge": "^1.8.1",
"tailwindcss": "^3.3.3",
"true-myth": "^6.2.0",
"zundo": "2.0.0-beta.12",
"zustand": "^4.3.2"
},
Expand Down
20 changes: 19 additions & 1 deletion packages/studio-ui/src/components/UndoRedo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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";
import platform from "platform";

/**
* Buttons for undo and redo actions.
Expand All @@ -22,6 +23,23 @@ export default function UndoRedo(): JSX.Element {
redo();
}, [redo]);

const handleUndoKeydown = useCallback(
(event: KeyboardEvent) => {
const actionKey =
platform.os.family === "OS X" ? event.metaKey : event.ctrlKey;
if (actionKey && event.key === "z") {
event.preventDefault();
undo();
}
},
[undo]
);

useEffect(() => {
document.addEventListener("keydown", handleUndoKeydown);
alextaing marked this conversation as resolved.
Show resolved Hide resolved
alextaing marked this conversation as resolved.
Show resolved Hide resolved
return () => document.removeEventListener("keydown", handleUndoKeydown);
}, [handleUndoKeydown]);

const disableUndo = pastStates.length === 0;
const disableRedo = futureStates.length === 0;
const undoClasses = classNames("w-4", {
Expand Down
38 changes: 38 additions & 0 deletions packages/studio-ui/tests/components/UndoRedo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import useStudioStore from "../../src/store/useStudioStore";
import useTemporalStore from "../../src/store/useTemporalStore";
import { searchBarComponent } from "../__fixtures__/componentStates";
import mockStore from "../__utils__/mockStore";
import platform from "platform";

jest.mock("platform", () => ({
__esModule: true,
default: {
os: {
family: null,
},
},
}));

describe("Undo/redo", () => {
beforeEach(() => {
Expand Down Expand Up @@ -46,6 +56,34 @@ describe("Undo/redo", () => {
expect(useStudioStore.getState().pages.activeComponentUUID).toBeUndefined();
});

it("undoes last state update using control + z if OS is not OS X", async () => {
alextaing marked this conversation as resolved.
Show resolved Hide resolved
platform.os.family = "Windows";
render(<UndoRedo />);
expect(useStudioStore.getState().pages.activeComponentUUID).toBe(
"searchbar-uuid"
);
await userEvent.keyboard("{Meta>}z{/Meta}");
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 if OS is OS X", async () => {
platform.os.family = "OS X";
render(<UndoRedo />);
expect(useStudioStore.getState().pages.activeComponentUUID).toBe(
"searchbar-uuid"
);
await userEvent.keyboard("{Control>}z{/Control}");
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
Loading