Skip to content

Commit

Permalink
feat: Add Escape key to reset to select mode [PT-185596247]
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmartin committed Jul 13, 2023
1 parent c006048 commit 7542104
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/components/drawing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export const Drawing = (props: Props) => {
};
};

const getNode = useCallback((id: string) => graph.nodes.find(n => n.id === id), [graph.nodes]);

const clearSelections = useCallback(() => {
setFirstEdgeNode(undefined);
setHighlightNode(undefined);
setRubberBand(undefined);
}, [setFirstEdgeNode, setHighlightNode]);

useEffect(() => {
if (drawingMode === "addEdge" && firstEdgeNode) {
const updateRubberBand = (e: MouseEvent) => {
Expand All @@ -119,13 +127,16 @@ export const Drawing = (props: Props) => {
}
}, [drawingMode, firstEdgeNode]);

const getNode = useCallback((id: string) => graph.nodes.find(n => n.id === id), [graph.nodes]);

const clearSelections = useCallback(() => {
setFirstEdgeNode(undefined);
setHighlightNode(undefined);
setRubberBand(undefined);
}, [setFirstEdgeNode, setHighlightNode]);
useEffect(() => {
const listenForEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") {
clearSelections();
setDrawingMode("select");
}
};
window.addEventListener("keydown", listenForEscape);
return () => window.removeEventListener("keydown", listenForEscape);
}, [drawingMode, setDrawingMode, clearSelections]);

const handleSetSelectMode = useCallback(() => {
setDrawingMode("select");
Expand Down

0 comments on commit 7542104

Please sign in to comment.