File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { FC } from "react" ;
2+ import { useKeyboardZoom } from "./useKeyboardZoom" ;
3+
4+ export const KeyboardZoomController : FC = ( ) => {
5+ useKeyboardZoom ( ) ;
6+
7+ return null ;
8+ } ;
Original file line number Diff line number Diff line change 1+ import { useEffect } from "react" ;
2+ import { useMap } from "react-leaflet" ;
3+ import type { Map as LeafletMap } from "leaflet" ;
4+
5+ interface KeyboardZoomOptions {
6+ step ?: number ;
7+ }
8+
9+ const isTypingTarget = ( target : EventTarget | null ) : boolean => {
10+ if ( ! ( target instanceof HTMLElement ) ) {
11+ return false ;
12+ }
13+
14+ const tag = target . tagName . toLowerCase ( ) ;
15+ return tag === "input" || tag === "textarea" || target . isContentEditable ;
16+ } ;
17+
18+ export const useKeyboardZoom = ( options : KeyboardZoomOptions = { } ) : void => {
19+ const { step = 1 } = options ;
20+ const map : LeafletMap = useMap ( ) ;
21+
22+ useEffect ( ( ) => {
23+ const handleKeyDown = ( event : KeyboardEvent ) : void => {
24+ if ( isTypingTarget ( event . target ) ) {
25+ return ;
26+ }
27+
28+ const { key } = event ;
29+ const currentZoom = map . getZoom ( ) ;
30+ const minZoom = map . getMinZoom ( ) ;
31+ const maxZoom = map . getMaxZoom ( ) ;
32+
33+ if ( key === "+" || key === "=" ) {
34+ event . preventDefault ( ) ;
35+ map . setZoom ( Math . min ( currentZoom + step , maxZoom ) ) ;
36+ }
37+
38+ if ( key === "-" ) {
39+ event . preventDefault ( ) ;
40+ map . setZoom ( Math . max ( currentZoom - step , minZoom ) ) ;
41+ }
42+ } ;
43+
44+ window . addEventListener ( "keydown" , handleKeyDown ) ;
45+
46+ return ( ) => {
47+ window . removeEventListener ( "keydown" , handleKeyDown ) ;
48+ } ;
49+ } , [ map , step ] ) ;
50+ } ;
Original file line number Diff line number Diff line change @@ -14,8 +14,6 @@ const { basic, selected } = offsets;
1414export const Pin : FC < PinProps > = memo ( ( { data, isActive, onClick } ) => {
1515 const { east, north } = getCoordinatesFromPaper ( data ) ;
1616
17- console . log ( "Pin rendering" ) ;
18-
1917 const handleClick = ( ) => onClick ( data ) ;
2018
2119 if ( ! east || ! north ) {
Original file line number Diff line number Diff line change @@ -9,12 +9,14 @@ import { Layer } from "./Layer";
99import { Pins } from "./Pins" ;
1010import { SelectionResetHandler } from "./SelectionResetHandler" ;
1111import { ZoomControls } from "./ZoomControls" ;
12+ import { KeyboardZoomController } from "./KeyboardZoomController" ;
1213
1314export const Geomap : FC = ( ) => (
1415 < HeightContainer >
1516 < MapContainer { ...CONFIG } className = "geomap_container" >
1617 < SelectionResetHandler />
1718 < ZoomControls />
19+ < KeyboardZoomController />
1820 < Layer />
1921 < Pins />
2022 </ MapContainer >
You can’t perform that action at this time.
0 commit comments