From bebaa973a4d856c5f64f1c29522441084ea30e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Or=C5=82=C3=B3w?= <adrian.orlow@fishbrain.com> Date: Wed, 10 Jan 2024 10:15:54 +0100 Subject: [PATCH] feat: add map action buttons --- src/assets/vectors/icons/location.svg | 3 ++ .../vectors/icons/magnifier-zoom-in.svg | 11 +++++ .../vectors/icons/magnifier-zoom-out.svg | 11 +++++ src/components/Map/Map.component.tsx | 2 + .../MapAdditionalActions.component.tsx | 44 +++++++++++++++++++ .../MappAdditionalActions.constants.ts | 3 ++ .../Map/MapAdditionalActions/index.ts | 1 + .../utils/useAdditionalActions.ts | 27 ++++++++++++ .../utils/__TEST__useSetBoundingExtent.ts | 39 ++++++++++++++++ .../MapViewer/utils/config/useOlMapLayers.ts | 19 +++++++- src/redux/map/map.reducers.ts | 16 +++++++ src/redux/map/map.slice.ts | 13 +++--- src/redux/map/map.types.ts | 6 +++ tailwind.config.ts | 3 ++ 14 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 src/assets/vectors/icons/location.svg create mode 100644 src/assets/vectors/icons/magnifier-zoom-in.svg create mode 100644 src/assets/vectors/icons/magnifier-zoom-out.svg create mode 100644 src/components/Map/MapAdditionalActions/MapAdditionalActions.component.tsx create mode 100644 src/components/Map/MapAdditionalActions/MappAdditionalActions.constants.ts create mode 100644 src/components/Map/MapAdditionalActions/index.ts create mode 100644 src/components/Map/MapAdditionalActions/utils/useAdditionalActions.ts create mode 100644 src/components/Map/MapViewer/utils/__TEST__useSetBoundingExtent.ts diff --git a/src/assets/vectors/icons/location.svg b/src/assets/vectors/icons/location.svg new file mode 100644 index 00000000..2c376ca5 --- /dev/null +++ b/src/assets/vectors/icons/location.svg @@ -0,0 +1,3 @@ +<svg width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg"> +<path d="M13.9998 9.33073C11.4215 9.33073 9.33317 11.4191 9.33317 13.9974C9.33317 16.5757 11.4215 18.6641 13.9998 18.6641C16.5782 18.6641 18.6665 16.5757 18.6665 13.9974C18.6665 11.4191 16.5782 9.33073 13.9998 9.33073ZM24.4298 12.8307C24.1656 10.4649 23.1047 8.25919 21.4214 6.57587C19.738 4.89255 17.5324 3.83166 15.1665 3.5674V1.16406H12.8332V3.5674C10.4673 3.83166 8.26163 4.89255 6.57831 6.57587C4.895 8.25919 3.8341 10.4649 3.56984 12.8307H1.1665V15.1641H3.56984C3.8341 17.5299 4.895 19.7356 6.57831 21.4189C8.26163 23.1022 10.4673 24.1631 12.8332 24.4274V26.8307H15.1665V24.4274C17.5324 24.1631 19.738 23.1022 21.4214 21.4189C23.1047 19.7356 24.1656 17.5299 24.4298 15.1641H26.8332V12.8307H24.4298V12.8307ZM13.9998 22.1641C9.48484 22.1641 5.83317 18.5124 5.83317 13.9974C5.83317 9.4824 9.48484 5.83073 13.9998 5.83073C18.5148 5.83073 22.1665 9.4824 22.1665 13.9974C22.1665 18.5124 18.5148 22.1641 13.9998 22.1641Z" fill="#8E92A1"/> +</svg> diff --git a/src/assets/vectors/icons/magnifier-zoom-in.svg b/src/assets/vectors/icons/magnifier-zoom-in.svg new file mode 100644 index 00000000..10df8aa3 --- /dev/null +++ b/src/assets/vectors/icons/magnifier-zoom-in.svg @@ -0,0 +1,11 @@ +<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> +<g clip-path="url(#clip0_4_190)"> +<path d="M15 12H12V15H9V12H6V9H9V6H12V9H15V12Z" fill="#8E92A1"/> +<path d="M23.5605 19.9395L19.5 15.879C20.4798 14.2558 20.9985 12.396 21 10.5C21 4.71 16.2885 0 10.5 0C4.7115 0 0 4.71 0 10.5C0 16.29 4.7115 21 10.5 21C12.3962 20.9994 14.2562 20.4808 15.879 19.5L19.9395 23.5605C20.0785 23.7003 20.2437 23.8112 20.4257 23.8869C20.6077 23.9626 20.8029 24.0016 21 24.0016C21.1971 24.0016 21.3923 23.9626 21.5743 23.8869C21.7563 23.8112 21.9215 23.7003 22.0605 23.5605L23.5605 22.0605C23.6999 21.9213 23.8105 21.756 23.886 21.574C23.9615 21.3921 24.0003 21.197 24.0003 21C24.0003 20.803 23.9615 20.6079 23.886 20.426C23.8105 20.244 23.6999 20.0787 23.5605 19.9395V19.9395ZM10.5 18C8.51068 17.9998 6.60291 17.2094 5.19639 15.8026C3.78987 14.3957 2.9998 12.4878 3 10.4985C3.0002 8.50918 3.79064 6.60141 5.19745 5.19489C6.60425 3.78837 8.51218 2.9983 10.5015 2.9985C12.4908 2.9987 14.3986 3.78915 15.8051 5.19595C17.2116 6.60275 18.0017 8.51068 18.0015 10.5C18.0013 12.4893 17.2109 14.3971 15.8041 15.8036C14.3972 17.2101 12.4893 18.0002 10.5 18V18Z" fill="#8E92A1"/> +</g> +<defs> +<clipPath id="clip0_4_190"> +<rect width="24" height="24" fill="white"/> +</clipPath> +</defs> +</svg> diff --git a/src/assets/vectors/icons/magnifier-zoom-out.svg b/src/assets/vectors/icons/magnifier-zoom-out.svg new file mode 100644 index 00000000..939f1a35 --- /dev/null +++ b/src/assets/vectors/icons/magnifier-zoom-out.svg @@ -0,0 +1,11 @@ +<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> +<g clip-path="url(#clip0_4_194)"> +<path d="M6 9H15V12H6V9Z" fill="#8E92A1"/> +<path d="M23.5605 19.9395L19.5 15.879C20.4798 14.2558 20.9985 12.396 21 10.5C21 4.71 16.2885 0 10.5 0C4.7115 0 0 4.71 0 10.5C0 16.29 4.7115 21 10.5 21C12.3962 20.9994 14.2562 20.4808 15.879 19.5L19.9395 23.5605C20.0785 23.7003 20.2437 23.8112 20.4257 23.8869C20.6077 23.9626 20.8029 24.0016 21 24.0016C21.1971 24.0016 21.3923 23.9626 21.5743 23.8869C21.7563 23.8112 21.9215 23.7003 22.0605 23.5605L23.5605 22.0605C23.6999 21.9213 23.8105 21.756 23.886 21.574C23.9615 21.3921 24.0003 21.197 24.0003 21C24.0003 20.803 23.9615 20.6079 23.886 20.426C23.8105 20.244 23.6999 20.0787 23.5605 19.9395V19.9395ZM10.5 18C8.51068 17.9998 6.60291 17.2094 5.19639 15.8026C3.78987 14.3957 2.9998 12.4878 3 10.4985C3.0002 8.50918 3.79064 6.60141 5.19745 5.19489C6.60425 3.78837 8.51218 2.9983 10.5015 2.9985C12.4908 2.9987 14.3986 3.78915 15.8051 5.19595C17.2116 6.60275 18.0017 8.51068 18.0015 10.5C18.0013 12.4893 17.2109 14.3971 15.8041 15.8036C14.3972 17.2101 12.4893 18.0002 10.5 18V18Z" fill="#8E92A1"/> +</g> +<defs> +<clipPath id="clip0_4_194"> +<rect width="24" height="24" fill="white"/> +</clipPath> +</defs> +</svg> diff --git a/src/components/Map/Map.component.tsx b/src/components/Map/Map.component.tsx index ea5a18e2..36ac1a1f 100644 --- a/src/components/Map/Map.component.tsx +++ b/src/components/Map/Map.component.tsx @@ -1,5 +1,6 @@ import { Drawer } from '@/components/Map/Drawer'; import { Legend } from '@/components/Map/Legend'; +import { MapAdditionalActions } from './MapAdditionalActions'; import { MapAdditionalOptions } from './MapAdditionalOptions'; import { MapViewer } from './MapViewer/MapViewer.component'; @@ -12,5 +13,6 @@ export const Map = (): JSX.Element => ( <Drawer /> <MapViewer /> <Legend /> + <MapAdditionalActions /> </div> ); diff --git a/src/components/Map/MapAdditionalActions/MapAdditionalActions.component.tsx b/src/components/Map/MapAdditionalActions/MapAdditionalActions.component.tsx new file mode 100644 index 00000000..77a7a682 --- /dev/null +++ b/src/components/Map/MapAdditionalActions/MapAdditionalActions.component.tsx @@ -0,0 +1,44 @@ +import locationIcon from '@/assets/vectors/icons/location.svg'; +import magnifierZoomInIcon from '@/assets/vectors/icons/magnifier-zoom-in.svg'; +import magnifierZoomOutIcon from '@/assets/vectors/icons/magnifier-zoom-out.svg'; +import Image from 'next/image'; +import { twMerge } from 'tailwind-merge'; +import { useAddtionalActions } from './utils/useAdditionalActions'; + +export const MapAdditionalActions = (): JSX.Element => { + const { zoomIn, zoomOut, zoomInToBioEntities } = useAddtionalActions(); + + return ( + <div + className={twMerge( + 'absolute bottom-6 right-6 z-10 flex flex-col gap-4', + 'drop-shadow-secondary', + )} + > + <button + type="button" + className="flex h-[48px] w-[48px] items-center justify-center rounded-full bg-white" + onClick={zoomInToBioEntities} + > + <Image src={locationIcon} alt="location icon" height={28} width={28} /> + </button> + <div className="flex h-auto w-[48px] flex-col items-center justify-center rounded-full bg-white py-2"> + <button + type="button" + className="flex h-[48px] w-[48px] items-center justify-center" + onClick={zoomIn} + > + <Image src={magnifierZoomInIcon} alt="location icon" height={24} width={24} /> + </button> + <div className="h-[1px] w-[32px] bg-[#F1F1F1]" /> + <button + type="button" + className="flex h-[48px] w-[48px] items-center justify-center" + onClick={zoomOut} + > + <Image src={magnifierZoomOutIcon} alt="location icon" height={24} width={24} /> + </button> + </div> + </div> + ); +}; diff --git a/src/components/Map/MapAdditionalActions/MappAdditionalActions.constants.ts b/src/components/Map/MapAdditionalActions/MappAdditionalActions.constants.ts new file mode 100644 index 00000000..08037944 --- /dev/null +++ b/src/components/Map/MapAdditionalActions/MappAdditionalActions.constants.ts @@ -0,0 +1,3 @@ +export const MAP_ZOOM_IN_DELTA = 1; + +export const MAP_ZOOM_OUT_DELTA = -1; diff --git a/src/components/Map/MapAdditionalActions/index.ts b/src/components/Map/MapAdditionalActions/index.ts new file mode 100644 index 00000000..1b226018 --- /dev/null +++ b/src/components/Map/MapAdditionalActions/index.ts @@ -0,0 +1 @@ +export { MapAdditionalActions } from './MapAdditionalActions.component'; diff --git a/src/components/Map/MapAdditionalActions/utils/useAdditionalActions.ts b/src/components/Map/MapAdditionalActions/utils/useAdditionalActions.ts new file mode 100644 index 00000000..ef7519f9 --- /dev/null +++ b/src/components/Map/MapAdditionalActions/utils/useAdditionalActions.ts @@ -0,0 +1,27 @@ +import { varyPositionZoom } from '@/redux/map/map.slice'; +import { useCallback } from 'react'; +import { useDispatch } from 'react-redux'; +import { MAP_ZOOM_IN_DELTA, MAP_ZOOM_OUT_DELTA } from '../MappAdditionalActions.constants'; + +interface UseAddtionalActionsResult { + zoomIn(): void; + zoomOut(): void; + zoomInToBioEntities(): void; +} + +export const useAddtionalActions = (): UseAddtionalActionsResult => { + const dispatch = useDispatch(); + + const varyZoomByDelta = useCallback( + (delta: number) => { + dispatch(varyPositionZoom({ delta })); + }, + [dispatch], + ); + + return { + zoomIn: () => varyZoomByDelta(MAP_ZOOM_IN_DELTA), + zoomOut: () => varyZoomByDelta(MAP_ZOOM_OUT_DELTA), + zoomInToBioEntities: (): void => {}, + }; +}; diff --git a/src/components/Map/MapViewer/utils/__TEST__useSetBoundingExtent.ts b/src/components/Map/MapViewer/utils/__TEST__useSetBoundingExtent.ts new file mode 100644 index 00000000..b3a30c6a --- /dev/null +++ b/src/components/Map/MapViewer/utils/__TEST__useSetBoundingExtent.ts @@ -0,0 +1,39 @@ +import { searchedBioEntitesSelectorOfCurrentMap } from '@/redux/bioEntity/bioEntity.selectors'; +import { usePointToProjection } from '@/utils/map/usePointToProjection'; +import { boundingExtent } from 'ol/extent'; +import { useEffect } from 'react'; +import { useSelector } from 'react-redux'; +import { MapInstance } from '../MapViewer.types'; + +interface Props { + mapInstance: MapInstance; +} + +export const useSetBoundingExtent = ({ mapInstance }: Props): void => { + const contentBioEntites = useSelector(searchedBioEntitesSelectorOfCurrentMap); + const pointToProjection = usePointToProjection(); + + useEffect(() => { + if (!mapInstance || contentBioEntites.length === 0 || true) { + return; + } + + const [allX, allY] = [contentBioEntites.map(e => e.x), contentBioEntites.map(e => e.y)]; + const [minX, maxX] = [Math.min(...allX), Math.max(...allX)]; + const [minY, maxY] = [Math.min(...allY), Math.max(...allY)]; + + console.log([ + [minX, maxY], + [maxX, minY], + ]); + + const ext = boundingExtent([ + pointToProjection({ x: minX, y: maxY }), + pointToProjection({ x: maxX, y: minY }), + ]); + + mapInstance.getView().fit(ext, { + size: mapInstance.getSize(), + }); + }, [mapInstance, contentBioEntites, pointToProjection]); +}; diff --git a/src/components/Map/MapViewer/utils/config/useOlMapLayers.ts b/src/components/Map/MapViewer/utils/config/useOlMapLayers.ts index b4db459d..6a1247da 100644 --- a/src/components/Map/MapViewer/utils/config/useOlMapLayers.ts +++ b/src/components/Map/MapViewer/utils/config/useOlMapLayers.ts @@ -1,10 +1,13 @@ /* eslint-disable no-magic-numbers */ +import { searchedBioEntitesSelectorOfCurrentMap } from '@/redux/bioEntity/bioEntity.selectors'; +import { mapDataSizeSelector } from '@/redux/map/map.selectors'; import { useEffect } from 'react'; +import { useSelector } from 'react-redux'; import { MapConfig, MapInstance } from '../../MapViewer.types'; +import { useOlMapOverlaysLayer } from './overlaysLayer/useOlMapOverlaysLayer'; import { useOlMapPinsLayer } from './pinsLayer/useOlMapPinsLayer'; import { useOlMapReactionsLayer } from './reactionsLayer/useOlMapReactionsLayer'; import { useOlMapTileLayer } from './useOlMapTileLayer'; -import { useOlMapOverlaysLayer } from './overlaysLayer/useOlMapOverlaysLayer'; interface UseOlMapLayersInput { mapInstance: MapInstance; @@ -15,6 +18,8 @@ export const useOlMapLayers = ({ mapInstance }: UseOlMapLayersInput): MapConfig[ const pinsLayer = useOlMapPinsLayer(); const reactionsLayer = useOlMapReactionsLayer(); const overlaysLayer = useOlMapOverlaysLayer(); + const contentBioEntites = useSelector(searchedBioEntitesSelectorOfCurrentMap); + const { maxZoom } = useSelector(mapDataSizeSelector); useEffect(() => { if (!mapInstance) { @@ -24,5 +29,17 @@ export const useOlMapLayers = ({ mapInstance }: UseOlMapLayersInput): MapConfig[ mapInstance.setLayers([tileLayer, reactionsLayer, pinsLayer, overlaysLayer]); }, [reactionsLayer, tileLayer, pinsLayer, mapInstance, overlaysLayer]); + useEffect(() => { + if (!mapInstance || contentBioEntites.length === 0) { + return; + } + + const extent = pinsLayer.getSource().getExtent(); + mapInstance.getView().fit(extent, { + size: mapInstance.getSize(), + maxZoom, + }); + }, [contentBioEntites, pinsLayer, mapInstance, maxZoom]); + return [tileLayer, pinsLayer, reactionsLayer, overlaysLayer]; }; diff --git a/src/redux/map/map.reducers.ts b/src/redux/map/map.reducers.ts index b17f1432..eef6f932 100644 --- a/src/redux/map/map.reducers.ts +++ b/src/redux/map/map.reducers.ts @@ -1,4 +1,5 @@ import { ZERO } from '@/constants/common'; +import { DEFAULT_ZOOM } from '@/constants/map'; import { ActionReducerMapBuilder } from '@reduxjs/toolkit'; import { getPointMerged } from '../../utils/object/getPointMerged'; import { MAIN_MAP } from './map.constants'; @@ -14,6 +15,7 @@ import { OpenMapAndSetActiveAction, SetActiveMapAction, SetBackgroundAction, + SetLastPositionZoomAction, SetMapDataAction, SetMapPositionDataAction, } from './map.types'; @@ -40,6 +42,20 @@ export const setMapPositionReducer = (state: MapState, action: SetMapPositionDat }; }; +export const varyPositionZoomReducer = ( + state: MapState, + action: SetLastPositionZoomAction, +): void => { + const { minZoom, maxZoom } = state.data.size; + const { delta } = action.payload; + const currentZ = state.data.position.last.z || DEFAULT_ZOOM; + const newZ = currentZ + delta; + const newZLimited = Math.min(Math.max(newZ, minZoom), maxZoom); + + state.data.position.last.z = newZLimited; + state.data.position.initial.z = newZLimited; +}; + const updateLastPositionOfCurrentlyActiveMap = (state: MapState): void => { const currentMapId = state.data.modelId; const currentOpenedMap = state.openedMaps.find(openedMap => openedMap.modelId === currentMapId); diff --git a/src/redux/map/map.slice.ts b/src/redux/map/map.slice.ts index 73cbb92f..d860047c 100644 --- a/src/redux/map/map.slice.ts +++ b/src/redux/map/map.slice.ts @@ -3,15 +3,16 @@ import { MAP_DATA_INITIAL_STATE, OPENED_MAPS_INITIAL_STATE } from './map.constan import { closeMapAndSetMainMapActiveReducer, closeMapReducer, + initMapBackgroundsReducer, + initMapPositionReducers, + initMapSizeAndModelIdReducer, + initOpenedMapsReducer, openMapAndSetActiveReducer, setActiveMapReducer, + setMapBackgroundReducer, setMapDataReducer, - initMapPositionReducers, setMapPositionReducer, - initOpenedMapsReducer, - initMapSizeAndModelIdReducer, - initMapBackgroundsReducer, - setMapBackgroundReducer, + varyPositionZoomReducer, } from './map.reducers'; import { MapState } from './map.types'; @@ -32,6 +33,7 @@ const mapSlice = createSlice({ closeMap: closeMapReducer, closeMapAndSetMainMapActive: closeMapAndSetMainMapActiveReducer, setMapPosition: setMapPositionReducer, + varyPositionZoom: varyPositionZoomReducer, setMapBackground: setMapBackgroundReducer, }, extraReducers: builder => { @@ -50,6 +52,7 @@ export const { closeMapAndSetMainMapActive, setMapPosition, setMapBackground, + varyPositionZoom, } = mapSlice.actions; export default mapSlice.reducer; diff --git a/src/redux/map/map.types.ts b/src/redux/map/map.types.ts index 16f5e542..3d15719a 100644 --- a/src/redux/map/map.types.ts +++ b/src/redux/map/map.types.ts @@ -82,6 +82,12 @@ export type GetUpdatedMapDataResult = Pick< export type SetMapPositionDataAction = PayloadAction<Point>; +export type SetLastPositionZoomActionPayload = { + delta: number; +}; + +export type SetLastPositionZoomAction = PayloadAction<SetLastPositionZoomActionPayload>; + export type InitMapDataActionPayload = { data: GetUpdatedMapDataResult | object; openedMaps: OppenedMap[]; diff --git a/tailwind.config.ts b/tailwind.config.ts index 75ce9dc5..15c5063b 100644 --- a/tailwind.config.ts +++ b/tailwind.config.ts @@ -36,6 +36,9 @@ const config: Config = { boxShadow: { primary: '4px 8px 32px 0px rgba(0, 0, 0, 0.12)', }, + dropShadow: { + secondary: '0px 4px 24px rgba(0, 0, 0, 0.08)', + }, }, fontFamily: { manrope: ['var(--font-manrope)'], -- GitLab