Auto deploy: 2025-12-09 06:11:18
This commit is contained in:
parent
36e1000250
commit
c110e94b6c
|
|
@ -1,7 +1,7 @@
|
|||
import maplibregl from "maplibre-gl";
|
||||
|
||||
/**
|
||||
* Supprime tous les marqueurs de la carte
|
||||
* Retire tous les marqueurs existants
|
||||
*/
|
||||
export function clearAllMarkers(markersRef, expandedElRef) {
|
||||
if (markersRef.current) {
|
||||
|
|
@ -20,16 +20,14 @@ export function clearAllMarkers(markersRef, expandedElRef) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Crée un DOM marker pour un post et l'ancre sur sa coordonnée.
|
||||
* - mapRef : ref vers la map MapLibre
|
||||
* - markersRef : ref vers la liste [{ id, marker, el, post }]
|
||||
* - expandedElRef : ref vers l'élément DOM du post actuellement "grand"
|
||||
* Crée un marqueur "pin" avec bulle + pointe.
|
||||
* Position verrouillée sur [lon, lat] via MapLibre (anchor: 'bottom').
|
||||
*/
|
||||
export function createMarkerForPost(post, mapRef, markersRef, expandedElRef) {
|
||||
const map = mapRef.current;
|
||||
if (!map) return;
|
||||
|
||||
// Récupère les coords (lat/lon / Lat/Lon / latitude/longitude)
|
||||
// --- Coordonnées ---
|
||||
const lat =
|
||||
typeof post.lat === "number"
|
||||
? post.lat
|
||||
|
|
@ -49,122 +47,68 @@ export function createMarkerForPost(post, mapRef, markersRef, expandedElRef) {
|
|||
: null;
|
||||
|
||||
if (lat == null || lon == null) {
|
||||
console.warn("Post sans coordonnée valide:", post);
|
||||
console.warn("post sans coordonnée:", post);
|
||||
return;
|
||||
}
|
||||
|
||||
// Petit texte (on garde court)
|
||||
const title = (post.title || post.Title || "").trim() || "Untitled";
|
||||
const snippet =
|
||||
(post.snippet || post.Snippet || "").trim() ||
|
||||
(post.body || post.Body || "").trim() ||
|
||||
title;
|
||||
|
||||
// ELEMENT DOM DU MARKER
|
||||
// --- DOM du pin ---
|
||||
const root = document.createElement("div");
|
||||
root.className = "post-marker post-marker-compact";
|
||||
root.className = "post-pin";
|
||||
|
||||
const inner = document.createElement("div");
|
||||
inner.className = "marker-inner";
|
||||
const bubble = document.createElement("div");
|
||||
bubble.className = "post-pin-bubble";
|
||||
|
||||
const dot = document.createElement("div");
|
||||
dot.className = "marker-dot";
|
||||
dot.className = "post-pin-dot";
|
||||
|
||||
const text = document.createElement("div");
|
||||
text.className = "marker-text";
|
||||
text.className = "post-pin-text";
|
||||
text.textContent = title;
|
||||
|
||||
inner.appendChild(dot);
|
||||
inner.appendChild(text);
|
||||
root.appendChild(inner);
|
||||
bubble.appendChild(dot);
|
||||
bubble.appendChild(text);
|
||||
|
||||
// Marker MapLibre -> POSITION = [lon, lat] SANS OFFSET
|
||||
const pointer = document.createElement("div");
|
||||
pointer.className = "post-pin-pointer";
|
||||
|
||||
root.appendChild(bubble);
|
||||
root.appendChild(pointer);
|
||||
|
||||
// --- Marker MapLibre : position = [lon, lat], ancré en bas ---
|
||||
const marker = new maplibregl.Marker({
|
||||
element: root,
|
||||
anchor: "bottom", // le bas du marker pointe sur la coordonnée
|
||||
anchor: "bottom", // la pointe touche la coordonnée
|
||||
})
|
||||
.setLngLat([lon, lat])
|
||||
.addTo(map);
|
||||
|
||||
// Sauvegarde pour pouvoir les nettoyer
|
||||
const entry = { id: post.id, marker, el: root, post };
|
||||
markersRef.current.push(entry);
|
||||
|
||||
// Quand on clique -> on ouvre le gros post par-dessus, mais à la même coordonnée
|
||||
// --- Gestion du mode "agrandi" ---
|
||||
root.addEventListener("click", (ev) => {
|
||||
ev.stopPropagation();
|
||||
|
||||
// Fermer le précédent gros post
|
||||
// Fermer l'ancien agrandi
|
||||
if (expandedElRef.current && expandedElRef.current !== root) {
|
||||
collapseMarkerElement(expandedElRef.current);
|
||||
expandedElRef.current.classList.remove("post-pin-expanded");
|
||||
}
|
||||
|
||||
// Toggle compact/expanded sur ce marker
|
||||
if (root.classList.contains("post-marker-expanded")) {
|
||||
collapseMarkerElement(root);
|
||||
expandedElRef.current = null;
|
||||
// Toggle sur ce pin
|
||||
const isExpanded = root.classList.toggle("post-pin-expanded");
|
||||
expandedElRef.current = isExpanded ? root : null;
|
||||
|
||||
// Mettre à jour le texte (plus long si agrandi)
|
||||
if (isExpanded) {
|
||||
text.textContent = snippet;
|
||||
} else {
|
||||
expandMarkerElement(root, { title, snippet });
|
||||
expandedElRef.current = root;
|
||||
text.textContent = title;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Passe un marker en mode "grand post"
|
||||
*/
|
||||
function expandMarkerElement(root, { title, snippet }) {
|
||||
root.classList.remove("post-marker-compact");
|
||||
root.classList.add("post-marker-expanded");
|
||||
|
||||
// Nettoie l'intérieur et reconstruit
|
||||
root.innerHTML = "";
|
||||
|
||||
const container = document.createElement("div");
|
||||
container.className = "marker-expanded-inner";
|
||||
|
||||
const header = document.createElement("div");
|
||||
header.className = "marker-expanded-header";
|
||||
|
||||
const dot = document.createElement("div");
|
||||
dot.className = "marker-dot";
|
||||
|
||||
const titleEl = document.createElement("div");
|
||||
titleEl.className = "marker-expanded-title";
|
||||
titleEl.textContent = title;
|
||||
|
||||
header.appendChild(dot);
|
||||
header.appendChild(titleEl);
|
||||
|
||||
const bodyEl = document.createElement("div");
|
||||
bodyEl.className = "marker-expanded-body";
|
||||
bodyEl.textContent = snippet;
|
||||
|
||||
container.appendChild(header);
|
||||
container.appendChild(bodyEl);
|
||||
|
||||
root.appendChild(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repasse un marker en mode petit post
|
||||
*/
|
||||
function collapseMarkerElement(root) {
|
||||
root.classList.remove("post-marker-expanded");
|
||||
root.classList.add("post-marker-compact");
|
||||
root.innerHTML = "";
|
||||
|
||||
const inner = document.createElement("div");
|
||||
inner.className = "marker-inner";
|
||||
|
||||
const dot = document.createElement("div");
|
||||
dot.className = "marker-dot";
|
||||
|
||||
const text = document.createElement("div");
|
||||
text.className = "marker-text";
|
||||
text.textContent = "…";
|
||||
|
||||
inner.appendChild(dot);
|
||||
inner.appendChild(text);
|
||||
root.appendChild(inner);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue