diff --git a/src/components/floor-plan/CanvasFrame.jsx b/src/components/floor-plan/CanvasFrame.jsx index ddce32a6..1167bd97 100644 --- a/src/components/floor-plan/CanvasFrame.jsx +++ b/src/components/floor-plan/CanvasFrame.jsx @@ -30,6 +30,7 @@ import { useCanvasSetting } from '@/hooks/option/useCanvasSetting' import { useCanvasMenu } from '@/hooks/common/useCanvasMenu' import { useEvent } from '@/hooks/useEvent' import { compasDegAtom } from '@/store/orientationAtom' +import { hotkeyStore } from '@/store/hotkeyAtom' export default function CanvasFrame() { const canvasRef = useRef(null) @@ -110,6 +111,38 @@ export default function CanvasFrame() { resetPcsCheckState() } + /** + * 캔버스가 있을 경우 핫키 이벤트 처리 + * hotkeyStore에 핫키 이벤트 리스너 추가 + * + * const hotkeys = [ + { key: 'c', fn: () => asdf() }, + { key: 'v', fn: () => qwer() }, + ] + setHotkeyStore(hotkeys) + */ + const [hotkeyState, setHotkeyState] = useRecoilState(hotkeyStore) + const hotkeyHandlerRef = useRef(null) + + useEffect(() => { + hotkeyHandlerRef.current = (e) => { + hotkeyState.forEach((hotkey) => { + if (e.key === hotkey.key) { + hotkey.fn() + } + }) + } + + document.addEventListener('keyup', hotkeyHandlerRef.current) + + return () => { + if (hotkeyHandlerRef.current) { + document.removeEventListener('keyup', hotkeyHandlerRef.current) + } + } + }, [hotkeyState]) + /** 핫키 이벤트 처리 끝 */ + return (