diff --git a/public/css/style.css b/public/css/style.css index 5416ea1..767f344 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -2391,3 +2391,19 @@ html, body { margin-top: 6px; padding: 3px 8px; } + +/* Places that preview on hover / press-and-hold (public/js/preview.js): + no text selection or callout menu getting in the way of the hold. */ +.search-panel__result, +.route-panel__point { + -webkit-touch-callout: none; + user-select: none; +} + +.route-panel__point { + cursor: pointer; +} + +.preview-highlight { + pointer-events: none; +} diff --git a/public/js/preview.js b/public/js/preview.js new file mode 100644 index 0000000..a97920e --- /dev/null +++ b/public/js/preview.js @@ -0,0 +1,97 @@ +import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs"; + +// Hovering a list item with a place (mouse), or pressing and holding it +// (touch), shows that place on the map for a moment; leaving or letting go +// puts the map back where it was. A click or tap commits: the item's own +// click handler moves the map for good, and nothing is restored. +const HOLD_MS = 400; +const RESTORE_DELAY_MS = 150; // moving straight to the next item doesn't bounce back in between +const DURATION_MS = 300; + +let saved = null; // the camera before the first preview +let restoreTimer = null; +let marker = null; +const wiredMaps = new WeakSet(); + +// target() → { lngLat: [lon, lat] } or { bbox: [w, s, e, n] }. +export function wirePreview(map, element, target) { + // Panning the map yourself during a preview keeps where you are now. + if (!wiredMaps.has(map)) { + wiredMaps.add(map); + map.on("dragstart", commit); + } + let holdTimer = null; + let held = false; + + element.addEventListener("pointerenter", (event) => { + if (event.pointerType === "mouse") show(map, target()); + }); + element.addEventListener("pointerleave", (event) => { + if (event.pointerType === "mouse") restoreSoon(map); + }); + + element.addEventListener("pointerdown", (event) => { + if (event.pointerType === "mouse") return; + held = false; + holdTimer = setTimeout(() => { + held = true; + show(map, target()); + }, HOLD_MS); + }); + const release = () => { + clearTimeout(holdTimer); + if (held) restoreSoon(map); + }; + element.addEventListener("pointerup", release); + element.addEventListener("pointercancel", release); // the list scrolled instead + element.addEventListener("contextmenu", (event) => event.preventDefault()); // the long press is ours + + // Capture, so this runs before the item's own click handler. + element.addEventListener( + "click", + (event) => { + if (held) { + // The end of a hold, not a tap. + held = false; + event.preventDefault(); + event.stopImmediatePropagation(); + return; + } + commit(); + }, + true, + ); +} + +function show(map, { lngLat, bbox }) { + clearTimeout(restoreTimer); + saved ||= { center: map.getCenter(), zoom: map.getZoom(), bearing: map.getBearing(), pitch: map.getPitch() }; + const point = bbox && (bbox[0] !== bbox[2] || bbox[1] !== bbox[3]) ? null : lngLat || bbox.slice(0, 2); + if (point) { + map.easeTo({ center: point, zoom: Math.max(saved.zoom, 14), duration: DURATION_MS }); + if (!marker) { + const element = document.createElement("div"); + element.className = "search-highlight preview-highlight"; + marker = new Marker({ element }); + } + marker.setLngLat(point).addTo(map); + } else { + map.fitBounds(bbox, { padding: 40, maxZoom: 15, duration: DURATION_MS }); + marker?.remove(); + } +} + +function restoreSoon(map) { + clearTimeout(restoreTimer); + restoreTimer = setTimeout(() => { + if (saved) map.easeTo({ ...saved, duration: DURATION_MS }); + saved = null; + marker?.remove(); + }, RESTORE_DELAY_MS); +} + +function commit() { + clearTimeout(restoreTimer); + saved = null; + marker?.remove(); +} diff --git a/public/js/route.js b/public/js/route.js index 57f77b1..1649c40 100644 --- a/public/js/route.js +++ b/public/js/route.js @@ -2,6 +2,7 @@ import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs"; import { ROUTE_ICON } from "/js/icons.js"; import { notifySidePanelOpen, onlyOneSidePanelOpen, notifyGpxStoreChanged, onRoutePoint, onRouteEdit } from "/js/panels.js"; import { isLoggedIn } from "/js/auth.js"; +import { wirePreview } from "/js/preview.js"; const ROUTE_URL = "/api/route"; const VEHICLE_URL = "/api/vehicle"; @@ -156,6 +157,9 @@ export class RoutePanelControl { this._pointButton("↓", "Move down", index < this._points.length - 1, () => this._move(point, 1)), this._pointButton("×", "Remove", true, () => this._removePoint(point)), ); + // Hover/hold previews the point; a click on its name commits. + wirePreview(this._map, text, () => ({ lngLat: point.lngLat })); + text.addEventListener("click", () => this._map.easeTo({ center: point.lngLat, zoom: Math.max(this._map.getZoom(), 14) })); this._list.appendChild(li); }); this._editingNote.hidden = !this._editing; diff --git a/public/js/search.js b/public/js/search.js index d665069..11f1472 100644 --- a/public/js/search.js +++ b/public/js/search.js @@ -2,6 +2,7 @@ import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs"; import { SEARCH_ICON } from "/js/icons.js"; import { escapeHtml } from "/js/gpx.js"; import { addRoutePoint } from "/js/panels.js"; +import { wirePreview } from "/js/preview.js"; const NOMINATIM_API = "https://nominatim.openstreetmap.org/search"; // "lat, lng" / "lat lng" decimal degrees — the same plain-text shape the @@ -225,6 +226,10 @@ export class SearchControl { }); }); this._resultsEl.querySelectorAll(".search-panel__result").forEach((el) => { + const result = this._results[Number(el.dataset.index)]; + if (result.bbox || result.lat != null) { + wirePreview(this._map, el, () => (result.lat != null ? { lngLat: [result.lng, result.lat] } : { bbox: result.bbox })); + } el.addEventListener("click", () => { const result = this._results[Number(el.dataset.index)]; result.open?.(); diff --git a/views/layout.erb b/views/layout.erb index a133949..785cebd 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -52,6 +52,7 @@ "/js/overlays.js": "/js/overlays.js?v=<%= ASSET_VERSION %>", "/js/panels.js": "/js/panels.js?v=<%= ASSET_VERSION %>", "/js/plugins.js": "/js/plugins.js?v=<%= ASSET_VERSION %>", + "/js/preview.js": "/js/preview.js?v=<%= ASSET_VERSION %>", "/js/radar.js": "/js/radar.js?v=<%= ASSET_VERSION %>", "/js/route.js": "/js/route.js?v=<%= ASSET_VERSION %>", "/js/share_import.js": "/js/share_import.js?v=<%= ASSET_VERSION %>",