diff --git a/frontend/src/component/canvas/useEventHandlers.tsx b/frontend/src/component/canvas/useEventHandlers.tsx index 23191544..1e7a3d7e 100644 --- a/frontend/src/component/canvas/useEventHandlers.tsx +++ b/frontend/src/component/canvas/useEventHandlers.tsx @@ -1,86 +1,86 @@ -import { useRef } from 'react'; -import { ICanvasRefMethods } from '@/component/canvas/Canvas.tsx'; -import { IMapObject, IMapRefMethods } from '@/component/maps/Map.types.ts'; - -// TODO: 리팩토룅 시 null을 처리하기 -interface IUseEventHandlers { - ( - canvasElement: HTMLCanvasElement | null, - canvasRefMethods: ICanvasRefMethods | null, - mapElement: HTMLElement | null, - mapRefMethods: IMapRefMethods | null, - mapObject: IMapObject | null, // 비동기 로딩 시 null로 처리가 될 수 있어서 예외처리 필요 - ): { - handleClick: (event: React.MouseEvent) => void; - handleMouseDown: (event: React.MouseEvent) => void; - handleMouseMove: (event: React.MouseEvent) => void; - handleMouseUp: (event: React.MouseEvent) => void; - }; -} - -interface IMouseEventState { - isMouseDown: boolean; - mouseDownPosition: { x: number; y: number }; - // mouseMovePosition: { x: number; y: number }; - mouseDeltaPosition: { x: number; y: number }; -} - -const MouseEventStateInitialValue = { - isMouseDown: false, - mouseDownPosition: { x: 0, y: 0 }, - // mouseMovePosition: { x: 0, y: 0 }, - mouseDeltaPosition: { x: 0, y: 0 }, -}; - -export const useEventHandlers: IUseEventHandlers = ( - canvasElement, - canvasRefMethods, - mapElement, - mapRefMethods, - mapObject, -) => { - // if (!canvasElement || !canvasElement || !mapElement || !mapRefMethods || !mapObject) - // throw new Error('🚀 useEventHandler error : null 값이 포함되어 있습니다.'); - - const mouseEventState = useRef({ ...MouseEventStateInitialValue }); - - const handleClick = (event: React.MouseEvent) => { - mapRefMethods?.onMouseClickHandler(event); - canvasRefMethods?.onMouseClickHandler(event); - }; - - const handleMouseDown = (event: React.MouseEvent) => { - if (!mapElement || !canvasElement) return; - mouseEventState.current.isMouseDown = true; - mouseEventState.current.mouseDownPosition = { x: event.clientX, y: event.clientY }; - canvasRefMethods?.onMouseDownHandler(event); - }; - - const handleMouseMove = (event: React.MouseEvent) => { - if (!mapElement || !canvasElement || !mouseEventState.current.isMouseDown) return; - - // TODO: 쓰로틀링 걸기 - mouseEventState.current.mouseDeltaPosition = { - x: -(event.clientX - mouseEventState.current.mouseDownPosition.x), - y: -(event.clientY - mouseEventState.current.mouseDownPosition.y), - }; - - // TODO: 범용 지도에 따른 Refactoring 필요, 우선은 네이버 지도에 한해서만 수행 - mapObject?.panBy( - new naver.maps.Point( - mouseEventState.current.mouseDeltaPosition.x, - mouseEventState.current.mouseDeltaPosition.y, - ), - ); - - canvasRefMethods?.onMouseMoveHandler(event); - }; - - const handleMouseUp = (event: React.MouseEvent) => { - if (!mapElement || !canvasElement) return; - mouseEventState.current = { ...MouseEventStateInitialValue }; - canvasRefMethods?.onMouseUpHandler(event); - }; - - return { handleClick, handleMouseDown, handleMouseMove, handleMouseUp }; -}; +// import { useRef } from 'react'; +// import { ICanvasRefMethods } from '@/component/canvas/Canvas.tsx'; +// import { IMapObject, IMapRefMethods } from '@/component/maps/Map.types.ts'; +// +// // TODO: 리팩토룅 시 null을 처리하기 +// interface IUseEventHandlers { +// ( +// canvasElement: HTMLCanvasElement | null, +// canvasRefMethods: ICanvasRefMethods | null, +// mapElement: HTMLElement | null, +// mapRefMethods: IMapRefMethods | null, +// mapObject: IMapObject | null, // 비동기 로딩 시 null로 처리가 될 수 있어서 예외처리 필요 +// ): { +// handleClick: (event: React.MouseEvent) => void; +// handleMouseDown: (event: React.MouseEvent) => void; +// handleMouseMove: (event: React.MouseEvent) => void; +// handleMouseUp: (event: React.MouseEvent) => void; +// }; +// } +// +// interface IMouseEventState { +// isMouseDown: boolean; +// mouseDownPosition: { x: number; y: number }; +// // mouseMovePosition: { x: number; y: number }; +// mouseDeltaPosition: { x: number; y: number }; +// } +// +// const MouseEventStateInitialValue = { +// isMouseDown: false, +// mouseDownPosition: { x: 0, y: 0 }, +// // mouseMovePosition: { x: 0, y: 0 }, +// mouseDeltaPosition: { x: 0, y: 0 }, +// }; +// +// export const useEventHandlers: IUseEventHandlers = ( +// canvasElement, +// canvasRefMethods, +// mapElement, +// mapRefMethods, +// mapObject, +// ) => { +// // if (!canvasElement || !canvasElement || !mapElement || !mapRefMethods || !mapObject) +// // throw new Error('🚀 useEventHandler error : null 값이 포함되어 있습니다.'); +// +// const mouseEventState = useRef({ ...MouseEventStateInitialValue }); +// +// const handleClick = (event: React.MouseEvent) => { +// mapRefMethods?.onMouseClickHandler(event); +// canvasRefMethods?.onMouseClickHandler(event); +// }; +// +// const handleMouseDown = (event: React.MouseEvent) => { +// if (!mapElement || !canvasElement) return; +// mouseEventState.current.isMouseDown = true; +// mouseEventState.current.mouseDownPosition = { x: event.clientX, y: event.clientY }; +// canvasRefMethods?.onMouseDownHandler(event); +// }; +// +// const handleMouseMove = (event: React.MouseEvent) => { +// if (!mapElement || !canvasElement || !mouseEventState.current.isMouseDown) return; +// +// // TODO: 쓰로틀링 걸기 +// mouseEventState.current.mouseDeltaPosition = { +// x: -(event.clientX - mouseEventState.current.mouseDownPosition.x), +// y: -(event.clientY - mouseEventState.current.mouseDownPosition.y), +// }; +// +// // TODO: 범용 지도에 따른 Refactoring 필요, 우선은 네이버 지도에 한해서만 수행 +// mapObject?.panBy( +// new naver.maps.Point( +// mouseEventState.current.mouseDeltaPosition.x, +// mouseEventState.current.mouseDeltaPosition.y, +// ), +// ); +// +// canvasRefMethods?.onMouseMoveHandler(event); +// }; +// +// const handleMouseUp = (event: React.MouseEvent) => { +// if (!mapElement || !canvasElement) return; +// mouseEventState.current = { ...MouseEventStateInitialValue }; +// canvasRefMethods?.onMouseUpHandler(event); +// }; +// +// return { handleClick, handleMouseDown, handleMouseMove, handleMouseUp }; +// }; diff --git a/frontend/src/component/maps/NaverMap.tsx b/frontend/src/component/maps/NaverMap.tsx index 0f9049d0..2718bdfa 100644 --- a/frontend/src/component/maps/NaverMap.tsx +++ b/frontend/src/component/maps/NaverMap.tsx @@ -1,47 +1,40 @@ -import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'; -import { setNaverMapSync } from '@/component/maps/naverMapUtils.ts'; -import { IMapOptions, IMapRefMethods } from '@/component/maps/Map.types.ts'; - -interface INaverMapProps extends IMapOptions { - onMapInit: (map: naver.maps.Map) => void; // 콜백 프로퍼티 추가 -} - -export const NaverMap = forwardRef((props, ref) => { - const mapObject = useRef(null); - const mapContainer = useRef(null); - - const [mapOptions, setMapOptions] = useState({ - lat: props.lat, - lng: props.lng, - zoom: props.zoom, - }); - - useEffect(() => { - setMapOptions({ - lat: props.lat, - lng: props.lng, - zoom: props.zoom, - }); - }, [props.lat, props.lng, props.zoom]); - - useEffect(() => { - if (mapContainer.current && mapOptions) { - mapObject.current = setNaverMapSync(mapContainer.current, mapOptions); - if (mapObject.current) props.onMapInit(mapObject.current); // 콜백 호출 - } - }, [mapOptions]); - - useImperativeHandle(ref, () => ({ - getMapObject: () => { - if (mapObject) return mapObject.current; - throw new Error('🚀 지도 로딩 오류 : 지도 객체가 존재하지 않습니다.'); - }, - getMapContainer: () => { - if (mapContainer) return mapContainer.current; - throw new Error('🚀 지도 로딩 오류 : 지도 컨테이너가 존재하지 않습니다.'); - }, - onMouseClickHandler: () => {}, - })); - - return
; -}); +// import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'; +// import { setNaverMapSync } from '@/component/maps/naverMapUtils.ts'; +// import { IMapOptions, IMapRefMethods } from '@/component/maps/Map.tsx'; +// +// interface INaverMapProps extends IMapOptions { +// onMapInit: (map: naver.maps.Map) => void; // 콜백 프로퍼티 추가 +// } +// +// export const NaverMap = forwardRef((props, ref) => { +// const naverMapObject = useRef(null); +// const naverMapContainer = useRef(null); +// const [mapOptions, setMapOptions] = useState({ +// lat: props.lat, +// lng: props.lng, +// zoom: props.zoom, +// }); +// +// useEffect(() => { +// setMapOptions({ +// lat: props.lat, +// lng: props.lng, +// zoom: props.zoom, +// }); +// }, [props.lat, props.lng, props.zoom]); +// +// useEffect(() => { +// if (naverMapContainer.current && mapOptions !== null) { +// naverMapObject.current = setNaverMapSync(naverMapContainer.current, mapOptions); +// if (naverMapObject.current !== null) props.onMapInit(naverMapObject.current); // 콜백 호출 +// } +// }, [mapOptions]); +// +// useImperativeHandle(ref, () => ({ +// getMapObject: () => naverMapObject.current, +// getMapContainer: () => naverMapContainer.current, +// onMouseClickHandler: () => {}, +// })); +// +// return
; +// }); diff --git a/frontend/src/component/maps/naverMapUtils.ts b/frontend/src/component/maps/naverMapUtils.ts index 8de7b4e1..22664b78 100644 --- a/frontend/src/component/maps/naverMapUtils.ts +++ b/frontend/src/component/maps/naverMapUtils.ts @@ -1,38 +1,38 @@ -import { IMapOptions } from '@/component/maps/Map.types.ts'; - -export const setNaverMapOption = (mapOptions: IMapOptions): IMapOptions => { - return { - ...mapOptions, - lat: mapOptions.lat ? mapOptions.lat : 37.42829747263545, - lng: mapOptions.lng ? mapOptions.lng : 126.76620435615891, - zoom: mapOptions.zoom ? mapOptions.zoom : 20, - }; -}; - -export const setNaverMap = ( - htmlElement: HTMLElement, - mapOptions: IMapOptions, -): Promise => { - const { lat, lng, ...restProps } = setNaverMapOption(mapOptions); - - return new Promise(resolve => { - const map = new naver.maps.Map(htmlElement, { - center: new naver.maps.LatLng(lat, lng), - ...restProps, - }); - - resolve(map); - }); -}; - -export const setNaverMapSync = ( - htmlElement: HTMLElement, - mapOptions: IMapOptions, -): naver.maps.Map => { - const { lat, lng, ...restProps } = setNaverMapOption(mapOptions); - - return new naver.maps.Map(htmlElement, { - center: new naver.maps.LatLng(lat, lng), - ...restProps, - }); -}; +// import { IMapOptions } from '@/component/maps/Map.types.ts'; +// +// export const setNaverMapOption = (mapOptions: IMapOptions): IMapOptions => { +// return { +// ...mapOptions, +// lat: mapOptions.lat ? mapOptions.lat : 37.42829747263545, +// lng: mapOptions.lng ? mapOptions.lng : 126.76620435615891, +// zoom: mapOptions.zoom ? mapOptions.zoom : 20, +// }; +// }; +// +// export const setNaverMap = ( +// htmlElement: HTMLElement, +// mapOptions: IMapOptions, +// ): Promise => { +// const { lat, lng, ...restProps } = setNaverMapOption(mapOptions); +// +// return new Promise(resolve => { +// const map = new naver.maps.Map(htmlElement, { +// center: new naver.maps.LatLng(lat, lng), +// ...restProps, +// }); +// +// resolve(map); +// }); +// }; +// +// export const setNaverMapSync = ( +// htmlElement: HTMLElement, +// mapOptions: IMapOptions, +// ): naver.maps.Map => { +// const { lat, lng, ...restProps } = setNaverMapOption(mapOptions); +// +// return new naver.maps.Map(htmlElement, { +// center: new naver.maps.LatLng(lat, lng), +// ...restProps, +// }); +// };