mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 04:26:57 +00:00
Use better touch gestures
This commit is contained in:
@@ -2,11 +2,21 @@ import { useEffect, useTransition } from "react";
|
||||
|
||||
import { useSetAtom } from "jotai";
|
||||
import L from "leaflet";
|
||||
import { useMapEvent } from "react-leaflet";
|
||||
import { GestureHandling } from "leaflet-gesture-handling";
|
||||
import "leaflet-gesture-handling/dist/leaflet-gesture-handling.css";
|
||||
import "leaflet/dist/leaflet.css";
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import { useMap, useMapEvent } from "react-leaflet";
|
||||
|
||||
import { mapBoundsAtom } from "@/atoms";
|
||||
|
||||
// Add Leaflet.GestureHandling for improved desktop and mobile touch gestures
|
||||
L.Map.addInitHook("addHandler", "gestureHandling", GestureHandling);
|
||||
|
||||
const MapEvents = () => {
|
||||
const locale = useLocale();
|
||||
const t = useTranslations("Map");
|
||||
const map = useMap();
|
||||
const setMapBounds = useSetAtom(mapBoundsAtom);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
@@ -16,7 +26,7 @@ const MapEvents = () => {
|
||||
L.latLng(51.17, 9.53),
|
||||
);
|
||||
|
||||
const map = useMapEvent("moveend", () => {
|
||||
useMapEvent("moveend", () => {
|
||||
// Set the map bounds atoms when the user pans/zooms
|
||||
startTransition(() => {
|
||||
setMapBounds(map.getBounds());
|
||||
@@ -28,7 +38,30 @@ const MapEvents = () => {
|
||||
map.setView(franceBounds.getCenter(), map.getBoundsZoom(franceBounds));
|
||||
map.setMaxBounds(worldBounds);
|
||||
map.options.maxBoundsViscosity = 1.0; // Prevents going past bounds while dragging
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
map.addHandler("gestureHandling", GestureHandling);
|
||||
|
||||
// @ts-expect-error Typescript does not see additional handler here
|
||||
map.gestureHandling.enable();
|
||||
}, [map]);
|
||||
|
||||
useEffect(() => {
|
||||
console.log("setting map language", locale);
|
||||
// @ts-expect-error Typescript does not see additional handler here
|
||||
map.options.gestureHandlingOptions.text = {
|
||||
touch: t("touch"),
|
||||
scroll: t("scroll"),
|
||||
scrollMac: t("scrollMac"),
|
||||
};
|
||||
|
||||
// We need to toggle the handler to update the text on prod. No idea why.
|
||||
|
||||
// @ts-expect-error Typescript does not see additional handler here
|
||||
map.gestureHandling.disable();
|
||||
|
||||
// @ts-expect-error Typescript does not see additional handler here
|
||||
map.gestureHandling.enable();
|
||||
}, [map, locale]);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -59,6 +59,12 @@
|
||||
"getInvolved": "Echecs France is completely free, open source and open to contributions. Find us on <link>GitHub</link> if you want to get involved."
|
||||
},
|
||||
|
||||
"Map": {
|
||||
"touch": "Use two fingers to move the map",
|
||||
"scroll": "Use ctrl + scroll to zoom the map",
|
||||
"scrollMac": "Use \u2318 + scroll to zoom the map"
|
||||
},
|
||||
|
||||
"Tournaments": {
|
||||
"loading": "Loading...",
|
||||
"searchLabel": "Search",
|
||||
|
||||
@@ -58,6 +58,12 @@
|
||||
"getInvolved": "Échecs France est complètement gratuit, open source et ouvert aux contributions. Retrouvez-nous sur <link>GitHub</link> si vous souhaitez vous impliquer."
|
||||
},
|
||||
|
||||
"Map": {
|
||||
"touch": "Utilisez deux doigts pour déplacer la carte",
|
||||
"scroll": "Utilisez ctrl + défilement pour zoomer sur la carte",
|
||||
"scrollMac": "Utilisez \u2318 + défilement pour zoomer sur la carte"
|
||||
},
|
||||
|
||||
"Tournaments": {
|
||||
"loading": "Téléchargement...",
|
||||
"searchLabel": "Rechercher",
|
||||
|
||||
Reference in New Issue
Block a user