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

Conversation

sarccasm
Copy link

Closes #[520]

Description of changes

Added a keyboard shortcut (Delete) for deleting nodes in the graph editor.
Implemented useEffect with an event listener for keydown in DeleteNodeMenuItem.tsx.

Additional context

Verified that nodes can be deleted using both the context menu and the Delete key.

@sarccasm sarccasm requested a review from keyserj as a code owner January 11, 2025 05:53
Copy link

netlify bot commented Jan 11, 2025

‼️ Deploy request for velvety-vacherin-4193fb rejected.

Name Link
🔨 Latest commit befcf24

Copy link

netlify bot commented Jan 11, 2025

Deploy Preview for ameliorate-docs canceled.

Name Link
🔨 Latest commit befcf24
🔍 Latest deploy log https://app.netlify.com/sites/ameliorate-docs/deploys/67869f5b1b08fc000857984f

Copy link
Collaborator

@keyserj keyserj left a comment

Choose a reason for hiding this comment

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

Welcome to Ameliorate 🙂

@@ -8,6 +10,20 @@ export const DeleteNodeMenuItem = ({ node }: { node: Node }) => {
const { sessionUser } = useSessionUser();
const userCanEditTopicData = useUserCanEditTopicData(sessionUser?.username);

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.

@sarccasm sarccasm force-pushed the feature/delete-node-hotkey branch from afe4e92 to 297b016 Compare January 11, 2025 22:01
@@ -37,11 +48,19 @@ 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("delete", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should maintain consistency with the other hotkeys above by creating a hotkeys.delete constant for "delete". The purpose for hotkeys.tsx is so that there's one spot to easily see what all the hotkeys are, and that we can update the button equivalent of the hotkey to show the hotkey if you hover it, like this:
image

useHotkeys("delete", () => {
const selectedNodeId = getSelectedNodeId();
if (selectedNodeId && nodeExists(selectedNodeId)) {
deleteNode(selectedNodeId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

As mentioned in the issue, this logic should work for both nodes and edges. Instead of deleteNode, we can just use the deleteGraphPart method, the same way the Delete button does:

deleteGraphPart(selectedGraphPart);

Comment on lines 30 to 38
const getSelectedNodeId = (): string | null => {
const selectedPart = getSelectedGraphPart();
return selectedPart ? selectedPart.id : null;
};

const nodeExists = (nodeId: string): boolean => {
const selectedPart = getSelectedGraphPart();
return selectedPart ? selectedPart.id === nodeId : false;
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these methods are needed. We can just return from our hook early if the getSelectedGraphPart() is null, like how the hotkey for scoring behaves:

const selectedPart = getSelectedGraphPart();
if (!selectedPart || !hotkeysEvent.keys) return;

And if the selected part isn't null, we can pass it directly to the deleteGraphPart method.

@@ -8,6 +10,20 @@ export const DeleteNodeMenuItem = ({ node }: { node: Node }) => {
const { sessionUser } = useSessionUser();
const userCanEditTopicData = useUserCanEditTopicData(sessionUser?.username);

useEffect(() => {
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants