Skip to content

Commit f21373a

Browse files
committed
feat: zoom level change with keyboard buttons
1 parent 5b2e063 commit f21373a

4 files changed

Lines changed: 60 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FC } from "react";
2+
import { useKeyboardZoom } from "./useKeyboardZoom";
3+
4+
export const KeyboardZoomController: FC = () => {
5+
useKeyboardZoom();
6+
7+
return null;
8+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
};

vis/js/templates/Geomap/Pins/Pin/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const { basic, selected } = offsets;
1414
export 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) {

vis/js/templates/Geomap/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import { Layer } from "./Layer";
99
import { Pins } from "./Pins";
1010
import { SelectionResetHandler } from "./SelectionResetHandler";
1111
import { ZoomControls } from "./ZoomControls";
12+
import { KeyboardZoomController } from "./KeyboardZoomController";
1213

1314
export const Geomap: FC = () => (
1415
<HeightContainer>
1516
<MapContainer {...CONFIG} className="geomap_container">
1617
<SelectionResetHandler />
1718
<ZoomControls />
19+
<KeyboardZoomController />
1820
<Layer />
1921
<Pins />
2022
</MapContainer>

0 commit comments

Comments
 (0)