From c49756f6521438119b66fe8ddcf598d5243c6145 Mon Sep 17 00:00:00 2001 From: Leo Hong <5917188+low-earth-orbit@users.noreply.github.com> Date: Sat, 31 Aug 2024 21:18:11 -0300 Subject: [PATCH] Delete shortcut (close #29) --- components/Canvas.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/components/Canvas.tsx b/components/Canvas.tsx index 48865f4..4ebebd7 100644 --- a/components/Canvas.tsx +++ b/components/Canvas.tsx @@ -87,6 +87,23 @@ export default function Canvas() { localStorage.setItem("canvasState", JSON.stringify(canvasObjects)); }, [canvasObjects]); + // keyboard shortcut + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === "Delete" || event.key === "Backspace") { + handleDelete(); + } + }; + + // Add event listener for keydown + document.addEventListener("keydown", handleKeyDown); + + // Clean up event listener on component unmount + return () => { + document.removeEventListener("keydown", handleKeyDown); + }; + }, [selectedObjectId, canvasObjects, handleDelete]); + function updateStyle(property: keyof CanvasObjectType, value: any) { // Dynamically update state if (property === "strokeWidth") {