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

Added hotkey for deleting nodes #637

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions src/web/common/components/ContextMenu/DeleteNodeMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect } from "react";

import { ContextMenuItem } from "@/web/common/components/ContextMenu/CloseOnClickMenuItem";
import { useSessionUser } from "@/web/common/hooks";
import { deleteNode } from "@/web/topic/store/createDeleteActions";
Expand All @@ -8,7 +10,21 @@ export const DeleteNodeMenuItem = ({ node }: { node: Node }) => {
const { sessionUser } = useSessionUser();
const userCanEditTopicData = useUserCanEditTopicData(sessionUser?.username);

if (!userCanEditTopicData) return <></>;
useEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea, but since this useEffect is defined within the DeleteNodeMenuItem, the delete key only works when this menu item is rendered, i.e. when right-clicking on a node.

Check out the Technical Details section of the issue #520 - the code referenced there shows an established pattern for adding hotkeys in Ameliorate. There, we add a hotkey hook to the TopicWorkspace, which is a parent container of the Diagram, so the hotkey will always be usable without needing any other specific components rendered.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you've added to TopicWorkspace is looking better, but this DeleteNodeMenuItem code still needs to be undone.

const handleGlobalDelete = (event: KeyboardEvent) => {
if (event.key === "Delete" && userCanEditTopicData) {
deleteNode(node.id);
}
};

window.addEventListener("keydown", handleGlobalDelete);

return () => {
window.removeEventListener("keydown", handleGlobalDelete);
};
}, [node.id, userCanEditTopicData]);

if (!userCanEditTopicData) return null;

return <ContextMenuItem onClick={() => deleteNode(node.id)}>Delete node</ContextMenuItem>;
return <ContextMenuItem onClick={() => deleteNode(node.id)}>Delete Node</ContextMenuItem>;
};
12 changes: 8 additions & 4 deletions src/web/topic/components/TopicWorkspace/TopicWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TourSetter } from "@/web/topic/components/TopicWorkspace/TourSetter";
import { TutorialAnchor } from "@/web/topic/components/TopicWorkspace/TutorialAnchor";
import { TutorialController } from "@/web/topic/components/TopicWorkspace/TutorialController";
import { WorkspaceContext } from "@/web/topic/components/TopicWorkspace/WorkspaceContext";
import { setScore } from "@/web/topic/store/actions";
import { deleteGraphPart, setScore } from "@/web/topic/store/actions";
import { playgroundUsername } from "@/web/topic/store/store";
import { isOnPlayground } from "@/web/topic/store/utilActions";
import { Score, possibleScores } from "@/web/topic/utils/graph";
Expand All @@ -37,11 +37,17 @@ const useWorkspaceHotkeys = (user: { username: string } | null | undefined) => {
const [score] = hotkeysEvent.keys;
if (!score || !possibleScores.some((s) => score === s)) return;

// seems slightly awkward that there's logic here not reused with the Score component, but hard to reuse that in a clean way
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intentionally remove this comment? It's still relevant for the code below it, and your hotkey changes seem unrelated to it. The same goes for the other two comments that you've removed.

const myUsername = isOnPlayground() ? playgroundUsername : user?.username;
if (!userCanEditScores(myUsername, getPerspectives(), getReadonlyMode())) return;
setScore(myUsername, selectedPart.id, score as Score);
});

useHotkeys([hotkeys.delete || "del"], () => {
const selectedPart = getSelectedGraphPart();
if (selectedPart?.id) {
deleteGraphPart(selectedPart.id);
}
});
};

export const TopicWorkspace = () => {
Expand All @@ -56,7 +62,6 @@ export const TopicWorkspace = () => {
const useSplitPanes = isLandscape && usingBigScreen;

return (
// h-svh to force workspace to take up full height of screen
<div className="relative flex h-svh flex-col">
<AppHeader />

Expand Down Expand Up @@ -94,7 +99,6 @@ export const TopicWorkspace = () => {
</WorkspaceContext.Provider>
)}

{/* prevents body scrolling when workspace is rendered*/}
<Global styles={{ body: { overflow: "hidden" } }} />
</div>

Expand Down
20 changes: 20 additions & 0 deletions src/web/topic/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ export const setGraphPartNotes = (graphPart: GraphPart, value: string) => {

useTopicStore.setState(finishDraft(state), false, "setGraphPartNotes");
};

export const deleteGraphPart = (graphPartId: string) => {
const state = createDraft(useTopicStore.getState());
const foundGraphPart = findGraphPartOrThrow(graphPartId, state.nodes, state.edges);

const updatedNodes = state.nodes.filter((node) => node.id !== foundGraphPart.id);
const updatedEdges = state.edges.filter((edge) =>
edge.source !== foundGraphPart.id && edge.target !== foundGraphPart.id
);

useTopicStore.setState(
finishDraft({
...state,
nodes: updatedNodes,
edges: updatedEdges
}),
false,
"deleteGraphPart"
);
};
1 change: 1 addition & 0 deletions src/web/topic/utils/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const hotkeys = {
showIndicators: "Alt + i",
zoomIn: "Ctrl + =",
zoomOut: "Ctrl + -",
delete: "del",
};