Skip to content

Commit

Permalink
Close popup using Escape
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Apr 11, 2024
1 parent dc41ca1 commit f41b2f1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Control, defaults as defaultControls } from "ol/control";
import { defaults as defaultInteractions } from "ol/interaction";
import { defaults as defaultInteractions, Interaction } from "ol/interaction";
import { PMTilesVectorSource } from "ol-pmtiles";
import { fromLonLat } from "ol/proj";
import EventType from "ol/events/EventType";
import Fill from "ol/style/Fill";
import GeolocationButton from "ol-ext/control/GeolocationButton";
import Map from "ol/Map";
Expand Down Expand Up @@ -81,6 +82,21 @@ fetchBulletins(date).then((bulletins) => {
function initMap() {
const mapElement = document.querySelector<HTMLDivElement>("#map")!;

class ClosePopup extends Interaction {
handleEvent(mapBrowserEvent: MapBrowserEvent<KeyboardEvent>): boolean {
if (
mapBrowserEvent.type !== EventType.KEYDOWN &&
mapBrowserEvent.type !== EventType.KEYPRESS
) {
return true;
} else if (mapBrowserEvent.originalEvent.key !== "Escape") {
return true;
}
popup.hide();
return false;
}
}

class DateControl extends Control {
constructor() {
const input = document.createElement("input");
Expand Down Expand Up @@ -126,6 +142,8 @@ function initMap() {
],
});

map.addInteraction(new ClosePopup());

map.addControl(new GeolocationButton());

const geocoder = new SearchNominatim({});
Expand Down

0 comments on commit f41b2f1

Please sign in to comment.